how to switch turns in a two player game
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
how to switch turns in a two player game
hi i would like to ask how to code a two player game,, i have a problem regarding switching of turns in my game.. i created a game just like snake and ladders. i have two players, since player 1 always starts first, how would i take player 2's turn.. like after player 1's turn...
Re: how to switch turns in a two player game
i have here my code for my dice loop
i have here, only player 1 will move. movePone is player 1's move
how and where will i put the switching of turns of both players?
Code: Select all
on mainEventLoop
// Continue this loop until gRun is set to false.
lock screen
--enableDisable
animateDice // Animate the dice
unlock screen
// Delay further execution by 50 milliseconds.
// But allow button clicks to be processed.
if gRun is true then
send "mainEventLoop" to me in 5 milliseconds
end if
if gRun is false then
movePone
end if
end mainEventLoop
how and where will i put the switching of turns of both players?
Re: how to switch turns in a two player game
Hi kesh17aki,
1. welcome to the forum!
2. Since I have no idea about your game, here some quick thoughts:
use e.g. a global variable which only holds 1 or 2 to indicate the current player.
Use this to identify the current player in your script(s).
"Initialize" it maybe on openstack and/or when you start a new game:
Then after every action for the (first) player have been finished, switch the global like this:
When starting a new game, just:
Get the picture?
Best
Klaus
1. welcome to the forum!
2. Since I have no idea about your game, here some quick thoughts:
use e.g. a global variable which only holds 1 or 2 to indicate the current player.
Use this to identify the current player in your script(s).
"Initialize" it maybe on openstack and/or when you start a new game:
Code: Select all
...
global gCurrentPlayer
put 1 into gCurrentPlayer
...
Code: Select all
command SwitchPlayers
global gCurrentPlayer
if gCurrentPlayer = 1 then
put 2 into gCurrentPlayer
else
put 1 into gCurrentPlayer
end if
end SwitchPlayers
Code: Select all
...
global gCurrentPlayer
put 1 into gCurrentPlayer
...
Best
Klaus
Re: how to switch turns in a two player game
thank you for the warm welcome Klaus thanks for the help..heres the whole code for this
Code: Select all
global pOnename
global pTwoname
global diceNum,rdiceNum,sdice,pOneMove,theCurrentLocation, nxtLocation,pMoveOne,pMoveOneLoc,theCurrentLocationTwo,pMoveOneLocTwo
global numdice1,numdice2
on preOpenCard
put pOnename into fld "pOne"
put pTwoname into fld "pTwo"
put 141,669 into theCurrentLocation--current location of player1
put 275,675 into theCurrentLocationTwo
put 0 into numdice1
put 0 into numdice2
--put sdice into numMoves
end preOpenCard
--on openCard
-- --move btn "plyrOne" from 141,669 to 651,681
-- set the loc of btn "plyrOne" to 141,669
--end openCard
--on mouseDown
-- grab button "plyrTwo"
-- grab button "plyrOne"
-- --put dragPone into image "plyrOne"
-- --put dragPtwo into image "pyrTwo"
--end mouseDown
--global variables for the animation
global gRun
local gDiceImg
local gDiceRedrawIntervalCount
local dDiceRotate
--global gWhatPlayer
on preOpenStack
set the idleRate to 50
put false into gRun
put 1 into gDiceImg
put 0 into gDiceRedrawIntervalCount
put 0 into gDiceRotate
#######
end preOpenStack
-- on openStack
-- /* player 1 always starts first */
-- put "plyrOne" into gWhatPlayer
--end openStack
-- on mouseDown
-- put btn "plyrOne" into oldLocation
-- end mouseDown
on mouseUp
--put "plyrOne" into pOneMove
put random(6) into sdice
if sdice is 1 then
put 13 into rdiceNum
else if sdice is 2 then
put 16 into rdiceNum
else if sdice is 3 then
put 10 into rdiceNum
else if sdice is 4 then
put 1 into rdiceNum
else if sdice is 5 then
put 7 into rdiceNum
else if sdice is 6 then
put 4 into rdiceNum
end if
--put sdice into nxtLocation
###############
-- if gWhatPlayer = "plyrOne" then
-- answer "its your turn" pOne
-- put "plyrTwo" into gWhichTeam
-- else if gWhichTeam = "plyrTwo" then
-- answer "its your turn" pTwo
-- put "plyrOne" into gWhichTeam
-- end if
###############
if the short name of target is "rollOnOff" then
if gRun is true then
set the label target to "Roll"
--put random (18)
// Set the run loop to false.
put false into gRun
else
--set the label of target to "Stop"
--put random (3) into button "di"
// Reset the environment.
preOpenStack
// Set the run loop to true.
put true into gRun
// Execute the main run loop.
mainEventLoop
end if
end if
end mouseUp
on animateDice
set the moveSpeed to 1
put rdiceNum into diceNum
// Allow for five scheduling cycles before
// drawing the next dice image.
if 17 > gDiceRedrawIntervalCount then
add 1 to gDiceRedrawIntervalCount
else
put 0 into gDiceRedrawIntervalCount
// Loop through the dice images,
// once the last one has been reached.
if gDiceImg is 18 then
if gDicelmg > 18 then
put false into gRun
put diceNum into gDiceImg
--13 = 1
--1 = 4
--put 1 into gDiceImg
end if
--put 1 into gDiceImg
else
add 1 to gDiceImg
end if
// Set the new dice image.
set the icon of button "di" to "die_" & gDiceImg & "_.png"
end if
end animateDice
on mainEventLoop
// Continue this loop until gRun is set to false.
lock screen
--enableDisable
animateDice // Animate the dice
unlock screen
// Delay further execution by 50 milliseconds.
// But allow button clicks to be processed.
if gRun is true then
send "mainEventLoop" to me in 5 milliseconds
end if
if gRun is false then
movePone
end if
end mainEventLoop
on movePone
put (numdice1+sdice) into sdice
put setofLocations(sdice) into pMoveOneLoc
getTheLocation "plyrOne"
if sdice > 37 then
answer "ksjdlkf"
end if
move btn "plyrOne" from theCurrentLocation to pMoveOneLoc in 100 ticks
put sdice into numdice1
-- moveVeins
pOneTrap
moveVeins
end movePone
on movePtwo
put (numdice2+sdice) into sdice
put setofLocations(sdice) into pMoveOneLoc
getTheLocation "plyrOne"
if sdice > 37 then
answer "ksjdlkf"
end if
move btn "plyrTwo" from theCurrentLocation to pMoveOneLoc in 100 ticks
put sdice into numdice2
-- moveVeins
pOneTrap
moveVeins
end movePtwo
on pOneTrap
--if sdice is 3 then show img "popShark"
--if btn "plyrOne" is in the rect of img "shark" then show img "popShark"
if the location of btn "plyrOne" is within the rect of img "shark" then show group "popupShark"
if the location of btn "plyrTwo" is within the rect of img "shark" then show group "popupSpider"
if the location of btn "plyrOne" is within the rect of img "spider" then show group "popupSpider"
end pOneTrap
on moveVeins
if the location of btn "plyrOne" is within the rect of img "six" then
move btn "plyrOne" from 932,673 to 747,558 in 100 ticks
end if
end moveVeins
on playerWins
if the location of btn "plyrOne" is within the rect of img "heli" then show img "popHeli"
end playerWins
on getTheLocation theplayer
put the location of btn theplayer into theCurrentLocation
end getTheLocation
on sharkAttack
if the location of btn "plyrOne" is within the rect of img "shark" then
move btn "plyrOne" from theCurrentLocation to 554,681 in 100 ticks
end if
end sharkAttack
--thesum refers to the sum of number of dice and the box number
function setofLocations thesum
put "1-458,681*2-554,681*3-651,681*4-748,681*5-842,681*6-932,673*7-931,567*8-841,558*9-747,558*10-650,558*11-553,558*12-458,552*13-458,446*14-553,441*15-650,441*16-749,440*17-842,440*19-932,443*20-932,328*21-841,321*22-748,322*23-650,322*24-554,322*25-458,315*26-458,207*27-553,201*28-650,201*29-748,201*30-842,202*31-932,194*32-932,81*33-842,74*34-748,74*35-651,74*36-553,74*37-458,74" into locationlist
set the itemDel to "*"
put the item thesum of locationlist into holder
set the itemDel to "-"
return the item 2 of holder
end setofLocations
Re: how to switch turns in a two player game
Hi kesh17aki.
so you got it working?
Coool
Best
Klaus
so you got it working?
Coool
Best
Klaus
Re: how to switch turns in a two player game
yes Klaus, thank you for the help its working
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: how to switch turns in a two player game
Hi kesh17aki,
I remember Klaus recommended me to use numtochar(1) as delimiter.
I'm a good (old) boy and I do it with success. Each time i don't hear the voice of Klaus about itemdel, i get a mistake.
http://forums.runrev.com/phpBB2/viewtop ... f=8&t=6434
Best regards
Jean-Marc
I remember Klaus recommended me to use numtochar(1) as delimiter.
I'm a good (old) boy and I do it with success. Each time i don't hear the voice of Klaus about itemdel, i get a mistake.
http://forums.runrev.com/phpBB2/viewtop ... f=8&t=6434
Best regards
Jean-Marc
https://alternatic.ch
Re: how to switch turns in a two player game
well its ok. i got it all working. but my problem is how to reset the game. i have two "Winner" cards for each players. and it has a play again and quit buttons.. my problem is if i click play again, the whole game wont reset. so i would like to reset them, like put empty into their field names, return the players into its current position w/c is their starting area. i'm still new about this and i would like to learn more about livecode. well anyway, heres the code for the "Winner" card of player 1.
Code: Select all
global pOnename,theCurrentLocation,theCurrentLocationTwo
local tButtonName,tControl
on openCard
put pOnename into fld "pWin"
end openCard
on mouseUp
set the itemDelimiter to space
put the short name of target into tButtonName
if item 1 of the name of target is "button" then
if tButtonName is "plyAgn" then
resetGame
else if tButtonName is "rollOnOff" then
gCurrentPlayer
end if
end if
end mouseUp
command resetGame
set the itemDelimiter to space
repeat with tControl = 1 to the number of controls of me
set visible of control tControl to true
end repeat
put empty into pOnename
put empty into pTwoname
put 141,669 into theCurrentLocation--current location of player1
put 275,675 into theCurrentLocationTwo
go to card "Choose Player"
end resetGame
Re: how to switch turns in a two player game
Hi kesh17aki,
...
put empty into field "whatever field you need to empty!!!" of card "another cardname, which is probably not the current card"
## Use OF CD XY (of stack yz) if the control you are addressing is not on the current card!
...
## Or did I miss something?
...
## Example:
global gOldPlayerPosition1, gOldPlayerPosition2
set the loc of btn "player 1" to gOldPlayerPosition1
set the loc of btn "player 2" to gOldPlayerPosition2
...
Check these stacks to get more of the important basics of Livecode:
http://www.runrev.com/developers/lesson ... nferences/
Best
Klaus
???kesh17aki wrote:... so i would like to reset them, like put empty into their field names...
...
put empty into field "whatever field you need to empty!!!" of card "another cardname, which is probably not the current card"
## Use OF CD XY (of stack yz) if the control you are addressing is not on the current card!
...
## Or did I miss something?
Of course you need to store the initial position of the players BEFORE you start the game the first time!kesh17aki wrote:... return the players into its current position w/c is their starting area...
...
## Example:
global gOldPlayerPosition1, gOldPlayerPosition2
set the loc of btn "player 1" to gOldPlayerPosition1
set the loc of btn "player 2" to gOldPlayerPosition2
...
Check these stacks to get more of the important basics of Livecode:
http://www.runrev.com/developers/lesson ... nferences/
Best
Klaus
Re: how to switch turns in a two player game
thanks. now i got it all working do you have any codes for 1 player vs computer? coz im planning to add another feature for this game, tnx
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: how to switch turns in a two player game
Since the game is like snakes and ladders the random number from the dice tells you how many steps the computer player can take so that part is easy. You will have to know the location of all the squares you can move to and which ones are snakes and which ones are ladders to move the player to the correct position.
All of that removes any "strategy" from the game, much easier to program.
Simon
All of that removes any "strategy" from the game, much easier to program.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: how to switch turns in a two player game
yes i know,, but in my part being a newbie.. i still need help from the experts like you hope you can help me,