Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help with this java program - I wrote this program that is a card game that

ID: 3804332 • Letter: N

Question

need help with this java program -

I wrote this program that is a card game that the end goal is to match 4 cards of the same value(1,2,3,4,5,6 etc..) but with all 4 different suits..for example

*queen of hearts, queen of spades, queen of diamonds, queen of clubs.* -> if a player gets this output then the game is over and that player has won.

I need help to make my computer Player class more functional, for example making it able to win the game. (ComputerPlayer.java)

I need help to make my computer Player class more functional, for example making it able to win the game. (ComputerPlayer.java) This is what i need help doing^^^^

right now it can never win the game. this is the directions for what the computer program is supposed to do:

1. The computer can either draw a card from the draw pile or pick up the card that the human player put onto the discard pile. Then the computer player must place one of its cards onto the discard pile. If the computer player now has four cards with the same value, it wins. Otherwise, it is the human player’s turn; however, this time the human player can either take a card from the draw pile or pick up the card that the computer player put onto the discard

here is my code so far:

****CardGame.java********

package cardgame;

import java.util.Collections;
import java.util.Queue;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

/**
*
* @author
*/
public class CardGame {

Queue deck;
ComputerPlayer computer;
Player human;
Stack discardPile;

public CardGame() {
char suit[] = {'H', 'S', 'D', 'C'};
deck = new LinkedList();

for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= 13; j++)
{
deck.add(new Card(j, suit[i]));
}
}

discardPile = new Stack();

human = new Player();
computer = new ComputerPlayer();
}

private void shuffle() {

Collections.shuffle((List) deck);

this.deck = (LinkedList)deck;

}
  

public void start() {
shuffle();
  
for (int i = 0; i < 4; i++)
{
human.addCard(deck.remove(0));
computer.addCard(deck.remove(0));
}
int turn = 0;
int choice;
Card current;
Card top;
Scanner in = new Scanner(System.in);
while (!human.hasWon() && !computer.hasWon())
{
if (deck.isEmpty())
{
deck.addAll(discardPile);
discardPile.clear();
shuffle();
}

if (turn == 0) {
System.out.println("Your cards are: ");
human.displayCards();
System.out.println("");
if (discardPile.isEmpty()) {
choice = 2;
System.out.println("The discard pile is empty, draw a card.");
System.out.println("");

} else {
top = discardPile.peek();
System.out.println("The top of the discard pile is the " + top);
System.out.println("Do you want to pick up the " + top.toString() + "(1) or draw a card (2)");
choice = in.nextInt();
}

if (choice == 1)
{
current = discardPile.pop();
System.out.println("You picked up " + current);

}
else
{
current = deck.remove(0);
System.out.println("You drew " + current);
}
human.addCard(current);
System.out.println("Now your cards are ");
human.displayCards();
System.out.println("");
System.out.println("Which one do you want to discard ?");
choice = in.nextInt();
  
discardPile.push(human.discard(choice - 1));
}
else
{
  
if (!discardPile.isEmpty() && discardPile.peek().getValue() == computer.getMaxValue()) {
top = discardPile.pop();
System.out.println("I will pick up the " + top);
computer.addCard(top);

} else {
System.out.println("I will draw a new card.");
current = deck.remove(0);
computer.addCard(current);
}
current = computer.discard();
discardPile.push(current);
System.out.println("I will discard the " + current);
}

turn = 1 - turn;
}

}

public void finish() {
System.out.println("--------------------------");
System.out.println("My cards are ");
computer.displayCards();
System.out.println("--------------------------");
System.out.println("Your cards are ");
human.displayCards();
System.out.println("--------------------------");
if (human.hasWon()) {
System.out.println("You have won !");
}
else
{
System.out.println("the computer wins !");
}
System.out.println("--------------------------");
}

public static void main(String[] args) {
CardGame game = new CardGame();
game.start();
game.finish();
}
}

***end of cardgame.java******

here is player class

******player.java***************

package cardgame;

import java.util.ArrayList;
import java.util.Collections;

public class Player {

String name;
ArrayList cards;

public Player(String playerName) {
name = playerName;
}

public Player() {
cards = new ArrayList();
}

public void addCard(Card card) {
cards.add(card);
Collections.sort(cards);
}

public Card discard(int index) {
return cards.remove(index);
}

public Card getCard(int index) {
return cards.get(index);
}

public boolean hasWon() {
int value = cards.get(0).getValue();
for (int i = 1; i < cards.size(); i++) {
if (cards.get(i).getValue() != value) {
return false;
} else {
}
}

return true;
}

public void displayCards() {
for (int i = 0; i < cards.size(); i++) {
System.out.println(i + 1 + ". " + cards.get(i));
}
}

}

*****end of player.java*****

