Question

In: Computer Science

Write a C++ program that inputs two simplified poker hands (as defined in homework #5) and...


Write a C++ program that inputs two simplified poker hands (as defined in homework #5) and
determines which (if either) of the hands is the winner, according to the following rules:
• If the two hands have different types (3-of-kind, straight, pair, high-card), the hand with
the better type (i.e., appears earlier in this list) wins.
• If they have the same type, the higher significant card wins.
• If the hands have the same type and significant card, there is no winner (a tie).
• Note that the only significant factors are four types of hand and what is the significant
card (high card or card in a pair/triple). In real poker, a tie in the criteria above would be
resolved by comparing the non-pair or next-highest card. You may do this if you wish,
but it is not required
Further Assignment Parameters:
Your program should be decomposed into functions and files. You should not have more
than one function that does the same thing. For example, there should be one function
(with three reference parameters) that inputs a hand. This function can be used twice
(passing different cards as parameters) in order to input the two hands.
• You may start with your solution to homework 5, or you may start with my solution, or
you may start over. If you start with your solution, you should deal with any concerns
that I wrote on it when I graded it. Note that much of this homework involves
converting previous code into functions. There will be very little new code, most of
which will be involved with comparing two hands in order to select a winner.
• Use the existing tool functions from class lectures whenever you can!


***Below is homework #5 (already done):

In the output, cards will be described as follows:
• 2-10 are described by the words for their numeric values: two, three, etc.
• 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king.
The evaluation of a hand is based on the first of the following cases that matches the cards:
• All three cards are equal to each other.
Sample output for 4 4 4: You have three fours.
• The three cards form a straight (three successive values).
Sample output for 68 7: You have an eight-high straight.
• Two cards (a pair) are equal.
Sample output for 6 3 6: You have a pair of sixes.
• Whatever is the highest card.
Sample output for 11 18: You have a jack.
I recommend sorting the card values before trying to evaluate the hand - that makes it much
easier. Also, work on one thing at a time: first determine what kind of hand (3 of kind, 2 of kind,
straight....) and then determine what is the "significant" card for that hand (it becomes very
messy and exhausting if you try to do both at the same time!). Check for card values being
equal to each other (card1 == card2), not specific values (card1 == 1 && card2 == 1 || card1 ==
2 && card 2 == 2 || ...). If you check each value, your program will be hundreds of lines long!

Solutions

Expert Solution

Hello! I have written the code below, comments are written in code to understand better . I tried to handle all the cases which can make answer wrong. I hope below code will pass all the test cases. If incase any issue arises let me know the test case i'll edit the answer with improve code.

  1. #include<bits/stdc++.h>  
  2. using namespace std;  
  3.   
  4.   
  5. //function which returns the type of hand , it takes array of cards as a parameter which have size 3.  
  6. int hands_type(int a[])  
  7. {  
  8.   
  9. //If all the cards are same  
  10. if(a[0] == a[1] == a[2])  
  11. {  
  12.     return 0;  
  13. }  
  14.   
  15. //If cards are straight and also the special case with Queen,King and ACE.  
  16. if( (a[0]+1 == a[1] && a[1]+1 == a[2] ) || (a[0]==1 && a[1]==12 && a[2]==13))  
  17. {  
  18.     return 1;  
  19. }  
  20.   
  21. //If two cards are same  
  22. if(a[0] == a[1] || a[1] == a[2])  
  23. {  
  24.     return 2;  
  25. }  
  26.    
  27. //If all cards are different   
  28. return 3;  
  29.   
  30. }  
  31.   
  32. //Function which resolve tie , by looking at significant card.  
  33. int resolve_tie(int a[],int b[])  
  34. {  
  35.     //If none of the hand has Ace which is the biggest card.  
  36.     if(a[0]!=1 && b[0]!=1)  
  37.     {  
  38.         if(a[2] > b[2])  
  39.         {  
  40.             return 0;  
  41.         }  
  42.         else if(a[2]<b[2])  
  43.         {  
  44.             return 1;  
  45.         }  
  46.     }else //If anyone hand has ACE  
  47.     {  
  48.         //If hand 'a' has ace then a[2] will hold the largest card which is 1(ACE)  
  49.         if(a[0] == 1)  
  50.              a[2]=1;  
  51.   
  52.         //If hand 'b' has ace then b[2] will hold the largest card which is 1(ACE)  
  53.         if(b[0] == 1)  
  54.             b[2]=1;   
  55.   
  56.          //Comparing significant card , hand with 1 will be the winner.  
  57.         if(a[2] < b[2])  
  58.         {  
  59.             return 0;  
  60.         }  
  61.         else if(a[2] > b[2])  
  62.         {  
  63.             return 1;  
  64.         }  
  65.     }  
  66.   
  67.     //When both hands siginificant card are same which means it is a tie.  
  68.     return 2;  
  69. }  
  70.   
  71. int main()  
  72. {  
  73.     int a[3];  
  74.     int b[3];  
  75.   
  76.     cout<<"Enter the first hand values:";  
  77.     cin>>a[0]>>a[1]>>a[2];  
  78.   
  79.   
  80.     cout<<"Enter the second hand values:";  
  81.     cin>>b[0]>>b[1]>>b[2];  
  82.   
  83.     sort(a,a+3);  
  84.     sort(b,b+3);  
  85.   
  86.     int h_a = hands_type(a);  
  87.     //cout<<h_a<<"\n";  
  88.     int h_b = hands_type(b);  
  89.     //cout<<h_b<<"\n";  
  90.   
  91.   
  92. //Winner will be the smallest type value return by hands_type() function   
  93.     if(h_a > h_b)  
  94.     {  
  95.         cout<<"Second is the winner";  
  96.     }else if(h_a < h_b)  
  97.     {  
  98.         cout<<"First is the winner";  
  99.     }else  //This case when hand_type value is same for both hand  
  100.     {  
  101.         if(resolve_tie(a,b) == 0)  
  102.             cout<<"First is the winner";  
  103.         else if(resolve_tie(a,b) == 1)  
  104.             cout<<"Second is the winner";  
  105.         else   
  106.             cout<<"It is a Tie";  
  107.     }  
  108.   
  109. }  

Related Solutions

Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB function should be voltage across the load (in Vrms, assume 0 phase), frequency Resistance of the load Inductance of the load power factor of the load target power factor The output of the function should be the size of the capacitor that one would need to place in parallel with the load to reach the target power factor. Please submit the code for the...
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number...
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number of rows r aand number of columns c - Create a two-dimensional array of integers int numbers[r][c] -input data for each element of numbers Your program must compute and display the largest number is each row and column of the number array
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards...
Poker hands with ranking. Consider a regular deck of 52 cards as usual with 5 cards dealt. You have learned all those 8 patterns: one pair, two pairs, three of a kind, straight, flush, full house, four of a kind, and straight flush (with royal flush a special kind of straight flush). Note ace is counted as both 1 point and 14 points. So in counting straight, A2345 and 10JQKA are both valid. Compute / derive the number of ways...
How many poker hands (of 5 total cards) are dealt such that the first three cards...
How many poker hands (of 5 total cards) are dealt such that the first three cards have the same rank, but the total hand does not contain another pair and is not a four of a kind? When counting for this problem, the order of the dealt cards matters.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT