Hi Friend’s
I am working on a cards Game code and facing some problem.
Case Is …
A deck of 52 cards is splitted into two halfs equally for both players. Now I want to select one card each from both players deck of 26 cards and throw it as a move….I used For loop as well as arrays….For Player one , cards 52 to 27 are being provided.Now I want on tapping button , frst 52 card is selected and moved and on second tap ,from remaining 25 cards for player 1 , 51 th card is selected and so on….untill card 27th is selected…I want to use for loop as weel as arrays for same…Please suggest how to do same???
local composer = require( “composer” )
local scene = composer.newScene()
local widget = require( “widget” )
local utility = require( “utility” )
local myData = require( “mydata” )
local halfW = display.contentWidth * 0.5
local halfH = display.contentHeight * 0.5
local params
–local sceneGroup=nil
local function handleLevelSelect( event )
if ( “ended” == event.phase ) then
– set the current level to the ID of the selected level
myData.settings.currentLevel = event.target.id
composer.removeScene( “game”, false )
composer.gotoScene( “game”, { effect = “crossFade”, time = 333 } )
end
end
local cardsGroup = display.newGroup()
local cards = {}
local cardsImages = {
“assets/images/cards/s1.png”,“assets/images/cards/s2.png”,“assets/images/cards/s3.png”,“assets/images/cards/s4.png”,“assets/images/cards/s5.png”,“assets/images/cards/s6.png”,“assets/images/cards/s7.png”,“assets/images/cards/s8.png”,“assets/images/cards/s9.png”,“assets/images/cards/s10.png”,“assets/images/cards/s11.png”,“assets/images/cards/s12.png”,“assets/images/cards/s13.png”,
“assets/images/cards/h1.png”,“assets/images/cards/h2.png”,“assets/images/cards/h3.png”,“assets/images/cards/h4.png”,“assets/images/cards/h5.png”,“assets/images/cards/h6.png”,“assets/images/cards/h7.png”,“assets/images/cards/h8.png”,“assets/images/cards/h9.png”,“assets/images/cards/h10.png”,“assets/images/cards/h11.png”,“assets/images/cards/h12.png”,“assets/images/cards/h13.png”,
“assets/images/cards/c1.png”,“assets/images/cards/c2.png”,“assets/images/cards/c3.png”,“assets/images/cards/c4.png”,“assets/images/cards/c5.png”,“assets/images/cards/c6.png”,“assets/images/cards/c7.png”,“assets/images/cards/c8.png”,“assets/images/cards/c9.png”,“assets/images/cards/c10.png”,“assets/images/cards/c11.png”,“assets/images/cards/c12.png”,“assets/images/cards/c13.png”,
“assets/images/cards/d1.png”,“assets/images/cards/d2.png”,“assets/images/cards/d3.png”,“assets/images/cards/d4.png”,“assets/images/cards/d5.png”,“assets/images/cards/d6.png”,“assets/images/cards/d7.png”,“assets/images/cards/d8.png”,“assets/images/cards/d9.png”,“assets/images/cards/d10.png”,“assets/images/cards/d11.png”,“assets/images/cards/d12.png”,“assets/images/cards/d13.png”
}
local indexes = {
SA=14,S2=2,S3=3,S4=4,
S5=5,S6=6,S7=7,S8=8,
S9=9,S10=10,SJ=11,SQ=12,SK=13,
HA=14,H2=2,H3=3,H4=4,
H5=5,H6=6,H7=7,H8=8,
H9=9,H10=10,HJ=11,HQ=12,HK=13,
CA=14,C2=2,C3=3,C4=4,
C5=5,C6=6,C7=7,C8=8,
C9=9,C10=10,CJ=11,CQ=12,CK=13,
DA=14,D2=2,D3=3, D4=4,
D5=5,D6=6,D7=7,D8=8,
D9=9,D10=10,DJ=11,DQ=12,DK=13
}
local function handleHomeButtonEventFn( event )
if ( “ended” == event.phase ) then
print(“handleHomeButtonEventFn Tap Working Properly”)
for i=#cardsImages,1,-1 do
display.remove( cards[i] )
cards[i] = nil – Set reference again to nill
end
print(“ScreenObjects Deletion Working Properly”)
composer.removeScene( “help”, false )
composer.gotoScene( “menu”, { effect = “crossFade”, time = 200 } )
end
end
local function cleanCardsMoveFn()
print("************************************************")
end
– Start the composer event handlers
–
function scene:create( event )
local sceneGroup = self.view
params = event.params
–
– setup a page background, really not that important though composer
– crashes out if there isn’t a display object in the view.
–
--local title = display.newBitmapText( titleOptions )
–
local background = display.newRect( 0, 0, 570, 360 )
background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert( background )
local gameTitle = display.newText(“Duki Pe Duki”, 100, 32, native.systemFontBold, 30 )
gameTitle.x = display.contentCenterX
gameTitle.y = 20
gameTitle:setFillColor( 0 )
transition.to(gameTitle,{time = 3000,delay=300,xScale = 1.1,yScale = 1.1,transition=easing.inOutElastic,iterations = -1})
sceneGroup:insert( gameTitle )
local gameScreenFirstHalfTitle = display.newText(“PLAYER 1”, 100, 32, native.systemFontBold,16 )
gameScreenFirstHalfTitle.x = display.contentCenterX-300
gameScreenFirstHalfTitle.y = display.contentCenterY-90
gameScreenFirstHalfTitle:setFillColor( 0 )
transition.to(gameScreenFirstHalfTitle,{ time=750,x=display.contentCenterX-175,display.contentCenterY-90 } )
– gameScreenFirstHalfTitle:addEventListener( “tap”, selectCardFn )
sceneGroup:insert( gameScreenFirstHalfTitle )
local gameScreenFirstHalf = display.newRect( 0, 0, 40, 40 )
gameScreenFirstHalf.x = display.contentCenterX-175
gameScreenFirstHalf.y = display.contentCenterY-130
gameScreenFirstHalf.strokeWidth = 2
gameScreenFirstHalf.alpha = 0
gameScreenFirstHalf:setFillColor( 0 )
gameScreenFirstHalf:setStrokeColor( 0)
transition.to(gameScreenFirstHalf,{ time=3000,alpha=1.0,x = display.contentCenterX-175,y = display.contentCenterY-130 } )
sceneGroup:insert(gameScreenFirstHalf)
local gameScreenSecondHalfTitle = display.newText(“PLAYER 2”, 100, 32, native.systemFontBold,16 )
gameScreenSecondHalfTitle.x = display.contentCenterX+300
gameScreenSecondHalfTitle.y = display.contentCenterY-90
gameScreenSecondHalfTitle:setFillColor( 0 )
– gameScreenSecondHalfTitle:addEventListener( “tap”, selectCardFn )
transition.to(gameScreenSecondHalfTitle,{ time=750,x=display.contentCenterX+175,display.contentCenterY-90 } )
sceneGroup:insert( gameScreenSecondHalfTitle )
local gameScreenSecondHalf = display.newRect( 0, 0, 40, 40)
gameScreenSecondHalf.x = display.contentCenterX+180
gameScreenSecondHalf.y = display.contentCenterY-130
gameScreenSecondHalf.strokeWidth = 2
gameScreenSecondHalf:setFillColor( 0 )
gameScreenSecondHalf:setStrokeColor( 0)
gameScreenSecondHalf.alpha = 0
transition.to(gameScreenSecondHalf,{ time=3000,alpha=1.0,x = display.contentCenterX+175,y = display.contentCenterY-130 } )
– sceneGroup:insert(gameScreenFirstHalf)
sceneGroup:insert(gameScreenSecondHalf)
local function playerMovesFn()
print(“Player Moves Started”)
print(“Player One Moves Initiated”)
local frontObject = display.newRect( display.contentCenterX-175,display.contentCenterY, 50, 50 )
frontObject.alpha = 0.8
frontObject:setFillColor( 0.5 )
frontObject.name = “Front Object”
– sceneGroup:insert(frontObject)
– sceneGroup:insert(frontObject)
local function tapListener( event )
local object = event.target
print( object.name…" TAPPED!" )
local function b()
local currentIndex = 52
local function a()
print(“second chance”)
transition.to(cards[51],{time=1000,y=display.contentCenterY+30,x=display.contentCenterX,width=110 ,height=140})
end
for i = currentIndex, 27,-1 do
– print( deck[i] )
frontObject:removeEventListener( “tap”, tapListener )
– currentIndex = currentIndex +1
– transition.to(cards[currentIndex],{time=1000,y=display.contentCenterY,x=display.contentCenterX,width=110 ,height=140})
transition.to(cards[i],{time=1000,y=display.contentCenterY,x=display.contentCenterX,width=110 ,height=140})
end
end
b()
end
frontObject:addEventListener( “tap”, tapListener )
end
local function splitCardsDeck()
for i = #cardsImages, 27 , -1 do
transition.to(cards[i],{time=200,y=display.contentCenterY+20,x=display.contentCenterX-170,width=110 ,height=140})
print(“First Player 26 Cards From Deck”…" = "…i)
cards[52]:removeEventListener( “tap”, splitCardsDeck,1)
end
for i = 26 , 1 , -1 do
transition.to(cards[i],{time=200,y=display.contentCenterY+20,x=display.contentCenterX+170,width=110 ,height=140})
print(“Second Player 26 Cards From Deck”…" = "…i)
end
local function nexto()
playerMovesFn()
end
timer.performWithDelay(1000,nexto,1)
end
local function shuffleCardsDeck()
local yAxis =math.random(display.contentCenterY-500,display.contentCenterY+500)
for i = 1 , #cardsImages , 1 do
cards[i] = display.newImageRect( cardsImages[math.random(1,#cardsImages)], 140, 180 )
cards[i].x=display.contentCenterX
cards[i].y=yAxis
transition.to(cards[i],{time=500,y=display.contentCenterY+20,x=display.contentCenterX,width=140 ,height=180})
print(“Total Cards In Deck”…" = "…i)
end
cards[52]:addEventListener( “tap”, splitCardsDeck,1)
end
timer.performWithDelay(1000,shuffleCardsDeck,1)
local homeButton = widget.newButton({
id = “button1”,
label = “Home”,
width = 100,
height = 30,
onEvent = handleHomeButtonEventFn
})
homeButton.x = display.contentCenterX
homeButton.y = display.contentHeight - 10
sceneGroup:insert( homeButton )
end
function scene:show( event )
local sceneGroup = self.view
params = event.params
if event.phase == “did” then
end
end
function scene:hide( event )
local sceneGroup = self.view
if event.phase == “will” then
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
– END OF YOUR IMPLEMENTATION
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene