java war game

Answer

Design

 

The program will need to simulate war card game.  There are 52 cards: 4 suits (diamond, hearts, spades and clubs), each suit has 13 cards each, ace, 2 to 10 and jack, queen and king. Jack, queens and kings are all valued at 11, 12, and 13 points respectively. Aces are valued 1 point. The game has 2 players player and computer. The cards are dealt randomly to player and computer. The player with the highest card wins that hand and keeps the two cards that were dealt. Display cards of players and display who has won this round. Each player has to keep track of the number of cards that have won. Selected cards are removed from the deck. If player and computer has a card of the same value, there is a tie and both players keep their card. If no more cards on the deck finish game and display game winner.

 

Algorithm of the program

 

 

Start main:

Declare arraylist  suits as ArrayList<Integer>

Declare arraylist  values as ArrayList<Integer>

 

Set numberPlayerCardsWon to 0

Set numberComputerCardsWon to 0

For  s=1 to 4 do

For v=1 to 13 do

add s to arraylist suits

add v to arraylist values

end for

end for

for d=0 to size of values divide 2  do

Deal card for Player

Set randomCard to random value from 0 to size of values

Set selectedSuitPlayer to selected suit from deck

Set selectedValuePlayer to selected valuefrom deck

Delete selected suit from arraylist suits

Delete selected value from arraylist values

Display player card calling function displayCard

Deal card for Computer

Set randomCard to random value from 0 to size of values

Set selectedSuitComputer to selected suit from deck

Set selectedValueComputer to selected valuefrom deck

Delete selected suit from arraylist suits

Delete selected value from arraylist values

Display computer card calling function displayCard

 

If selectedValuePlayer>selectedValueComputer then

Display message “***Player won this hand!***”

Set numberPlayerCardsWon to numberPlayerCardsWon 2

else if selectedValuePlayer<selectedValueComputer then

Display message “***Computer won this hand!***”

Set numberComputerCardsWon to numberComputerCardsWon+2

else

Display message “***Tie!***”

Set numberPlayerCardsWon to numberPlayerCardsWon+1

Set numberComputerCardsWon to numberComputerCardsWon+1

End if

Display message “Press ‘Enter’ to continue…”

delay

Edn for

 

If numberPlayerCardsWon>numberComputerCardsWon then

Display message “***Player won this game!***”

else if numberPlayerCardsWon<numberComputerCardsWon then

Display message “***Computer won this game!***”

else

Display message “***Tie!***”

End if

Stop

 

Start suitToString(suit)

switch (suit)

case 1:

return “Clubs”

case 2:

return “Diamonds”

case 3:

return “Hearts”

case 4:

return “Spades”

default:

return null

end switch

Stop

 

Start valueToString(value)

switch (value)

case 1:

return “Ace”

case 2:

return “2”

case 3:

return “3”

case 4:

return “4”

case 5:

return “5”

case 6:

return “6”

case 7:

return “7”

case 8:

return “8”

case 9:

return “9”

case 10:

return “10”

case 11:

return “Jack”

case 12:

return “Queen”

case 13:

return “King”

default:

return null

end switch

Stop

 

Start displayCard(suit, value)

return valueToString(value)+” of “+suitToString(suit)

Stop

 

Implementation

 

 

package warcardgame;

 

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

 

public class WarCardGame {

/**

* Main method

* @param args

*/

public static void main(String[] args) {

//Scanner for keyBoard

Scanner keyBoard =new Scanner(System.in);

Random rand=new Random();

//ArrayList of Integer to store suits

ArrayList<Integer> suits =new ArrayList<Integer>();

//ArrayList of Integer to store values

ArrayList<Integer> values =new ArrayList<Integer>();

//variable for number player cards won

int numberPlayerCardsWon=0;

//variable for number computer cards won

int numberComputerCardsWon=0;

for(int s=1;s<=4;s++){

for(int v=1;v<=13;v++){

//add suits to ArrayList suits

suits.add(s);

//add values to ArrayList suits

values.add(v);

}

}

//play

for(int d=0;d<values.size()/2;d++){

//deal card for Player

//select random card from deck

int randomCard=rand.nextInt(values.size());

//select random suit from deck

int selectedSuitPlayer=suits.get(randomCard);

//select random value from deck

int selectedValuePlayer=values.get(randomCard);

//remove selected suit from deck

suits.remove(randomCard);

//remove selected value from deck

values.remove(randomCard);

//display player card

System.out.println(“Player card: “+displayCard(selectedSuitPlayer, selectedValuePlayer));

 

//deal card for Computer

randomCard=rand.nextInt(values.size());

//select random suit from deck

int selectedSuitComputer=suits.get(randomCard);

//select random value from deck

int selectedValueComputer=values.get(randomCard);

//remove selected suit from deck

suits.remove(randomCard);

//remove selected value from deck

values.remove(randomCard);

//display computer card

System.out.println(“Computer card: “+displayCard(selectedSuitComputer, selectedValueComputer));

//check if player won this hand

if(selectedValuePlayer>selectedValueComputer){

//the player with the highest card wins that hand and keeps the two cards that were dealt.

System.out.println(“***Player won this hand!***”);

numberPlayerCardsWon+=2;

}else if(selectedValuePlayer<selectedValueComputer){ //check if computer won this hand

System.out.println(“***Computer won this hand!***”);

numberComputerCardsWon+=2;

}else{//display tie

System.out.println(“***Tie!***”);

//If both player has a card of the same value, there is a tie and both players keep their card

numberPlayerCardsWon++;

numberComputerCardsWon++;

}

//promt user to press “Enter”

System.out.println(“Press ‘Enter’ to continue…”);

//delay

keyBoard.nextLine();

}

//close keyBoard

keyBoard.close();

//Once the game is complete the winner must be displayed.

if(numberPlayerCardsWon>numberComputerCardsWon){

//display mesage: “***Player won this game!***”

System.out.println(“***Player won this game!***”);

}else if(numberPlayerCardsWon<numberComputerCardsWon){

//display mesage: “***Computer won this game!***”

System.out.println(“***Computer won this game!***”);

}else{

//display mesage: “***Tie!***”

System.out.println(“***Tie!***”);

}

}

/**

* Suit to string

* @param suit

* @return

*/

private static String suitToString(int suit) {

switch (suit) {

case 1:

return “Clubs”;

case 2:

return “Diamonds”;

case 3:

return “Hearts”;

case 4:

return “Spades”;

default:

return null;

}

}

/**

* Convert value to string

* @param value

* @return

*/

private static String valueToString(int value) {

switch (value) {

case 1:

return “Ace”;

case 2:

return “2”;

case 3:

return “3”;

case 4:

return “4”;

case 5:

return “5”;

case 6:

return “6”;

case 7:

return “7”;

case 8:

return “8”;

case 9:

return “9”;

case 10:

return “10”;

case 11:

return “Jack”;

case 12:

return “Queen”;

case 13:

return “King”;

default:

return null;

}

}

/**

* Display card

* @param suit

* @param value

* @return

*/

private static String displayCard(int suit,int value){

return valueToString(value)+” of “+suitToString(suit);

}

}

 

 

