I have a while
loop below, but I'm not quite sure if it's the most 'pythonic' thing. I've omitted most of the code, and have the skeletal structure below (but have all the code necessary to understand my game_status()
function). And the main thing in question is whether/how to use a function as a condition in a while
statement. (In my case, the game_status()
function).
#Legal board positions
BOARD_POSITIONS = list(range(1, 10))
#All possible combinations of board positions giving 8 winning lines.
WINNING_LINES = [[1, 2, 3], [4, 5, 6], [7, 8, 9],
[1, 4, 7], [2, 5, 8], [3, 6, 9],
[1, 5, 9], [3, 5, 7]]
def game_status(board):
"""Updates the status of the game based on three outcomes:
Returning 'X' or 'O' is either has three in a row; 'tie' if the
board is full, but no players have won; or 'unfinished' otherwise"""
for i, j, k in WINNING_LINES:
if not available(board, i) and board[i] == board[j] == board[k]:
return board[i]
if any(available(board, i) for i in BOARD_POSITIONS):
return 'unfinished'
return 'tie'
while True:
game_board = (' ' for i in range(10))
while game_status(game_board) is 'unfinished':
#Main game code
else:
if game_status(game_board) is 'X':
#Code for X winning goes here
elif game_status(game_board) is 'O':
#Code for O winning goes here
else:
#Code for a tie goes here
As you notice, I've got to call game_status()
multiple times in the else
part of my while
statement in order to check the value of game_status()
so that I know to print who won or if the game was a tie.
One idea I had is the following:
else:
status = game_status(game_board)
if status = 'X':
#Code
elif status = 'O':
#Code
and so on. However, I am quite curious as to the most pythonic way to do things, as well as whether or not using a function as a conditional is pythonic.
is
checks work. As it happens, CPython (and presumably other Python implementations) will use the same object for equal string literals in different places, but I don't believe the language guarantees that this will always be the case stackoverflow.com/questions/17679861/does-python-intern-strings \$\endgroup\$ – Steve Jessop Nov 9 '13 at 1:31