def game_won(game_board, symbol): """ (str, str) -> bool Return True if and only if the player using symbol has won the tic-tac-toe game represented by game_board. >>> game_won('XXX-O-O--', 'X') True >>> game_won('XOXOXOOXO', 'X') False """ board_size = tictactoe_functions.get_board_size(game_board) winning_string = symbol * board_size for col in range(1, board_size + 1): extract = tictactoe_functions.extract_line(game_board, 'down', col) if extract == winning_string: return True for row in range(1, board_size + 1): extract = tictactoe_functions.extract_line(game_board, 'across', row) if extract == winning_string: return True extract = tictactoe_functions.extract_line(game_board, 'down_diagonal', 1) if extract == winning_string: return True extract = tictactoe_functions.extract_line(game_board, 'up_diagonal', 1) if extract == winning_string: return True return False
def format_game_board(game_board): r""" (str) -> str Format the tic-tac-toe game_board in a nice grid format for printing. >>> format_game_board('XOX-') "\nThe X's and O's board:\n\n 1 2 \n\n1 X | O\n ---+---\n2 X | -\n" """ board_size = tictactoe_functions.get_board_size(game_board) formatted_board = '' # Format the title. formatted_board += '\nThe X\'s and O\'s board:\n\n' # Add in the column numbers. formatted_board += ' ' for col in range(1, board_size): formatted_board += (' ' + str(col) + ' ') formatted_board += (' ' + str(board_size) + ' \n\n') # Add in the row numbers, board contents and grid markers. position = 0 for row in range(1, board_size + 1): formatted_board += (str(row) + ' ') for col in range(1, board_size): formatted_board += (' ' + game_board[position] + ' |') position = position + 1 formatted_board += (' ' + game_board[position] + '\n') position = position + 1 if row < board_size: formatted_board += (' ' + '---+' * (board_size - 1) + '---\n') return formatted_board
def get_row_col(is_human_move, game_board): """ (bool, str) -> tuple of (int, int) Return an ordered pair of row and column indices that are valid for the tic-tac-toe game_board. When is_human_move is True, player is prompted for indices, otherwise they are randomly generated. (No docstring example given since the function either depends on user input or randomly generated numbers.) """ board_size = tictactoe_functions.get_board_size(game_board) if is_human_move: print('Your move. Enter row and column numbers between 1 and ' + str(board_size) + '.') row = get_valid_response('Enter row number: ', 'Your suggested row number was invalid!', 1, board_size) col = get_valid_response('Enter col number: ', 'Your suggested col number was invalid!', 1, board_size) else: row = random.randint(1, board_size) col = random.randint(1, board_size) return (row, col)
def is_occupied(row_index, col_index, game_board): """ (int, int, str) -> int Precondition: row_index and col_index are valid indices for a cell in the tic-tac-toe game_board. Return True if and only if the cell with indices row_index and col_index in the tic-tac-toe game_board does not contain the EMPTY cell character. >>> is_occupied(1, 1, 'XOX-') True >>> is_occupied(2, 2, 'XOX-') False """ board_size = tictactoe_functions.get_board_size(game_board) position = tictactoe_functions.get_position(row_index, col_index, board_size) return game_board[position] != tictactoe_functions.EMPTY
def get_row_col(is_human_move, game_board): """ (bool, str) -> tuple of (int, int) Return an ordered pair of row and column indices that are valid for the tic-tac-toe game_board. When is_human_move is True, player is prompted for indices, otherwise they are randomly generated. """ board_size = tictactoe_functions.get_board_size(game_board) if is_human_move: print('Your move. Enter row and column numbers between 1 and ' + str(board_size) + '.') row = get_valid_response('Enter row number: ', 'Your suggested row number was invalid!', 1, board_size) col = get_valid_response('Enter col number: ', 'Your suggested col number was invalid!', 1, board_size) else: row = random.randint(1, board_size) col = random.randint(1, board_size) return (row, col)
'{0}'.format(type(result)) assert result, \ 'tictactoe_functions.is_between(2, 1, 3) should return True, but ' \ 'returned {0}.'.format(result) # Type check and simple test for tictactoe_functions.game_board_full result = tictactoe_functions.game_board_full('----') assert isinstance(result, bool), \ 'tictactoe_functions.game_board_full should return a bool, but ' \ 'returned {0}.'.format(type(result)) assert not result, \ "tictactoe_functions.game_board_full('----') should return False, " \ 'but returned {0}.'.format(result) # Type check and simple test for tictactoe_functions.get_board_size result = tictactoe_functions.get_board_size('-') assert isinstance(result, int), \ 'tictactoe_functions.get_board_size should return an int, but ' \ 'returned {0} .'.format(type(result)) assert result == 1, \ "tictactoe_functions.get_board_size('-') should return 1, but "\ 'returned {0}.'.format(result) # Type check and simple test for tictactoe_functions.make_empty_board result = tictactoe_functions.make_empty_board(1) assert isinstance(result, str), \ 'tictactoe_functions.make_empty_board should return a str, but ' \ 'returned {0}.'.format(type(result)) assert result == constant_before[0], \ "tictactoe_functions.make_empty_board(1) should return '" \ + constant_before[0] + "', but returned '{0}'.".format(result)