Basically for my class i need to make the card game war. there doesn’t need to be any player input. just make a deck split it and compare cards and add a score value each time it works. However im stuck on the draw method which is supposed to be used to split the deck between playerOne and playerTwo.
Player.java
package javaWeek06FinalProject;
import java.util.ArrayList;
import java.util.List;
public class Player {
List<Card> hand = new ArrayList<Card>();
int score = 0;
String name;
Player (List<Card> hand , int score, String name) {
this.hand = hand;
this.score = score;
this.name = name;
}
public void describe() {
for (Card card : this.hand) {
card.describe();
}
System.out.println("You have " + this.score);
System.out.println("Your name is " + this.name);
}
public Card flip () {
Card card = this.hand.remove(0);
return card;
}
public List<Card> draw() {
Deck deck = new Deck();
hand.add(deck.cards.remove(0));
return hand;
}
public int incrementScore() {
score += 1;
return score;
}
}
Deck.java
package javaWeek06FinalProject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Deck {
List<Card> cards = new ArrayList<Card>();
Deck () {
String[] numbers = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
for (int i = 1; i <= 4; i++) {
int count = 2;
for (String number : numbers) {
Card card = new Card(number, count);
this.cards.add(card);
count++;
}
}
}
public void describe() {
for (Card card : this.cards) {
card.describe();
}
}
public void shuffle() {
Collections.shuffle(cards);
}
public Card draw() {
Card card = this.cards.remove(0);
return card;
}
}
Card.java
package javaWeek06FinalProject;
public class Card {
int value;
String name;
Card (String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void describe() {
System.out.println(this.name + " --- value = " + this.value);
}
}
App.java
package javaWeek06FinalProject;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) {
Deck deck = new Deck();
List<Card> hand = new ArrayList<Card>();
Player playerOne = new Player(hand, 0, "Player One");
Player playerTwo = new Player(hand, 0, "Player Two");
deck.shuffle();
deck.describe();
for (int i = 0; i < 26; i++) {
if (playerOne.hand.size() <= playerTwo.hand.size()) {
playerOne.draw();
} else {
playerTwo.draw();
}
}
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("---------------------");
/*
playerOne.incrementScore();
playerOne.incrementScore();
playerOne.incrementScore();
playerTwo.incrementScore();
playerTwo.incrementScore();
*/
playerOne.describe();
playerTwo.describe();
}
}
Output
Two --- value = 2
Eight --- value = 8
Six --- value = 6
Seven --- value = 7
Four --- value = 4
Three --- value = 3
Six --- value = 6
Ace --- value = 14
Six --- value = 6
Seven --- value = 7
Queen --- value = 12
Seven --- value = 7
Queen --- value = 12
Eight --- value = 8
Four --- value = 4
Jack --- value = 11
Five --- value = 5
Ace --- value = 14
Ten --- value = 10
Seven --- value = 7
Eight --- value = 8
Ten --- value = 10
Ace --- value = 14
Five --- value = 5
Four --- value = 4
Two --- value = 2
Nine --- value = 9
Five --- value = 5
Eight --- value = 8
Three --- value = 3
Jack --- value = 11
Ace --- value = 14
Five --- value = 5
Nine --- value = 9
King --- value = 13
Jack --- value = 11
Two --- value = 2
Jack --- value = 11
Queen --- value = 12
Six --- value = 6
Nine --- value = 9
King --- value = 13
King --- value = 13
Three --- value = 3
Three --- value = 3
Two --- value = 2
Ten --- value = 10
Four --- value = 4
Ten --- value = 10
Nine --- value = 9
Queen --- value = 12
King --- value = 13
---------------------
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
You have 0
Your name is Player One
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
Two --- value = 2
You have 0
Your name is Player Two
I cant for the life of me understand why its looping 2.
I have tried many things. too many to count or explain
GOS Playz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.