Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would I modify this method (playGame) to print the winner or winners of this card game? Below are examples of the wanted output: I

How would I modify this method (playGame) to print the winner or winners of this card game?

Below are examples of the wanted output:

image text in transcribed

I have everything working except for printing out who the winner is. You do not need to look at most of the code, just the playGame method, so please don't ignore because of the seemingly lengthy question.

Please include a discription of your algorithm as a comment above the method aswell, thank you. Here is my code:

-----------------------------------------------------------------------------------------------------

/**

* This program contains methods for a card game

*/

import java.util.Scanner;

/**

* @author

*

*/

public class Poker {

/**

* @param args

*/

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int players;

players = getInteger(in, "Enter the number of players", 0, 26);

playGame(players, 2);

}

/**

* [algorithm description here]

* @param players

* @param cards

*/

public static void playGame(int players, int cards) {

if (players * cards > 52) {

System.out.println("Not enough cards for that many players.");

return;

}

boolean[] deck = new boolean[52];

for (int i = 0; i

System.out.println("Player " + (i + 1));

int[] hand = dealHandFromDeck(cards, deck);

printHand(hand);

System.out.println("Score = " + pointValue(hand));

System.out.println();

}

}

public static int getInteger(Scanner in, String prompt, int lower, int upper) {

boolean valid = false, outOfRange = false;

int input = -1;

do

{

System.out.print(prompt);

if (outOfRange) System.out.printf(" [between %d and %d inclusive]", lower, upper);

System.out.print(": ");

while (!in.hasNextInt())

{

in.next();

System.out.println("Please enter an integer.");

}

input = in.nextInt();

if (input >= lower && input

{

valid = true;

}

else

{

System.out.println("Value is out of range.");

outOfRange = true;

}

} while (!valid);

return input;

}

public static int pointValue(int[] hand) {

int pointValue = -1;

if (Cards.isPair(hand) == true) {

pointValue = 5;

}else if (Cards.isStraight(hand) == true) {

pointValue = 3;

}else if (Cards.isFlush(hand) == true) {

pointValue = 2;

}else if (Cards.isJackOrHigher(hand) == true) {

pointValue = 1;

}else {

pointValue = 0;

}

return pointValue;

}

public static int[] dealHandFromDeck(int numberOfCards, boolean[] deck) {

int hand[] = new int[2];

hand = Cards.dealHand(2);

int dealt1 = hand[0];

int dealt2 = hand[1];

while (deck[dealt1] == true || deck[dealt2] == true) {

hand = dealHand(2);

dealt1 = hand[0];

dealt2 = hand[1];

}

if (deck[dealt1] == false && deck[dealt2] == false) {

deck[dealt1] = true;

deck[dealt2] = true;

}

return hand;

}

public static void printHand(int[] hand) {

printCard(hand);

}

public static void printCard(int[] hand) {

String s = "A23456789TJQK";

int rank1 = Cards.rank(hand[0]);

int rank2 = Cards.rank(hand[1]);

char card1 = s.charAt(rank1);

char card2 = s.charAt(rank2);

char suit1 = Cards.suit(hand[0]);

char suit2 = Cards.suit(hand[1]);

System.out.println(card1 + "/" + suit1);

System.out.println(card2 + "/" + suit2);

}

public static int[] dealHand(int n) {

int[] hand = new int[n];

int rand;

int max = 51;

int min = 0;

int range = max - min + 1;

do{

for (int i = 0; i

rand = (int) (Math.random()* range) + min;

hand[i] = rand;

}

} while (hand[0] == hand[1]);

return hand;

}

public static char suit(int card) {

char suit;

if (0

suit = 'H';

}else if (13

suit = 'S';

}else if (26

suit = 'D';

}else if (39

suit ='C';

} else {

suit = 'x';

}

return suit;

}

public static int rank(int card) {

int cardValue = -1;

if (card == 0 || card == 13 || card == 26 || card == 39) {

cardValue = 0;

}else if (card == 1 || card == 14 || card == 27 || card == 40) {

cardValue = 1;

}else if (card == 2 || card == 15 || card == 28 || card == 41) {

cardValue = 2;

}else if (card == 3 || card == 16 || card == 29 || card == 42) {

cardValue = 3;

}else if (card == 4 || card == 17 || card == 30 || card == 43) {

cardValue = 4;

}else if (card == 5 || card == 18 || card == 31 || card == 44) {

cardValue = 5;

}else if (card == 6 || card == 19 || card == 32 || card == 45) {

cardValue = 6;

}else if (card == 7 || card == 20 || card == 33 || card == 46) {

cardValue = 7;

}else if (card == 8 || card == 21 || card == 34 || card == 47) {

cardValue = 8;

}else if (card == 9 || card == 22 || card == 35 || card == 48) {

cardValue = 9;

}else if (card == 10 || card == 23 || card == 36 || card == 49) {

cardValue = 10;

}else if (card == 11 || card == 24 || card == 37 || card == 50) {

cardValue = 11;

}else if (card == 12 || card == 25 || card == 38 || card == 51) {

cardValue = 12;

}

return cardValue;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books

Students also viewed these Databases questions

Question

How to solve maths problems with examples

Answered: 1 week ago