You are on page 1of 9

CARD GAME DOCUMENTATION

Morgan Davies
OXFORD BROOKES UNIVERSITY FD Computing Yr 1/2
The Problem:
I am required to produce a program of a simplified version of War in Java, using methods and
arrays. In this version of War, 2 players are involved, the user and the computer. Each player is
given a card from a deck of 52 cards, the player with the highest card out of the 2, the winner gets to
keep both cards. However, if the player receives cards of the same value, both players will keep their
respective card if its a draw. The game is over when all 52 cards are dealt from the deck, the player
with the highest number of cards wins the game. But if both players receive the same number of
cards, the game ends with a draw.

The card features of the game:


52 cards, with values of (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K)
Picture cards such as the Jack, Queen and King have the values of 11, 12 and 13
The value of Ace is 1
The suits of any card is irrelevant
There will be 4 sets of cards from 1 to 13

The functional requirements of this program:


The program must run a game with 2 players and a deck of 52 cards
Each card must be dealt to each player and removed from the deck
The cards dealt to each player must be compared
The player with the highest card wins both the cards dealt
If both card has the same card value, both players get to keep their cards
Each player needs to keep track of the cards won
The game ends when there are no more cards in the deck
Once the game ends, the winner of the game is shown

The description of the game:


When the game starts, the game starts with both scores set at 0 and a deck with 52 cards
A card is randomly selected and given to each player and is removed from the deck or set to
0
Each players card is compared to find the highest out of the 2 dealt
The player with the highest value gets to keep both dealt cards
The 2 cards is then added to players score
If both players have the card with the same value, the players get to keep their cards
Each player gets a card added to their scores
The scores are then shown on how many cards the player has won
This continues to all the cards are dealt and the deck is at 0
The final score is the printed with the winner of the game

Possible errors:
There are possible errors that might occur while implementing, testing and running the program that
follows the brief, this includes:

The game continuing after all cards are dealt


The dealt card is not removed or set to 0
Both cards are dealt the same for each player
The incorrect scores are given to each player
The winner is not declared
The wrong player declared as the winner
The cards are not compared

Possible variables and data types


The table below shows the possible variables and data types that may be implemented and used in
the program. Each has a different type and different uses that will help implement the design to a
program

Number Name Type Description


1 randomCard int This will be used to generate a number from 0 to 51
in the deck. This will be used to retrieve a card from
the array of cards
2 drawCard int This will hold the index value of the random
generated number from randomCard. This will be
used to pass it on to the player and the computer
3 playerCard int This will hold the index value from the card drawn
from randomCard and serves as the players card.
This will be used to compare the computers card
4 cpuCard int This will hold the index value from the card drawn
from randomCard and serves as the computers
card. This will be used to compare the players card
5 playerScore int This will hold the players score throughout the
game, the score will be set to 0 to begin with.
Throughout the game, cards will be added to it if the
player wins the turn or draws
6 cpuScore int This will hold the computers score throughout the
game, the score will be set to 0 to begin with.
Throughout the game, cards will be added to it if the
computer wins the turn or draws
7 deck int This will be used to represent the number of cards
in the deck. The value will be set to 52 at the start
and will be deducted by 2, representing both cards
have been dealt to the players

Possible arrays
This table below shows the possible arrays that will be used to store the values during the game. I
assume that the state below arrays will be used for cards and scores.

Number Name Type Description


1 cards int This array will be used to store the 52 cards (4 sets
of 1 to 13)
2 scores int This array will store the scores that will be displayed
and updated as the game progresses
The pseudocode
This below is the initial idea of how the code will be implemented for the game, this provides the
basis of how the code will be implemented, how methods are called and passing parameters, if the
loops are going to be completed, and ensuring that conditions and statements put in place are true
and completed
Method deckCard
An array of cards = 4 sets of 1 to 13
Return the deck of cards

Method dealCard (receives the deck of cards)


A randomCard is generated from 0 to 51
drawCard will receive the card value from the index
while the card value is 0
A randomCard is generated from 0 to 51
If the card value is not 0
Set the card value in the index to 0
Return the dealt card

Method updateScore (receives the deck of cards)


The deck of cards is initialised here
The player receives its card from dealCard
The computer receives its card from dealCard
An array of scores needed to be added with 2 indices
If the player card is greater than the computers card
Show the cards dealt
Show that the player has won the turn
Set the first index to 2
Return the score
If the computer card is greater than the players card
Show the cards dealt
Show that the computer has won the turn
Set the second index to 2
Return the score
If there is a draw
Show the cards dealt
Show that there is a draw
Set both indices to 1
Return the scores
Return the scores

The main method


