|
| 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 | +``` |
0 commit comments