PongGame V6.0
On completion of this lab you will have introduced an array of Players and build simple data analysis algorithms based on the stored data. In this lab, you will work on PongGameV6.0, PongGame7.0 and PongGameV8.0.
In Version 6 of the game, we introduce a new Player class. For this version, the Player class will have limited functionality.
Open your latest version of PongGame version 5. If you want, you can use this solution here.
Save this as PongGameV6.0. Now, make the changes listed below.
We will create a Player who plays a game. This class will store details about the player:
the player's name
the number of games they wish to play for the current tournament
a collection (array) of the scores of each of the games in the current tournament
the number of scores added (games played) at this time.
We need to have getters and setters for these fields.
We need a method that adds a value to the scores array.
We also have a toString() method that returns a String version of the object. This is useful when we want to print details of a player object.
Create a new tab called Player and add the following code in your sketch (don't cut and paste it, type it in):
public class Player
{
private String playerName;
private int[] scores;
//no accessor and mutator is created for the count field as it is an internal field that
//has a dual purpose:
// 1. represents the next available index in the array
// 2. represents the number of high scores added to the array
private int count;
//Constructor to create a player with a starting name and sets the size of the array
//to the number of games in the tournament, as entered by the player.
public Player(String playerName, int numberOfGames)
{
this.playerName = playerName.trim().substring(0,6);
scores = new int[numberOfGames];
count = 0;
}
//accessors
public String getPlayerName()
{
return playerName;
}
public int[] getscores()
{
return scores;
}
//mutator for player name
public void setPlayerName(String playerName)
{
this.playerName = playerName.substring(0,6);
}
//mutator for high score array
public void setscores(int[] scores)
{
this.scores = scores;
}
//method to add a score at the next available location in the scores array
public void addScore(int score)
{
if (score >= 0){
scores[count] = score;
count++;
}
}
//method builds a nice String representation of the player name and their high scores.
//This string is then returned
public String toString()
{
String str = "Scores for " + playerName + "\n";
for(int i = 0; i < count; i++){
str = str + " Score " + (i+1) + ": " + scores[i] + "\n";
}
return str;
}
}
Ensure that you understand the code you have written in. For example:
What validation rule is imposed on the player's name?
How does the toString() work? Why do we need a loop?
What is the purpose of the count data field in the addScore() method and overall?
Firstly, we need to declare a Player in the class (just under the declaration of the Ball and Paddle objects:
Player player;
Then we need to add in the following line of code to the setup method to construct the player.
void setup()
{
:
:
//create a player object
player = new Player(" PongMaster ", maxNumberOfGames);
}
The effect of these two pieces of code is that a Player object, called player setup and ready to be used and to play.
So, when do we add the scores to the scores array? We do this when we have lost all our lives, thus finished the game. So look for the part of the code in the draw() method that deals with when the player has no more lives left in the current game. To do this, you need to add the following line of code:
player.addScore(score);
so that the full part of if..else statement is
//If the player has no lives left in the current game
else{
player.addScore(score); //add the score of the current game to the array in player
numberOfGamesPlayed++;
//If the player has more games left in the tournament,
//display their score and ask them if they want to continue with the tournament.
if (numberOfGamesPlayed < maxNumberOfGames){
resetGame();
}
Now that we have information stored about the player and their scores for the games, we will use this information at the end of the tournament. Specifically, we will print out the player's details using the toString() method using the following code, so change the tournamentOver() code to:
//method displays the player information, high scores and statistics, before exiting the program.
void tournamentOver()
{
println("Game Over!\n");
println(player.getPlayerName() + ", your tournament is over!\n"
+ "Number of games played: " + numberOfGamesPlayed
+ "\n\n" + player.toString());
exit();
}
You should be able to now play the game and get the information reported at the end of the tournament. The information will be sent to the console. Later we will redirect this information to the dialog boxes via JOptionPane.
Save your work.
Make sure you understand the code before moving onto PongGameV7_0.
If your code is not working, the solution can be found here.