Question
C# Please help with method creation. please use basic math and only use this class essentially make the rest of the methods like the isStraight
C# Please help with method creation.
please use basic math and only use this class essentially make the rest of the methods like the isStraight method
Someone, please add the methods to evaluate the following.
a. Four of a kind (e.g. 4 Aces) b. Full House (2 of and kind and 3 of a kind in the same hand) c. Flush (all 5 cards of the same suit) d. Straight (all 5 cards face values in sequence (2,3,4,5,6) e. Three of a Kind. f. 2 Pairs g. Pair
class DeckOfCardsTest { static void Main(string[] args) { Card[] compHand = new Card[5]; Card[] playerHand = new Card[5];
int compHandValue = 0; // to determine winner int playerHandValue = 0; // to determine winner
DeckOfCards myDeck = new DeckOfCards(); myDeck.Shuffle();
for (int i = 0; i < 5; i++) { playerHand[i] = myDeck.DealCard(); //compHand[i] = new Card((i+2).ToString(), "Hearts"); // force a straight compHand[i] = myDeck.DealCard();
} // end of for
SortHand(compHand); SortHand(playerHand);
// *** display hands Console.WriteLine("Player's Hand:"); for (int i = 0; i < 5; i++) { playerHand[i].DisplayCard(1, i * 6);
}
Console.SetCursorPosition(0, 12); Console.WriteLine("Computer's Hand:"); for (int i = 0; i < 5; i++) { compHand[i].DisplayCard(13, i * 6);
} // ** end display hands
// set comphand hand value for all different hands... // four of kind 7, full house 6, flush 5, straight 4, three of kind 3, 2 pair 2, pair 1 if (isStraigt(playerHand)) { playerHandValue = 4; // set playerhand value
} if (isStraigt(compHand)) { compHandValue = 4; // set comphand value } if (FullHouse(playerHand)) { playerHandValue = 6; } if (FullHouse(compHand)) { compHandValue = 6; }
Console.WriteLine(); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.DarkCyan; // text color for computer Console.WriteLine($"Computer's hand value {compHandValue}"); Console.ForegroundColor = ConsoleColor.DarkYellow; // text color for player Console.WriteLine($"player's hand value {playerHandValue}"); Console.ForegroundColor = ConsoleColor.Black; // text color for console // is comp hand higher ? then comp wins... etc...
Console.WriteLine(); Console.WriteLine();
} // end of Main
static bool isStraigt(Card[] hand) { int[] intHand = new int[hand.Length]; // make an array to hold int value of face for (int i = 0; i < hand.Length; i++) { intHand[i] = Int32.Parse(hand[i].Face); } // now use the numbers to determine if a straight if (intHand[4] == (intHand[3] + 1) && (intHand[3] == (intHand[2] + 1) && (intHand[2] == (intHand[1] + 1) && (intHand[1] == (intHand[0] + 1))))) { return true; } else { return false; } } // end of isStraight method
static bool FullHouse(Card[] hand) { int[] intHand = new int[hand.Length]; for (int i = 0; i < hand.Length; i++) { intHand[i] = Int32.Parse(hand[i].Face); } if ((intHand[0] == intHand[1] && intHand[0] == intHand[2] && intHand[3] == intHand[4]) || (intHand[0] == intHand[1] && intHand[2] == intHand[3] && intHand[2] == intHand[4])) { return true; } return false; } // end of fullhouse method
// sort array using selection sort Page 754 in your book public static void SortHand(Card[] hand) { // loop over data.Length - 1 elements for (var i = 0; i < hand.Length - 1; ++i) { var smallest = i; // first index of remaining array
// loop to find index of smallest element for (var index = i + 1; index < hand.Length; ++index) { if (Int32.Parse(hand[index].Face) < Int32.Parse(hand[smallest].Face)) { smallest = index; } }
Swap(ref hand[i], ref hand[smallest]); // swap elements
} }
// helper method to swap values in two elements public static void Swap(ref Card first, ref Card second) { var temporary = first; // store first in temporary first = second; // replace first with second second = temporary; // put temporary in second }
} }
class Card { private string face; private string suit;
public string Face { get { return face; } } public string Suit { get { return suit; } }
public Card(string cardFace, string cardSuit) { face = cardFace; suit = cardSuit; } // end of constructor
public Card() { }
public override string ToString() { return face + " of " + suit; } // end of ToString
public void DisplayCard(int row, int col) { Console.SetCursorPosition(col, row); Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Blue; Console.Write(""); Console.SetCursorPosition(col, row + 1); Console.Write(" "); if (suit == "Diamonds" || suit == "Hearts") { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Black; } switch (face) { case "1": Console.Write("A"); break; case "10": Console.Write("T"); break; case "11": Console.Write("J"); break; case "12": Console.Write("Q"); break; case "13": Console.Write("K"); break; default: Console.Write(face); break; }
Console.ForegroundColor = ConsoleColor.Blue; Console.Write(" "); Console.SetCursorPosition(col, row + 2); Console.Write(" "); Console.SetCursorPosition(col, row + 3); Console.Write(" "); if (suit == "Diamonds" || suit == "Hearts") { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.Black; } if (suit == "Diamonds") { Console.Write("D"); } if (suit == "Hearts") { Console.Write("H"); } if (suit == "Spades") { Console.Write("S"); } if (suit == "Clubs") { Console.Write("C"); } Console.ForegroundColor = ConsoleColor.Blue; Console.Write(""); Console.SetCursorPosition(col, row + 4); Console.Write("");
Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.White; }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started