The deck is set to 52 to simulate the full deck
The scores are set to 0
While the deck is not 0
Call the updateScore method
Add the first index to the players score
Add the second index to the computers score
Take 2 away from the deck
At the end the final score will be displayed
If the player has the most cards
Show that the player has won the game
If the computer has the most cards
Show that the computer has won the game
If both player have the same amount of cards
Show that the game is a draw
What has happened?
The code was then implemented using NetBeans for Java, this was tested using print statements to
ensure that each card is withdrawn from the deck, points are given to each player and that the
winner was declared.

Testing of the initial code


After implementing the initial code, based on the pseudocode designed along with the specifications
and requirements of the game, the game has been tested to ensure that the functions do follow the
specification. The results of the test show that it does reflect the basics of the requirements, but
there were some aspects that needed to be addressed and resolved.
What needs to be resolved is that when the updateScore method is called, the card is set again and
we end up getting numbers that have previously been drawn and discarded.
Number Test Name Expected Result Actual Result Improvements?
1 Showing the I am expecting a result to show all The result of the test is that Improvements are
cards the cards initialised, a print the array is initialised with not necessary
statement is put in place to show 4 sets of 1 to 13, simulating
the array of cards with 4 sets of 1 the cards in a deck
to 13
2 Showing the I am expecting the result to show The result of the test shows Improvements are
dealt cards that each player has been given the that the players are getting not necessary
index value after a number a card value from 1 to 13
between 0 to 51 is generated
3 Changing the I am expecting the index value from The card has been The card may need to
value to 0 the dealt card to be changed to 0, removed, but when the be initialised outside
indicating that the card is removed method is called again, the the method or at the
from the deck cards are set back to its main method
original values
4 Adding the I am expecting to show the winning The winning player has Improvements are
scores to a player of the turn and add the been shown and the score not necessary
player score to the total is updated with the
winning player
5 Adding both I am expecting to show that both Both players have tied and Improvements are
scores in players have tied and add a point to is shown, each players not necessary
event of a each player score has been added by 1
type
6 Finding the I am expecting a winner to be The result of the test does Improvements are
winner of the shown after 52 cards have been show a winner if 1 players not necessary
game dealt. This includes the amount of has more amount of cards
cards both players have won in compared to the other
total
7 The game I am expecting a draw after 52 The result of the test does Improvements are
ending in a cards have been dealt. This show a draw if both players not necessary
draw includes the amount of cards have have the same amount of
won in total cards
It seems possible that the card is initialised in the loop, therefore, I can assume that the card is reinitialised after when
updateScore is in the loop. With that, I need to find a way that the method would receive the dealt cards and compare it
before adding the scores.

Method updateScore (receives the deck of cards)


The deck of cards is initialised here
The player receives its card from dealCard
The computer receives its card from dealCard
An array of scores needed to be added with 2 indices
If the player card is greater than the computers card
Show the cards dealt
Show that the player has won the turn
Set the first index to 2
Return the score
If the computer card is greater than the players card
Show the cards dealt
Show that the computer has won the turn
Set the second index to 2
Return the score
If there is a draw
Show the cards dealt
Show that there is a draw
Set both indices to 1
Return the scores
Return the scores

What could be done is that the method should receive the card values and not the deck of cards itself, and the deck of
cards to be initialised at the main method, along with dealing cards to the players.

Method updateScore (receives the players and computers dealt cards)


An array of scores needed to be added with 2 indices
If the player card is greater than the computers card
Show the cards dealt
Show that the player has won the turn
Set the first index to 2
If the computer card is greater than the players card
Show the cards dealt
Show that the computer has won the turn
Set the second index to 2
If there is a draw
Show the cards dealt
Show that there is a draw
Set both indices to 1

The initialising of cards and dealing of the cards will be done on the main method and will be passed on to the
updateScore method as parameters.

The main method


The deck is set to 52 to simulate the full deck
The deck of cards is initialised here
The scores are set to 0
While the deck is not 0
The player receives its card from dealCard
The computer receives its card from dealCard
Call the updateScore method and passing on the player and computer cards
Add the first index to the players score
Add the second index to the computers score
Take 2 away from the deck
At the end the final score will be displayed
If the player has the most cards
Show that the player has won the game
If the computer has the most cards
Show that the computer has won the game
If both player have the same amount of cards
Show that the game is a draw

The final step of the game, which is finding the winner of the game or the game ending as a draw, can be a separate
method and the final scores can be passed on to the parameters as reference

