Don't like ads? PRO users don't see any ads ;-)

TwoUp.cs

By: Mitchell931993 on May 15th, 2012  |  syntax: C#  |  size: 5.31 KB  |  hits: 238  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8.  
  9. namespace SharedGamesClasses {
  10.    
  11.     /// <summary>
  12.     /// TwoUp.cs represents game play of Two Up
  13.     /// with the user matched against the computer
  14.     /// </summary>
  15.     public static class TwoUp {
  16.  
  17.         private const string HEADS = "\nYou threw Heads - you win\n";
  18.         private const string ODDS = "\nYou threw Odds\n";
  19.         private const string TAILS = "\nYou threw Tails - I win\n";
  20.  
  21.         private const string PLAY_AGAIN = "\n\nPlay again? (Y or N):";
  22.         private const string THROW_COINS = "\nPress any key to throw coins again.";
  23.  
  24.         private const int PLAYER = 2,
  25.             COMPUTER = 1,
  26.             TIE = 0;
  27.         private static Coin coin1;
  28.         private static Coin coin2;
  29.  
  30.         private int playerScore = 0;
  31.         private int computerScore = 0;
  32.         private bool playAgain = true;
  33.  
  34.  
  35.        
  36.         //************* more class variables needed **************************
  37.  
  38.         /// <summary>
  39.         /// Since the class is static, we need to be able to initialise local coins.
  40.         /// </summary>
  41.         public static void SetUpCoins() {
  42.             coin1 = new Coin();
  43.             coin2 = new Coin();
  44.         } //end SetUpCoins
  45.  
  46.  
  47.         /// <summary>
  48.         /// Reset all class variables to "zeroed" values,
  49.         /// counters and flags but not the coins.
  50.         ///
  51.         /// used by both Console and GUI to ensure subsequent
  52.         /// playing of the game is correct
  53.         /// </summary>
  54.         public static void ResetGlobals(){
  55.             // Code needed.
  56.         }//end ResetGlobals
  57.  
  58.  
  59.         /// <summary>
  60.         /// Plays the game of Two Up until the user decides to stop
  61.         /// Used by Console version only
  62.         /// </summary>
  63.         public static void PlayConsole() {
  64.             // Code needed.
  65.             //Will contain the fuctions from within this class
  66.             int winner = 0;
  67.             do
  68.             {
  69.                
  70.                 ThrowCoins();
  71.                 winner = OutputWinner();
  72.  
  73.                 switch (winner) {
  74.                     case TIE:
  75.                         Console.WriteLine(ODDS);
  76.                         playAgain = true;
  77.                         break;
  78.  
  79.                     case PLAYER:
  80.                         Console.WriteLine(HEADS);
  81.                         playAgain = WantToPlayAgain_ConsoleOnly();
  82.                         break;
  83.  
  84.                     case COMPUTER:
  85.                         Console.WriteLine(TAILS);
  86.                         playAgain = WantToPlayAgain_ConsoleOnly();
  87.                         break;
  88.                 }
  89.                
  90.             }while (playAgain = true);
  91.             // Before exiting the game, reset class variables
  92.             // to appropriate starting values.
  93.             ResetGlobals();
  94.         }//end PlayConsole
  95.  
  96.         /// <summary>
  97.         /// Flips both coins
  98.         /// Pre: Local coins 1 and 2 exist
  99.         /// Post: coin1 and coin2 flipped.
  100.         /// </summary>
  101.         public static void ThrowCoins() {
  102.             coin1.Flip();
  103.             coin2.Flip();
  104.         }// end ThrowCoins
  105.  
  106.         /// <summary>
  107.         /// Determines the winner of the current throw if one exists.
  108.         /// Pre: true
  109.         /// Post: updates class variables appropriately and displays the scorecard if a winner is found.
  110.         /// </summary>
  111.         /// <returns>The number of heads from the current throw: 0, 1, or 2.</returns>
  112.         public static int OutputWinner() {
  113.             int numberOfHeads = 0;
  114.             if (coin1.IsHeads() && coin2.IsHeads()) {
  115.                 numberOfHeads = 2;
  116.             } else if (coin1.IsHeads() ^ coin2.IsHeads()) { // OR || would surfice too
  117.                 numberOfHeads = 1;
  118.             }
  119.  
  120.             return numberOfHeads;
  121.         }// end OutputWinner
  122.  
  123.         /// <summary>
  124.         /// Asks the user if they wish to play again.
  125.         /// Pre:  none
  126.         /// Post: whether the user wishes to play again.
  127.         /// </summary>
  128.         /// <returns>
  129.         /// true if the user wishes to play again,
  130.         /// false otherwise
  131.         /// </returns>
  132.         private static bool WantToPlayAgain_ConsoleOnly() {
  133.             string input;
  134.             bool validInput = false;
  135.             do {
  136.                 Console.Write("Play Again? (Y or N): ");
  137.                 input = Console.ReadLine().ToUpper();
  138.                 validInput = input == "Y" || input == "N";
  139.             } while (!validInput);
  140.  
  141.             return input == ("Y");
  142.         } // end WantToPlayAgain_ConsoleOnly
  143.  
  144.         /// <summary>
  145.         /// Displays a prompt and waits for a key press.
  146.         /// Pre:  prompt to display; not null, nor empty
  147.         /// Post: prompt was displayed and user pressed a key.
  148.         /// </summary>
  149.         /// <param name="prompt">the prompt to display to the user</param>
  150.         private static void WaitForKey_ConsoleOnly(string prompt) {
  151.             Debug.Assert(!String.IsNullOrEmpty(prompt), "prompt != null");
  152.  
  153.             Console.Write(prompt);
  154.             Console.ReadKey();
  155.             Console.WriteLine();
  156.         } // end WaitForKey_ConsoleOnly
  157.  
  158.     }//end class TwoUp
  159. }//end namespace