Created
November 8, 2021 02:39
-
-
Save dangrous/f69a855d2ac11201f4200f7a66c6ea32 to your computer and use it in GitHub Desktop.
A basic Tic Tac Toe game using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Plays tic tac toe until you want it to stop.''' | |
def print_board(board): | |
''' | |
Prints the board and a key for the moves. | |
Parameters: | |
board (list): The board of moves and empty spaces. | |
''' | |
print(f'\n {board[0]} | {board[1]} | {board[2]} 1 | 2 | 3 ') | |
print("---+---+--- ---+---+---") | |
print(f' {board[3]} | {board[4]} | {board[5]} 4 | 5 | 6 ') | |
print("---+---+--- ---+---+---") | |
print(f' {board[6]} | {board[7]} | {board[8]} 7 | 8 | 9 \n') | |
def play_game(): | |
'''Starts and manages the game.''' | |
board = [' '] * 9 | |
current_player = "X" | |
winner = False | |
while not winner: | |
print_board(board) | |
print(f"It's {current_player}'s turn...") | |
board = make_move(current_player, board) | |
if check_win(board) or check_complete(board): | |
winner = True | |
game_wrapper('Would you like to play again? (y/n) ') | |
if current_player == "X": | |
current_player = "O" | |
else: | |
current_player = "X" | |
def make_move(player, board): | |
''' | |
Requests a valid move from the player. | |
Parameters: | |
player (str): The current player of the game (X or O). | |
board (list): The current state of the game. | |
''' | |
move = None | |
while not valid_move(board,move): | |
move = input("Choose where you'd like to move. (1-9) ") | |
board[int(move)-1] = player | |
return board | |
def valid_move(board, move): | |
''' | |
Checks whether or not a player can make the specified move. | |
Parameters: | |
board (list): The current state of the game. | |
move (int): The move being attempted, represented by a number. | |
''' | |
if move is None: | |
return False | |
if not move.isdigit() or int(move) > 9 or int(move) < 1 or not board[int(move)-1] == " ": | |
print("That's not a valid move.") | |
return False | |
else: | |
return True | |
def check_win(board): | |
''' | |
Checks whether or not there is a winner yet. | |
Parameters: | |
board (list): The current state of the game. | |
''' | |
if board[0] == board[1] == board[2] and board[0] is not " ": | |
winner = board[0] | |
elif board[3] == board[4] == board[5] and board[3] is not " ": | |
winner = board[3] | |
elif board[6] == board[7] == board[8] and board[6] is not " ": | |
winner = board[6] | |
elif board[0] == board[3] == board[6] and board[0] is not " ": | |
winner = board[0] | |
elif board[1] == board[4] == board[7] and board[1] is not " ": | |
winner = board[1] | |
elif board[2] == board[5] == board[8] and board[2] is not " ": | |
winner = board[2] | |
elif board[0] == board[4] == board[8] and board[0] is not " ": | |
winner = board[0] | |
elif board[2] == board[4] == board[6] and board[2] is not " ": | |
winner = board[2] | |
else: | |
return False | |
print_board(board) | |
print("***********************") | |
print(f"* {winner} has won the game! *") | |
print("***********************") | |
return True | |
def check_complete(board): | |
''' | |
Checks whether there are any valid moves left in the game. | |
Parameters: | |
board (list): The current state of the game. | |
''' | |
for box in board: | |
if box is " ": | |
return False | |
else: | |
print_board(board) | |
print("There are no more moves! The game ends in a tie.") | |
return True | |
def game_wrapper(question): | |
''' | |
Asks the user if they want to start a game. | |
Parameters: | |
question (str): The specific question to ask the user. | |
''' | |
answer = input(question) | |
if answer == "y": | |
play_game() | |
else: | |
exit | |
game_wrapper('Do you want to play a game (y/n)? ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment