Professional Documents
Culture Documents
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.
Possible errors:
There are possible errors that might occur while implementing, testing and running the program that
follows the brief, this includes:
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.
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.
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 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
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("Both players tied");
}
}