-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPong.java
More file actions
21 lines (19 loc) · 592 Bytes
/
Pong.java
File metadata and controls
21 lines (19 loc) · 592 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Pong {
private final int[] scores = new int[2];
private final int maxScore;
private int player;
Pong(int maxScore) {
this.maxScore = maxScore;
}
String play(int ballPos, int playerPos) {
if (scores[0] == maxScore || scores[1] == maxScore) {
return "Game Over!";
}
player ^= 1;
boolean hit = Math.abs(ballPos - playerPos) < 4;
if (hit || ++scores[player] < maxScore) {
return String.format("Player %d has %s the ball!", 2 - player, hit ? "hit" : "missed");
}
return String.format("Player %d has won the game!", player + 1);
}
}