Created
May 4, 2012 04:51
-
-
Save thegrubbsian/2592113 to your computer and use it in GitHub Desktop.
Tic-Tac-Toe
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
1.9.2-p290 |
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
class ComputerPlayer | |
end |
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
source "http://rubygems.org" | |
gem "rspec" |
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
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 |
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
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 |
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
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