$black, $white, $empty, $outer = 1, 2, 0, 3 $directions = [-11, -10, -9, -1, 1, 9, 10, 11] def show(board) puts " 12345678" for row in [10,20,30,40,50,60,70,80] s = "#{row}" for col in 1..8 square = board[row + col] if square==$empty s += 27.chr + "[32;42" elsif square==$white s += 27.chr + "[37;42" else s += 27.chr + "[30;42" end s += "m" s += 27.chr + "[1m"; if square==$empty s += " " else s += "+" end end puts s + 27.chr + "[0m" end puts # gets.chomp end def opponent_color(player) if player==$black return $white else return $black end end def bracket(board, player, square) opp = opponent_color(player) for d in $directions #$directions = [-11, -10, -9, -1, 1, 9, 10, 11] k = square + d # Check the eight squares around "square" #next if board[k]!=opp if board[k] == opp while board[k]==opp #Keep checking in the same direction k = k + d end if board[k]==player #If you're not on the edge k = k - d # back up one square while k != square # and go backwards and change color to "player" board[k] = player k = k - d # Keep moving backwards down the same path end end end end board[square]=player # this is the piece you just put down end def would_bracket(board, player, square) opp = opponent_color(player) for d in $directions #$directions = [-11, -10, -9, -1, 1, 9, 10, 11] k = square + d # Check each of the eight directions around "square" #next if board[k]!=opp if board[k] == opp while board[k]==opp #Keep checking in the same direction k = k + d end if board[k]==player #If you end up on your color, return true return true # This is a possible move end end end return false end def get_legal_moves(board, player) possible = [] for row in [10,20,30,40,50,60,70,80] for col in 1..8 square = row + col #next if board[square]!=$empty if board[square] == $empty if would_bracket(board, player, square) possible << square end end end end return possible end def count(board, player) total = 0 for row in [10,20,30,40,50,60,70,80] for col in 1..8 square = board[row + col] #if square == player: if square == player total = total + 1 end end end return total end def pick(board,player) return pick_rand(board,player) # make this better end def pick_rand(board,player) poss=get_legal_moves(board,player) if poss.length==0 return -1 # pass else return poss[rand(poss.length)] end end def play_one(strategies) board = [$empty] * 100 board[0...10] = [$outer] * 10 board[90...100] = [$outer] * 10 for k in [10,20,30,40,50,60,70,80] board[k + 0] = $outer board[k + 9] = $outer end board[44], board[55] = $white, $white # initial pieces on board board[45], board[54] = $black, $black show(board) player, squares, stuck = $black, 4, 0 one, two = 0, 0 while squares < 64 and stuck < 2 square = strategies[player].call(board.clone, player) #Player 1, Black, uses "pick()" Player 2, White, uses "pick_random()" possible = get_legal_moves(board, player) if square != -1 # pass if !possible.include?(square) if player == $white puts "White is disqualified for an illegal move." return $black else puts "Black is disqualified for an illegal move." return $white end end bracket(board, player, square) squares += 1 stuck = 0 elsif possible.length == 0 stuck += 1 else if player == $white puts "White is disqualified for passing illegally." return $black else puts "Black is disqualified for passing illegally." return $white end end # player = opponent_color(player) show(board) if player == 1 puts "Black moves to square #{square}" else puts "White moves to square #{square}" end b = count(board, $black) w = count(board, $white) puts "Black: #{b} White: #{w}" gets.chomp player = opponent_color(player) end w, b = count(board, $white), count(board, $black) puts "White: #{w}" puts "Black: #{b}" puts if w == b return 0 elsif w > b return $white else return $black end end def main() strategies = [nil, lambda { |x,y| pick(x,y) }, lambda { |x,y| pick_rand(x,y) }] # strategies = [nil, lambda { |x,y| pick_rand(x,y) }, lambda { |x,y| pick(x,y) }] result = play_one(strategies) if result == 0 puts "It's a tie!" elsif result == $white puts "White wins." else puts "Black wins." end puts end main()