def post(self, request): serializer = NewGameSerializer(data=request.data) if serializer.is_valid(): new_game = Sgf_game(size=serializer.data.get('board_size')) game = Game.objects.create( player_w_id=serializer.data.get('player_w_id'), player_b_id=serializer.data.get('player_b_id'), sgf_file=new_game.serialise()) return Response({'game_id': game.pk}, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def set_game_sgf(new_move, game, player_color): sgf_string = str(game.sgf_file) game_sgf = Sgf_game.from_string(sgf_string) new_node = game_sgf.extend_main_sequence() new_node.set_move(player_color, new_move) game.sgf_file = game_sgf.serialise() return game_sgf
def is_game_over(sgf): """ If two pass moves in a row, then game is over """ g = Sgf_game.from_string(sgf) # check last three moves because first move is intialized as (None, None) last_three_moves = [node.get_move() for node in g.get_main_sequence()][-3:] # game has to have at least two moves total if len(last_three_moves) < 3: return False elif last_three_moves[0][1] is None and last_three_moves[1][1] is None: return True return False