Skip to content

Instantly share code, notes, and snippets.

@pumpkincouture
Last active August 29, 2015 14:04
Show Gist options
  • Save pumpkincouture/b283748fdd0fdce15519 to your computer and use it in GitHub Desktop.
Save pumpkincouture/b283748fdd0fdce15519 to your computer and use it in GitHub Desktop.
class Game
attr_reader :board, :computer_player, :human_player, :no_space
def initialize(board, computer_player, human_player)
@board = board
@computer_player = computer_player
@human_player = human_player
@no_space=no_space
end
@no_space=8
def game_over?
@no_space=0
end
def end_game
print "The game has ended!" if game_over?
end
end
class Board
attr_reader :computer_player, :human_player, :user_interface, :board
def initialize(computer_player, human_player, user_interface)
@computer_player= computer_player
@human_player= human_player
@ui= user_interface
@board={"1"=>"1", "2"=>"2", "3"=>"3", "4"=>"4", "5"=>"5",
"6"=>"6", "7"=>"7", "8"=>"8", "9"=>"9"}
end
def fill_space(computer_player, human_player)
if computer_player.computer_move
@board[computer_player.computer_move]="X"
puts "Computer chose space number #{computer_player.computer_move}"
else
@board[human_player.human_move]="O"
puts "Human chose space number #{human_player.human_move}"
end
@ui.display_board
end
end
class ComputerPlayer
attr_reader :computer_move, :board
def initialize(board)
@computer_move=computer_move
@board = board
end
@computer_move=[]
def make_move
move=[]
@board.each do |k, v|
move << k if @board[k]!= "X" && @board[k]!="O"
move.map!(&:to_s)
pick_one=move[-1]
@computer_move << pick_one
@no_space -= 1
end
end
end
class HumanPlayer
attr_reader :human_move, :board, :user_interface
def initialize(board, user_interface)
@human_move=human_move
@board=board
@ui = user_interface
end
@human_move=[]
def user_turn
@ui.user_prompt
answer=gets.chomp
if @board[answer] !~ /\d+/
@ui.user_error
elsif @board[answer].include? "X" || "O"
@ui.user_error
else
@human_move << answer
@no_space -= 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment