static void Main(string[] args) { List <Player> players = new List <Player>(); List <Match> matches = new List <Match>(); List <Round> rounds = new List <Round>(); Random rand = new Random(); Random randomNumber = new Random(rand.Next()); // create a random number object // create the Computer that everyone plays against. //call methos CreatePlayer() with player name. // Player p1 = new Player() // { // Fname = "Max", // Lname = "Headroom" // }; // players.Add(p1); RpsGameRepositoryLayer gameContext = new RpsGameRepositoryLayer(); Player p1 = gameContext.CreatePlayer("Max", "HeadRoom"); // create the Console.WriteLine("This is The Official Batch Rock-Paper-Scissors Game"); // program loop starts here. int logInOrQuitInt; do {//Menu to log in or quit. start loop for logged in player. exits when he logs out do { Console.WriteLine("Enter 1 to log in and 2 to quit the program."); string logInOrQuit = Console.ReadLine(); bool logInOrQuitBool = int.TryParse(logInOrQuit, out logInOrQuitInt); if (logInOrQuitBool == false) { Console.WriteLine("I SAID... Enter 1 to play again, Enter 2 to Log out. Get it right!"); } } while (logInOrQuitInt != 1 && logInOrQuitInt != 2); if (logInOrQuitInt == 2) { break; } //log in or create a new player. unique fName and lName means create a new player, other wise, grab the existing player string[] userNamesArray; do { Console.WriteLine("\n\tPlease enter your first and last name.\n\tIf you enter unique first and last name I will create a new player.\n"); string userNames = Console.ReadLine(); userNamesArray = userNames.Split(' '); } while (userNamesArray[0] == ""); Player p2 = players.Where(x => x.Fname == userNamesArray[0] && x.Lname == userNamesArray[1]).FirstOrDefault(); if (p2 == null) // if this is a new player { if (userNamesArray.Length == 1) //if the user unputted just one name { p2 = new Player(); p2.Fname = userNamesArray[0]; } if (userNamesArray.Length > 1) //if the user unputted 2 names { p2 = new Player(); p2.Fname = userNamesArray[0]; p2.Lname = userNamesArray[1]; } players.Add(p2); } int response1Parsed; do //game loop starts here. { Match match = new Match(); match.Player1 = p1; match.Player2 = p2; Console.WriteLine("\n\tStarting the game...\n"); do // start loop to last till one player wins 2 games. { Round round = new Round(); Choice userChoice; // declare these two variables to be used int he do/while loop bool userResponseParsed; do { Console.WriteLine($"Welcome, {p2.Fname}. Please choose Rock, Paper, or Scissors by typing 0, 1, or 2 and hitting enter."); Console.WriteLine("\t0. Rock \n\t1. Paper \n\t2. Scissors"); string userResponse = Console.ReadLine(); // read the users unput userResponseParsed = Choice.TryParse(userResponse, out userChoice); // parse the users input to am int if (userResponseParsed == false || ((int)userChoice > 2 || (int)userChoice < 0)) // give a message if the users unput was invalid { Console.WriteLine("Your response is invalid."); } } while (userResponseParsed == false || (int)userChoice > 2 || (int)userChoice < 0); // state conditions for if we will repeat the loop Choice computerChoice = (Choice)randomNumber.Next(2); // get a randon number 1, 2, or 3. round.Player1Choice = computerChoice; round.Player2Choice = userChoice; Console.WriteLine($"The computer choice is => {round.Player1Choice}."); // compare the numebrs to see who won. if (userChoice == computerChoice) // is the playes tied { Console.WriteLine("This game was a tie"); //rounds.WinningPlayer is default null rounds.Add(round); match.Rounds.Add(round); match.RoundWinner(); // send in the player who won. empty args means a tie round } else if (((int)userChoice == 1 && (int)computerChoice == 0) || // if the user won ((int)userChoice == 2 && (int)computerChoice == 1) || ((int)userChoice == 0 && (int)computerChoice == 2)) { Console.WriteLine("Congrats. You (the user) WON!."); // if the computer won. round.WinningPlayer = p2; rounds.Add(round); match.Rounds.Add(round); match.RoundWinner(p2); } else { Console.WriteLine("We're sorry. The computer won."); round.WinningPlayer = p1; rounds.Add(round); match.Rounds.Add(round); match.RoundWinner(p1); } } while (match.MatchWinner() == null);// end the game when once a player wins 2 rounds Console.WriteLine("\n\tPrinting out the Game information"); foreach (Round round1 in match.Rounds) { Console.WriteLine($"\nROUND - \nThe GUID is {round1.RoundId}.\n P1 Choice is {round1.Player1Choice}\n P2 Choice is {round1.Player2Choice}\nThe winning player is {round1.WinningPlayer.Fname}"); } do { Console.WriteLine("Do you want to play again? Enter 1 to play again, Enter 2 to Log out."); string response1 = Console.ReadLine(); bool r1pBool = int.TryParse(response1, out response1Parsed); if (r1pBool == false) { Console.WriteLine("I SAID... Enter 1 to play again, Enter 2 to Log out. Get it right!"); } } while (response1Parsed != 1 && response1Parsed != 2); // play again or quit. } while (response1Parsed == 1); // end of the game loop. } while (logInOrQuitInt != 2); // log out }
static void Main(string[] args) { // List<Player> players = new List<Player>(); // List<Match> matches = new List<Match>(); // List<Round> rounds = new List<Round>(); // Random rand = new Random(); // Random randomNumber = new Random(rand.Next()); // create a random number object // create the Computer that everyone plays against. //call methos CreatePlayer() with player name. // Player p1 = new Player() // { // Fname = "Max", // Lname = "Headroom" // }; // players.Add(p1); Player p1 = gameContext.CreatePlayer("Max", "HeadRoom"); // create the computer Console.WriteLine("This is The Official Batch Rock-Paper-Scissors Game"); // program loop starts here. int logInOrQuitInt; do { //Menu to log in or quit. start loop for logged in player. exits when he logs out logInOrQuitInt = MainMenu(); if (logInOrQuitInt == 2) { break; } //log in or create a new player. unique fName and lName means create a new player, other wise, grab the existing player string[] userNamesArray = GetUserNames(); Player p2; if (userNamesArray.Length == 1) //if the user unputted just one name { p2 = gameContext.CreatePlayer(fName: userNamesArray[0]); } else if (userNamesArray.Length > 1) //if the user unputted 2 names { p2 = gameContext.CreatePlayer(userNamesArray[0], userNamesArray[1]); } else { throw new ArgumentNullException("User input for name was invalid."); } // if the userNamesArray is empty. int response1Parsed; do //game loop starts here. { Match match = gameContext.CreateMatch(p1, p2); Console.WriteLine("\n\tStarting the Match...\n"); do // start loop to last till one player wins 2 games. { Choice userChoice = UserChoiceMenu(match); Choice computerChoice = gameContext.GetRandomChoice(); // get a randon number 1, 2, or 3. Round round = gameContext.PlayRound(match, computerChoice, userChoice); Console.WriteLine($"The computer choice is => {round.Player1Choice}."); TellUserWhoWonTheRound(round); } while (match.MatchWinner().Fname == "null");// end the game when once a player wins 2 rounds gameContext.UpdateWinLossRecords(match); gameContext.AddCompletedMatch(match); Console.WriteLine("\n\tPrinting out the Game information"); do { Console.WriteLine("Do you want to play again? Enter 1 to play again, Enter 2 to Log out."); string response1 = Console.ReadLine(); bool r1pBool = int.TryParse(response1, out response1Parsed); if (r1pBool == false) { Console.WriteLine("I SAID... Enter 1 to play again, Enter 2 to Log out. Get it right!"); } } while (response1Parsed != 1 && response1Parsed != 2); // play again or quit. } while (response1Parsed == 1); // end of the game loop. } while (logInOrQuitInt != 2); // log out // get all inputted info and pront it to the console. List <Player> players = gameContext.GetPlayers(); List <Round> rounds = gameContext.GetRounds(); List <Match> matches = gameContext.GetMatches(); PrintPlayersData(players); PrintRoundsData(rounds); PrintGamesData(matches); }// END Main end
static void gameSelect(int selectedGame, Player p1, Player p2, List <Player> players, bool loggedIn, Match match, Round round) { do { // main menu Console.WriteLine($"\nWelcome, {p2.FName}.\n"); Console.WriteLine("1) 1 Round"); Console.WriteLine("2) Best of 3"); Console.WriteLine("3) Best of 5"); Console.WriteLine("4) Logout"); Console.WriteLine("5. Exit"); Console.Write("Please choose a game mode: "); try // error catching on selectedGame { selectedGame = Convert.ToInt32(Console.ReadLine()); } catch (Exception) { selectedGame = 6; } switch (selectedGame) { case 1: { int playerChoiceInt = 0; Console.WriteLine("Single Round\n"); Console.WriteLine($"Welcome, {p2.FName}."); Console.WriteLine("1) Rock"); Console.WriteLine("2) Paper"); Console.WriteLine("3) Scissors"); Console.WriteLine("4) Exit / Previous Menu"); Console.Write("Please make a selection: "); try // error catching { playerChoiceInt = Convert.ToInt32(Console.ReadLine()); } catch (Exception) { Console.WriteLine("Invalid Entry."); selectedGame = 5; } round = oneRound(p1, p2, playerChoiceInt); if (round.WinningPlayer == p2) { match.RoundWinner(p2); Console.WriteLine("\n" + round.Player2Choice.ToString() + " beats " + round.Player1Choice.ToString() + ". You win!\n"); } else if (round.WinningPlayer == p1) { match.RoundWinner(p1); Console.WriteLine("\n" + round.Player1Choice.ToString() + " beats " + round.Player2Choice.ToString() + ". You lose.\n"); } else if (round.WinningPlayer == null) { match.RoundWinner(null); Console.WriteLine("\nBoth players chose " + round.Player2Choice.ToString() + ". Game is a tie.\n"); } break; } case 2: { /* Choice playerChoice = new Choice(); * int pWins = 0; * int cWins = 0; * while (pWins < 2 && cWins < 2 && (int)playerChoice != 4) * { * // Best of 3 menu * Console.WriteLine("\nBest of 3 - Current score: P " + pWins + " | C " + cWins); * drawGameMenu(); * try * { * playerChoice = (Choice)Convert.ToInt32(Console.ReadLine()); * } * catch (Exception){ * selectedGame = 5; * } * // run oneRound then adjust new scores * int roundResult = oneRound((int)playerChoice); * if ((int)roundResult == 1) * { * pWins++; * } * else if ((int)roundResult == 2) * { * cWins++; * } * } * // score checking * if (pWins > cWins && inInt != 4) * { * Console.WriteLine("\nYou win Best of 3!\n"); * } * else if (cWins > pWins || inInt == 4) * { * Console.WriteLine("\nYou lose Best of 3.\n"); * } */ break; } case 3: { /* int inInt = 0; * int pWins = 0; * int cWins = 0; * while (pWins < 3 && cWins < 3 && inInt != 4) * { * // Best of 5 menu * Console.WriteLine("\nBest of 5 - Current score: P " + pWins + " | C " + cWins); * drawGameMenu(); * try // error catching on selectedGame * { * inInt = Convert.ToInt32(Console.ReadLine()); * } * catch (Exception){ * selectedGame = 5; * } * if (inInt == 4) * break; * // run oneRound then adjust new scores * int newRound = oneRound((int)playerChoice); * if (newRound == 1) * { * pWins++; * } * else if (newRound == 2) * { * cWins++; * } * } * // score checking * if (pWins > cWins && inInt != 4) * { * Console.WriteLine("\nYou win Best of 5!\n"); * } * else if (cWins > pWins || inInt == 4) * { * Console.WriteLine("\nYou lose Best of 5.\n"); * } */ break; } case 4: loggedIn = false; userLogin(selectedGame, p1, p2, players, loggedIn, match, round); break; case 5: Environment.Exit(0); break; default: { // catches any invalid selectedGame Console.WriteLine("Invalid Game Selection. Please enter a valid choice.\n"); break; } } } while (selectedGame != 5 && loggedIn); }
static void userLogin(int selectedGame, Player p1, Player p2, List <Player> players, bool loggedIn, Match match, Round round) { foreach (Player p in players) { Console.WriteLine($"{p.FName} {p.LName} {p.PlayerId}"); } string userName = ""; string[] userNameArray; int m1 = 0; Console.WriteLine("1) Enter Player Name (Unique name will create new Player)"); Console.WriteLine("2) Exit"); Console.Write("Please make a selection:"); //log in or create a new player - unique information will create new player try // error catching on selectedGame { m1 = Convert.ToInt32(Console.ReadLine()); } catch (Exception) { Console.WriteLine("Invalid Entry."); m1 = 3; } switch (m1) { case 1: { Player pTemp = new Player(); do { Console.Write("Please enter First and Last Name separated by ' ': "); userName = Console.ReadLine(); userNameArray = userName.Split(' '); } while (userName == "" || userName == null); if (userNameArray.Length == 1) { pTemp.FName = userNameArray[0]; } else if (userNameArray.Length > 1) { pTemp.FName = userNameArray[0]; pTemp.LName = userNameArray[1]; } Console.WriteLine($"{pTemp.FName} {pTemp.LName} {pTemp.PlayerId}"); foreach (Player p in players) { Console.WriteLine($"{p.FName} {p.LName} {p.PlayerId}"); } p2 = userVerify(players, pTemp); if (p2.PlayerId == pTemp.PlayerId) { players.Add(p2); Console.WriteLine("New player successfully added. Enjoy."); loggedIn = true; } else { Console.WriteLine("User logged in. Welcome back."); loggedIn = true; } gameSelect(selectedGame, p1, p2, players, loggedIn, match, round); break; } case 2: Environment.Exit(0); break; default: break; } }
// method to run one round of RPS static Round oneRound(Player p1, Player p2, int playerChoiceInt) { Round currentRound = new Round(); var rand = new Random((int)DateTime.Now.Millisecond); Choice playerChoice = (Choice)playerChoiceInt; Choice compChoice = (Choice)rand.Next(1, 4); currentRound.Player2Choice = playerChoice; currentRound.Player1Choice = compChoice; switch ((int)playerChoice) { case 1: { if ((int)compChoice == 1) { currentRound.WinningPlayer = null; } else if ((int)compChoice == 2) { currentRound.WinningPlayer = p1; } else if ((int)compChoice == 3) { currentRound.WinningPlayer = p2; } return(currentRound); } case 2: { if ((int)compChoice == 1) { currentRound.WinningPlayer = p2; } else if ((int)compChoice == 2) { currentRound.WinningPlayer = null; } else if ((int)compChoice == 3) { currentRound.WinningPlayer = p1; } return(currentRound); } case 3: { if ((int)compChoice == 1) { currentRound.WinningPlayer = p1; } else if ((int)compChoice == 2) { currentRound.WinningPlayer = p2; } else if ((int)compChoice == 3) { currentRound.WinningPlayer = null; } return(currentRound); } default: return(currentRound = new Round()); } }
static void Main(string[] args) { Console.WriteLine("This is The Official Batch Rock-Paper-Sissors Game"); //Console.WriteLine("Please choose Rock, Paper, or Scissors by typing 1, 2, or 3 Aand hiiting enter."); //Console.WriteLine("\t1. Rock \n\t2. Paper \n\t3. Scissors"); //Console.WriteLine("2. Paper"); //Console.WriteLine("3. Scissors"); //string userResponse = Console.ReadLine(); //var userResponse = Console.ReadLine(); //var will make it the proper type 'string' //Console.WriteLine(userResponse); //int userResponseInt = int.Parse(userResponse); //Will have an exception if noyt an int //try {int userResponseInt = int.Parse(userResponse);} //catch (FotmatException ex) {//throw new FormatException("There was a problem with parsing the user input", ex);} /* * int number; * bool userResponseParsed = int.TryParse(userResponse, out number); * if(userResponseParsed == false) { * // Error * } */ // CLASS EXAMPLE /* * int userChoice; * bool userResponseParsed; * do * { * Console.WriteLine("Please choose Rock, Paper, or Scissors by typing 1, 2, or 3 Aand hiiting enter."); * Console.WriteLine("\t1. Rock \n\t2. Paper \n\t3. Scissors"); * var userResponse = Console.ReadLine(); //var will make it the proper type 'string' * * userResponseParsed = int.TryParse(userResponse, out userChoice); // parse the users input to an int * * if(userResponseParsed == false || userChoice > 3 || userChoice < 1) // give a message if the users input was invalid * { * Console.WriteLine("Your response is invalid."); * } * } while (userResponseParsed == false || userChoice > 3 || userChoice < 1); // state conditions for if we will repeat the loop * * Console.WriteLine("Starting the game..."); * * Random randomNumber = new Random(10); // create a random number object * int computerChoice = randomNumber.Next(1, 4); // get a random number 1, 2, or 3. * * Console.WriteLine(computerChoice); * // Compare Two Choices * if(userChoice == computerChoice) * { * Console.WriteLine("The game was a tie"); * } * else if((userChoice == 2 && computerChoice == 1) || * (userChoice == 3 && computerChoice == 2) || * (userChoice == 1 && computerChoice == 3) * ) { * Console.WriteLine("Congratulations you (the user) WON!"); * } * else * { * Console.WriteLine("We're sorry.The computer won."); * } */ // GROUP 2 Ryan Archer, Anthony Mungle, Lucas Stang & Chris Sophiea // Version 1 /* * string userInput; * int userSelection; * bool userSelectionParsed; * int botSelection; * while (true) * { * Console.WriteLine("Please choose Rock, Paper, or Scissors by typing 1, 2, or 3 and hitting enter."); * Console.WriteLine("\t1) Rock \n\t2) Paper \n\t3) Scissors"); * Console.Write("\nYour choice: "); * userInput = Console.ReadLine(); * userSelectionParsed = int.TryParse(userInput, out userSelection); * if (!userSelectionParsed || userSelection < 1 || userSelection > 3) * { * Console.WriteLine("Input is invalid. Enter a number 1-3."); * } * else * { * break; * } * } * Random rnd = new Random(); * botSelection = rnd.Next(1, 4); * switch (userSelection) * { * case 1: * switch (botSelection) * { * case 1: Console.WriteLine("Tie. You chose rock. Bot chose rock."); break; * case 2: Console.WriteLine("Lose. You chose rock. Bot chose paper."); break; * default: Console.WriteLine("Win. You chose rock. Bot chose scissors."); break; * } * break; * case 2: * switch (botSelection) * { * case 1: Console.WriteLine("Win. You chose paper. Bot chose rock."); break; * case 2: Console.WriteLine("Tie. You chose paper. Bot chose paper."); break; * default: Console.WriteLine("Lose. You chose paper. Bot chose scissors."); break; * } * break; * default: * switch (botSelection) * { * case 1: Console.WriteLine("Lose. You chose scissors. Bot chose rock."); break; * case 2: Console.WriteLine("Win. You chose scissors. Bot chose paper."); break; * default: Console.WriteLine("Tie. You chose scissors. Bot chose scissors."); break; * } * break; * } * } */ /* * public enum Choice{ * Rock, * Paper, * Scissors * } */ // Version 2 /* * Console.WriteLine("This is The Group 2 Awesome Rock-Paper-Sissors Game!\n"); * while(true) { * int userSelection; * bool userResponseParsed; * int botSelection; * Console.WriteLine("Please choose Rock, Paper, or Scissors by typing 1, 2, or 3 Aand hiiting enter."); * Console.WriteLine("\t1. Rock \n\t2. Paper \n\t3. Scissors"); * var userResponse = Console.ReadLine(); //var will make it the proper type 'string' * userResponseParsed = int.TryParse(userResponse, out userSelection); // parse the users input to an int * * if(userResponseParsed == false || userSelection > 3 || userSelection < 1) // give a message if the users input was invalid * { * Console.WriteLine("Your response is invalid."); * continue; * } * * Console.WriteLine("Starting the game..."); * * Random rnd = new Random(); * botSelection = rnd.Next(1, 4); * switch (userSelection) * { * case 1: * switch (botSelection) * { * case 1: Console.WriteLine("Tie. You chose rock. Bot chose rock."); break; * case 2: Console.WriteLine("Lose. You chose rock. Bot chose paper."); break; * default: Console.WriteLine("Win. You chose rock. Bot chose scissors."); break; * } * break; * case 2: * switch (botSelection) * { * case 1: Console.WriteLine("Win. You chose paper. Bot chose rock."); break; * case 2: Console.WriteLine("Tie. You chose paper. Bot chose paper."); break; * default: Console.WriteLine("Lose. You chose paper. Bot chose scissors."); break; * } * break; * default: * switch (botSelection) * { * case 1: Console.WriteLine("Lose. You chose scissors. Bot chose rock."); break; * case 2: Console.WriteLine("Win. You chose scissors. Bot chose paper."); break; * default: Console.WriteLine("Tie. You chose scissors. Bot chose scissors."); break; * } * break; * } * * Console.WriteLine("\nWould you like to play again?\n\tType y for Yes\n\tType n for No\n"); * string playAgain = Console.ReadLine(); // User input and add into playAgain * if(playAgain.ToLower() == "y" || playAgain.ToLower() == "yes") { * continue; * } else { * Console.WriteLine("\nGood bye."); * break; * } * } */ List <Player> players = new List <Player>(); List <Match> matches = new List <Match>(); List <Round> rounds = new List <Round>(); /* * Player p1 = new Player(); * p1.PlayerId = -1; * Console.WriteLine(p1.PlayerId); * Match myMatch = new Match(); * myMatch.RoundWinner(); */ // create the Computer that everyone plays against. Player p1 = new Player() { Fname = "Max", Lname = "Headroom" }; players.Add(p1); //loging or create a new player. unique fName and lName means create a new player, other wise, grab the existing player string[] userNamesArray; do { Console.WriteLine("Please enter your firstname.\n If you enter a unique first and last name I will create a new player."); string userNames = Console.ReadLine(); userNamesArray = userNames.Split(' '); } while(userNamesArray[0] == null); Player p2 = new Player(); // create player 2's name based on if they have first and last name or not if (userNamesArray.Length > 1) { p2.Fname = userNamesArray[0]; p2.Lname = userNamesArray[1]; } else { p2.Fname = userNamesArray[0]; } /* * foreach(var name in userNamesArray){ * Console.WriteLine(name); * } */ players.Add(p2); Match match = new Match(); match.Player1 = p1; match.Player2 = p2; Round round = new Round(); // user will choose Rock, Paper, Scissors Choice userChoice; bool userResponseParsed; do { Console.WriteLine($"Welcome, {p2.Fname}. Please choose Rock, Paper, or Scissors by typing 0, 1, or 2 Aand hiiting enter."); Console.WriteLine("\t0. Rock \n\t1. Paper \n\t2. Scissors"); var userResponse = Console.ReadLine(); //var will make it the proper type 'string' userResponseParsed = Choice.TryParse(userResponse, out userChoice); // parse the users input to an int if (userResponseParsed == false || (int)userChoice > 2 || (int)userChoice < 0) // give a message if the users input was invalid { Console.WriteLine("Your response is invalid."); } } while (userResponseParsed == false || (int)userChoice > 2 || (int)userChoice < 0); // state conditions for if we will repeat the loop Console.WriteLine("Starting the game..."); Random randomNumber = new Random(10); // create a random number object Choice computerChoice = (Choice)randomNumber.Next(0, 3); // get a random number 0, 1, or 2. round.Player1Choice = computerChoice; round.Player2Choice = userChoice; Console.WriteLine($"The computer chose is => {computerChoice}."); // Compare Two Choices if (userChoice == computerChoice) { Console.WriteLine("The game was a tie"); // round winning player is null by default match.RoundWinner(); } else if (((int)userChoice == 1 && (int)computerChoice == 0) || ((int)userChoice == 2 && (int)computerChoice == 1) || ((int)userChoice == 0 && (int)computerChoice == 2) ) { Console.WriteLine("Congratulations you (the user) WON!"); round.WinningPlayer = p2; match.RoundWinner(p2); } else { Console.WriteLine("We're sorry.The computer won."); round.WinningPlayer = p1; match.RoundWinner(p1); } rounds.Add(round); match.Rounds.Add(round); foreach (Round round1 in rounds) { Console.WriteLine($"\nROUND - \nThe GUID is {round.RoundId.ToString()}.\nP1 Choice is {round.Player1Choice}\nP1 Choice is {round.Player1Choice}\n The winning player is {round.WinningPlayer.Fname}"); } }