Skip to content

Instantly share code, notes, and snippets.

@thegrubbsian
Created May 4, 2012 04:51
Show Gist options
  • Save thegrubbsian/2592113 to your computer and use it in GitHub Desktop.
Save thegrubbsian/2592113 to your computer and use it in GitHub Desktop.
Tic-Tac-Toe
class ComputerPlayer
end
source "http://rubygems.org"
gem "rspec"
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
rspec-mocks (~> 2.10.0)
rspec-core (2.10.0)
rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.10.0)
PLATFORMS
ruby
DEPENDENCIES
rspec
class TicTacToe
class << self
def choose_first_player
rand(2) == 0 ? "X" : "O"
end
end
attr_accessor :plays
def initialize
@plays = { "X" => [], "O" => [] }
end
def move(player, position)
raise "Invalid move." if @plays.values.flatten.include?(position)
@plays[player] << position
end
def generate_board
output = " 0 | 1 | 2 \n-----------\n 3 | 4 | 5 \n-----------\n 6 | 7 | 8 "
@plays.each do |k,v|
v.each { |pos| output.gsub!("#{pos}", k) }
end
output.gsub!(/\d/, " ")
end
def winner?(player)
[[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]].each do |position|
return true if (position - @plays[player]) == []
end
return false
end
end
require_relative "../tic_tac_toe"
describe TicTacToe do
describe ".move" do
it "raises an exception if the move is illegal"
it "sets the move for the player if the move is legal"
end
describe ".output_board" do
it "outputs the state of the board when empty"
it "outputs the state of the board when moves have been made"
end
describe ".winner?" do
it "returns true if the player has won"
it "returns false if the player has not won"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment