Question: There is a problem in my code which i can't see where it is. Please read the description of this project(http://people.cs.umass.edu/~liberato/courses/2017-spring-compsci190d/assignments/programming-assignment-11-war/)go to this link. It
There is a problem in my code which i can't see where it is. Please read the description of this project(http://people.cs.umass.edu/~liberato/courses/2017-spring-compsci190d/assignments/programming-assignment-11-war/)go to this link. It is just a simple war card game.
/**
*
* A Java class to simulate the card game War. See assignment writeup for details.
*
* @author liberato
*
*/
public class War {
/**
* Determines the winner of a game of War, returning 1 if player A wins, -1 if player B wins, 0 if a draw.
*
* The rules of the game are defined in the assignment writeup.
*
* @param deck
* @return 1 if player A wins, -1 if player B wins, 0 if a draw
*/
List
List
Queue
Queue
int who;
boolean gameOver;
int time = 0;
War(List
givenCard = deck;
who = 2;
gameOver = false;
}
public void splitTheCard(){
if(givenCard.size()==0){
who = 0; gameOver = true;
}
if(givenCard.size()==1){
playerA.addAll(givenCard);
}
if(givenCard.size()%2==0){
for(int i = 0; i playerA.add(givenCard.get(i));playerB.add(givenCard.get(i+1)); } } if(givenCard.size()%2!=0){ for(int i = 0; i playerA.add(givenCard.get(i)); } for(int i = 1; i playerB.add(givenCard.get(i+1)); } } } public int findWinner(){ splitTheCard(); while(!gameOver){ if(time>1000){ return 0; } battle(); } return who; } public void battle(){ if(playerA.isEmpty()&&playerB.isEmpty()){ who = 0; gameOver = true; } if(playerA.isEmpty()){ who = -1; gameOver = true; } if(playerB.isEmpty()){ who = 1; gameOver = true; } else{ time++; int a = playerA.poll(); int b = playerB.poll(); spoils.add(a);spoils.add(b); if(a>b){ playerA.addAll(spoils); spoils = new ArrayList<>(); } if(a playerB.addAll(spoils); spoils = new ArrayList<>(); } else{ war(); } } } public void war(){ if(playerA.size()<=4&&playerB.size()<=4){ who = 0; gameOver = true; } if(playerA.size()>=4&&playerB.size()<=4){ who = 1; gameOver = true; } if(playerA.size()<=4&&playerB.size()>=4){ who = -1; gameOver = true; } spoils.add(playerA.poll());spoils.add(playerA.poll());spoils.add(playerA.poll()); spoils.add(playerB.poll());spoils.add(playerB.poll());spoils.add(playerB.poll()); } public static int findWinner(List War w = new War(deck); return w.findWinner(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
