I am stuck on my lab and need some help. Below is the following API, I need help
ID: 3711663 • Letter: I
Question
I am stuck on my lab and need some help. Below is the following API, I need help creating both of these classes. The Higher Card game is played as follows: each player is dealt a number of cards, one per round. Then the players each play a card per round. The player with the highest card wins that round. When all the rounds are done, the players(s) who has wont he most rounds is the winner.
Here are the abstract classes that I have created that the above two classes need to extend.
Thank you in advance.
HighercCardComputerplaveroiavepayer that extends Highercardplayer. This clas Now create a HigherCardComputerPlayer that extends HigherCardPlayer. This class should provide definitions for all the abstract methods in its ancestors. The class simulates the computer playing, and simply randomly chooses the next card to play. HigherCardGame.java Finally, define the class HigherCardGame that extends CardGame. It should provide implementations for all the abstract methods in CardGame, that play the Higher card game as described. In the constructor, it should also create the deck and deal out the cards to the players. The HigherCardGame has an instance variable as follows:Explanation / Answer
import java.util.Random;
import java.util.Arrays;
import java.util.Collections;
public abstract class HigherCardPlayer {
protected Hand hand;
protected int roundsWon;
public static String rankToString(int rank) {
switch (rank) {
case 1:
return "Ace";
case 2:
return "Deuce";
case 3:
return "Three";
case 4:
return "Four";
case 5:
return "Five";
case 6:
return "Six";
case 7:
return "Seven";
case 8:
return "Eight";
case 9:
return "Nine";
case 10:
return "Ten";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
//Handle an illegal argument. There are generally two
//ways to handle invalid arguments, throwing an exception
//(see the section on Handling Exceptions) or return null
return null;
}
}
public HigherCardPlayer(String name, int rounds) {
int temp = rounds;
roundsWon = 0;
System.out.println("Player's Name: " + name);
System.out.println("Number of rounds " + rounds);
for(int i = 0; i < temp; i++) {
Random rand = new Random();
int n = rand.nextInt(13);
string card = rankToString(n);
System.out.println("Cards in Deal " + i + ": " + card);
hand.add(c);
rounds--;
System.out.println();
}
updateRoundsWon();
}
public abstract void updateRoundsWon() {
Random rand = new Random();
int n = rand.nextInt(13);
for(int i = 0; i < temp; i++) {
if(hand.get(i).intValue() > n) {
roundsWon++;
System.out.println("Rounds Won: " + roundsWon);
}
}
}
public abstract int getRoundsWon() {
return roundsWon;
}
}
public abstract class CardGame {
private CardPlayer[] players;
public static String stringToRank(string card) {
switch (card) {
case "Ace":
return 1;
case "Deuce":
return 2;
case "Three":
return 3;
case "Four":
return 4;
case "Five":
return 5;
case "Six":
return 6;
case "Seven":
return 7;
case "Eight":
return 8;
case "Nine":
return 9;
case "Ten":
return 10;
case "Jack":
return 11;
case "Queen":
return 12;
case "King":
return 13;
default:
//Handle an illegal argument. There are generally two
//ways to handle invalid arguments, throwing an exception
//(see the section on Handling Exceptions) or return null
return null;
}
}
public CardGame(CardPlayer[] players) {
this.players = 0;
}
public abstract void play(){
System.out.println("The Game Begins..");
String[] SUITS = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
String[] RANKS = {
"2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King", "Ace"
};
// initialize deck
int n = SUITS.length * RANKS.length;
String[] deck = new String[n];
for (int i = 0; i < RANKS.length; i++) {
for (int j = 0; j < SUITS.length; j++) {
deck[SUITS.length*i + j] = RANKS[i] + " of " + SUITS[j];
}
}
// shuffle
for (int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n-i));
String temp = deck[r];
deck[r] = deck[i];
deck[i] = temp;
}
// deal
for(int i = 0; i < 52; i += 4) {
int p = 0;
int round[4];
while(p < 4) {
int val = stringToRank(deck[i]);
int round[p] = val;
System.out.println("Player " + p + " receives " + deck[i]);
p++;
}
int largest = 0;
for(int i = 0; i < round.length; i++) {
if(array[i] > array[largest])
largest = i;
}
player[i]++;
}
}
public abstract boolean isGameOver() {
return true;
}
public abstract String getWinner() {
int largest = 0;
for(int i = 0; i < 13; i++) {
if(array[i] > array[largest]) {
largest = i;
}
}
return players[largest];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.