Sorry if I am coming to you with such a basic issue. I tried searching(and searching some more) but I cannot find any solution. I am still a newbie to programming, I am trying to create a java version of the card game WAR. I have been troubleshooting this for over a week and have changed and tried many things, but nothing seems to solve my issue. I feel that my logic is what is causing the problem. My App class(where I am getting the error is on the deck.draw line) looks like this:
package warCardGame;
public class App {
public static void main(String[] args) {
Deck deck = new Deck();
deck.shuffle();
Player p1 = new Player("Harvey");
Player p2 = new Player("Dee");
for(int i = 1; i < 26; i++) {
p1.hand.add(deck.draw());
p2.hand.add(deck.draw());
}
for(int i = 0; i < 26; i++) {
Card p1Card = p1.flip();
Card p2Card = p2.flip();
if(p1Card.getValue() > p2Card.getValue()) {
p1.incrementScore();
} else if(p2Card.getValue() > p1Card.getValue()) {
p2.incrementScore();
} else {
System.out.println("A rematch is needed!");
}
}
if(p1.score > p2.score) {
System.out.println("The final score for " + p1.name +" is" + p1.score + "\n");
System.out.println("The final score for " + p2.name +" is" + p2.score + "\n");
System.out.println("The winner today is " + p1.name + " !");
}else if(p2.score > p1.score) {
System.out.println("The final score for " + p2.name +" is" + p2.score + "\n");
System.out.println("The final score for " + p1.name +" is" + p1.score + "\n");
System.out.println("The winner today is " + p2.name + " !" + "\n");
}else {
System.out.println("The final score is a DRAW! A rematch is needed!");
}
}
}
My card class looks like this:
package warCardGame;
public class Card {
public static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
public static final String[] RANK = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
private static int name;
private static int value;
public void describe() {
for(int i = 0; i < SUIT.length - 1; i++) {
for(int j = 0; j < RANK.length - 1; j++) {
System.out.println("The current suit is " + SUIT + " " + ", and the current rank is " + RANK + " .");
}
}
}
//method to assign int values to the SUIT string array
public static int defineSuitValue() {
value = 0;
for(int i = 0; i <= SUIT.length; i++) {
value = value + 1;
}
return value;
}
//method to assign int values to the RANK string array
public static int defineRankValue() {
name = 1;
for(int i = 0; i <= RANK.length; i++) {
name = name + 1;
}
return name;
}
public Card(int name, int value) {
Card.name = defineRankValue();
Card.value = defineSuitValue();
}
public int getName() {
return defineSuitValue();
}
public void setName(int name) {
Card.name = defineSuitValue();
}
public int getValue() {
return defineRankValue();
}
public void setValue(int value) {
Card.value = defineRankValue();
}
}
The deck class:
package warCardGame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck {
List<Card> cards = new ArrayList<Card>();
Card[] deck = new Card[52];
public Deck() {
//int c = 0;
for(int i = 0; i < 4; i++) {
for(int j = 2; j <= 14; j++) {
cards.add(new Card(i, j)); //need to rework
}
}
}
public List<Card> getCards(){
return cards;
}
public void setCard(List<Card> cards) {
this.cards = cards;
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card draw() {
if(cards.isEmpty()) {
return null;
}else {
Card topCard = cards.get(0);
cards.remove(0);
return topCard;
}
}
}
My player class:
package warCardGame;
import java.util.List;
public class Player {
List<Card> hand;
int score;
String name;
public Player(String name) {
this.score = 0;
this.name = name;
}
public void describe() {
System.out.println(name + " " + "has a score of " + score + " " + "!");
for(Card card : hand) {
card.describe();
}
}
public List<Card> getHand() {
return hand;
}
public void setHand(List<Card> hand) {
this.hand = hand;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Card flip() {
Card topCard = hand.get(0);
hand.remove(0);
return topCard;
}
public void draw(Deck deck) {
Card card = deck.draw();
hand.add(card);
}
public void incrementScore() {
score = getScore() + 1;
}
}
I have spent so many days trying to fix this, and I am not sure if I have made it worse by adding additional code. I TRULY appreciate any feedback you can provide, thanks!!