def test_valid_black_move_bishop(self): board_string_sample_black_bishop = " QQQ QbQ QQQ " valid_black_bishops_moves_sample = [[3, 1, 4, 2, 10], [3, 1, 2, 2, 10], [3, 1, 4, 0, 10], [3, 1, 2, 0, 10]] get_board_sample_9 = get_board(board_string_sample_black_bishop) result = valid_black_move(get_board_sample_9) self.assertEqual(result, valid_black_bishops_moves_sample)
def test_valid_black_move_queen(self): board_string_sample_black_queen = " QQQ QqQ QQQ " valid_black_queen_moves_sample = [[3, 1, 4, 1, 5], [3, 1, 3, 0, 5], [3, 1, 3, 2, 5], [3, 1, 2, 1, 5], [3, 1, 4, 2, 5], [3, 1, 2, 2, 5], [3, 1, 4, 0, 5], [3, 1, 2, 0, 5]] get_board_sample_7 = get_board(board_string_sample_black_queen) result = valid_black_move(get_board_sample_7) self.assertEqual(result, valid_black_queen_moves_sample)
def test_valid_black_move_pawn(self): board_string_sample_black_pawns = " p Q Q p p " valid_black_pawns_moves_sample = [[6, 6, 7, 6, 4], [6, 6, 7, 6, 1], [4, 7, 5, 7, 4], [4, 7, 5, 7, 1], [3, 1, 4, 2, 10], [3, 1, 4, 0, 10], [3, 1, 5, 1, 3], [3, 1, 4, 1, 1]] get_board_sample_6 = get_board(board_string_sample_black_pawns) result = valid_black_move(get_board_sample_6) self.assertEqual(result, valid_black_pawns_moves_sample)
def test_valid_black_move_king(self): board_string_sample_black_kings = " QQQ QkQ QQQ k " valid_black_kings_moves_sample = [[8, 9, 9, 9, 1], [8, 9, 8, 8, 1], [8, 9, 8, 10, 1], [8, 9, 7, 9, 2], [8, 9, 9, 10, 1], [8, 9, 7, 10, 1], [8, 9, 9, 8, 1], [8, 9, 7, 8, 1], [3, 1, 4, 1, 10], [3, 1, 3, 0, 10], [3, 1, 3, 2, 10], [3, 1, 2, 1, 10], [3, 1, 4, 2, 10], [3, 1, 2, 2, 10], [3, 1, 4, 0, 10], [3, 1, 2, 0, 10]] get_board_sample_10 = get_board(board_string_sample_black_kings) result = valid_black_move(get_board_sample_10) self.assertEqual(result, valid_black_kings_moves_sample)
async def play(websocket): while True: try: response = await websocket.recv() print(f"< {response}") data = json.loads(response) if data['event'] == 'update_user_list': pass if data['event'] == 'gameover': pass if data['event'] == 'ask_challenge': if data['data']['username'] == 'dinok': await send( websocket, 'accept_challenge', { 'board_id': data['data']['board_id'], }, ) if data['event'] == 'your_turn': #import ipdb;ipdb.set_trace() board = data['data']['board'] b = get_board(board) player_color = data['data']['actual_turn'] x = valid_white_move(b) y = valid_black_move(b) j = best_move_white(x) k = best_move_white(y) print('MY COLOR :', data['data']['actual_turn']) print('MOVES LEFT :', data['data']['move_left']) print('OPONENT :', data['data']['opponent_username']) #print('WHITE',x) #print('BLACK',y) if player_color == 'white': #import ipdb;ipdb.set_trace() await send( websocket, 'move', { 'board_id': data['data']['board_id'], 'turn_token': data['data']['turn_token'], 'from_row': j[0][0], 'from_col': j[0][1], 'to_row': j[0][2], 'to_col': j[0][3], }, ) else: await send( websocket, 'move', { 'board_id': data['data']['board_id'], 'turn_token': data['data']['turn_token'], 'from_row': k[0][0], 'from_col': k[0][1], 'to_row': k[0][2], 'to_col': k[0][3], }, ) except Exception as e: print('error {}'.format(str(e))) break # force login again