-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.pde
149 lines (138 loc) · 4.22 KB
/
Player.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class Player //class for player object - derived from the Pong Game player class
{
private String playerName;
private int[] results;
//no getters and setters created for variables numBattles or bulletsFired as they are used only in the Player Class.
private int numBattles;
private int bulletsFired;
public Player()//default constructor
{
}
/*
overloaded constructor method taking in 2 parameters and setting default values
*/
public Player(String playerName, int numBattles)
{
this.playerName = playerName;
results = new int [numBattles];
numBattles = 0;
bulletsFired = 0;
}
//getters
public String getPlayerName()
{
return playerName;
}
public int[] getResult()
{
return results;
}
//setters
public void setPlayerName(String playerName)
{
this.playerName = playerName;
}
public void setResult(int[] results)
{
this.results = results;
}
/*
public method with no return to add scores from each battle and store them in the results array - if score is greater than or equal to 0.
also increasing variable numBattles by 1 .
*/
public void addScore(int score)
{
if (score >= 0)
{
results[numBattles] = score;
numBattles++;
}
}
/*
public method returning a string used to display the results of each battle, including the player name
battle number and score for each.
*/
public String toString()
{
String display = "You have completed all of your battles!\n\n Scores for " + playerName + "\n";
for ( int i = 0; i<numBattles; i++)
{
display = display + " Battle " + (i+1) + ": " + results[i] + "\n";
}
return display;
}
/*
public method returning an integer calculating the highest score of all battles played. This sets the score of the first battle as the highest
and then checks each subsequent battle score to see if it is higher - if it is then it replaces the starting value as the highest.
*/
public int highestScore()
{
int highestScore = results[0];
for (int i = 0; i<numBattles; i++)
{
if (results[i] > highestScore)
{
highestScore = results[i];
}
}
return highestScore;
}
/*
public method returning an integer calculating the lowest score of all battles played. This sets the score of the first battle as the lowest
and then checks each subsequent battle score to see if it is lower - if it is then it replaces the starting value as the lowest.
*/
public int lowestScore()
{
int lowestScore = results[0];
for (int i=0; i<numBattles; i++)
{
if (results[i] < lowestScore)
{
lowestScore = results[i];
}
}
return lowestScore;
}
/*
public method returning an integer calculating the average score of all battles played. This adds the results of all
battles to the total variable and then divides this number by the number of battles that have taken place resulting in an average score.
*/
public int averageScore()
{
int total = 0;
for (int i=0; i<numBattles; i++)
{
total = total + results[i];
}
return total/numBattles;
}
/*
public method returning an integer calculating the total score of all battles played. This adds the results of all
battles to the total variable and returns the value of the total variable.
*/
public int totalScore()
{
int total = 0;
for (int i=0; i<numBattles; i++)
{
total = total + results[i];
}
return total;
}
/*
public method returning an integer calculating the accuracy of player by dividing the total score by the total number of bulletsFired.
*/
public int bulletAccuracy()
{
//turning result into a float to provide % accuracy - turning it back into an int so as to provide a result without decimal places.
return (int)(((float)totalScore()/(float)bulletsFired) *100);
}
/*
public method with no return incrementing the bulletsFired variable - therefore counting the number of bullets fired.
This is used by the bulletAccuracy method above.
*/
public void bulletFired()
{
bulletsFired++;
}
}