This file contains 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
def play! | |
who_goes_first | |
until game_over | |
board.next_move_message(current_player) | |
board.display_board | |
if current_player.player_type == "human" | |
user_input = gets.chomp | |
move = clean_up_input(user_input) | |
if board.available_cell?(move) | |
board.add_marker(move, current_player.marker) |
This file contains 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
def check_move(cell) | |
if board.available_cell?(cell) | |
advance_game(cell, current_player) | |
elsif board.valid_cell?(cell) | |
board.taken_cell_message(cell) | |
else | |
board.bad_cell_message(cell) | |
end | |
end |
This file contains 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
def apply_minimax(board, player, cell, depth) | |
return get_score(board, player) if board.game_over?(player) | |
if player.turn == 1 | |
min_alphabeta(board, player, depth, alpha=-999, beta=999) | |
else | |
max_alphabeta(board, player, depth, alpha=-999, beta=999) | |
end | |
end | |
def min_alphabeta(board, player, depth, alpha, beta) |
This file contains 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
def create_board_hash | |
new_board = Hash.new | |
alpha = 'A' | |
numeric = 1 | |
num_of_rows.times do | |
num_of_rows.times do | |
key = numeric.to_s + alpha | |
numeric += 1 | |
new_board[key] = nil | |
end |
This file contains 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 Game | |
attr_accessor :player_one, :player_two, :board, :game_over, :difficulty_level, :ai, :ui | |
def initialize(board) | |
@board = board | |
@ui = UI.new(@board) | |
@player_one = Player.new('X', get_player_type('X'), @board) | |
@player_two = Player.new('O', get_player_type('Y'), @board) | |
@player_one.opponent = @player_two | |
@player_two.opponent = @player_one | |
@game_over = false |
This file contains 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
def get_move_score(board, player, cell) | |
board.add_marker(cell, player.marker) | |
best_score = apply_minimax(board, player, cell, depth=0, NEG_INF, POS_INF) | |
board.remove_marker(cell) | |
best_score | |
end | |
def apply_minimax(board, player, cell, depth, alpha, beta) | |
return get_score(board, player) if board.game_over?(player) | |
if player.turn == 1 |
This file contains 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
describe 'UI' do | |
context 'player move messages' do | |
before :each do | |
board = double('board') | |
@ui = UI.new(board) | |
@player = double('player', :marker => 'X' ) | |
end | |
describe '#first_move_message' do |
This file contains 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
### DIP -- gets winner regardless of board size or specified winning lines | |
def winner?(board) | |
board_markers = board.all_cells.select { |k,v| v == self.marker }.keys | |
board.winning_lines.each do |line| | |
return true if (line & board_markers).length == board.num_of_rows | |
end | |
false | |
end |
This file contains 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 Game | |
attr_accessor :player_one, :player_two, :board, :game_over, :difficulty_level, :ai, :ui | |
def initialize(board) | |
@board = board | |
@ui = UI.new(@board) | |
@player_one = Player.new('X', get_player_type('X'), @board) | |
@player_two = Player.new('O', get_player_type('Y'), @board) | |
@player_one.opponent = @player_two | |
@player_two.opponent = @player_one | |
@game_over = false |
This file contains 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 Game | |
attr_accessor :player_one, :player_two, :board, :game_over, :difficulty_level, :ai, :ui | |
def initialize(board, player_one, player_two, difficulty_level) | |
@board = board | |
@ui = UI.new(@board) | |
@player_one = player_one | |
@player_two = player_two | |
@game_over = false | |
@ai = AI.new | |
@difficulty_level = difficulty_level |