Last active
November 5, 2020 06:09
-
-
Save mcgain/1b685cb73947ee8d4467786ff75abfa9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is not my code, I took it from https://blog.arkency.com/10-lessons-learnt-from-the-ruby-refactoring-kata-tennis-game/. | |
| # I'd advise not clicking through to that link since it shows some refactoring. | |
| require 'minitest/autorun' | |
| class TennisGame1 | |
| def initialize(player1Name, player2Name) | |
| @player1Name = player1Name | |
| @player2Name = player2Name | |
| @p1points = 0 | |
| @p2points = 0 | |
| end | |
| def won_point(playerName) | |
| if playerName == "player1" | |
| @p1points += 1 | |
| else | |
| @p2points += 1 | |
| end | |
| end | |
| def score | |
| result = "" | |
| tempScore=0 | |
| if (@p1points==@p2points) | |
| result = { | |
| 0 => "Love-All", | |
| 1 => "Fifteen-All", | |
| 2 => "Thirty-All", | |
| }.fetch(@p1points, "Deuce") | |
| elsif (@p1points>=4 or @p2points>=4) | |
| minusResult = @p1points-@p2points | |
| if (minusResult==1) | |
| result ="Advantage player1" | |
| elsif (minusResult ==-1) | |
| result ="Advantage player2" | |
| elsif (minusResult>=2) | |
| result = "Win for player1" | |
| else | |
| result ="Win for player2" | |
| end | |
| else | |
| (1...3).each do |i| | |
| if (i==1) | |
| tempScore = @p1points | |
| else | |
| result+="-" | |
| tempScore = @p2points | |
| end | |
| result += { | |
| 0 => "Love", | |
| 1 => "Fifteen", | |
| 2 => "Thirty", | |
| 3 => "Forty", | |
| }[tempScore] | |
| end | |
| end | |
| result | |
| end | |
| end | |
| TEST_CASES = [ | |
| [0, 0, "Love-All", 'player1', 'player2'], | |
| [1, 1, "Fifteen-All", 'player1', 'player2'], | |
| [2, 2, "Thirty-All", 'player1', 'player2'], | |
| [3, 3, "Deuce", 'player1', 'player2'], | |
| [4, 4, "Deuce", 'player1', 'player2'], | |
| [1, 0, "Fifteen-Love", 'player1', 'player2'], | |
| [0, 1, "Love-Fifteen", 'player1', 'player2'], | |
| [2, 0, "Thirty-Love", 'player1', 'player2'], | |
| [0, 2, "Love-Thirty", 'player1', 'player2'], | |
| [3, 0, "Forty-Love", 'player1', 'player2'], | |
| [0, 3, "Love-Forty", 'player1', 'player2'], | |
| [4, 0, "Win for player1", 'player1', 'player2'], | |
| [0, 4, "Win for player2", 'player1', 'player2'], | |
| [2, 1, "Thirty-Fifteen", 'player1', 'player2'], | |
| [1, 2, "Fifteen-Thirty", 'player1', 'player2'], | |
| [3, 1, "Forty-Fifteen", 'player1', 'player2'], | |
| [1, 3, "Fifteen-Forty", 'player1', 'player2'], | |
| [4, 1, "Win for player1", 'player1', 'player2'], | |
| [1, 4, "Win for player2", 'player1', 'player2'], | |
| [3, 2, "Forty-Thirty", 'player1', 'player2'], | |
| [2, 3, "Thirty-Forty", 'player1', 'player2'], | |
| [4, 2, "Win for player1", 'player1', 'player2'], | |
| [2, 4, "Win for player2", 'player1', 'player2'], | |
| [4, 3, "Advantage player1", 'player1', 'player2'], | |
| [3, 4, "Advantage player2", 'player1', 'player2'], | |
| [5, 4, "Advantage player1", 'player1', 'player2'], | |
| [4, 5, "Advantage player2", 'player1', 'player2'], | |
| [15, 14, "Advantage player1", 'player1', 'player2'], | |
| [14, 15, "Advantage player2", 'player1', 'player2'], | |
| [6, 4, 'Win for player1', 'player1', 'player2'], | |
| [4, 6, 'Win for player2', 'player1', 'player2'], | |
| [16, 14, 'Win for player1', 'player1', 'player2'], | |
| [14, 16, 'Win for player2', 'player1', 'player2'], | |
| [6, 4, 'Win for player1', 'player1', 'player2'], | |
| [4, 6, 'Win for player2', 'player1', 'player2'], | |
| [6, 5, 'Advantage player1', 'player1', 'player2'], | |
| [5, 6, 'Advantage player2', 'player1', 'player2'] | |
| ] | |
| class TestTennis < Minitest::Test | |
| def play_game(tennisGameClass, p1Points, p2Points, p1Name, p2Name) | |
| game = tennisGameClass.new(p1Name, p2Name) | |
| (0..[p1Points, p2Points].max).each do |i| | |
| if i < p1Points | |
| game.won_point(p1Name) | |
| end | |
| if i < p2Points | |
| game.won_point(p2Name) | |
| end | |
| end | |
| game | |
| end | |
| def test_Score_Game1 | |
| TEST_CASES.each do |testcase| | |
| (p1Points, p2Points, score, p1Name, p2Name) = testcase | |
| game = play_game(TennisGame1, p1Points, p2Points, p1Name, p2Name) | |
| assert_equal(score, game.score()) | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment