def test_violate_ko(self) : boards = [godomain.GoBoard(5,5,0) for i in range(5)] boards[0].grid = np.zeros((5,5)) boards[1].grid = np.array([ [0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]) boards[2].grid = np.array([ [0, 0, 0, 0, 0], [0, 1, 2, 0, 0], [1, 2, 0, 2, 0], [0, 1, 2, 0, 0], [0, 0, 0, 0, 0] ]) boards[3].grid = np.array([ [0, 0, 0, 0, 0], [0, 1, 2, 0, 0], [1, 0, 1, 2, 0], [0, 1, 2, 0, 0], [0, 0, 0, 0, 0] ]) boards[4].grid = np.zeros((5,5)) states = [godomain.GameState(boards[0],1,None,None)] player = godomain.Player.black for i in range(1,5) : state = godomain.GameState(boards[i],player.opp,states[i-1],None) states.append(state) move = godomain.Move(gohelper.Point(row=2,col=1)) assert states[2].violate_ko(godomain.Player.black, godomain.Move(gohelper.Point(row=4, col=1))) == False assert states[4].violate_ko(godomain.Player.black, godomain.Move(gohelper.Point(row=1, col=1))) == True assert states[3].violate_ko(godomain.Player.white,move) == True
def test_remove_dead_stones(self): board = godomain.GoBoard(5, 5, 0) board.grid= np.array([ [1, 0, 2, 0, 0], [2, 2, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]) gs = godomain.GameState(board, godomain.Player.black, None, None) move = godomain.Move(gohelper.Point(row=0, col=1)) assert gs.is_suicide(godomain.Player.black, move) == True with pytest.raises(ValueError): gs = gs.apply_move(move) gs.board.display_board()
def test_is_valid_move(self): boards = [godomain.GoBoard(5, 5, 0) for i in range(5)] boards[0].grid = np.zeros((5, 5)) boards[1].grid = np.array([ [0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]) boards[2].grid = np.array([ [0, 0, 0, 0, 0], [0, 1, 0, 0, 0], [1, 0, 1, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0] ]) boards[3].grid = np.array([ [0, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]) boards[4].grid = np.array([ [0, 2, 0, 0, 0], [2, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]) states = [godomain.GameState(boards[0], 2, None, None)] player = godomain.Player.white for i in range(1, 5): state = godomain.GameState(boards[i], player.opp, states[i - 1], None) states.append(state) player = player.opp move = godomain.Move(gohelper.Point(row=2, col=1)) assert states[1].is_valid_move(godomain.Move(gohelper.Point(row=1, col=1))) == False assert states[2].is_valid_move(godomain.Move(gohelper.Point(row=2, col=1))) == False assert states[3].is_valid_move(godomain.Move(gohelper.Point(row=0, col=0))) == True assert states[4].is_valid_move(godomain.Move(gohelper.Point(row=0, col=0))) == True assert states[4].is_valid_move(godomain.Move(gohelper.Point(row=0, col=1))) == False
def gnugov_vs_trojan(boardsize, komi, Trojanbots, black='Trojan'): ''' on 5*5 board: Calling invention: GNU A1 = (1,1), B3 = (2,3) indexed from 1 : Trojan A1 = (1,1), B3 = (3,1) # row 3 column 1 indexed from 0 ''' game = godomain.GameState.new_game(boardsize) if black is 'Trojan': gnu = GTPFacade("white", GNUGO_MONTE_CARLO) gnu.boardsize(boardsize) gnu.komi(komi) gnu.clear_board() first_pass = False while True: # First select move vertex = Trojanbots.select_move(game) # Then Apply move if its not a PASS game = game.apply_move(vertex) if not vertex.point: if first_pass: break else: first_pass = True else: first_pass = False # Change Cord of Trojan move to GNU move vertex = (vertex.point[1] + 1, boardsize - vertex.point[0]) # GNU play that move gnu.play(BLACK, vertex) gnu.showboard() #display_board(game.board) # Trojan has finish his first move # genmove applied the move it generated, so GNU finished vertex = gnu.genmove(WHITE) if vertex == PASS: if first_pass: break else: first_pass = True else: first_pass = False # Now translate the move to Trojan language vertex = godomain.Move( gohelper.Point(boardsize - vertex[1], vertex[0] - 1)) game = game.apply_move(vertex) gnu.showboard() #display_board(game.board) gnu.final_score() gnu.close() else: gnu = GTPFacade("black", GNUGO) gnu.boardsize(boardsize) gnu.komi(komi) gnu.clear_board() first_pass = False while True: vertex = gnu.genmove(BLACK) if vertex == PASS: if first_pass: break else: first_pass = True else: first_pass = False vertex = godomain.Move( gohelper.Point(boardsize - vertex[1], vertex[0] - 1)) game = game.apply_move(vertex) gnu.showboard() vertex = Trojanbots.select_move(game) if not vertex.point: if first_pass: break else: first_pass = True else: first_pass = False vertex = (vertex.point[1] + 1, boardsize - vertex.point[0]) gnu.play(WHITE, vertex) gnu.showboard() gnu.final_score() gnu.close()