Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Games {
- /// <summary>
- /// Plays a game of two-up, using Simple rules or Complex rules
- /// </summary>
- class TwoUp {
- //create two coin objects
- Coin coin1 = new Coin();
- Coin coin2 = new Coin();
- //initialise
- bool coinOneHeads;
- bool coinTwoHeads;
- int playerWins = 0;
- int houseWins = 0;
- /// <summary>
- /// asks the user if (s)he whould like to play again,
- /// reads the response and sets it as variable 'answer'
- /// answer is then evaluated and action is taken further
- /// in the code in the 'Play' method.
- /// </summary>
- private void PlayAgain() {
- //initialise
- string answer;
- string yes = "Y";
- string no = "N";
- // displays wins/loses
- // asks and evaluates if the player would like to play again
- Console.WriteLine("You have {0} wins and I have {1} wins.", playerWins, houseWins);
- Console.WriteLine("Would you like to play again? (Y/N)");
- answer = Console.ReadLine();
- while ((answer.ToUpper() != yes) && (answer.ToUpper() != no)) {
- Console.WriteLine("Please enter the correct response, (Y/N)");
- answer = Console.ReadLine();
- }//end while
- if (answer == no) {
- Console.WriteLine("Thank you for playing");
- }//end if
- }//end PlayAgain
- /// <summary>
- /// Flips the two coins and
- /// determines the facevalue of both
- /// and assigns the variables
- /// coinXIsHeads to be true or false.
- /// </summary>
- private void FlipCoinsAndCheck() {
- coin1.Flip();
- if (coin1.IsHeads() == true) {
- coinOneHeads = true;
- } else if (coin1.IsHeads() == false) {
- coinOneHeads = false;
- }//end else if
- coin2.Flip();
- if (coin2.IsHeads() == true) {
- coinTwoHeads = true;
- } else if (coin2.IsHeads() == false) {
- coinTwoHeads = false;
- }//end else if
- }//end FlipCoinsAndCheck
- /// <summary>
- /// Plays TwoUp by the simple rules
- /// as long as the menuOption is equal
- /// to one.
- /// </summary>
- private void SimpleRules() {
- const int WIN = 1;
- // flip both coins and checks facevalue
- FlipCoinsAndCheck();
- // if 'IsHeads' is true for both
- // player wins
- if ((coinOneHeads == true) && (coinTwoHeads == true)) {
- Console.WriteLine("Your threw Heads - You've won!");
- playerWins = playerWins + WIN;
- // else if 'IsHeads' is false for both
- // house/computer wins
- } else if ((coinOneHeads == false) && (coinTwoHeads == false)) {
- Console.WriteLine("Your threw Tails - I've won!");
- houseWins = houseWins + WIN;
- //end else if
- // otherwise, while'IsHeads' is true for one
- // and not the other: flip again
- } while (((coinOneHeads == true) && (coinTwoHeads == false)) ||
- ((coinOneHeads == false) && (coinTwoHeads == true))) {
- Console.WriteLine("Your threw Odds - Flip again!");
- FlipCoinsAndCheck();
- }//end while
- }//end SimpleRules
- /// <summary>
- /// Plays TwoUp by the complex rules
- /// as long as the menuOption is equal
- /// to two.
- /// </summary>
- private void ComplexRules() {
- //initialise counters
- int headsCounter = 0;
- int tailsCounter = 0;
- int counterIsThree = 3;
- // continue flipping coins and checking
- // for the faceValue to be heads/tails
- // until double heads/tails is
- // flipped thrice.
- do {
- FlipCoinsAndCheck();
- if ((coinOneHeads == true) && (coinTwoHeads == true)) {
- Console.WriteLine("Your threw Heads!");
- headsCounter++;
- } else if ((coinOneHeads == false) && (coinTwoHeads == false)) {
- Console.WriteLine("Your threw Tails!");
- tailsCounter++;
- //end else if
- } else if (((coinOneHeads == true) && (coinTwoHeads == false)) ||
- ((coinOneHeads == false) && (coinTwoHeads == true))) {
- Console.WriteLine("Your threw Odds!");
- }//end else if
- }while((headsCounter != counterIsThree) && (tailsCounter != counterIsThree));
- //end while
- // if double heads is flipped three times, player wins!
- // else if double tails is flipped three times, player loses!
- if (headsCounter == counterIsThree) {
- playerWins++;
- } else if (tailsCounter == counterIsThree) {
- houseWins++;
- }//end else if
- }//end ComplexRules
- /// <summary>
- /// Plays the game by calling the
- /// game methods and 'play again' method
- /// and by the chosen rules as long as
- /// the variable 'answer' remains yes.
- /// </summary>
- /// <param name="menuOption"></param>
- public void Play(int menuOption) {
- //initialise variables
- const int SIMPLERULES = 1;
- const int COMPLEXRULES = 2;
- string answer = "";
- string yes = "Y";
- string no = "N";
- // menuoption will be determined as one
- // and therefore a game of two up
- // with simple rules, until the player
- // choses not to
- if (menuOption == SIMPLERULES) {
- do {
- SimpleRules();
- PlayAgain();
- } while (answer == yes);
- // otherwise menuoption is two and therefore
- // play by the complex rules until the player
- // decides not to play anymore.
- } else if (menuOption == COMPLEXRULES) {
- do {
- ComplexRules();
- PlayAgain();
- } while (answer == yes);
- } else {
- Console.WriteLine("If this message appears, then there has been an error with the menuOption");
- }
- }//end play
- } //end class TwoUp
- }//end Games
Add Comment
Please, Sign In to add comment