The endGame method with the parameters of the players and computers scores
If the player has the most cards
Show that the player has won the game
If the computer has the most cards
Show that the computer has won the game
If both player have the same amount of cards
Show that the game is a draw
Testing the new code
After updating and implementing new lines of code, similar tests have been performed and further
tests has been conducted to include the new method implemented, along with the changes of where
the card is initialised and the dealing of the cards
Number Test Name Expected Result Previous Result New Result
1 Showing the I am expecting a result to The result of the test is that The results remain the
cards show all the cards initialised, the array is initialised with same
a print statement is put in 4 sets of 1 to 13, simulating
place to show the array of the cards in a deck
cards with 4 sets of 1 to 13
2 Showing the I am expecting the result to The result of the test shows The results remain the
dealt cards show that each player has that the players are getting same
been given the index value a card value from 1 to 13
after a number between 0 to
51 is generated
3 Changing the I am expecting the index The card has been The card has been
value to 0 value from the dealt card to removed, but when the removed and is set to 0,
be changed to 0, indicating method is called again, the when the method is called
that the card is removed from cards are set back to its again in the loop, the array
the deck original values retains the 0 value when
set
4 Adding the I am expecting to show the The winning player has The results remain the
scores to a winning player of the turn been shown and the score same
player and add the score to the total is updated with the winning
player
5 Adding both I am expecting to show that Both players have tied and The results remain the
scores in both players have tied and is shown, each players same
event of a add a point to each player score has been added by 1
type
6 Finding the I am expecting a winner to be The result of the test does The results remain the
winner of the shown after 52 cards have show a winner if 1 players same
game been dealt. This includes the has more amount of cards
amount of cards both players compared to the other
have won in total
7 The game I am expecting a draw after The result of the test does The results remain the
ending in a 52 cards have been dealt. show a draw if both players same
draw This includes the amount of have the same amount of
cards have won in total cards

Conclusions based on results


Based on the results here, the program is now working with the specification and requirements that
is specified on the brief given. The cards have been initialised and is referenced at the main method
and can be changed on calls of a function, the cards are dealt randomly and is set to 0 after use, the
cards can be compared against each other (for each player), the scores are added depending on the
results, the game does end when the deck is at 0 and does show if there is a winner or draw.

The Source Code


package cardgame;
import java.util.Random; //This imports the random tool that will be used to generate random numbers for the game

public class CardGame {

public static int[] deckCard() {


int[] cards = {1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13};
return cards;
}
public static int dealCard(int[] cards) {
Random rand = new Random();
int randomCard = rand.nextInt(52); //This generates a number between 0 to 51
int drawCard = cards[randomCard]; //The number is then used to get the index value in the array of cards
if (drawCard == 0) {
do {
randomCard = rand.nextInt(52); //If the card is 0, a number is generated again while the value is still 0
drawCard = cards[randomCard]; //The value is then stored in the variable that will be given to each player as its card
} while (drawCard == 0);
}
if (drawCard!= 0) {
cards[randomCard] = 0; //If the card is not 0, the variable remains stored, but the number is set to 0 in the array
}
return drawCard; //The varible is returned and will be given to the players when the method is called upon
}

public static int[] updateScores(int playerCard, int cpuCard) { //This will recieve the players card from the main method when called
int[] scores = new int [2]; //This is to store the scores and will be added after the method is called
if (playerCard > cpuCard){
scores[0] = 2; //When the player wins, the score's first index is set to 2
System.out.println("The player's card is " + playerCard + " and the player's card is " + cpuCard);
System.out.println("Player wins the turn");
return scores;
}
if (playerCard < cpuCard){
scores[1] = 2; //When the computer wins, the score's second index is set to 2
System.out.println("The player's card is " + playerCard + " and the player's card is " + cpuCard);
System.out.println("Computer wins the turn");
return scores;
}
if (playerCard == cpuCard) {
scores[0] = 1; //When both players draw, both score's indices is set to 1
scores[1] = 1;
System.out.println("The player's card is " + playerCard + " and the player's card is " + cpuCard);
System.out.println("It's a tie turn");
return scores;
}
return scores;
}

public static void endGame(int playerScore, int cpuScore) { //After the deck is 0, the function recieves the final score and is compared
if (playerScore > cpuScore) { //And it will print the player that has won the match, or show that is a draw
System.out.println("The Player wins");
}

if (playerScore < cpuScore) {


System.out.println("The Computer wins");
}

if (playerScore == cpuScore) {
System.out.println("Both players tied");
}
}

public static void main(String[] args) {


int[] cards = deckCard(); //This is to intialise the deck at the start of the game
int playerScore = 0;
int cpuScore = 0;
int deck = 52; //This is to simulate the full deck of cards
while (deck != 0) {
System.out.println("The current score is: Player: " + playerScore + " and Computer: " + cpuScore);
deck = deck - 2; //This is to show that both cards have been dealt to each player
int playerCard = dealCard(cards); //This method is called along with cards as a reference
int cpuCard = dealCard(cards); //This gives the player it's card value
int[] addScore = updateScores(playerCard, cpuCard); //They are then passed on to this method to compare the cards
dealt for each player
playerScore = playerScore + addScore[0]; //The score depends on the result after the comparison
cpuScore = cpuScore + addScore[1]; //And the values of the scores from the function is added to the respective
player
}
System.out.println("The final score is: Player: " + playerScore + " and Computer: " + cpuScore);
endGame(playerScore, cpuScore); //In this, the method has the total score of each player and is used to show the final
score of the game
}
}

You might also like