Computer Science Canada

Taking Turns in A Dice Game Question

Author:  KukuriChan [ Fri Mar 19, 2004 11:11 am ]
Post subject:  Taking Turns in A Dice Game Question

I'm doing my dice game right now but I don't know a good way to alternate between characters in the game. Since, the number of char changes every single time in the game, I'm really confused about what I can do so that the players would take turns rolling the die. I did think about using arrays but I'm still not sure what to do. Please help me. ^^ [/code]

Author:  recneps [ Fri Mar 19, 2004 11:25 am ]
Post subject: 

Use a counter, after someone goes, the counter goes up by one like
loop
rolldice
c+=1
end loop

then, in the loop, have it mod the counter by the number of people.
eg. num ppl = 4
if c mod ppl =0 then
put "Player 1's turn."
elsif c mod ppl =1 then
put "player 2's turn"
elsisf c mod ppl = 2 then
put "Player 3's turn"
elsif c mod ppl = 3 then
put "player 4's turn."
end if


and so on, that should work. Wink

Author:  KukuriChan [ Fri Mar 19, 2004 11:31 am ]
Post subject: 

So, if the number of characters differ each time it's played, do I just make a counted loop?

Author:  KukuriChan [ Fri Mar 19, 2004 11:39 am ]
Post subject: 

I did that. But there's something wrong. Sad Here's the code.

code:
var roll, count, numplayers : int
count := 0

put "Number of players: " ..
get numplayers

loop
delay (200)
    count += 1
    for i : 1 .. numplayers
        if count mod numplayers = 0 then
            put "Player 1's turn"
        elsif count mod numplayers = 1 then
            put "Player 2's turn"
        elsif count mod numplayers = 2 then
            put "Player 3's turn"
        elsif count mod numplayers = 3 then
            put "Player 4's turn"
        end if
    end for
end loop

Author:  Paul [ Fri Mar 19, 2004 11:50 am ]
Post subject: 

The way you set up your code, each player goes 4 times? I dunno, if your doing this for your flight game, each player rolls once each turn right? There can only be 4 players, so you should limit it.

Heres a simple example showing however number of players within 4, taking turns rolling the die
code:

var players, roll: int
loop
put "Enter number of players 1-4"
get players
if players >0 and players <=4 then
cls
exit
end if
cls
end loop
loop
for a: 1..players
randint (roll, 1,6)
if a=1 then
put "Player ", a, " Rolled a ", roll
elsif a=2 then
put "Player ", a, " Rolled a ",roll
elsif a=3 then
put "Player ", a, " Rolled a ", roll
elsif a=4 then
put "Player ", a, " Rolled a ", roll
end if
delay (1000)
end for
cls
end loop

Author:  KukuriChan [ Fri Mar 19, 2004 12:09 pm ]
Post subject: 

Hey it worked! Plus, it's what I want. Thanks Paul.You're such a big help. You too recneps. Smile

Author:  recneps [ Fri Mar 19, 2004 1:57 pm ]
Post subject: 

Yeah, well Paul's is simpler ;D oh well, thats how i did mine for tic tac toe, i had it mod 2, if its 0, then its player 1 turn, if its 1 then player 2 turn ;D

Author:  SuperGenius [ Fri Mar 19, 2004 2:14 pm ]
Post subject: 

code:

for a: 1..players
randint (roll, 1,6)
if a=1 then
put "Player ", a, " Rolled a ", roll
elsif a=2 then
put "Player ", a, " Rolled a ",roll
elsif a=3 then
put "Player ", a, " Rolled a ", roll
elsif a=4 then
put "Player ", a, " Rolled a ", roll
end if


just nitpicking, but this if structure isnt required is it?
Couldnt this be replaced with

code:

for a: 1..players
randint (roll, 1,6)
put "Player ", a, " Rolled a ", roll
end if

Author:  KukuriChan [ Fri Mar 19, 2004 5:34 pm ]
Post subject: 

It could be but then there's a lot of other things I have to check for:

(1) If the player rolls a 6, then it can move either a pawn in the waitng area or move a pawn that's already out(since there's a limit. The maximum number of pawns you can have is 4.) Once you have all four out, you can choose which pawn you want to move. If the player doesn't get a six, then it can't move and it goes on to the next player <-- that means that I have to have a loop and then make a lot of if statements right?
(2) There are 4 different colours that can be chosen : red, blue, green, or yellow. When the game starts, each player chooses a colour. Then it goes into the actual game where you start rolling the die.
(3) There are different colours on the board. If the player lands on the colour that they chose, they can skip to the next colour square that they chose.
and some other things I have to check for.

So I think the if statement structure is perfect. But... I'm stuck on the first part where I have to check for stuff. I have part of it in code but there's some error in it, and I don't know how to fix it. ^^

code:
var players, roll, move1, move2, move3, move4, pawn, numpawns : int
var player1, player2, player3, player4 : boolean := false
move1 := 0
move2 := 0
move3 := 0
move4 := 0

