Created
July 25, 2014 02:10
-
-
Save pumpkincouture/4dd245a7dd4bba34c8cd 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
class Board | |
attr_reader :board, :no_error | |
def initialize | |
@board={"1"=>"1", "2"=>"2", "3"=>"3", "4"=>"4", "5"=>"5", | |
"6"=>"6", "7"=>"7", "8"=>"8", "9"=>"9"} | |
end | |
def human_move(answer, board, ui, new_game) | |
if board.board[answer] !~ /\d+/ | |
ui.user_error | |
@no_error=false | |
elsif board.board[answer].include? "X" || "O" | |
ui.user_error | |
@no_error=false | |
else | |
@no_error=true | |
board.board[answer]="O" | |
ui.choice | |
self.display_board | |
new_game.decrease_space | |
end | |
end | |
def computer_move(answer, new_game) | |
@board[answer]="X" | |
puts "Computer chose space number #{answer}" | |
self.display_board | |
new_game.decrease_space | |
end | |
def display_board | |
puts "#{@board["1"]} | #{@board["2"]} | #{@board["3"]}" | |
puts "---------" | |
puts "#{@board["4"]} | #{@board["5"]} | #{@board["6"]}" | |
puts "---------" | |
puts "#{@board["7"]} | #{@board["8"]} | #{@board["9"]}" | |
end | |
end | |
class ComputerPlayer | |
attr_reader :computer_move | |
def initialize | |
@computer_move=computer_move | |
end | |
def computer_turn(board) | |
move=[] | |
board.board.each do |k, v| | |
move << k if board.board[k]!= "X" && board.board[k]!="O" | |
end | |
move.map!(&:to_s) | |
@computer_move=move[-1] | |
end | |
end | |
class HumanPlayer | |
attr_reader :human_move | |
def initialize | |
@human_move=human_move | |
end | |
def user_turn(ui) | |
ui.user_prompt | |
@human_move=gets.chomp | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment