Skip to content

Commit f4f3214

Browse files
* docs: kata description * feat: kata/pong-basics * test: cover player 1 win case --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent f297cf8 commit f4f3214

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

kata/6-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
- [+1 Array](plus-1-array "5514e5b77e6b2f38e0000ca9")
272272
- [Points in the circle](points-in-the-circle "5b55c49d4a317adff500015f")
273273
- [Polybius square cipher - encode](polybius-square-cipher-encode "542a823c909c97da4500055e")
274+
- [Pong! [Basics]](pong-basics "5b432bdf82417e3f39000195")
274275
- [Positions Average](positions-average "59f4a0acbee84576800000af")
275276
- [Possibilities of throwing a coin n times](possibilities-of-throwing-a-coin-n-times "5ad6266b673f2f067b000004")
276277
- [Prime factorization](prime-factorization "534a0c100d03ad9772000539")

kata/6-kyu/pong-basics/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [Pong! [Basics]](https://www.codewars.com/kata/pong-basics "https://www.codewars.com/kata/5b432bdf82417e3f39000195")
2+
3+
You must finish the `Pong` class. It has a constructor which accepts the `maximum score` a player can get throughout the game, and a method
4+
called `play`. This method determines whether the current player hit the ball or not, i.e. if the paddle is at the sufficient height to hit
5+
it back. There are 4 possible outcomes: player successfully hits the ball back, player misses the ball, player misses the ball **and his
6+
opponent reaches the maximum score winning the game**, either player tries to hit a ball despite the game being over. You can see the input
7+
and output description in detail below.
8+
9+
### "Play" method input:
10+
11+
* ball position - The Y coordinate of the ball
12+
* player position - The Y coordinate of the centre(!) of the current player's paddle
13+
14+
### "Play" method output:
15+
16+
One of the following strings:
17+
18+
* `"Player X has hit the ball!"` - If the ball "hits" the paddle
19+
* `"Player X has missed the ball!"` - If the ball is above/below the paddle
20+
* `"Player X has won the game!"` - If one of the players has reached the maximum score
21+
* `"Game Over!"` - If the game has ended when the `play` method is called
22+
23+
### Important notes:
24+
25+
* Players take turns hitting the ball, always starting the game with the Player 1.
26+
* The paddles are `7` pixels in height.
27+
* The ball is `1` pixel in height.
28+
29+
___
30+
31+
## Example
32+
33+
```
34+
let game = new Pong(2); // Here we say that the score to win is 2
35+
game.play(50, 53)
36+
->
37+
"Player 1 has hit the ball!"; // Player 1 hits the ball
38+
game.play(100, 97)
39+
->
40+
"Player 2 has hit the ball!"; // Player 2 hits it back
41+
game.play(0, 4)
42+
->
43+
"Player 1 has missed the ball!"; // Player 1 misses so Player 2 gains a point
44+
game.play(25, 25)
45+
->
46+
"Player 2 has hit the ball!"; // Player 2 hits the ball
47+
game.play(75, 25)
48+
->
49+
"Player 2 has won the game!"; // Player 1 misses again. Having 2 points Player 2 wins, so we return the corresponding string
50+
game.play(50, 50)
51+
->
52+
"Game Over!"; // Another turn is made even though the game is already over
53+
```

kata/6-kyu/pong-basics/main/Pong.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Pong {
2+
private final int[] scores = new int[2];
3+
private final int maxScore;
4+
private int player;
5+
6+
Pong(int maxScore) {
7+
this.maxScore = maxScore;
8+
}
9+
10+
String play(int ballPos, int playerPos) {
11+
if (scores[0] == maxScore || scores[1] == maxScore) {
12+
return "Game Over!";
13+
}
14+
player ^= 1;
15+
boolean hit = Math.abs(ballPos - playerPos) < 4;
16+
if (hit || ++scores[player] < maxScore) {
17+
return String.format("Player %d has %s the ball!", 2 - player, hit ? "hit" : "missed");
18+
}
19+
return String.format("Player %d has won the game!", player + 1);
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class SolutionTest {
6+
@Test
7+
void player1() {
8+
Pong game = new Pong(1);
9+
assertEquals("Player 1 has hit the ball!", game.play(360, 363));
10+
assertEquals("Player 1 has won the game!", game.play(575, 582));
11+
assertEquals("Game Over!", game.play(-1, -1));
12+
}
13+
14+
@Test
15+
void player2() {
16+
Pong game = new Pong(2);
17+
assertEquals("Player 1 has hit the ball!", game.play(50, 53));
18+
assertEquals("Player 2 has hit the ball!", game.play(100, 97));
19+
assertEquals("Player 1 has missed the ball!", game.play(0, 4));
20+
assertEquals("Player 2 has hit the ball!", game.play(25, 25));
21+
assertEquals("Player 2 has won the game!", game.play(75, 25));
22+
assertEquals("Game Over!", game.play(50, 50));
23+
}
24+
}

0 commit comments

Comments
 (0)