loop
    put "Enter number of players 1-4"
    get players
    put "Enter number of pawns 1-4"
    get numpawns
    if players >= 2 and players <= 4 and numpawns > 0 and numpawns <= 4 then
        cls
        exit
    end if
    cls
end loop

loop
    for a : 1 .. players
        randint (roll, 1, 6)
        if a = 1 then
            put "Player ", a, " Rolled a ", roll
            if roll = 6 then
                player1 := true
                put "Which Pawn do you want to move?"
                get pawn
                put "Pawn", pawn, "moved into 'Start Flying' mode"
                exit
            end if
            if roll <= 5 and player1 = true then
                put "Which Pawn do you want to move?"
                get pawn
                assert pawn > 0
                if pawn = 1 then
                    move1 := move1 + roll
                    put "Pawn ", pawn, " moved", move1
                elsif pawn = 2 then
                    move2 := move2 + roll
                    put "Pawn ", pawn, " moved", move2
                elsif pawn = 3 then
                    move3 := move3 + roll
                    put "Pawn ", pawn, " moved", move3
                elsif pawn = 4 then
                    move4 := move4 + roll
                    put "Pawn ", pawn, " moved", move4
                end if
                exit
            end if
        elsif a = 2 then
            put "Player ", a, " Rolled a ", roll
            if roll = 6 then
                player2 := true
                put "Which Pawn do you want to move?"
                get pawn
                assert pawn > 0
                put "Pawn ", pawn, " moved into 'Start Flying' mode"
                exit
            end if
            if roll <= 5 and player2 = true then
                put "Which Pawn do you want to move?"
                get pawn
                assert pawn > 0
                if pawn = 1 then
                    move1 := move1 + roll
                    put "Pawn ", pawn, " moved", move1
                elsif pawn = 2 then
                    move2 := move2 + roll
                    put "Pawn ", pawn, " moved", move2
                elsif pawn = 3 then
                    move3 := move3 + roll
                    put "Pawn ", pawn, " moved", move3
                elsif pawn = 4 then
                    move4 := move4 + roll
                    put "Pawn ", pawn, " moved", move4
                end if
                exit
            end if
        elsif a = 3 then
            put "Player ", a, " Rolled a ", roll
            if roll = 6 then
                player3 := true
                put "Which Pawn do you want to move?"
                get pawn
                assert pawn > 0
                put "Pawn ", pawn, " moved into 'Start Flying' mode"
                exit
            end if
            if roll <= 5 and player3 = true then
                put "Which Pawn do you want to move?"
                get pawn
                if pawn = 1 then
                    move1 := move1 + roll
                    put "Pawn ", pawn, " moved", move1
                elsif pawn = 2 then
                    move2 := move2 + roll
                    put "Pawn ", pawn, " moved", move2
                elsif pawn = 3 then
                    move3 := move3 + roll
                    put "Pawn ", pawn, " moved", move3
                elsif pawn = 4 then
                    move4 := move4 + roll
                    put "Pawn ", pawn, " moved", move4
                end if
                exit
            end if
        elsif a = 4 then
            put "Player ", a, " Rolled a ", roll
            if roll = 6 then
                player4 := true
                put "Which Pawn do you want to move?"
                get pawn
                assert pawn > 0
                put "Pawn ", pawn, " moved into 'Start Flying' mode"
                exit
            end if
        elsif roll <= 5 and player4 = true then
            put "Which Pawn do you want to move?"
            get pawn
            if pawn = 1 then
                move1 := move1 + roll
                put "Pawn ", pawn, " moved", move1
               
            elsif pawn = 2 then
                move2 := move2 + roll
                put "Pawn ", pawn, " moved", move2
            elsif pawn = 3 then
                move3 := move3 + roll
                put "Pawn ", pawn, " moved", move3
            elsif pawn = 4 then
                move4 := move4 + roll
                put "Pawn ", pawn, " moved", move4
                 exit
            end if
           
            delay (1000)
        end if
        cls
    end for
end loop

Author:  Thuged_Out_G [ Fri Mar 19, 2004 5:37 pm ]
Post subject: 

well, your asking for 1-4 players correct?

but in your code, you only allow 2-4 players....i dont even understand wahts going on in the game

Author:  Paul [ Fri Mar 19, 2004 5:39 pm ]
Post subject: 

that would be simpler, but I was going for the one thats ez to understand yes?

Author:  KukuriChan [ Fri Mar 19, 2004 5:43 pm ]
Post subject: 

I forgot to mention that the game has to have at least 2 people playing. ^^ That's why I put 2-4 people in the code.

Author:  Paul [ Fri Mar 19, 2004 6:21 pm ]
Post subject: 

Yea, I kinda figured that after, but hey, I use to play this by myself when I was sick back then Very Happy

Author:  KukuriChan [ Fri Mar 19, 2004 7:47 pm ]
Post subject: 