Testing

 

Data Expected Result Actual result
Deck of 52 cards

 

Player card: 7 of Hearts

Computer card: 4 of Clubs

Display message: “Player won this hand”

Value 7 is bigger than 4 that’s why computer won this hand.

Player card: 7 of Hearts

Computer card: 4 of Clubs

***Player won this hand!***

Deck of 52 cards

 

Player card: 9 of Spades

Computer card: Jack of Diamonds

Display message: “Computer won this hand”

 

Value Jack(11)  is bigger than 9, that’s why computer won this hand.

Player card: 9 of Spades

Computer card: Jack of Diamonds

***Computer won this hand!***

 

Deck of 52 cards Display message: “Player won game”

Player won game, because he has more cards than computer.

run:

Player card: 7 of Hearts

Computer card: 4 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: Queen of Spades

Computer card: 3 of Diamonds

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 9 of Spades

Computer card: Jack of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: King of Hearts

Computer card: Ace of Hearts

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 8 of Hearts

Computer card: 3 of Hearts

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 9 of Hearts

Computer card: 5 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 8 of Diamonds

Computer card: 6 of Spades

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: King of Clubs

Computer card: 6 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 10 of Hearts

Computer card: 10 of Clubs

***Tie!***

Press ‘Enter’ to continue…

 

Player card: 2 of Spades

Computer card: Queen of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 7 of Spades

Computer card: Queen of Hearts

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 2 of Clubs

Computer card: 7 of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 8 of Clubs

Computer card: 7 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

***Player won this game!***

Deck of 52 cards

 

Player card: 2 of Clubs

Computer card: 7 of Diamonds

Display message:  ***Computer won this hand!***

 

Value 7  is bigger than 2, that’s why computer won this hand.

Player card: 2 of Clubs

Computer card: 7 of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

Deck of 52 cards

 

Player card: King of Clubs

Computer card: 6 of Clubs

Display message:   ***Player won this hand!***

 

Value King (13)  is bigger than 6, that’s why Player won this hand.

Player card: King of Clubs

Computer card: 6 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

Deck of 52 cards Display message: “Computer won game”

 

Computer won game, because it has more cards than player.

run:

Player card: Jack of Clubs

Computer card: 3 of Spades

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 5 of Clubs

Computer card: King of Spades

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 2 of Clubs

Computer card: 6 of Hearts

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 9 of Spades

Computer card: 10 of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 6 of Diamonds

Computer card: 8 of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 3 of Diamonds

Computer card: 9 of Hearts

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 10 of Spades

Computer card: 7 of Diamonds

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: Jack of Spades

Computer card: 7 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: Jack of Hearts

Computer card: Ace of Hearts

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 5 of Hearts

Computer card: 6 of Clubs

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: Ace of Diamonds

Computer card: 3 of Hearts

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: Jack of Diamonds

Computer card: Queen of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 9 of Diamonds

Computer card: 9 of Clubs

***Tie!***

Press ‘Enter’ to continue…

 

***Computer won this game!***

Deck of 52 cards Display message: “Tie”

 

Computer has the same number of cards as  player.

run:

Player card: 9 of Hearts

Computer card: King of Diamonds

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 2 of Diamonds

Computer card: 3 of Spades

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: 9 of Spades

Computer card: King of Hearts

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: Ace of Spades

Computer card: 6 of Clubs

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: King of Spades

Computer card: 10 of Hearts

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 7 of Hearts

Computer card: 5 of Spades

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 8 of Hearts

Computer card: 6 of Spades

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 4 of Clubs

Computer card: 4 of Diamonds

***Tie!***

Press ‘Enter’ to continue…

 

Player card: 2 of Clubs

Computer card: Queen of Spades

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: Queen of Hearts

Computer card: Jack of Hearts

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: Queen of Diamonds

Computer card: Ace of Diamonds

***Player won this hand!***

Press ‘Enter’ to continue…

 

Player card: 3 of Diamonds

Computer card: 9 of Clubs

***Computer won this hand!***

Press ‘Enter’ to continue…

 

Player card: Jack of Clubs

Computer card: 3 of Clubs

***Player won this hand!***

Press ‘Enter’ to continue…

 

***Tie!***

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: