Compare commits
3 Commits
gen-search
...
master
Author | SHA1 | Date |
---|---|---|
Dan Ballard | 280a38eb54 | |
Dan Ballard | 5b1d69ebfe | |
Dan Ballard | e383c27a8d |
8
README
8
README
|
@ -1,13 +1,17 @@
|
|||
Checkers AI engine
|
||||
|
||||
For Ruby 1.9.1
|
||||
|
||||
Right now I'm working on the search.
|
||||
|
||||
To test
|
||||
|
||||
% load "checkers.rb"
|
||||
% b = Board.new
|
||||
% b.setup
|
||||
% b.play
|
||||
|
||||
and then to look
|
||||
also look at
|
||||
|
||||
% b.search(TEAM_1)
|
||||
|
||||
Right now entering jump moves or the AI playing jumps in the .play() interface may not work
|
||||
|
|
93
checkers.rb
93
checkers.rb
|
@ -59,7 +59,7 @@ class Board
|
|||
}
|
||||
str += "\n"
|
||||
}
|
||||
str += 'TEAM_1: ' + @stats[TEAM_1]['count'].to_s + ' -- TEAM_2: ' + @stats[TEAM_2]['count'].to_s + "\n"
|
||||
str += 'White: ' + @stats[TEAM_1]['count'].to_s + ' -- Black: ' + @stats[TEAM_2]['count'].to_s + "\n"
|
||||
return str
|
||||
end
|
||||
|
||||
|
@ -300,5 +300,96 @@ class Board
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_coords(str)
|
||||
if str.length != 2
|
||||
return false
|
||||
end
|
||||
|
||||
y = str.downcase[0]
|
||||
x = str[1]
|
||||
|
||||
if x >= '0' and x <= '7'
|
||||
x = x.ord - '0'.ord
|
||||
else
|
||||
return false
|
||||
end
|
||||
|
||||
if y >= 'a' and y <= 'h'
|
||||
y = y.ord - 'a'.ord
|
||||
else
|
||||
return false
|
||||
end
|
||||
return [x,y]
|
||||
end
|
||||
|
||||
# todo: deal with loss when no move avail
|
||||
def play()
|
||||
setup()
|
||||
color = ''
|
||||
while color != 'w' and color != 'b'
|
||||
print "Choose color ([W]hite or [B]lack): "
|
||||
color = gets
|
||||
color = color.downcase[0]
|
||||
end
|
||||
|
||||
turn = 1
|
||||
team = TEAM_1
|
||||
while true
|
||||
print "Turn " + turn.to_s + ": "
|
||||
if team == TEAM_1
|
||||
print "white "
|
||||
if color == 'w'
|
||||
print "(player)"
|
||||
else
|
||||
print "(ai)"
|
||||
end
|
||||
else
|
||||
print "black "
|
||||
if color == 'b'
|
||||
print '(player)'
|
||||
else
|
||||
print '(ai)'
|
||||
end
|
||||
end
|
||||
print "\n"
|
||||
puts to_s()
|
||||
|
||||
if (team == TEAM_1 and color == 'w') or (team == TEAM_2 and color == 'b')
|
||||
valid = false
|
||||
while !valid
|
||||
print "Move piece: "
|
||||
from = gets().strip!
|
||||
print "to: "
|
||||
to = gets().strip!
|
||||
from = parse_coords(from)
|
||||
to = parse_coords(to)
|
||||
puts to.to_s + " or " + from.to_s
|
||||
if !to or !from
|
||||
next
|
||||
end
|
||||
if team(from[0], from[1]) != team
|
||||
next
|
||||
end
|
||||
if !piece_valid_moves(from[0], from[1], team).include?(to)
|
||||
next
|
||||
end
|
||||
valid = true
|
||||
end
|
||||
do_move(from[0], from[1], to, team)
|
||||
else
|
||||
puts "AI MOVE"
|
||||
end
|
||||
|
||||
if @stats[TEAM_1]['count'] == 0
|
||||
puts "Black wins!"
|
||||
elsif @stats[TEAM_2]['count'] == 0
|
||||
puts "White wins!"
|
||||
end
|
||||
team = opposite_team(team)
|
||||
turn += 1
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue