Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- namespace SharedGamesClasses {
- /// <summary>
- /// TwentyOne.cs plays the game of Twenty One -
- /// a card game whereby two parties compete to get a hand of playing cards
- /// whose summed facevalues are closest to 21, without exceeding it.
- ///
- /// </summary>
- public static class TwentyOne {
- public const int NUM_OF_PLAYERS = 2;
- public const int USER = 0;
- public const int DEALER = 1;
- private const string USERS_NAME = "Player";
- private const string DEALERS_NAME = "Dealer";
- private const int NUM_OF_STARTING_CARDS = 2;
- // More constants and/or class variables may be needed.
- private static CardPile cardPile = new CardPile(true);
- private static Hand[] hands = new Hand[NUM_OF_PLAYERS];
- private static int[] pointsTotal = new int[NUM_OF_PLAYERS] { 0, 0 };
- private static int[] numOfGamesWon = new int[NUM_OF_PLAYERS] { 0, 0 };
- private static int numOfUserAcesWithValueOne;
- /// <summary>
- /// Set up to play the game,
- /// by shuffling the pile of cards and dealing the two hands.
- /// </summary>
- public static void SetUpGame() {
- cardPile.Shuffle();
- hands[USER] = new Hand(cardPile.DealCards(NUM_OF_STARTING_CARDS));
- hands[DEALER] = new Hand(cardPile.DealCards(NUM_OF_STARTING_CARDS));
- numOfUserAcesWithValueOne = 0;
- } //end SetUpGame
- /// <summary>
- /// Plays the game of Twenty One until the user decides to stop.
- /// </summary>
- public static void PlayConsole() {
- SetUpGame();
- DisplayHand(DEALER);
- DisplayHand(USER);
- // Play 21 until the user decides to stop.
- } //end PlayConsole
- /// <summary>
- /// Output the hand of cards of either the User or the Dealer
- /// using DisplayHand of Hand class
- /// Pre: hands initialised
- /// Post: Displays the hand of cards held by the specified player.
- /// </summary>
- public static void DisplayHand(int player) {
- string player_name;
- if (player == USER) {
- player_name = USERS_NAME;
- } else {
- player_name = DEALERS_NAME;
- }
- Console.Write("{0} has ", player_name);
- hands[player].DisplayHand(false, false);
- // Code needed.
- } // end DisplayHand
- /* The following methods are intended to be called by your GUI code.
- * I.e. you shouldn't need them when writing your Console game,
- * but do not delete them. */
- /// <summary>
- /// Helps the GUI to display what is in a player's hand.
- /// </summary>
- /// <returns>the Hand of the specified player</returns>
- public static Hand GetHand(int player) {
- return hands[player];
- }
- /// <summary>
- /// Helps the GUI to display the number of Aces that
- /// the user has chosen to have value 1, rather than 11.
- /// </summary>
- /// <returns>the numOfUserAcesWithValueOne</returns>
- public static int GetNumOfUserAcesWithValueOne() {
- return numOfUserAcesWithValueOne;
- }
- /// <summary>
- /// Helps the GUI to display the points total of each player.
- /// </summary>
- /// <returns>the points total of the specified player</returns>
- public static int GetPointsTotal(int player) {
- return pointsTotal[player];
- }
- /// <summary>
- /// Helps the GUI to display the number of games won by each player.
- /// </summary>
- /// <returns>the number of games won by the specified player</returns>
- public static int GetNumOfGamesWon(int player) {
- return numOfGamesWon[player];
- }
- } //end class TwentyOne
- } //end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement