Last active
November 4, 2023 08:39
-
-
Save SharafatKarim/23d251c3abce5a0525d244cb148c31dc to your computer and use it in GitHub Desktop.
Tick-tack-toe game with python. Supports singleplayer (easy mode for now) and multiplayer!
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
# Global variables | |
one = "1" | |
two = "2" | |
three = "3" | |
four = "4" | |
five = "5" | |
six = "6" | |
seven = "7" | |
eight = "8" | |
nine = "9" | |
# players var | |
player_one = "X" | |
player_two = "O" | |
# winners list | |
Winner = [] | |
# resetting choice every single time | |
# a game is started | |
def choice_reset(): | |
global one, two, three, four, five, six, seven, eight, nine | |
one = "1" | |
two = "2" | |
three = "3" | |
four = "4" | |
five = "5" | |
six = "6" | |
seven = "7" | |
eight = "8" | |
nine = "9" | |
# drawing boards auto using global vars | |
def draw_board(): | |
global one, two, three, four, five, six, seven, eight, nine | |
print(f" {one} | {two} | {three} ") | |
print(f"---|---|---") | |
print(f" {four} | {five} | {six} ") | |
print(f"---|---|---") | |
print(f" {seven} | {eight} | {nine} ") | |
# anouncements | |
def play_again(): | |
print("") | |
print("Do you want to play again? (y/n)") | |
if input("=> ") == "y": | |
mode_selection() | |
def anounce_draw(): | |
print("It's a draw!") | |
play_again() | |
def anounce_winner(player): | |
print(f"The winner is player {player}") | |
play_again() | |
# working with choices | |
def choice_availability(choice): | |
global one, two, three, four, five, six, seven, eight, nine | |
if choice == "1" and one == "1": | |
return True | |
elif choice == "2" and two == "2": | |
return True | |
elif choice == "3" and three == "3": | |
return True | |
elif choice == "4" and four == "4": | |
return True | |
elif choice == "5" and five == "5": | |
return True | |
elif choice == "6" and six == "6": | |
return True | |
elif choice == "7" and seven == "7": | |
return True | |
elif choice == "8" and eight == "8": | |
return True | |
elif choice == "9" and nine == "9": | |
return True | |
return False | |
def choice_selection(choice, player): | |
global one, two, three, four, five, six, seven, eight, nine | |
if choice == "1" and one == "1": | |
one = player | |
return True | |
elif choice == "2" and two == "2": | |
two = player | |
return True | |
elif choice == "3" and three == "3": | |
three = player | |
return True | |
elif choice == "4" and four == "4": | |
four = player | |
return True | |
elif choice == "5" and five == "5": | |
five = player | |
return True | |
elif choice == "6" and six == "6": | |
six = player | |
return True | |
elif choice == "7" and seven == "7": | |
seven = player | |
return True | |
elif choice == "8" and eight == "8": | |
eight = player | |
return True | |
elif choice == "9" and nine == "9": | |
nine = player | |
return True | |
return False | |
def board_check(player): | |
global one, two, three, four, five, six, seven, eight, nine | |
if (one == player and two == player and three == player): | |
Winner.append(player) | |
return True | |
elif (four == player and five == player and six == player): | |
Winner.append(player) | |
return True | |
elif (seven == player and eight == player and nine == player): | |
Winner.append(player) | |
return True | |
elif (one == player and four == player and seven == player): | |
Winner.append(player) | |
return True | |
elif (two == player and five == player and eight == player): | |
Winner.append(player) | |
return True | |
elif (three == player and six == player and nine == player): | |
Winner.append(player) | |
return True | |
elif (one == player and five == player and nine == player): | |
Winner.append(player) | |
return True | |
elif (three == player and five == player and seven == player): | |
Winner.append(player) | |
return True | |
elif one != "1" and two != "2" and three != "3" and four != "4" and five != "5" and six != "6" and seven != "7" and eight != "8" and nine != "9": | |
Winner.append("draw") | |
return True | |
return False | |
# for everyone | |
def user_input(): | |
global one, two, three, four, five, six, seven, eight, nine | |
while True: | |
Inp = input("=> ") | |
if choice_availability(Inp): | |
break | |
print("Invalid choice!") | |
return Inp | |
def multiplayer_game(): | |
global one, two, three, four, five, six, seven, eight, nine | |
choice_reset() | |
while True: | |
draw_board() | |
print(f"Player {player_one}'s turn") | |
print("Select an available block (0-9)") | |
choice_selection(user_input(), player_one) | |
if board_check(player_one): | |
break | |
print("") | |
draw_board() | |
print(f"Player {player_two}'s turn") | |
print("Select an available block (0-9)") | |
choice_selection(user_input(), player_two) | |
if board_check(player_two): | |
break | |
print("") | |
print("") | |
print("Last preview,") | |
draw_board() | |
print("") | |
if Winner[-1] == "draw": | |
anounce_draw() | |
else: | |
anounce_winner(Winner[-1]) | |
# for multiplayer - easy mode | |
try: | |
from random import randint | |
except: | |
raise Exception( | |
"Please make sure latest version of python 3 (3.10.8 or above) is installed!") | |
def easy_bot_play(): | |
while True: | |
first_choice = randint(1, 9) | |
if choice_availability(str(first_choice)): | |
break | |
print(f"System choosed {first_choice}") | |
return str(first_choice) | |
def singleplayer_easy_game_first_player(): | |
global one, two, three, four, five, six, seven, eight, nine | |
choice_reset() | |
while True: | |
draw_board() | |
print(f"Player {player_one}'s turn") | |
print("Select an available block (0-9)") | |
choice_selection(user_input(), player_one) | |
if board_check(player_one): | |
break | |
print("") | |
choice_selection(easy_bot_play(), player_two) | |
if board_check(player_two): | |
break | |
print("") | |
print("") | |
print("Last preview,") | |
draw_board() | |
print("") | |
if Winner[-1] == "draw": | |
anounce_draw() | |
else: | |
anounce_winner(Winner[-1]) | |
def singleplayer_easy_game_second_player(): | |
global one, two, three, four, five, six, seven, eight, nine | |
choice_reset() | |
while True: | |
choice_selection(easy_bot_play(), player_one) | |
if board_check(player_one): | |
break | |
print("") | |
draw_board() | |
print(f"Player {player_two}'s turn") | |
print("Select an available block (0-9)") | |
choice_selection(user_input(), player_two) | |
if board_check(player_two): | |
break | |
print("") | |
print("") | |
print("Last preview,") | |
draw_board() | |
print("") | |
if Winner[-1] == "draw": | |
anounce_draw() | |
else: | |
anounce_winner(Winner[-1]) | |
# multiplayer_game() | |
# singleplayer_easy_game_first_player() | |
def singleplayer_easy(): | |
print("Enter your character: ") | |
print("1) First character (X)") | |
print("2) Second character (O)") | |
print("0) Main menu") | |
match input("=> "): | |
case "0": | |
print() | |
mode_selection() | |
case "1": | |
print() | |
singleplayer_easy_game_first_player() | |
case "2": | |
print() | |
singleplayer_easy_game_second_player() | |
def mode_selection(): | |
print("Enter your game mode: ") | |
print("1) Single player (easy)") | |
print("2) Multi player") | |
print("3) Statistics") | |
print("0) Exit") | |
print("Your choice, ") | |
match input("=> "): | |
case "0": | |
print("Exiting...") | |
exit() | |
case "1": | |
print() | |
singleplayer_easy() | |
case "2": | |
print() | |
multiplayer_game() | |
case "3": | |
print() | |
print("Stats") | |
print(Winner) | |
print() | |
mode_selection() | |
mode_selection() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment