Jump to content

Help with Taking turns..

- - - - -

  • Please log in to reply
3 replies to this topic

#1
johnm1191

johnm1191

    Newbie

  • Members
  • Pip
  • 2 posts
I'm taking an intro to c# class, and my teacher has asked us to make a 2 player board game with a dice that lets players take turns rolling.. I have written the code, but for some reason my game isn't taking turns with the players rolling, and it only lets player 1 roll. If anyone could offer any helpful suggestions or point out what's wrong in my code, i would appreciate it.

My players are labeled avatar, and they are a string with 2 elements.
Player[] avatar = new Player[2]; 

My Player Turning code:

 protected override void Update(GameTime gameTime)

        {

            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                this.Exit();


            // TODO: Add your update logic here


            


            for (int j = 0; j < 2; j++)

                if (avatar[j].TurnSpent == false)

                {

                    PlayerTurn(j);

                    avatar[j].TurnSpent = true;

                }

                 foreach (Player a in avatar)

                     a.TurnSpent = false;

            

            base.Update(gameTime);

        }

My playerTurn Method:

public void PlayerTurn( int j)

        {            

            KeyboardState cur = Keyboard.GetState();

            

                if(avatar[j].TurnSpent == false)

            {

                if (cur.IsKeyDown(Keys.Space) && !old.IsKeyDown(Keys.Space) && (avatar[j].Position < 49))

                {

                    // handle space down

                    int tempPos = 0;

                    int i = d.Roll();

                    avatar[j].Move(i);

                    message = "Player 1 rolled a " + i;

                    tempPos = avatar[j].Position;

                    avatar[j].Move(adjustment[space_types[avatar[j].Position]]);

                    if (space_types[tempPos] == 1)

                        space_types[tempPos] = 2;

                    avatar[j].Roll();

                }

                else if (avatar[j].Position == 49)

                {

                }

                old = cur;                

            }

        }

My PlayerClass:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace JohnGame

{

    public class Player

    {

        private int position;

        private int rollCount;

        private bool turnSpent;



        public bool TurnSpent

        {

            get { return turnSpent; }

            set { turnSpent = value; }

        }


        public int Position

        {

            get { return position; }

            set { position = value; }

        }

        public int RollCount

        {

            get { return rollCount; }

            set { rollCount = value; }

        }

        public Player()

        {

            position = 0;

            RollCount = 0;

            turnSpent = false;

        }

        private void SetPosition(int pos)

        {

            if (pos >= 49)

                pos = 49;

            if (pos <= 0)

                pos = 0;

            position = pos;

        }

        public void Move(int i)

        {

            SetPosition(position + i);

        }

        public void Roll()

        {

            RollCount++;

        }

    }

}


Thanks,
John

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You hard coded the string to say "Player 1 rolled..." That's why it looks like player 1 always takes a turn.

Quote


 for (int j = 0; j < 2; j++)

                if (avatar[j].TurnSpent == false)

                {

                    PlayerTurn(j);

                    avatar[j].TurnSpent = true;

                }

                 foreach (Player a in avatar)

                     a.TurnSpent = false;


Seems like here you're resetting both players' TurnSpent every time the game Update function is called, regardless of whether player 1 has moved or not. You're also calling PlayerTurn on both players each time the Update function is called, which wastes time. You only need to call it for the player whose turn it currently is.

The way I would do this would be to create a class variable to store which player's turn it currently is, and flip this if the player has taken their turn. So for example, in your main class, you'd have:

int player_turn = 0; // Player 1 goes first

Then, your Update function would look like this:

protected override void Update(GameTime gameTime)

        {

            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

                this.Exit();


            // TODO: Add your update logic here


            PlayerTurn(player_turn);

            

            base.Update(gameTime);

        }

And finally, your PlayerTurn function would look like this:

public void PlayerTurn( int j)

        {            

            KeyboardState cur = Keyboard.GetState();

            

                if (cur.IsKeyDown(Keys.Space) && !old.IsKeyDown(Keys.Space) && (avatar[j].Position < 49))

                {

                    // handle space down

                    int tempPos = 0;

                    int i = d.Roll();

                    avatar[j].Move(i);

                    // Notice, you had the string hard coded here. I fixed that.

                    message = "Player " + (player_turn + 1).ToString() + " rolled a " + i.ToString();

                    tempPos = avatar[j].Position;

                    avatar[j].Move(adjustment[space_types[avatar[j].Position]]);

                    if (space_types[tempPos] == 1)

                        space_types[tempPos] = 2;

                    avatar[j].Roll();


                    // The player's turn is over, switch the turn to the other player.

                    if (player_turn == 0)

                        player_turn = 1;

                    else

                        player_turn = 0;

                }

                else if (avatar[j].Position == 49)

                {

                }

                old = cur;

        }


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
johnm1191

johnm1191

    Newbie

  • Members
  • Pip
  • 2 posts
This works perfectly! I didn't think about a switch. I had tried detecting if the number was even or odd before the boolean attempt, but that didn't work either. This did the trick perfectly.
Thanks again,
John

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts

gregwarner said:

Seems like here you're resetting both players' TurnSpent every time the game Update function is called, regardless of whether player 1 has moved or not. You're also calling PlayerTurn on both players each time the Update function is called, which wastes time. You only need to call it for the player whose turn it currently is.

If you look carefully at that, it's just bad indenting. There is no open { on the for line so it just uses the next statement for the loop. That would be just the if.

This is one of the reasons I advocate the use of {} in (almost) all cases. You will thank yourself later.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users