I am a new programmer, learning from youtube and other sites I can get my hands on. The assignment was clear. Create a Tic Tac Toe game using functions as much as possible and where needed.
I am posting this because I am very much interested what this community thinks of this effort and also if there would be any suggestions.
The code is a 2 player game. No AI.
This is what the code does:
- Display welcome and instructions
- Display board
- We start with player X
- While no one has won and it isn't a tie loop
- get player's move
- Update the board with the new move and check legal move, so move that hasn't been taken
- Switch Turns
- Congratulate Winner or say it's a Tie
Actual code:
def show_welcome_message():
"""Display Welcome to Tic Tac Toe """
print(
"""
Hi dear players and welcome to this Tic Tac Toe game_board.
You can win by having three 'X' or three 'O' in a row.
This can be horizontal, vertical or diagonal
"""
)
def show_game_board(game_board):
"""display the board"""
for x in range(3):
print("+---+---+---+" )
for y in range(3):
print("|", game_board[x*3+y], end = " ")
print("|")
print("+---+---+---+" )
def check_winner(game_board):
"""check if there is a win, draw"""
# check rows
for i in range(3):
if game_board[i*3] == game_board[i*3+1] == game_board[i*3+2] and game_board[i*3] in ('X', 'O'):
return game_board[i*3]
# check columns
for i in range(3):
if game_board[i] == game_board[i+3] == game_board[i+6] and game_board[i] in ('X', 'O'):
return game_board[i]
# check diagonals
if game_board[0] == game_board[4] == game_board[8] and game_board[0] in ('X', 'O'):
return game_board[0]
if game_board[2] == game_board[4] == game_board[6] and game_board[2] in ('X', 'O'):
return game_board[2]
# check tie
if all(x in ('X', 'O') for x in game_board):
return 'TIE' #lower case
# no winner or tie
return False
def get_player_move(game_board,user_piece):
""" Get the player's move """
player_move = int(input(f" Please make a move on an unoccupied square player with piece {user_piece} "))
while not is_move_legal(game_board,player_move):
player_move = int(input( " That move is not legal try again "))
return player_move
def is_move_legal(game_board,player_move):
"""Check if the move is legal"""
legal_play = True
if game_board[player_move -1] in ['X','O']:
legal_play = False
return legal_play
def update_game_board(game_board,user_piece,player_move):
""" Modify the board positions into X or O"""
game_board[player_move - 1] = user_piece
def display_game_result(user_piece):
""" display win for X or O or a tie"""
if user_piece == "TIE":
print("This is a TIE players")
else:
print(f"Yes the player with {user_piece} won !!" )
#Addition of stackoverflow
def change_player_turns(user_piece):
""" Change player's piece into X or O"""
return 'O' if user_piece == 'X' else 'X'
def main():
""" game_board starting point """
game_board = [1,2,3,4,5,6,7,8,9]
user_piece = None
show_welcome_message()
show_game_board(game_board)
user_piece = 'X'
print(f"First player starts with {user_piece}")
while not check_winner(game_board):
player_move = get_player_move(game_board,user_piece)
update_game_board(game_board,user_piece,player_move)
show_game_board(game_board)
user_piece = change_player_turns(user_piece)
display_game_result(check_winner(game_board))
if __name__ == "__main__":
main()