Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Last active August 29, 2015 13:59
Show Gist options
  • Save davidpaulhunt/10645911 to your computer and use it in GitHub Desktop.
Save davidpaulhunt/10645911 to your computer and use it in GitHub Desktop.
require 'rspec'
class Match
attr_reader :players, :winner
def initialize
@players = [TennisPlayer.new] * 2
end
def winner
@players.each do |player|
if player.score == 3
@winner = player
end
end
@winner
end
def start_game(player)
skill = rand(0..1)
if skill == 0
player.miss
else
player.hit
end
end
def officiate_the_match
@players.each do |player|
if player.missed == false
player.score_a_point
winner
end
end
end
def play_the_match
until winner
@players.each do |player|
start_game(player)
end
officiate_the_match
end
end
end
class TennisPlayer
attr_accessor :missed, :score, :skill
def initialize
@missed = false
@score = 0
end
def hit
@missed = false
end
def miss
@missed = true
end
def score_a_point
@score += 1
end
def compete
@skill = rand(0..1)
end
end
describe Match do
let(:match) { Match.new }
it 'should have two players' do
match.players.count.should eq(2)
end
it 'should not have a winner until started' do
match.winner.should eq(nil)
end
it 'should have a winner after starting' do
match.play_the_match
match.winner.should_not eq(nil)
end
end
describe TennisPlayer do
let(:player) { TennisPlayer.new }
it 'should hit the ball' do
player.missed = true
player.hit
player.missed.should eq(false)
end
it 'should miss the ball' do
player.miss
player.missed.should eq(true)
end
it 'score should start at zero' do
player.score.should eq(0)
end
end
@davidpaulhunt
Copy link
Author

I'm not happy with this, the AI and functionality with choosing a winner is too flawed. I'm sorry, it's weak.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment