Question

How to I change this code to print the highest value(s) in the array? The wanted output (you just need to make it print the winner, ignore what comes before) is included below:

Example:s Player 1 4/S J/D Score 1 Player 1 K/H 9/D Score 1 Player 1 9/C J/H Score 1 Player 2 2/C T/C Score -2 Player 2 2/S A/C Score 3 Player 2 5/H J/C Score 1 Player 3 J/S 9/H Score 1 Player 3 K/S Q/D Score 3 Player 3 J/S 8/H Score 1 Player 2 is a winner. Player 2 is a winner. Player 3 is a winner Player 1 is a winner. Player 2 is a winner Player 3 is a winner

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

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];

int[] playerScore = new int[players];

for (int i = 0; i < players; i++) {

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

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

Cards.printHand(hand);

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

System.out.println();

playerScore[i] = Cards.pointValue(hand);

}

//go through playerScore array twice, first to find the highest score, then print which player(s) have that score

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • variable 'highest_score' stores the maximum value
  • and second for loop prints all the players whose score is equal to highesy_score

Screenshot of code:

1 public static void playGame (int players, int cards) 2 52) { if (players cards > System.out.println(Not enough cards for that many players.); return; 4 boolean[] deck new boolean [52]; int[] playerScore new int[players]; 6 8 // this will store the highest score int highest score -1 10 12 ▼ for (int i = 0; 1 〈 players; 1++) { 14 15 16 17 18 19 20 21 System.out.println( Player(i + 1)); int[] hand Cards.dealHandFromDeck (cards, deck); Cards.printHand (hand); System.out.println(Score Cards.pointValue (hand)); System.out.println(); playerScore[i] Cards.pointValue (hand); // finding the highest score if (Cards.pointValue (hand) > highest_score) ( highest_score Cards.pointValue(hand); 23 24 25 26 27 28 29 ▼ 30 // now the variable highest_score has the maximum score value // now just iterate over all the players and those players having the same // score as highest_score print that player for (int i = 0; i 〈 players; 1++) { int[] hand Cards.dealHandFromDeck (cards, deck); if (Cards.pointValue (hand)highest_score) System.out.println( Player (i+1) + is a winner); 35

Textual Code :

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];

int[] playerScore = new int[players];

// this will store the highest score

int highest_score = -1;

for (int i = 0; i < players; i++) {

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

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

Cards.printHand(hand);

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

System.out.println();

playerScore[i] = Cards.pointValue(hand);

// finding the highest score

if(Cards.pointValue(hand) > highest_score) {

highest_score = Cards.pointValue(hand);

}

}

// now the variable highest_score has the maximum score value

// now just iterate over all the players and those players having the same

// score as highest_score print that player

for (int i = 0; i < players; i++) {

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

if(Cards.pointValue(hand) == highest_score) {

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

}

}

}

Add a comment
Know the answer?
Add Answer to:
How to I change this code to print the highest value(s) in the array? The wanted...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming The following code creates a deck of cards, shuffles it, and deals to players....

    C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...

  • Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods...

    Please try to not use array lists Main topics: Random number generators Arrays Programmer defined methods Program Specification: You are to develop a program to play a variation on a game of chance called single player Poker. Game Description: • There is a Player who starts with 100 chips, each worth $1.00. • There is a deck of 36 cards: – Each card has a number in the range of [1, 9] printed on it - 9 possible values /...

  • Complete a program In C#: some code is included, you edit the areas that have not...

    Complete a program In C#: some code is included, you edit the areas that have not been filled. For your C# program you will complete code that plays the War card game. In this game, the deck of cards is evenly divided among two players. The players are not allowed to look at the cards in their hand. On each round of the game, both players lay down the top card from their hand. The player with the higher value...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I wrote a card shuffle program and I am trying to see how I can add...

    I wrote a card shuffle program and I am trying to see how I can add a sorting function to the program to sort the players hands before printing them. How would I do this? #include <iostream> #include <iomanip> #include <vector> #include <algorithm> using namespace std; void printHand(int []); void deal(vector<int> &, int[], int[], int[], int[]); void unwrapDeck(vector<int> &deck) { for(int i=0; i<=51; i++) deck.push_back(i); } void shuffleDeck(vector<int> &deck) { //Status of cards before shuffling cout << "Before shuffling: "...

  • C++ program This program involves writing a VERY simplified version of the card game War. You...

    C++ program This program involves writing a VERY simplified version of the card game War. You may know this game or not but my rules are these 1. Split the deck between player1 and player2. Only the face values matter (2-14) and not the suits 2. Each player puts a card down on the table. The higher face value wins that hand. If the card values match, you will simply indicate tie and neither player wins.The original rules would require...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT