Untitled
unknown
plain_text
2 years ago
8.9 kB
12
Indexable
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Assignment_1 { class Poker { private PlayingCards deck = new PlayingCards(); private List<String> hand1 = new List<string>(); private List<String> hand2 = new List<string>(); public void dealHands(){ for (int i =0; i < 5; i++){ hand1[i] = deck.getNextCard(); hand2[i] = deck.getNextCard(); } } public Poker(){ PlayingCards c1 = new PlayingCards(); deck.Shuffle(); dealHands(); } public Poker(List<string> l1, List<string> l2){ PlayingCards c1 = new PlayingCards(); hand1 = l1; hand2 = l2; } public void showHand(int num){ if (num == 1){ Console.WriteLine("Player 1's Hand:"); for (int i = 0; i < hand1.Count; i++){ Console.Write(hand1[i]); if (i < hand1.Count - 1){ Console.Write(","); } } Console.WriteLine(); } else{ Console.WriteLine("Player 2's Hand:"); for (int i = 0; i < hand2.Count; i++){ Console.Write(hand2[i]); if (i < hand2.Count - 1){ Console.Write(","); } } Console.WriteLine(); } } public int[] countSuite(List<string> givenHand){ //LOOP THROUGH HAND AND STRING SPLIT BY SPACE //count of clubs into cell 0 //count of diamonds in cell 1 //count of hearts in cell 2 //count of spades in cell 3 int[] countSuiteArray = {0,0,0,0}; string[] suiteTokenArray = new string[4]; for (int i = 0; i < 5; i++){ suiteTokenArray = givenHand[i].Split(' '); if (suiteTokenArray[i] == "Clubs"){ countSuiteArray[0]++; } else if(suiteTokenArray[i] == "Diamonds"){ countSuiteArray[1]++; } else if(suiteTokenArray[i] == "Hearts"){ countSuiteArray[2]++; } else if(suiteTokenArray[i] == "Spades"){ countSuiteArray[3]++; } } return countSuiteArray; } public int[] countValues(List<string> givenHand) { int[] countValuesArray = new int[14]; string[] valueTokenArray = new string[14]; for (int i = 0; i < givenHand.Count; i++){ valueTokenArray = givenHand[i].Split(' '); if (valueTokenArray[i] == "Ace"){ countValuesArray[0]++; } else if(valueTokenArray[i] == "Two"){ countValuesArray[1]++; } else if(valueTokenArray[i] == "Three"){ countValuesArray[2]++; } else if(valueTokenArray[i] == "Four"){ countValuesArray[3]++; } else if(valueTokenArray[i] == "Five"){ countValuesArray[4]++; } else if(valueTokenArray[i] == "Six"){ countValuesArray[5]++; } else if(valueTokenArray[i] == "Seven"){ countValuesArray[6]++; } else if(valueTokenArray[i] == "Eight"){ countValuesArray[7]++; } else if(valueTokenArray[i] == "Nine"){ countValuesArray[8]++; } else if(valueTokenArray[i] == "Ten"){ countValuesArray[9]++; } else if(valueTokenArray[i] == "Jack"){ countValuesArray[10]++; } else if(valueTokenArray[i] == "Queen"){ countValuesArray[11]++; } else if(valueTokenArray[i] == "King"){ countValuesArray[12]++; } } return countValuesArray; } public int numPairs(int[] countValuesArray) { int count = 0; for (int i =0; i < countValuesArray.Count(); i++) { if(countValuesArray[i] == 2) { count++; } } return count; } public int threeOfAKind(int[] countValuesArray) { for (int i =0; i < countValuesArray.Count(); i++) { if(countValuesArray[i] == 3) { return i; } } return 0; } public int fourOfAKind(int[] countValuesArray) { for (int i = 0; i < countValuesArray.Count(); i++) { if (countValuesArray[i] == 4) { return i; } } return 0; } public bool fullHouse(int[] countValuesArray) { bool threeOfAKind = false; int np = numPairs(countValuesArray); for (int i =0; i < countValuesArray.Count(); i++) { if(countValuesArray[i] == 3) { threeOfAKind = true; } } if ((np > 0) && (threeOfAKind == true)){ return true; } return false; } public bool straight(int[] countValuesArray){ bool nstraight = true; for (int i = 0; i < countValuesArray.Count(); i++){ if (countValuesArray[i] == 1){ for (int j = 0; j < 5; j++){ if(countValuesArray[j] != 1){ nstraight = false; } } } } return nstraight; } public bool flush(int[] countSuiteArray){ for (int i = 0; i < countSuiteArray.Count(); i++){ if (countSuiteArray[i] == 5){ return true; } } return false; } public bool straightFlush(int[] countValuesArray, int[] countSuiteArray){ bool checkFlush = flush(countSuiteArray); bool checkStraight = straight(countValuesArray); if((checkFlush == true) && (checkStraight == true)){ return true; } return false; } public bool royalFlush(int[] countValuesArray, int[] countSuiteArray){ for(int i = 0; i < countSuiteArray.Count(); i++){ if (countSuiteArray[i] == 5){ if (countValuesArray[1] == 1){ for(int J = 10; i <=13; i++){ if (countValuesArray[J] != 1){ return false; } } return true; } } } return false; } public string scoreHand(int number){ int[] countValuesArray = new int[13]; int[] countSuiteArray = new int[4]; List<string> givenHand; if (number == 1){ givenHand = hand1; } else{ givenHand = hand2; } countValuesArray = countValues(givenHand); countSuiteArray = countSuite(givenHand); if (royalFlush(countValuesArray, countSuiteArray) == true){ return "royal flush"; } if (straightFlush(countValuesArray, countSuiteArray) == true){ return "straight flush"; } if (flush(countValuesArray) == true){ return "royal flush"; } if (straight(countValuesArray) == true){ return "straight"; } if (fullHouse(countValuesArray) == true){ return "full house"; } if (fourOfAKind(countValuesArray) > 0){ return "four of a kind"; } if (threeOfAKind(countValuesArray) > 0){ return "three of a kind"; } return "high card"; } } }
Editor is loading...