I can't seem to figure out how I can fix the error though..... I mean, I changed the number of characters but then it keeps on repeating the same player over and over again. I don't know how I can fix it. Sad

Author:  SuperGenius [ Fri Mar 19, 2004 8:09 pm ]
Post subject: 

I've improved on the form of this signifigantly I think, and as you can see the number on lines is a lot less this way. It's just more convenient.
By the way, You are trying to make the game Parchesi, are you not?
code:

var players, roll, move1, move2, move3, move4, pawn, numpawns, playernum : int
var player : array 1 .. 4 of boolean
move1 := 0
move2 := 0
move3 := 0
move4 := 0
for a : 1 .. 4
    player (a) := false
end for
proc movedecisions
    put "Player ", playernum, " Rolled a ", roll
    if roll = 6 then
        player (playernum) := true
        put "Which Pawn do you want to move?"
        get pawn
        put "Pawn", pawn, "moved into 'Start Flying' mode"
    end if
    if roll <= 5 and player (playernum) = true then
        put "Which Pawn do you want to move?"
        get pawn
        assert pawn > 0
        if pawn = 1 then
            move1 := move1 + roll
            put "Pawn ", pawn, " moved", move1
        elsif pawn = 2 then
            move2 := move2 + roll
            put "Pawn ", pawn, " moved", move2
        elsif pawn = 3 then
            move3 := move3 + roll
            put "Pawn ", pawn, " moved", move3
        elsif pawn = 4 then
            move4 := move4 + roll
            put "Pawn ", pawn, " moved", move4
        end if
    end if
end movedecisions

loop
    put "Enter number of players 1-4"
    get players
    put "Enter number of pawns 1-4"
    get numpawns
    if players >= 2 and players <= 4 and numpawns > 0 and numpawns <= 4 then
        cls
        exit
    end if
    cls
end loop

loop
    for a : 1 .. players
        randint (roll, 1, 6)
        playernum := a
        movedecisions
        delay (1000)
        cls
    end for
end loop

Author:  Paul [ Fri Mar 19, 2004 8:54 pm ]
Post subject: 

Hm, I thought you could only move airplanes that are already in flight mode.
oh yea, how do you plan to move the pieces kukrichan?

Author:  KukuriChan [ Sat Mar 20, 2004 12:18 am ]
Post subject: 

Thanks SuperGenius. ^^

Paul: I'm planning on using arrays to move the pawns. See, I already have all the x and y coordinates for each circle so I was thinking that if I add the number that the player rolled onto the original number in that particular array, it can just draw everything and then the pawn will appear whatever the position in the array will be. But... I don't know if that'll really work yet since I haven't tried. ^^;; I'm hoping that'll work. But I have to remember that once they move the airplane, it'll have it's own path to take. So, that means, I have to make four different arrays for the path of each colour right? Smile

Author:  SuperGenius [ Sun Mar 21, 2004 4:54 pm ]
Post subject: 

your welcome.

Your problem of drawing the pieces is kind of like what i did for my connect 4 game, except more complicated. I had a 2d array that held coords. and then this structure to draw the circles, with a little 'formula' that would convert the coords on the board to pixel coords on the screen.

code:

proc drawboard
    for h : 1 .. 8
        for c : 1 .. 8
            if board (h, c) not= " " then
                if board (h, c) = "RED" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, red)
                elsif board (h, c) = "GREEN" then
                    Draw.FillOval (130 + c * 60, 80 + h * 60, 25, 25, green)
                end if
            end if
        end for
    end for
end drawboard


perhaps the same concept would work for your project?

Author:  KukuriChan [ Sun Mar 21, 2004 8:15 pm ]
Post subject: 

Oh, I have the board drawn out already. I used PhotoPaint to do that. I just have to figure out a way of using arrays to move the pieces and then redraw all the pieces. That part is giving me a headache. Sad

Author:  SuperGenius [ Tue Mar 23, 2004 10:55 pm ]
Post subject: 

What i was suggesting was a method to update the screen after each move. As for updating the arrays, i dont know. The fact that the path turns and twists presents a bit of a challenge. Perhaps you could have a loop that would move the piece one square forward at a time, with the direction that it's moving in as a variable that would be changed when the piece rests on a corner space.

Author:  Paul [ Wed Mar 24, 2004 2:19 pm ]
Post subject: 

Wow, thats difficult. But as a suggestion, if I were doing this program I would either map out the whole thing with a 2D array or something, or I'd draw the whole board again, from a text file, somethink like isometrics. Then you can set rules that it can only move in one direction. Also check to win or something, just my opinion.

Author:  KukuriChan [ Wed Mar 24, 2004 10:46 pm ]
Post subject: 

Hey thanks for your suggestions/advice you guys. I ended up doing it with a two dimensional array. Now the only thing I have to do is to get it to work when it comes to move the pawns on the actual path when you get the value you roll from the die. I have to do the checking stuff too. But I don't think that'll be that hard after I get the pawns moving.

^^


: