Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
- [+1 Array](plus-1-array "5514e5b77e6b2f38e0000ca9")
- [Points in the circle](points-in-the-circle "5b55c49d4a317adff500015f")
- [Polybius square cipher - encode](polybius-square-cipher-encode "542a823c909c97da4500055e")
- [Pong! [Basics]](pong-basics "5b432bdf82417e3f39000195")
- [Positions Average](positions-average "59f4a0acbee84576800000af")
- [Possibilities of throwing a coin n times](possibilities-of-throwing-a-coin-n-times "5ad6266b673f2f067b000004")
- [Prime factorization](prime-factorization "534a0c100d03ad9772000539")
Expand Down
53 changes: 53 additions & 0 deletions kata/6-kyu/pong-basics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# [Pong! [Basics]](https://www.codewars.com/kata/pong-basics "https://www.codewars.com/kata/5b432bdf82417e3f39000195")

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
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
it back. There are 4 possible outcomes: player successfully hits the ball back, player misses the ball, player misses the ball **and his
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
and output description in detail below.

### "Play" method input:

* ball position - The Y coordinate of the ball
* player position - The Y coordinate of the centre(!) of the current player's paddle

### "Play" method output:

One of the following strings:

* `"Player X has hit the ball!"` - If the ball "hits" the paddle
* `"Player X has missed the ball!"` - If the ball is above/below the paddle
* `"Player X has won the game!"` - If one of the players has reached the maximum score
* `"Game Over!"` - If the game has ended when the `play` method is called

### Important notes:

* Players take turns hitting the ball, always starting the game with the Player 1.
* The paddles are `7` pixels in height.
* The ball is `1` pixel in height.

___

## Example

```
let game = new Pong(2); // Here we say that the score to win is 2
game.play(50, 53)
->
"Player 1 has hit the ball!"; // Player 1 hits the ball
game.play(100, 97)
->
"Player 2 has hit the ball!"; // Player 2 hits it back
game.play(0, 4)
->
"Player 1 has missed the ball!"; // Player 1 misses so Player 2 gains a point
game.play(25, 25)
->
"Player 2 has hit the ball!"; // Player 2 hits the ball
game.play(75, 25)
->
"Player 2 has won the game!"; // Player 1 misses again. Having 2 points Player 2 wins, so we return the corresponding string
game.play(50, 50)
->
"Game Over!"; // Another turn is made even though the game is already over
```
21 changes: 21 additions & 0 deletions kata/6-kyu/pong-basics/main/Pong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
}
}
24 changes: 24 additions & 0 deletions kata/6-kyu/pong-basics/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void player1() {
Pong game = new Pong(1);
assertEquals("Player 1 has hit the ball!", game.play(360, 363));
assertEquals("Player 1 has won the game!", game.play(575, 582));
assertEquals("Game Over!", game.play(-1, -1));
}

@Test
void player2() {
Pong game = new Pong(2);
assertEquals("Player 1 has hit the ball!", game.play(50, 53));
assertEquals("Player 2 has hit the ball!", game.play(100, 97));
assertEquals("Player 1 has missed the ball!", game.play(0, 4));
assertEquals("Player 2 has hit the ball!", game.play(25, 25));
assertEquals("Player 2 has won the game!", game.play(75, 25));
assertEquals("Game Over!", game.play(50, 50));
}
}