Created
November 19, 2010 22:45
-
-
Save tom025/707342 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
board = ARGV[0] | |
def corner_available?(board) | |
[0,2,6,8].each do |num| | |
if board[num].chr.to_s == '-' | |
return num | |
end | |
end | |
end | |
$ouput_move = false | |
def return_move(move) | |
unless $output_move | |
puts move | |
end | |
$output_move = true | |
end | |
def always_get_valid(board) | |
$always_get_valid_called = true | |
counter = 0 | |
board.each_char do |char| | |
if char == '-' | |
return_move(counter) | |
break | |
end | |
counter += 1 | |
end | |
end | |
def silly_stuff(board) | |
begin | |
if board[4].chr.to_s == '-' | |
return_move 4 | |
elsif corner_available?(board) | |
return_move corner_available?(board) | |
else | |
always_get_valid(board) | |
end | |
rescue | |
if not($always_get_valid) | |
always_get_valid(board) | |
end | |
end | |
end | |
def make_move(board, position) | |
new_board = board.dup | |
new_board[position] = what_player(board) | |
new_board | |
end | |
def who_has_won(board) | |
if board.match /xxx/ or board.match /x..x..x../ or board.match /.x..x..x./ or board.match /..x..x..x/ or board.match /x...x...x/ or board.match /..x.x.x../ | |
return 'x' | |
elsif board.match /000/ or board.match /0..0..0../ or board.match /.0..0..0./ or board.match /..0..0..0/ or board.match /0...0...0/ or board.match /..0.0.0../ | |
return '0' | |
else | |
return nil | |
end | |
end | |
def generate_moves(board) | |
output = [] | |
counter = 0 | |
board.each_char do |char| | |
if char.to_s == '-' | |
output << counter | |
end | |
counter += 1 | |
end | |
return output | |
end | |
def count_xs(board) | |
board.select {|x| x == 'x'}.length | |
end | |
def count_zeroes(board) | |
board.select {|x| x == '0'}.length | |
end | |
def count_stuff(board, stuff) | |
board.select {|x| x == stuff}.length | |
end | |
def what_player(board) | |
split_board = board.split(//) | |
if count_zeroes(split_board) == count_xs(split_board) | |
return '0' | |
else | |
return 'x' | |
end | |
end | |
$other_lookup = {'x' => '0', '0' => 'x'} | |
def driver(board) | |
current_player = what_player(board) | |
$us = current_player | |
$other = $other_lookup[$us] | |
best_score = 0 | |
best_move = -1 | |
generate_moves(board).each do |move| | |
new_board = make_move(board, move) | |
score = lookahead(new_board, 1000) | |
if score > best_score | |
best_score = score | |
best_move = move | |
end | |
end | |
return best_move | |
end | |
def evaluate_end_game(board, limit) | |
winner = who_has_won(board) | |
if winner == $us | |
return 1000 * limit | |
else | |
return -1000 * limit | |
end | |
end | |
def lookahead(board, limit) | |
if who_has_won(board) == 'x' or who_has_won(board) == '0' | |
return evaluate_end_game(board, limit) | |
end | |
best_score = 0 | |
current_player = what_player(board) | |
generate_moves(board).each do |move| | |
new_board = make_move(board, move) | |
score = lookahead(new_board, limit-1) | |
if score > best_score | |
best_score = score | |
best_move = move | |
end | |
end | |
return best_score | |
end | |
begin | |
if board.match /---------/ | |
return_move 5 | |
else | |
foo = driver(board) | |
if foo != -1 | |
return_move foo | |
else | |
silly_stuff(board) | |
end | |
end | |
rescue | |
silly_stuff(board) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment