full forward motion and jump simulation and run_1st_path test method

This commit is contained in:
Dan Ballard 2011-09-06 21:59:31 -07:00
parent 8c0cfc5037
commit 1e75bdd77a
1 changed files with 60 additions and 11 deletions

View File

@ -11,6 +11,8 @@ BOARD_EMPTY = ' _ '
BOARD_SIZE = 8
class Board
attr :stats, true
def initialize()
@board = Array.new(BOARD_SIZE) {|i| Array.new(BOARD_SIZE, BOARD_EMPTY)}
@stats = {TEAM_1 => {}, TEAM_2 => {}}
@ -19,9 +21,9 @@ class Board
def setup()
0.step(BOARD_SIZE) {|i|
peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN)
if [0,2,5,7].include? i
if [0,2,6].include? i
start = 1
elsif [1,6].include? i
elsif [1,5,7].include? i
start = 0
else
next
@ -57,6 +59,7 @@ class Board
}
str += "\n"
}
str += 'TEAM_1: ' + @stats[TEAM_1]['count'].to_s + ' -- TEAM_2: ' + @stats[TEAM_2]['count'].to_s + "\n"
return str
end
@ -75,7 +78,7 @@ class Board
end
def valid_coords(x, y)
return (x >= 0 and x < BOARD_SIZE and y >= 0 and y <= BOARD_SIZE)
return (x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE)
end
def gen_moves(x, y, x_mod, y_mod)
@ -108,7 +111,7 @@ class Board
final_x = new_x + xm
final_y = new_y + ym
if (valid_coords(final_x, final_y) and empty?(final_x, final_y))
jumps += gen_jumps(path + [[final_x, final_y]], final_x, final_y, x_mod, y_mod)
jumps += gen_jumps(path + [[final_x, final_y]], final_x, final_y, team, x_mod, y_mod)
end
end
end
@ -140,6 +143,10 @@ class Board
return EMPTY
end
def opposite_team(team)
return (team == TEAM_1 ? TEAM_2 : TEAM_1)
end
def man?(x, y)
return (@board[y][x] == TEAM_1_MAN or @board[y][x] == TEAM_2_MAN)
end
@ -171,17 +178,42 @@ class Board
b.set(x, y, get(x, y))
end
end
b.stats[TEAM_1] = @stats[TEAM_1].dup
b.stats[TEAM_2] = @stats[TEAM_2].dup
return b
end
#def do_move(x, y, move, team)
# move.each
def between(c1, c2)
return [ c2[0] + (c1[0]-c2[0])/2, c2[1] + (c1[1]-c2[1])/2 ]
end
def gen_next_move_boards()
# doesnt validate
def do_move(x, y, move, team)
puts "MOVES " + move.to_s
if move[0].is_a?(Array)
# jump
move.each { |jump|
enemy = between([x,y], jump)
set(enemy[0], enemy[1], BOARD_EMPTY)
@stats[opposite_team(team)]['count'] -= 1
piece = get(x, y)
set(x, y, BOARD_EMPTY)
set(jump[0], jump[1], piece)
}
else
# move
piece = get(x, y)
set(x, y, BOARD_EMPTY)
set(move[0], move[1], piece)
end
end
def gen_next_move_boards(team)
boards = []
for x in 0..(BOARD_SIZE-1)
for y in 0..(BOARD_SIZE-1)
if piece?(x, y)
if piece?(x, y) and team(x,y) == team
moves = piece_valid_moves(x, y, team(x, y))
moves.each { |move|
board = dup()
@ -194,9 +226,26 @@ class Board
return boards
end
def run_1st_path(team)
if team == TEAM_1
puts "TEAM_1's turn:"
else
puts "TEAM_2'2 turn:"
end
puts to_s #moves[0].to_s
if @stats[TEAM_1]['count'] == 0
puts "TEAM_2 WON!"
elsif @stats[TEAM_2]['count'] == 0
puts "TEAM_1 WON!"
else
moves = gen_next_move_boards(team)
if moves == []
puts "NO MOVES AVAILABLE?"
else
moves[0].run_1st_path(opposite_team(team))
end
end
end
end
b = Board.new()