Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions