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 givenCard;

List spoils = new ArrayList<>();

Queue playerA = new LinkedList<>();

Queue playerB = new LinkedList<>();

int who;

boolean gameOver;

int time = 0;

War(List deck){

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 deck) {

War w = new War(deck);

return w.findWinner();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!