//Score Play static void ScorePlay(Player Player1, Player Player2, dice GameDie) //governs the score play mode { for (int i = 1; i < 6; i++) //plays through 5 rounds { Console.WriteLine("Round {0}:", i); Console.ReadKey(); Console.WriteLine(); RoundScore(GameDie, Player1, Player2);//plays the round Console.WriteLine(); Console.WriteLine("Player 1's total is now {0}", Player1.Score);//gives the total scores for player 1 and 2 (or computer if in single player) if (!Player2.computer) { Console.WriteLine("Player 2's total is now {0}", Player2.Score); } else { Console.WriteLine("The Computer's total is now {0}", Player2.Score); } Console.ReadKey(); Console.Clear(); } ScorePlayEnd(Player1, Player2);//after playing 5 rounds goes to end subroutine which decides winner etc. }
//Match Play static void MatchPlay(Player Player1, Player Player2, dice GameDie)//governs the match play mode { int iRoundCounter = 1; while ((Player1.Score < 5) && (Player2.Score < 5)) //loops until one player gets to 5 points { Console.WriteLine("Match Play"); //introductory dialogue to the round Console.WriteLine("Round {0}:", iRoundCounter); Console.WriteLine(); Console.ReadKey(); int RoundResult = RoundMatch(GameDie, Player2.computer); //jumps to RoundMatch which runs a round of the game and returns an int value 1, 2 or 3 (1=player 2 win, 2=player 1 win, 3=draw) Console.WriteLine(); if (RoundResult == 1) //if player 2 won that round { if (!Player2.computer) //alternative outputs for singleplayer mode { Console.WriteLine("Player 2 wins round {0}", iRoundCounter); } else { Console.WriteLine("The computer wins round {0}", iRoundCounter); } Player2.Score += 1; //icrease player 2 score by one as they have won that round } else if (RoundResult == 2) //if player 1 won that round { Console.WriteLine("Player 1 wins round {0}", iRoundCounter); //output to user who won Player1.Score += 1; //increase player 1 score by one as they won the round } else { Console.WriteLine("That round was a draw");//if the result is not 1 or 2 it must be 3 which represents a draw } Console.WriteLine("Current score:"); //Gives an update on the current score Console.WriteLine("Player 1: {0}", Player1.Score); if (!Player2.computer) //alternative outputs if in singleplayer mode { Console.WriteLine("Player 2: {0}", Player2.Score); } else { Console.WriteLine("Computer: {0}", Player2.Score); } Console.WriteLine(); iRoundCounter += 1;//increments for the next round Console.ReadKey(); Console.Clear(); } MatchPlayEnd(Player1, Player2);//one player has reached 5 points therefore game is over - jumps to MatchPlayEnd to check winner and output to user }
//Turn() was modified to use a list instead of a temporary int value - 6/12/17 by M Micklewright static private int Turn(dice GameDie, int NumberOfDice)//runs a turn (used in both match and score play) - rolls dice required number of times and outputs result to console then returns highest dice value { List <int> rolls = new List <int>(); for (int i = 0; i < NumberOfDice; i++) //rolls 3, 2 or 1 times depending on what is passed { rolls.Add(GameDie.roll()); //uses the roll function from the GameDie object to randomly generate a number 1-6 , this is addedd to the list of rolls from this turn Console.Write("{0} ", rolls.Last <int>()); //output value of roll most recent roll (last roll in list) } Console.WriteLine(); return(rolls.Max <int>()); //returns the highest roll of the turn }
static int RoundMatch(dice GameDie, bool comp) //governs a round of match play and returns int value of 1,2 or 3 (1=player 2 win, 2=player 1 win, 3=draw) { int iP1Total = 0; //player 1's total score for that round , compared with player 2s at the end of the round to see who won int iP2Total = 0; for (int i = 3; i > 0; i--) //loops 3 times for player 1s section of the round - with i (number of dice rolled) decreasing from 3 by 1 each time { Console.WriteLine("Player 1 rolls..."); iP1Total += Turn(GameDie, i); //goes to Turn which rolls i number of times and returns the highest dice roll } Console.WriteLine("Total = {0}", iP1Total); //ouputs player 1's score for that round Console.ReadKey(); Console.WriteLine(); for (int i = 3; i > 0; i--) //loop 3 times for player 2s section of the round - with i (number of dice rolled) decreasing from 3 by 1 each time { if (comp) //alternative outputs for singleplayer mode { Console.WriteLine("Computer rolls..."); } else { Console.WriteLine("Player 2 rolls..."); } iP2Total += Turn(GameDie, i); //goes to Turn which rolls i number of times and returns the highest dice roll } Console.WriteLine("Total = {0}", iP2Total); //outputs player 2's total score for that round //determines who scored most and therefore won the round and returns 1, 2 or 3 to match play which interprets the ints to mean P2 win, P1 win or draw respectively if (iP2Total > iP1Total)//player 2 scores most (P2 Win) { return(1); } else if (iP2Total < iP1Total)//player 1 scores most (P1 Win) { return(2); } else//both scored the same (Draw) { return(3); } }
static void PlayGame(bool bComp)//sets up the game and gets the input for the match play or score play option { Console.Clear(); dice GameDie = new dice(); //creates the dice object that is used in the game Player Player1 = new Player(); //creates the player objects Player Player2 = new Player(); if (bComp)//if the bool comp is passed as true then the user wants singleplayer therefore the computer attribute of the player2 object must be set to true { Player2.computer = true; Console.WriteLine("Single Player:"); } else { Console.WriteLine("Two Player:"); } Console.WriteLine("1. Match Play");//outputs menu options Console.WriteLine("2. Score Play"); Console.WriteLine("9. Return to main menu"); int iMenuInput = GetMenuInput();//get input via validation function switch (iMenuInput) { case 1: Console.Clear(); MatchPlay(Player1, Player2, GameDie); break; case 2: Console.Clear(); ScorePlay(Player1, Player2, GameDie); break; default: Console.WriteLine("Error"); break; } }
static void RoundScore(dice GameDie, Player Player1, Player Player2)//governs a round of score play { int iP1Total = 0; int iP2Total = 0; for (int i = 3; i > 0; i--)//takes 3 turns with 3 dice, then 2 then 1, each time accumulating the highest value rolled in p1Total { Console.WriteLine("Player 1 rolls..."); iP1Total += Turn(GameDie, i); } Console.WriteLine("Player 1 Scored {0}", iP1Total);//outputs player 1's score for that round Console.ReadKey(); Console.WriteLine(); for (int i = 3; i > 0; i--) //takes 3 turns with 3 dice, then 2 then 1, each time accumulating the highest value rolled in p2Total { if (!Player2.computer) //writes alternative messages for single and two player { Console.WriteLine("Player 2 rolls..."); } else { Console.WriteLine("Computer rolls..."); } iP2Total += Turn(GameDie, i); } if (!Player2.computer)//writes alternative messages for single and two player { Console.WriteLine("Player 2 Scored {0}", iP2Total); } else { Console.WriteLine("The computer Scored {0}", iP2Total); } Player1.Score += iP1Total;//adds scores from that round to player totals Player2.Score += iP2Total; }