Question
Hello, I need help finding any issues with my code, which include me to update multiple classes for a spades game for my Java Class.
Hello, I need help finding any issues with my code, which include me to update multiple classes for a spades game for my Java Class. I need help finding any issues and if I have any Please let me know what I need wrong. I need this asap this is an assignment due soon. I am not a coding major so don't hate on my lack of knowledge thanks! :)
Constants class | |
Update enum Face to include the following Two has the value of 2 Three has the value of 3 Four has the value of 4 Five has the value of 5 Six has the value of 6 Seven has the value of 7 Eight has the value of 8 Nine has the value of 9 Ten has the value of 10 Jack has the value of 11 Queen has the value of 12 King has the value of 13 Ace has the value of 14 Add field value of data type int Add a public getter to return the field value Add a private constructor for enum Face with one parameter of type int; set the field value to the parameter passed in Update enum Suit to include the following Clubs has the rank of 0 Diamonds has the rank of 1 Hearts has the rank of 2 Spades has the rank of 3 Add field rank of data type int Add a public getter to return the field rank Add a private constructor for enum Suit with one parameter of type int; set the field rank to the parameter passed in Example code: public enum Suit { CLUBS (0), DIAMONDS (1), HEARTS (2), SPADES (3);
private int rank;
public int getRank() { return rank; }
private Suit(int rank) { this.rank = rank; } }
| |
core package | |
AiPlayer class | Update method placeBid() to do the following: Create a local variable of type int for the players bid Using an enhanced for loop, loop through the players hand (i.e. the ArrayList of class Card called hand) and increment the players bid based on the following logic for each card in the players hand:If, the face of the card is equal to an ACE or KING, increment the bid by one Else if, the suit of the card is equal to SPADES and the face value of the card is greater than TEN, increment the bid by one (1) If the bid counter is equal to zero(0), then by default set it equal to one (1) Call method .setBid() on the super class passing as an argument the local variable defined in step a. Return the bid by calling method .getBid() on the super class |
Game class | Update the class to do the following: Custom constructor Game()Comment out the call to method outputTeams() Add call to method play() Update method setTable() to do the following:Using an enhanced for loop output the names of each Player in the member variable table representing the setup of the players seated at the table Update method dealHand() to comment out any System.out.println() statements that show what card each player is being dealt if they exist Update method displayHands() to do the following:Call method .sortBySuit() for each player Only call method .displayHand() if the player is an instanceof class HumanPlayer Create method play() to do the following:Return type of void Empty parameter list Call method getBids() Create method getBids() to do the following:Return type of void Empty parameter list Create a local variable of data type int representing the number of bids placed initialized to the value of zero (0) Create a local variable of data type int to represent the index of the lead player, not initialized Using an if condition, check if the member value of the dealerIdx if less than 3If true then set the lead player index created in step d. equal to the dealerIdx plus one Else set the lead player index to zero(0) Set the member variable of class Player representing the leaderPlayer equal to method call .get() of the member variable table passing the lead player index as an argument Using an if condition, check if the leaderPlayer member variable is an instanceof class HumanPlayerIf true, create an instance of class HumanPlayer set equal to an explicit type cast of the member variable leadPlayer and call method .placeBid() on class HumanPlayer Else, create an instance of class AiPlayer set equal to an explicit type cast of the member variable leadPlayer and call method .placeBid() on class AiPlayer Increment the bids placed counter by one Create a local variable of data type int to represent the current player index, not initialized Using an if condition check if the lead player index is less than 3If true, set the player index equal to the lead player index plus one (1) Else, set the player index equal to the value of zero (0) Instantiate an instance of class Player set equal to the method call .get() on member variable table passing as an argument the player index created in step i. to get the nextPlayer While the number of bids submitted is less or equal to the number of players (i.e. 4)Write an if condition to determine if the local variable nextPlayer is an instanceof class HumanPlayerIf true, instantiate an instance of class HumanPlayer set equal to an explicit type cast of local variable nextPlayer and call method .placeBid() Else, instantiate an instance of class AiPlayer set equal to an explicit type cast of local variable AiPlayer set equal to an explicit type cast of local variable nextPlayer an call method .placeBid() Increment the bids placed counter Write an if condition to check if the value of the local variable for the player index is less than 3If true, increment the player index variable by one (1) Else, set the player index variable to zero (0) Set the local variable representing the nextPlayer equal to method call .get() on member variable table passing the player index as an argument |
HumanPlayer class | Update method placeBid() to do the following: Request the players bid using a System.out.println() method call Instantiate an instance of class Scanner passing System.in as an argument to the constructor Create a local variable of data type in set equal to the method call .nextInt() on the instance of class Scanner created in step b. Using a for condition, check if the inputted bid value is less than one (1), if true, output to the user they must bid at least one trick and set the local variable to the value of one (1) Call method .setBid() on the super class passing as an argument the local variable defined in step c. Return the bid by calling method .getBid() on the super class |
Player class | Update class to include the following: Add method sortBySuit() to do the following:Instantiate an instance of class ArrayList allowing only for instances of class Card to be elements that represents the sorted hand Loop while the size of member variable hand is greater than zero (0)Create a local variable of type int representing the current position in the ArrayList of member variable hand, initialize it to zero (0) Instantiate and instance of class Card (reference as firstCard) set equal to the first element in the ArrayList of member variable hand (hint: use the .get() method of class ArrayList) Using a for loop, starting at index 1, loop through the size of member variable ArrayList hand to do the following:Instantiate an instance of class Card (reference as nextCard) set equal to the next element in the ArrayList hand (i.e. index 1) Write an if statement to sort the cards based on suit and face value ranking with the following logic:If the suits rank of the nextCard in the hand is less than the suits rank of the firstCard in the hand ORThe suit of the nextCard is equal to the suit of the firstCard AND The face value of the nextCard in the hand is less than the face value of the firstCard in the handUpdate local variable position to equal the for loops looping variable Set the firstCard equal to the nextCard Remove from the member variable hand the card at the current position (hint: on member variable hand call method .remove() passing the argument defined in step i.) Add the instance represented as firstCard to the local ArrayList that represents the sorted hand created in step a. Set member variable hand equal to the local ArrayList that represents the sorted hand. |
Images of my Code are below:
Constants:
package constants;
/** * * @author Matth */
public class Constants { //Initiated constants with all caps and non changeable values public static final int AI_PLAYERS = 3; public static final int NUMBER_OF_CARDS = 52; public static final int CARDS_DEALT = 13; public static final int NUMBER_OF_ROUNDS = 13; public static final int BID_VALUE = 1; public static final int WINNING_SCORE = 200; public static final int ONE = 0; public static final int TWO = 1; public static final int NUMBER_OF_PLAYERS = 4; //Created enumerations for color, suit and face of each card. public enum Color { RED,BLACK } public enum Suit { CLUBS(0), DIAMONDS(1), HEARTS(2), SPADES(3); private int rank; public int getRank() { return rank; } private Suit(int rank) { this.rank = rank; } } public enum Face { TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14); private int value; public int getValue() { return value; } private Face(int value) { this.value = value; } } }
AiPlayer Class:
package core;
import constants.Constants; import constants.Constants.Face; import constants.Constants.Suit;
/** * * @author Matth */
//Created a public class AiPlayer so it extends the abstract class player public class AiPlayer extends Player{ //implements methods inherited from class player. @Override public Card playCard(){ return null; } @Override public int placeBid() { System.out.println(super.getName() + " place your bid"); int bid = 0; //The AI will assume that if they have an Ace or Kind of any suit they will get the //The AI will assume that if they have the Jack or Queen of spades they will get the for(Card card : super.getHand()) { //Chooses the Ace and King of any suit if(card.getFace() == Face.ACE || card.getFace() == Face.KING) { bid++; } //Only chooses Jack and Queen of Spades //If player already received King or Ace It chooses Jack and Queen of Spades. else if(card.getSuit() == Suit.SPADES && card.getFace().getValue() > Face.TEN.getValue()) { bid++; } } if (bid == 0) bid = 1; super.setBid(bid); System.out.println("Player " + this.getName() + " bid " + bid); return super.getBid(); } }
Game Class:
package core;
/** * * @author Matth */
import constants.Constants; import constants.Constants.Suit; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import java.util.Scanner;
public class Game { //Member Variables private Suit trump = Suit.SPADES; private Player leadPlayer; private Player dealer; private Player wonTrick; private int round; private ArrayList teams; private Deck deck; private Scanner scan; private ArrayList table; private int dealerIdx; //getters and setters for remainding member variables. /** * @return the deck */ public Deck getDeck() { return deck; }
/** * @param deck the deck to set */ public void setDeck(Deck deck) { this.deck = deck; }
/** * @return the leadPlayer */ public Player getLeadPlayer() { return leadPlayer; }
/** * @param leadPlayer the leadPlayer to set */ public void setLeadPlayer(Player leadPlayer) { this.leadPlayer = leadPlayer; }
/** * @return the dealer */ public Player getDealer() { return dealer; }
/** * @param dealer the dealer to set */ public void setDealer(Player dealer) { this.dealer = dealer; }
/** * @return the wonTrick */ public Player getWonTrick() { return wonTrick; }
/** * @param wonTrick the wonTrick to set */ public void setWonTrick(Player wonTrick) { this.wonTrick = wonTrick; }
/** * @return the round */ public int getRound() { return round; }
/** * @param round the round to set */ public void setRound(int round) { this.round = round; } //Custom constructor that calls method createTeams/outputTeams/generateDeck public Game() { createTeams(); //outputTeams(); createDeck(); setTable(); dealHand(); displayHands(); //Calls out play(); Method. play(); }
//Create Deck Method to initialize classes variable. private void createDeck() { //Initialize classes variable deck = new Deck(); } private void play() { System.out.println("**************"); System.out.println("Play the game!"); System.out.println("************** "); System.out.println("***************"); System.out.println("Get player bids"); System.out.println("***************"); //Starting with the player to the left of the dealer //Each player plays their best card. //Players must follow suit if they can, if not they trump if needed. getBids(); } private void getBids() { //Amount of bids placed int bidsPlaced = 0; //Setting up the lead player int leadIdx; if(dealerIdx < 3) leadIdx = dealerIdx++; else leadIdx = 0; leadPlayer = table.get(leadIdx); System.out.println("Lead player is player " + leadPlayer.getName()); //Starts with lead player and moves through to the others. if(leadPlayer instanceof HumanPlayer) { //Prompt active player to play a card. HumanPlayer hp = (HumanPlayer)leadPlayer; hp.placeBid(); } else { AiPlayer ai = (AiPlayer)leadPlayer; ai.placeBid(); } bidsPlaced++; //Receive remaining bids. int playerIdx; if(leadIdx < 3) playerIdx = leadIdx++; else playerIdx = 0; //Next player after lead player. Player nextPlayer = table.get(playerIdx); while(bidsPlaced <= Constants.NUMBER_OF_PLAYERS) { System.out.println("Getting bid from player " + nextPlayer.getName()); if(nextPlayer instanceof HumanPlayer) { //Asks player to play a card. HumanPlayer hp = (HumanPlayer)nextPlayer; hp.placeBid(); } else { AiPlayer ai = (AiPlayer)nextPlayer; ai.placeBid(); } //Increment Counter up bidsPlaced++; //Moves to next player if(playerIdx < 3) playerIdx++; else playerIdx = 0; nextPlayer = table.get(playerIdx); } } private void playHand() { } private void createTeams() { //Instantiate the teams ArrayList teams = new ArrayList(); //Instantiate Team One and add to the ArrayList Team teamOne = new Team(); teamOne.setTeamName("Team One"); teams.add(teamOne); //Instantiate Team Two and add to ArrayList Team teamTwo = new Team(); teamTwo.setTeamName("Team Two"); teams.add(teamTwo); //Adding Human Player to Team One scan = new Scanner(System.in); System.out.println("Enter human player name"); String name = scan.next(); HumanPlayer hp = new HumanPlayer(); hp.setName(name); System.out.println("Human Player added to Team One"); teamOne.getTeam().add(hp); //Create the AI Players and add them to a team for(int p = 1; p<= Constants.AI_PLAYERS; p++) { AiPlayer aip = new AiPlayer(); aip.setName("AI " + p); //Add AI Player to a team if(teamOne.getTeam().size() < 2) teamOne.getTeam().add(aip); else teamTwo.getTeam().add(aip); } } private void outputTeams() { //For loop to loop through the collection of member variable type team for (Team team: teams) { System.out.println(team.getTeamName() + " includes players:"); //and enhanced for loop to loop through collection of players. for(Player player : team.getTeam()) { System.out.println("Player: " + player.getName()); } //Ending inner bracket for loop }//Ending outter bracket for loop } private void setTable() { //Players and ai are set up so team members sit across from each other //After the deal would be set to TeamOne.PlayerOne, TeamTwo.PlayerTwo table = new ArrayList(); //Brings the separate players into the game. Team teamOne = teams.get(Constants.ONE); Team teamTwo = teams.get(Constants.TWO); //Players get taken from each team. Player teamOnePlayerOne = teamOne.getTeam().get(Constants.ONE); Player teamOnePlayerTwo = teamOne.getTeam().get(Constants.TWO); Player teamTwoPlayerOne = teamTwo.getTeam().get(Constants.ONE); Player teamTwoPlayerTwo = teamTwo.getTeam().get(Constants.TWO); /* Explicitly state which seat each player is sitting at... Then use the add method to take two arguements and... set position for 1st argument in the arraylist and... the assisted object for that position. */ table.add(0, teamOnePlayerOne); table.add(1, teamTwoPlayerOne); table.add(2, teamOnePlayerTwo); table.add(3, teamTwoPlayerTwo); //Selects the first dealer which gets randomized. Random random = new Random(); dealerIdx = random.nextInt(Constants.NUMBER_OF_PLAYERS); dealer = table.get(dealerIdx); System.out.println("Players at the table are"); for(Player player : table) { System.out.println(player.getName()); } } private void dealHand() { System.out.println("Player " + dealer.getName() + " will deal the hand"); //Keep track of what player has taken the card //Reset when it gets to 3 //Set the Playeridx to check which player was selected as the dealer and int playerIdx; //Iterate 1 to it. if (dealerIdx < 3) playerIdx = dealerIdx++; else playerIdx = 0; //Loops through suffled deck and deals 13 cards to each player for(Iterator currentCard = deck.getDeck().iterator(); currentCard.hasNext();) { Card card = currentCard.next(); //System.out.println("Dealing " + card.getFace() + " of " + card.getSuit()); //Adds player's card to their hand //System.out.println("Adding to player " + table.get(playerIdx).getName() + " hand "); table.get(playerIdx).getHand().add(card); //Increments player's index until value hits 3, then it resets to 0 if(playerIdx == 3) playerIdx = 0; else playerIdx++; //Removes card from deck so it can't be dealt again. currentCard.remove(); } } private void displayHands() { for(Team team : teams) { System.out.println("*************************"); System.out.println(" " + team.getTeamName().toUpperCase()); System.out.println("*************************"); for(Player player : team.getTeam()) { player.displayHand(); if(player instanceof HumanPlayer) { player.displayHand(); } } } } }
HumanPlayer Class:
package core;
import java.util.Scanner;
/** * * @author Matth */
//Created a public class HumanPlayer so it extends the abstract class player public class HumanPlayer extends Player{ @Override public Card playCard(){ throw new UnsupportedOperationException("Not supported yet."); } @Override public int placeBid() { System.out.println(super.getName() + "place your bid"); Scanner scanner = new Scanner(System.in); int bid = scanner.nextInt(); if(bid < 1) { System.out.println("You must bid at least one trick"); bid = 1; } return super.getBid(); } }
Player Class:
package core;
import java.util.ArrayList;
/** * * @author Matth */ public abstract class Player implements IPlayer{
//member variables private String name; private int tricks; private int bid; private int score; private ArrayList hand; //Abstract methods. @Override public abstract Card playCard(); @Override public abstract int placeBid(); /** * @return the hand */ public ArrayList getHand() { return hand; }
/** * @param hand the hand to set */ public void setHand(ArrayList hand) { this.hand = hand; }
public Player () { hand = new ArrayList(); } /** * @return the name */ public String getName() { return name; }
/** * @param name the name to set */ public void setName(String name) { this.name = name; }
/** * @return the tricks */ public int getTricks() { return tricks; }
/** * @param tricks the tricks to set */ public void setTricks(int tricks) { this.tricks = tricks; }
/** * @return the bid */ public int getBid() { return bid; }
/** * @param bid the bid to set */ public void setBid(int bid) { this.bid = bid; }
/** * @return the score */ public int getScore() { return score; }
/** * @param score the score to set */ public void setScore(int score) { this.score = score; } public void displayHand() { System.out.println("**************************"); System.out.println("Player" + name + " hand is "); System.out.println("**************************"); for(Card card : hand) { System.out.println(card.getFace() + " of " + card.getSuit()); } } public void sortBySuit() { //Sets up the cards into increasing sorted value. //Cards with same value end up being sorted by suit. ArrayList sortedHand = new ArrayList(); while (hand.size() > 0) { int position = 0; Card firstCard = hand.get(0); for (int i = 1; i < hand.size(); i++) { Card nextCard = hand.get(i); if (nextCard.getSuit().getRank() < firstCard.getSuit().getRank() || (nextCard.getSuit() == firstCard.getSuit() && nextCard.getFace().getValue() < firstCard.getFace().getValue())) { position = i; firstCard = nextCard; } } hand.remove(position); sortedHand.add(firstCard); } hand = sortedHand; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started