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 alphabeta(board, player, depth, alpha, beta) | |
board.open_cells.each_key do |cell| | |
board.add_marker(player.opponent.marker, cell) | |
score = (apply_minimax(board, player.opponent, cell, depth += 1, alpha, beta) / depth.to_f) | |
alpha = player.get_alpha(alpha, score) | |
beta = player.get_beta(beta, score) | |
board.remove_marker(cell) | |
break if alpha >= beta | |
end | |
player.return_value(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
class Minimizing < Player | |
attr_accessor :marker, :turn, :opponent | |
def initialize(player) | |
@marker = player.marker | |
@turn = player.turn | |
@opponent = player.opponent | |
end | |
def get_alpha(alpha, score) | |
score > alpha ? score : alpha |
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, alpha, beta) | |
return get_score(board, player) if board.game_over? | |
if player.current_player? | |
maximizing_player = Maximizing.new(player) | |
alphabeta(board, maximizing_player, depth, alpha, beta) | |
else | |
minimizing_player = Minimizing.new(player) | |
alphabeta(board, minimizing_player, depth, alpha, beta) | |
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
Gem::Specification.new do |s| | |
s.name = 'ruby_ttt' | |
s.version = '0.0.2' | |
s.summary = "Tic-tac-toe game with computer player AI option." | |
s.description = "A tic-tac-toe game with human vs. human, human vs. computer, and computer vs. computer options. When 'hard' level is selected, the computer is unbeatable and the AI uses the minimax algorithm." | |
s.authors = ["Taryn Sauer"] | |
s.email = '[email protected]' | |
s.homepage = 'http://rubygems.org/gems/ruby_ttt' | |
s.add_development_dependency "rspec" |
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
helpers do | |
def start_game(player_one_type, player_two_type) | |
setup = new_game_setup | |
setup.set_opponents | |
setup.who_goes_first | |
setup.player_one.player_type = player_one_type | |
setup.player_two.player_type = player_two_type | |
set_sessions(setup.board, setup.player_one, setup.player_two) | |
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 WebUI < UI | |
def print_active_board | |
board_string = '' | |
board.all_rows.each do |row| | |
board_string += "<div class='row'>" | |
row.each { |cell| board_string += "<button name='move' value='#{cell}'> #{board.all_cells[cell]} <span class='cell'>.</span></button>" } | |
board_string += "</div>" | |
end | |
board_string |
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 | |
def verify_move(cell) | |
board.add_marker(current_player.marker, cell) if board.available_cell?(cell) | |
end | |
def get_game_message | |
board.empty? ? ui.first_move_message(current_player) : ui.next_move_message(current_player) | |
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
# GET ------------------------------ | |
get '/' do | |
session.clear | |
erb :index | |
end | |
get '/play' do | |
@board = session[:game].board | |
@message = @board.empty? ? session[:setup].who_goes_first : session[:game].game_status_check | |
if [email protected]_over? |
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 CLIGame < Game | |
attr_accessor :ui | |
def initialize(board, player_one, player_two) | |
super | |
@ui = CLIUI.new(board) | |
end | |
def start_cli_game! | |
begin |
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 :board, :player_one, :player_two, :ui, :player_first_move | |
def initialize(settings) | |
@board = settings[:board] | |
@player_one = settings[:player_one] | |
@player_two = settings[:player_two] | |
@player_first_move = settings[:player_first_move] | |
@ui = UI.new | |
end |