here is computerplayer class

*****ComputerPlayer.java************

package cardgame;


public class ComputerPlayer extends Player
{

private int decideDiscard()
{
int pValue = cards.get(0).getValue();
int index = 0;
int count = 1;
int min = 1;
for (int i = 1; i < cards.size(); i++)
{
if (cards.get(i).getValue() != pValue)
{
if (count < min) {
min = count;
index = i - 1;
pValue = cards.get(i).getValue();

}
count = 1;

} else {
count++;
}
}
return index;
}

public Card discard() {
return cards.remove(decideDiscard());
}

public int getMaxValue() {
int pValue = cards.get(0).getValue();
int count = 1;
int max = 1;

for (int i = 1; i < cards.size(); i++) {
if (cards.get(i).getValue() != pValue) {
if (count > max) {
max = count;
pValue = cards.get(i).getValue();
}
count = 1;
} else {
count++;
}
}
return pValue;
}
}

***end of computerplayer class********

here is card class

*****Card.java*********


package cardgame;


public class Card implements Comparable {

int value;
char suit;
private static final String[] VALUENAMES = {"", "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

public Card(int cardValue, char cardSuit)
{
value = cardValue;
suit = cardSuit;
}

int getValue() {
return value;

}

char getSuit() {
return suit;
}

@Override
public int compareTo(Card c) {
if (value < c.value) {
return -1;
} else if (value > c.value) {
return 1;
} else {
return 0;
}
}

@Override
public String toString() {
String suits = VALUENAMES[value] + " of ";
switch (suit) {
case 'H':
suits += "Hearts";
break;
case 'S':
suits += "Spades";
break;
case 'D':
suits += "Diamonds";
break;
default:
suits += "Clubs";
break;
}

return suits;
}

}

****end of card.java**************

Explanation / Answer

Yes. I have gone through all your code and there is major functionality which needs to be added i.e, For predicting the next emphasis of the loop, whether to check and confirm that the nextCard will become the currentCard, which helps in solving the prediction to be efficient.

I have added one method in ComputerPlayer.java program i.e, predictPlay() for predicting the next card and the proper result to be provided to the user.

Code:

package cardgame;

public class ComputerPlayer extends Player
{
private int decideDiscard()
{
int pValue = cards.get(0).getValue();
int index = 0;
int count = 1;
int min = 1;

for (int i = 1; i < cards.size(); i++)
{
if (cards.get(i).getValue() != pValue)
{
if (count < min) {
min = count;
index = i - 1;
pValue = cards.get(i).getValue();
}
count = 1;
} else {
count++;
}
}
return index;
}
public Card discard() {
return cards.remove(decideDiscard());
}

private static int predictPlay() {

Deck d = new Deck();
Card currentCardList;
Card nextCardList;
int correctGuesses ;
char predict;   
d.shuffle();
correctGuesses = 0;
currentCardList = d.dealCard();
System.out.println("This is your first card " + currentCardList);
  
while (true) {
  
System.out.print("To know whether your next card will be greater (G) or smaller (S)? ");
do {
predict = TextIO.getlnChar();
predict = Character.toUpperCase(predict);
if (predict != 'G' && predict != 'S')
System.out.print("Kindly prompt us with option G or S: ");
} while (predict != 'G' && predict != 'S');

  

nextCardList = d.dealCard();
System.out.println("Your next card is " + nextCardList);
  

if (nextCardList.getValue() == currentCardList.getValue()) {
System.out.println("You have received the same value as your previous one.!!!");
System.out.println("The game is tied. Please try again");
break;
}
else if (nextCardList.getValue() > currentCardList.getValue()) {
if (predict == 'G') {
System.out.println("The predictions conducted is efficient and correct.");
correctGuesses++;
}
else {
System.out.println("The predictions conducted is inappropriate and incorrect..");
break;
}
}
else {
if (predict == 'S') {
System.out.println("The predictions conducted is efficient and correct.");
correctGuesses++;
}
else {
System.out.println("The predictions conducted is inappropriate and incorrect..");
break;
}
}
  
currentCardList = nextCardList;
System.out.println();
System.out.println("The final card remaining is " + currentCardList);

}
  
System.out.println();
System.out.println("Finaly the game is over. Hope you had a great time.");
System.out.println("Great. Atlast you have made " + correctGuesses
+ " efficient predictions.");
System.out.println();
  
return correctGuesses;
  
}

public int getMaxValue() {
int pValue = cards.get(0).getValue();
int count = 1;
int max = 1;
for (int i = 1; i < cards.size(); i++) {
if (cards.get(i).getValue() != pValue) {
if (count > max) {
max = count;
pValue = cards.get(i).getValue();
}
count = 1;
} else {
count++;
}
}
return pValue;
}
}