Jump to content

Taking turns in C#

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi, I've been asked to create a simple game in C#, I have the basic code down however I need to make it so that Player 1 has their turn then player 2, at the minute both players take their turns at the same time when a key is pressed, how can I change this?

Thanks

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
we don't give you the source code here , but try the link below see the last section on Hockey u have all that you ask in there
Blue Rose Games - XNA Game Programming Tutorials

#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
If we could see your code we might be able to help. The info you gave us tells us little to nothing about how your game is coded and thus a solution is not likely. With a little code posted this should be an easy fix.

#4
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Sorry, here's the code:

class Program

    {

        static void Main(string[] args)

        {

            int playerOnePosition = 0,

          playerOneToken = 0,

          playerTwoPosition = 0,

          playerTwoToken = 0;



            Console.WriteLine("Press R to roll the dice...");

            string choice = Console.ReadLine();


            bool flag = true;

            while (flag)

            {


                if (choice == "R")

                {


                    Random myRandomiser = new Random();

                    die myDice = new die(myRandomiser);


                    int player1Roll = myDice.Roll();

                    int player2Roll = myDice.Roll();


                    Console.WriteLine("Player One you rolled a {0}", player1Roll);

                    Console.WriteLine("Player Two you rolled a {0}", player2Roll);


                    playerOnePosition += player1Roll;

                    Console.WriteLine("Player One you are now on square {0}", playerOnePosition);


                    playerTwoPosition += player2Roll;

                    Console.WriteLine("Player One you are now on square {0}", playerTwoPosition);


                }


                if (playerOnePosition >= 36)

                {

                    playerOnePosition = 0;

                    playerOneToken++;


                    Console.WriteLine("P1 Wins!");

                    Console.WriteLine("P1 you now have {0} tokens", playerOneToken);

                }


                if (playerTwoPosition >= 36)

                {

                    playerTwoPosition = 0;

                    playerTwoToken++;


                    Console.WriteLine("P2 Wins!");

                    Console.WriteLine("P2 you now have {0}", playerTwoToken);

                }


                if (playerTwoToken == 5)

                {

                    Console.WriteLine("PLAYER TWO WINS!!!");

                }


                Console.ReadLine();


            }

        }

P.S. This is just a quick attempt it will have to be put into methods etc. and generally just sorted out.

#5
Deadlock

Deadlock

    Learning Programmer

  • Members
  • PipPipPip
  • 81 posts
If you want the turn to alternate. you can create an "int" variable, call it turn. And every time check, If its even, then its player1's turn, otherwise its player2's turn.

if(turn%2==0) //if turn is even
{
//player1 turn
turn++;
}
else //otherwise its odd
{
//player2 turn
turn++;
}