Question: Modify the game logic in the Card Games application so that after the initial deal players can replace any of their cards with a value

Modify the game logic in the Card Games application so that after the initial deal players can replace any of their cards with a value of 7 or less for a new card from the deck in an effort to improve their overall score.

To introduce some randomness (humanity) into this process players only replace an eligible card with a probability of 0.75.

In addition modify the Game classs toString method to monitor all replacements before printing out the normal Game dump.

An example follows in which the 0thplayer made no replacements despite one card being eligible (randomness) and player 1 and 3 tied with the highest score after both asking for replacement cards.

public class Game { private Deck d; private Hand[] hands; private int scores[]; private final int NUMBER_OF_HANDS, CARDS_IN_A_HAND; private int maxScore; //CONSTRUCTORS public Game(int initNumberOfHands, int initCardsInAHand){ NUMBER_OF_HANDS = initNumberOfHands; //input parameter required in multiple methods CARDS_IN_A_HAND = initCardsInAHand; //input parameter required in multiple methods d = new Deck(); d.fill(); d.shuffle(); if (NUMBER_OF_HANDS * CARDS_IN_A_HAND => hands = new Hand[NUMBER_OF_HANDS]; for (int i = 0; i hands[i] = new Hand(CARDS_IN_A_HAND); } } } //ACCESSORS //not required //MUTATORS //not required //OTHER METHODS public void play(){ scores = new int[NUMBER_OF_HANDS]; //elements are auto=init to 0 dealCards(); //GAME LOGIC //sum card values for each hand for (int i = 0; i for (int j = 0; j scores[i] += hands[i].getCard(j).getValue(); } } //find max score maxScore = scores[0]; for (int i = 1; i if (scores[i] > maxScore) { maxScore = scores[i]; } } } private void dealCards(){ for (int i = 0; i for (int j = 0; j hands[i].acceptCard(d.deal()); } } } public String toString(){ String state = \"\"; for (int i = 0; i state += \" Hand #\" + i + \", Score:\" + scores[i] + \" \"; state += hands[i].toString(); } state += \" \"; //allow for ties for (int i = 0; i if (scores[i] == maxScore) state += \"Hand #\" + i + \" wins with a score of \" + maxScore + \" \"; } return state; }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!