full forward motion and jump simulation and run_1st_path test method
This commit is contained in:
parent
8c0cfc5037
commit
1e75bdd77a
71
checkers.rb
71
checkers.rb
|
@ -11,6 +11,8 @@ BOARD_EMPTY = ' _ '
|
||||||
BOARD_SIZE = 8
|
BOARD_SIZE = 8
|
||||||
|
|
||||||
class Board
|
class Board
|
||||||
|
attr :stats, true
|
||||||
|
|
||||||
def initialize()
|
def initialize()
|
||||||
@board = Array.new(BOARD_SIZE) {|i| Array.new(BOARD_SIZE, BOARD_EMPTY)}
|
@board = Array.new(BOARD_SIZE) {|i| Array.new(BOARD_SIZE, BOARD_EMPTY)}
|
||||||
@stats = {TEAM_1 => {}, TEAM_2 => {}}
|
@stats = {TEAM_1 => {}, TEAM_2 => {}}
|
||||||
|
@ -19,9 +21,9 @@ class Board
|
||||||
def setup()
|
def setup()
|
||||||
0.step(BOARD_SIZE) {|i|
|
0.step(BOARD_SIZE) {|i|
|
||||||
peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN)
|
peice = (i < 3 ? TEAM_1_MAN : TEAM_2_MAN)
|
||||||
if [0,2,5,7].include? i
|
if [0,2,6].include? i
|
||||||
start = 1
|
start = 1
|
||||||
elsif [1,6].include? i
|
elsif [1,5,7].include? i
|
||||||
start = 0
|
start = 0
|
||||||
else
|
else
|
||||||
next
|
next
|
||||||
|
@ -57,6 +59,7 @@ class Board
|
||||||
}
|
}
|
||||||
str += "\n"
|
str += "\n"
|
||||||
}
|
}
|
||||||
|
str += 'TEAM_1: ' + @stats[TEAM_1]['count'].to_s + ' -- TEAM_2: ' + @stats[TEAM_2]['count'].to_s + "\n"
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -75,7 +78,7 @@ class Board
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_coords(x, y)
|
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
|
end
|
||||||
|
|
||||||
def gen_moves(x, y, x_mod, y_mod)
|
def gen_moves(x, y, x_mod, y_mod)
|
||||||
|
@ -108,7 +111,7 @@ class Board
|
||||||
final_x = new_x + xm
|
final_x = new_x + xm
|
||||||
final_y = new_y + ym
|
final_y = new_y + ym
|
||||||
if (valid_coords(final_x, final_y) and empty?(final_x, final_y))
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -140,6 +143,10 @@ class Board
|
||||||
return EMPTY
|
return EMPTY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def opposite_team(team)
|
||||||
|
return (team == TEAM_1 ? TEAM_2 : TEAM_1)
|
||||||
|
end
|
||||||
|
|
||||||
def man?(x, y)
|
def man?(x, y)
|
||||||
return (@board[y][x] == TEAM_1_MAN or @board[y][x] == TEAM_2_MAN)
|
return (@board[y][x] == TEAM_1_MAN or @board[y][x] == TEAM_2_MAN)
|
||||||
end
|
end
|
||||||
|
@ -171,17 +178,42 @@ class Board
|
||||||
b.set(x, y, get(x, y))
|
b.set(x, y, get(x, y))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
b.stats[TEAM_1] = @stats[TEAM_1].dup
|
||||||
|
b.stats[TEAM_2] = @stats[TEAM_2].dup
|
||||||
return b
|
return b
|
||||||
end
|
end
|
||||||
|
|
||||||
#def do_move(x, y, move, team)
|
def between(c1, c2)
|
||||||
# move.each
|
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 = []
|
boards = []
|
||||||
for x in 0..(BOARD_SIZE-1)
|
for x in 0..(BOARD_SIZE-1)
|
||||||
for y 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 = piece_valid_moves(x, y, team(x, y))
|
||||||
moves.each { |move|
|
moves.each { |move|
|
||||||
board = dup()
|
board = dup()
|
||||||
|
@ -194,9 +226,26 @@ class Board
|
||||||
return boards
|
return boards
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
b = Board.new()
|
b = Board.new()
|
||||||
|
|
Loading…
Reference in New Issue