I'm writing a Tic Tac Toe game and I want to switch players between '1' and '2'.
How can I set the winning condition (horizontal, vertical and diagonal wins)?
def isWinner(self):
'''
Checks whether the current player has just made a winning move. In order
to win, the player must have just completed a line (of 3 squares) with
matching marks (i.e. 3 Xs or 3 Os). That line can be horizontal, vertical,
or diagonal.
Inputs: none
Returns: True if current player has won with their most recent move;
False otherwise
'''
def switch_players():
players = [1,2]
player = players[self.turn]
self.turn = (self.turn + 1) % len(players)
switch_players()
return player
Here is my entire code.
class ClassicTicTacToe:
def __init__(self):
'''
Initializes an empty Classic Tic Tac Toe board.
Inputs: none
Returns: None
'''
self.continue_game = True
self.turn = 1
print('------------------------------\nThis is a Classic Tic Tac Toe.')
self.drawBoard()
def drawBoard(self):
'''
Displays the current state of the board, formatted with column and row
indices shown.
Inputs: none
Returns: None
'''
global local_board
display_local_board()
if self.boardFull():
self.continue_game = False
self.isWinner()
while self.continue_game:
self.pos_y_c = int(input('Player '+ player +', please enter a row: '))
self.pos_x_c = int(input('Player '+ player +', please enter a column: '))
self.update(self.pos_y_c,self.pos_x_c,'X')
def squareIsEmpty(self, row, col):
'''
Checks if a given square is "empty", or if it already contains an X or O.
Inputs:
row (int) - row index of square to check
col (int) - column index of square to check
Returns: True if square is "empty"; False otherwise
'''
global local_board
return {row, col} <= {0, 1, 2} and local_board[row+1][col+1] == ' '
def update(self, row, col, mark):
'''
Assigns the string, mark, to the board at the provided row and column,
but only if that square is "empty".
Inputs:
row (int) - row index of square to update
col (int) - column index of square to update
mark (str) - entry to place in square
Returns: True if attempted update was successful; False otherwise
'''
global local_board
if row < 0 or row > 2:
row = int(input('Error: row not in correct range. Player'+ player +', please enter a row: '))
self.update(row,col,mark)
elif col < 0 or col > 2:
col = int(input('Error: column not in correct range. Player'+ player +', please enter a column: '))
self.update(row,col,mark)
if self.squareIsEmpty(row,col):
local_board[row+1][col+1] = mark
self.drawBoard()
else:
print('Error: could not make move!')
self.drawBoard()
def boardFull(self):
'''
Checks if the board has any remaining "empty" squares.
Inputs: none
Returns: True if the board has no "empty" squares (full); False otherwise
'''
global local_board
check = True
for row in local_board:
if " " in row:
check = False
return check
def isWinner(self):
'''
Checks whether the current player has just made a winning move. In order
to win, the player must have just completed a line (of 3 squares) with
matching marks (i.e. 3 Xs or 3 Os). That line can be horizontal, vertical,
or diagonal.
Inputs: none
Returns: True if current player has won with their most recent move;
False otherwise
'''
def switch_players():
players = [1,2]
player = players[self.turn]
self.turn = (self.turn + 1) % len(players)
switch_players()
return player