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
Taking turns in C#
Started by Alex_j, Mar 30 2010 06:40 AM
4 replies to this topic
#1
Posted 30 March 2010 - 06:40 AM
|
|
|
#2
Posted 30 March 2010 - 07:16 AM
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
Blue Rose Games - XNA Game Programming Tutorials
#3
Posted 30 March 2010 - 07:32 AM
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
Posted 30 March 2010 - 08:02 AM
Sorry, here's the code:
P.S. This is just a quick attempt it will have to be put into methods etc. and generally just sorted out.
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
Posted 30 March 2010 - 09:37 AM
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++;
}


Sign In
Create Account


Back to top









