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;
- using System.Diagnostics;
- using System.Threading;
- using System.Windows.Forms;
- namespace SharedGamesClasses {
- /// <summary>
- /// TwoUp.cs represents game play of Two Up
- /// with the user matched against the computer
- /// </summary>
- public static class TwoUp {
- private const string HEADS = "\nYou threw Heads - you win\n";
- private const string ODDS = "\nYou threw Odds\n";
- private const string TAILS = "\nYou threw Tails - I win\n";
- private const string PLAY_AGAIN = "\n\nPlay again? (Y or N):";
- private const string THROW_COINS = "\nPress any key to throw coins again.";
- private const int PLAYER = 2,
- COMPUTER = 1,
- TIE = 0;
- private static Coin coin1;
- private static Coin coin2;
- private int playerScore = 0;
- private int computerScore = 0;
- private bool playAgain = true;
- //************* more class variables needed **************************
- /// <summary>
- /// Since the class is static, we need to be able to initialise local coins.
- /// </summary>
- public static void SetUpCoins() {
- coin1 = new Coin();
- coin2 = new Coin();
- } //end SetUpCoins
- /// <summary>
- /// Reset all class variables to "zeroed" values,
- /// counters and flags but not the coins.
- ///
- /// used by both Console and GUI to ensure subsequent
- /// playing of the game is correct
- /// </summary>
- public static void ResetGlobals(){
- // Code needed.
- }//end ResetGlobals
- /// <summary>
- /// Plays the game of Two Up until the user decides to stop
- /// Used by Console version only
- /// </summary>
- public static void PlayConsole() {
- // Code needed.
- //Will contain the fuctions from within this class
- int winner = 0;
- do
- {
- ThrowCoins();
- winner = OutputWinner();
- switch (winner) {
- case TIE:
- Console.WriteLine(ODDS);
- playAgain = true;
- break;
- case PLAYER:
- Console.WriteLine(HEADS);
- playAgain = WantToPlayAgain_ConsoleOnly();
- break;
- case COMPUTER:
- Console.WriteLine(TAILS);
- playAgain = WantToPlayAgain_ConsoleOnly();
- break;
- }
- }while (playAgain = true);
- // Before exiting the game, reset class variables
- // to appropriate starting values.
- ResetGlobals();
- }//end PlayConsole
- /// <summary>
- /// Flips both coins
- /// Pre: Local coins 1 and 2 exist
- /// Post: coin1 and coin2 flipped.
- /// </summary>
- public static void ThrowCoins() {
- coin1.Flip();
- coin2.Flip();
- }// end ThrowCoins
- /// <summary>
- /// Determines the winner of the current throw if one exists.
- /// Pre: true
- /// Post: updates class variables appropriately and displays the scorecard if a winner is found.
- /// </summary>
- /// <returns>The number of heads from the current throw: 0, 1, or 2.</returns>
- public static int OutputWinner() {
- int numberOfHeads = 0;
- if (coin1.IsHeads() && coin2.IsHeads()) {
- numberOfHeads = 2;
- } else if (coin1.IsHeads() ^ coin2.IsHeads()) { // OR || would surfice too
- numberOfHeads = 1;
- }
- return numberOfHeads;
- }// end OutputWinner
- /// <summary>
- /// Asks the user if they wish to play again.
- /// Pre: none
- /// Post: whether the user wishes to play again.
- /// </summary>
- /// <returns>
- /// true if the user wishes to play again,
- /// false otherwise
- /// </returns>
- private static bool WantToPlayAgain_ConsoleOnly() {
- string input;
- bool validInput = false;
- do {
- Console.Write("Play Again? (Y or N): ");
- input = Console.ReadLine().ToUpper();
- validInput = input == "Y" || input == "N";
- } while (!validInput);
- return input == ("Y");
- } // end WantToPlayAgain_ConsoleOnly
- /// <summary>
- /// Displays a prompt and waits for a key press.
- /// Pre: prompt to display; not null, nor empty
- /// Post: prompt was displayed and user pressed a key.
- /// </summary>
- /// <param name="prompt">the prompt to display to the user</param>
- private static void WaitForKey_ConsoleOnly(string prompt) {
- Debug.Assert(!String.IsNullOrEmpty(prompt), "prompt != null");
- Console.Write(prompt);
- Console.ReadKey();
- Console.WriteLine();
- } // end WaitForKey_ConsoleOnly
- }//end class TwoUp
- }//end namespace
Advertisement
Add Comment
Please, Sign In to add comment