Last active
October 4, 2023 21:57
-
-
Save DevBramz/4fb581c00f47fda7e8ccca8725153a6c to your computer and use it in GitHub Desktop.
bramztietactoe
This file contains 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
# Set up the game board as a list | |
def intro(): | |
# This function introduces the rules of the game Tic Tac Toe | |
print("Hello! Welcome to Bramz's Tic Tac Toe game!") | |
print("\n") | |
print( | |
"Rules: There will be two playes,PlayerX and PlayerO " | |
" The two players will take turns marking " | |
) | |
print("\n") | |
print( | |
"The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins.If no one wins, then the game is said to be draw." | |
) | |
print( | |
"There is no universally agreed rule as to who plays first, but for this game playerX will start." | |
) | |
print("\n") | |
input("Press enter to start game.") | |
print("\n") | |
board = ["-", "-", "-", "-", "-", "-", "-", "-", "-"] | |
# Define a function to print the game board | |
def prettify_board(): | |
print(board[0] + " | " + board[1] + " | " + board[2]) | |
print("----------") | |
print(board[3] + " | " + board[4] + " | " + board[5]) | |
print("----------") | |
print(board[6] + " | " + board[7] + " | " + board[8]) | |
print("----------") | |
def take_turn(player): | |
print("player" + player + "'s turn.") | |
position = input("Choose a position from 1-9: ") | |
if position not in [str(i) for i in range(1, 10)]: | |
print("You have entered " + " " + position + " " + "which is an invalid input") | |
position = input("Please only Choose a position from 1-9: ") | |
position = int(position) - 1 | |
while board[position] != "-": | |
position = ( | |
int(input("Position already taken. Choose a different position: ")) - 1 | |
) | |
placed_position = int(position) + 1 | |
print("placed " + " " + player + " " + "at" + " " + str(placed_position)) | |
board[position] = player | |
prettify_board() | |
# Defines a function to check if the game is over | |
def is_game_over(): | |
# Check for a win | |
if ( | |
(board[0] == board[1] == board[2] != "-") | |
or (board[3] == board[4] == board[5] != "-") | |
or (board[6] == board[7] == board[8] != "-") | |
or (board[0] == board[3] == board[6] != "-") | |
or (board[1] == board[4] == board[7] != "-") | |
or (board[2] == board[5] == board[8] != "-") | |
or (board[0] == board[4] == board[8] != "-") | |
or (board[2] == board[4] == board[6] != "-") | |
): | |
return "win" | |
# Check for a draw | |
elif "-" not in board: | |
return "draw" | |
# Game is not over | |
else: | |
return "play" | |
# Define the main game loop | |
def start_game(): | |
intro() | |
prettify_board() | |
current_player = "X" | |
game_over = False | |
while not game_over: | |
take_turn(current_player) | |
game_result = is_game_over() | |
if game_result == "win": | |
print("player" + current_player + " wins!") | |
game_over = True | |
elif game_result == "draw": | |
print("Game Over It's a draw!") | |
game_over = True | |
else: | |
current_player = "O" if current_player == "X" else "X" | |
# Start the game | |
start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment