Last active
November 4, 2018 13:08
-
-
Save shabbir-hasan/c7b6ff03b8f03b292f1b2f7b39f0badd to your computer and use it in GitHub Desktop.
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
# Tic-Tac-Toe Program - Trail Version 1 ('Buggy') by Shabbir Hasan | |
# Programming inspired by Sentdex YouTube Channel and follow his new | |
# Basic Tutorial series at the linkbelow | |
# https://www.youtube.com/playlist?list=PLQVvvaa0QuDeAams7fkdcwOGBpGdHpXln | |
from os import system | |
game = [[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
Game_Finished = False | |
def welcome(): | |
_ = system('clear') | |
print("###############################") | |
print(" Welcome to Tic Tac Toe Game ") | |
print("###############################") | |
#global player_1_data, player_2_data | |
player_1_data = int(input("Player 1, Please Choose '0' or '1' >>> ")) | |
if player_1_data != 0: | |
player_2_data = 0 | |
print("Player 1, is playing as '1'.\nPlayer 2, is playing as '0'.\n ") | |
else: | |
player_2_data = 1 | |
print("Player 1, is playing as '0'.\nPlayer 2, is playing as '1'.\n ") | |
return player_1_data, player_2_data | |
def initiate_game(): | |
print("#############################################################") | |
print(" Type Your Move Position as ") | |
print(" 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 ") | |
print("#############################################################") | |
def translate(move_position): | |
move_positions = {'1': (0, 0), '2': (0, 1), '3': (0, 2), | |
'4': (1, 0), '5': (1, 1), '6': (1, 2), | |
'7': (2, 0), '8': (2, 1), '9': (2, 2)} | |
return move_positions[move_position] | |
# def game_board(): | |
# print(" 0 1 2") | |
# for count, row in enumerate(game): | |
# print(count, row) | |
def game_board(): | |
for row in game: | |
print(row) | |
def game_cal(): | |
global Game_Finished | |
a, b, c, d, e, f, g, h = set(), set(), set(), set(), set(), set(), set(), set() | |
for row in range(3): | |
for column in range(3): | |
if row == column: | |
g.add(game[row][row]) # Left Diagonal | |
if row == 0: | |
a.add(game[row][column]) # Top Row | |
if column == 2: | |
h.add(game[row][column]) # Right Diagonal | |
elif row == 1: | |
b.add(game[row][column]) # Middle Row | |
if column == 1: | |
h.add(game[row][column]) # Right Diagonal | |
elif row == 2: | |
c.add(game[row][column]) # Bottom Row | |
if column == 0: | |
h.add(game[row][column]) # Right Diagonal | |
if column == 0: | |
d.add(game[row][column]) # Left Column | |
elif column == 1: | |
e.add(game[row][column]) # Middle Column | |
elif column == 2: | |
f.add(game[row][column]) # Right Column | |
result = [a, b, c, d, e, f, g, h] | |
for item in result: | |
if len(item) == 1 : | |
Game_Finished = True | |
if item == {0} : | |
if player_1_data == 0 : | |
print("Player 1 Wins, Congratulations !!! π π π") | |
else : | |
print("Player 2 Wins, Congratulations !!! π π π") | |
elif item == {1} : | |
if player_1_data == 1 : | |
print("Player 1 Wins, Congratulations !!! π π π") | |
else : | |
print("Player 2 Wins, Congratulations !!! π π π") | |
# def game_play(): | |
# player_1_move = str(input("Player 1, Please Type Your Move Position >>>")) | |
# player_1_move_position = int(player_1_move[0]), int(player_1_move[0]) | |
# player_2_move = str(input("Player 2, Please Type Your Move Position >>>")) | |
# player_2_move_position = int(player_2_move[0]), int(player_2_move[0]) | |
# return player_1_move_position, player_2_move_position | |
def replay(user_choice: str): | |
while user_choice != "": | |
if user_choice.lower() == 'y' : | |
global game, Game_Finished | |
game = [[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
Game_Finished = False | |
# moves_played[:] = [] # del moves_played[:] empty it | |
player_1_move_made, player_2_move_made = False, False | |
print("\n") | |
initiate_game() | |
print("\n") | |
game_board() | |
print("\n") | |
return player_1_move_made, player_2_move_made, [] | |
elif user_choice.lower() == 'n' : | |
print("Thank You For Playing, Goodbye! π , Exiting Game.... ") | |
raise SystemExit | |
else: | |
user_choice = input("Wrong Choice, Do you want to Replay[Y] or Exit[N] >>> ") | |
def game_play(): | |
global Game_Finished | |
moves_played = [] | |
player_1_move_made, player_2_move_made = False, False | |
print("\n") | |
initiate_game() | |
print("\n") | |
game_board() | |
print("\n") | |
while not Game_Finished : | |
if not player_1_move_made: | |
player_1_move = input("Player 1, Please Type Your Move Position >>> ") | |
if player_1_move not in moves_played: | |
player_1_move_made, player_2_move_made = True, False | |
moves_played.append(player_1_move) | |
player_1_move_position = translate(player_1_move) | |
game[player_1_move_position[0]][player_1_move_position[1]] = player_1_data | |
print("\n") | |
game_board() | |
print("\n") | |
game_cal() | |
if Game_Finished: | |
replay_choice = input("\nDo You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
# break | |
elif len(moves_played) == 9: | |
print("Nobody Won !!!, Please Replay.\n") | |
replay_choice = input("Do You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
else: | |
print("Your Opponent has already made this move, Try a diffrent move position !\n") | |
player_1_move_made, player_2_move_made = False, True | |
continue | |
elif not player_2_move_made: | |
player_2_move = input("Player 2, Please Type Your Move Position >>> ") | |
if player_2_move not in moves_played: | |
player_1_move_made, player_2_move_made = False, True | |
moves_played.append(player_2_move) | |
player_2_move_position = translate(player_2_move) | |
game[player_2_move_position[0]][player_2_move_position[1]] = player_2_data | |
print("\n") | |
game_board() | |
print("\n") | |
game_cal() | |
if Game_Finished: | |
replay_choice = input("\nDo You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
# break | |
elif len(moves_played) == 9: | |
print("Nobody Won !!!, Please Replay.\n") | |
replay_choice = input("Do You want to replay, Choose: [Y], [N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
else: | |
print("Your Opponent has already made this move, Try a diffrent move position !\n") | |
player_1_move_made, player_2_move_made = True, False | |
continue | |
# return player_1_move_position, player_2_move_position | |
player_1_data, player_2_data = welcome() | |
game_play() # Play Game |
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
# Tic-Tac-Toe Program - Trail Version 2 ('Buggy') by Shabbir Hasan | |
# Programming inspired by Sentdex YouTube Channel and follow his new | |
# Basic Tutorial series at the linkbelow | |
# https://www.youtube.com/playlist?list=PLQVvvaa0QuDeAams7fkdcwOGBpGdHpXln | |
# | |
# Version 2: Play Against AutoBot Functionality has been added | |
from os import system | |
import random | |
game = [[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
Game_Finished = False | |
Autobot_Playing = False | |
def welcome(): | |
_ = system('clear') | |
global Autobot_Playing | |
print("###############################") | |
print(" Welcome to Tic Tac Toe Game ") | |
print("###############################") | |
print("\nHow do you want to play this game ?\n[A] Play Against Human \n[B] Play Against Bot") | |
player_choice = input("\nType your choice [A] or [B] and prss enter to continue >>> ") | |
if player_choice.lower() == 'a': | |
Autobot_Playing = False | |
player_1_name = input("\nPlayer 1, Please Type Your Name >>> ").upper() | |
player_1_data = int(input("\nPlayer 1, Please Choose '0' or '1' >>> ")) | |
player_2_name = input("\nPlayer 2, Please Type Your Name >>> ").upper() | |
if player_1_data != 0: | |
player_2_data = 0 | |
print(f"\n{player_1_name} [Player 1], is playing as '1'.\n\n{player_2_name} [Player 2], is playing as '0'.\n ") | |
else: | |
player_2_data = 1 | |
print(f"\n{player_1_name} [Player 1], is playing as '0'.\n\n{player_2_name} [Player 2], is playing as '1'.\n ") | |
elif player_choice.lower() == 'b': | |
Autobot_Playing = True | |
player_1_name = input("\nPlayer 1, Please Type Your Name >>> ").upper() | |
player_1_data = int(input("\nPlayer 1, Please Choose '0' or '1' >>> ")) | |
player_2_name = '__AutoBot__' | |
if player_1_data != 0: | |
player_2_data = 0 | |
print(f"\n{player_1_name} [Player 1], is playing as '1'.\n\n{player_2_name} [Player 2], is playing as '0'.\n ") | |
else: | |
player_2_data = 1 | |
print(f"\n{player_1_name} [Player 1], is playing as '0'.\n\n{player_2_name} [Player 2], is playing as '1'.\n ") | |
return player_1_data, player_2_data, player_1_name, player_2_name | |
def initiate_game(): | |
print("\n#############################################################") | |
print(" Type Your Move Position as ") | |
print(" 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9 ") | |
print("#############################################################") | |
def translate(move_position): | |
move_positions = {'1': (0, 0), '2': (0, 1), '3': (0, 2), | |
'4': (1, 0), '5': (1, 1), '6': (1, 2), | |
'7': (2, 0), '8': (2, 1), '9': (2, 2)} | |
return move_positions[move_position] | |
def game_board(): | |
print("\n") | |
for row in game: | |
print(row) | |
print("\n") | |
def game_cal(player_1_data, player_2_data, player_1_name, player_2_name): | |
global Game_Finished | |
a, b, c, d, e, f, g, h = set(), set(), set(), set(), set(), set(), set(), set() | |
for row in range(3): | |
for column in range(3): | |
if row == column: | |
g.add(game[row][row]) # Left Diagonal | |
if row == 0: | |
a.add(game[row][column]) # Top Row | |
if column == 2: | |
h.add(game[row][column]) # Right Diagonal | |
elif row == 1: | |
b.add(game[row][column]) # Middle Row | |
if column == 1: | |
h.add(game[row][column]) # Right Diagonal | |
elif row == 2: | |
c.add(game[row][column]) # Bottom Row | |
if column == 0: | |
h.add(game[row][column]) # Right Diagonal | |
if column == 0: | |
d.add(game[row][column]) # Left Column | |
elif column == 1: | |
e.add(game[row][column]) # Middle Column | |
elif column == 2: | |
f.add(game[row][column]) # Right Column | |
result = [a, b, c, d, e, f, g, h] | |
for item in result: | |
if len(item) == 1 : | |
Game_Finished = True | |
if item == {0} : | |
if player_1_data == 0 : | |
print(f"{player_1_name} Wins, Congratulations !!! π π π") | |
else : | |
print(f"{player_2_name} Wins, Congratulations !!! π π π") | |
elif item == {1} : | |
if player_1_data == 1 : | |
print(f"{player_1_name} Wins, Congratulations !!! π π π") | |
else : | |
print(f"{player_2_name} Wins, Congratulations !!! π π π") | |
def replay(user_choice: str): | |
while user_choice != "": | |
if user_choice.lower() == 'y' : | |
global game, Game_Finished | |
game = [[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
Game_Finished = False | |
# moves_played[:] = [] # del moves_played[:] empty it | |
player_1_move_made, player_2_move_made = False, False | |
print("\n") | |
initiate_game() | |
game_board() | |
return player_1_move_made, player_2_move_made, [] | |
elif user_choice.lower() == 'n' : | |
print("\nThank You For Playing, Goodbye! π , Exiting Game.... \n") | |
raise SystemExit | |
else: | |
user_choice = input("\nWrong Choice, Do you want to Replay[Y] or Exit[N] >>> ") | |
# def findItem(theList, item): | |
# return [(ind, theList[ind].index(item)) for ind in range(len(theList)) if item in theList[ind]] | |
def findItem(theList, item): | |
location = [] | |
for ind in range(len(theList)): | |
if item in theList[ind]: | |
location.append(ind) | |
location.append(theList[ind].index(item)) | |
return location | |
def bot_play(moves_played: list): | |
x = str(moves_played[-1]) | |
possible_moves = {'1': (2,3,4,5,7,9), '2': (1,3,5,8), '3': (1,2,5,6,7,9), | |
'4': (1,5,6,7), '5': (1,2,3,4,6,7,8,9), '6': (3,4,5,9), | |
'7': (1,3,4,5,8,9), '8': (2,5,7,9), '9': (1,3,5,6,7,8)} | |
if x in possible_moves: | |
predicted_opponent_moves = possible_moves[x] | |
player_2_move = str(random.choice(predicted_opponent_moves)) | |
while player_2_move in moves_played: | |
player_2_move = str(random.choice(predicted_opponent_moves)) | |
return player_2_move | |
def game_play(): | |
global Game_Finished | |
moves_played = [] | |
player_1_move_made, player_2_move_made = False, False | |
player_1_data, player_2_data, player_1_name, player_2_name = welcome() | |
initiate_game() | |
game_board() | |
while not Game_Finished : | |
if not player_1_move_made: | |
player_1_move = input(f"\n{player_1_name}, Please Type Your Move Position >>> ") | |
if player_1_move not in moves_played: | |
player_1_move_made, player_2_move_made = True, False | |
moves_played.append(player_1_move) | |
player_1_move_position = translate(player_1_move) | |
game[player_1_move_position[0]][player_1_move_position[1]] = player_1_data | |
game_board() | |
game_cal(player_1_data, player_2_data, player_1_name, player_2_name) | |
if Game_Finished: | |
replay_choice = input("\nDo You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
# break | |
elif len(moves_played) == 9: | |
print("\nNobody Won !!!, Please Replay.\n") | |
replay_choice = input("Do You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
else: | |
print(f"\n{player_2_name} has already made this move, Try a diffrent move position !\n") | |
player_1_move_made, player_2_move_made = False, True | |
continue | |
elif not player_2_move_made: | |
if not Autobot_Playing: | |
player_2_move = input(f"\n{player_2_name}, Please Type Your Move Position >>> ") | |
if player_2_move not in moves_played: | |
player_1_move_made, player_2_move_made = False, True | |
moves_played.append(player_2_move) | |
player_2_move_position = translate(player_2_move) | |
game[player_2_move_position[0]][player_2_move_position[1]] = player_2_data | |
game_board() | |
game_cal(player_1_data, player_2_data, player_1_name, player_2_name) | |
if Game_Finished: | |
replay_choice = input("\nDo You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
# break | |
elif len(moves_played) == 9: | |
print("\nNobody Won !!!, Please Replay.\n") | |
replay_choice = input("Do You want to replay, Choose: [Y], [N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
else: | |
print(f"\n{player_1_name} has already made this move, Try a diffrent move position !\n") | |
player_1_move_made, player_2_move_made = True, False | |
continue | |
elif Autobot_Playing: | |
player_2_move = bot_play(moves_played) | |
if player_2_move not in moves_played: | |
player_1_move_made, player_2_move_made = False, True | |
moves_played.append(player_2_move) | |
print(f"\n{player_2_name}, has played on Position >>> {player_2_move}") | |
player_2_move_position = translate(player_2_move) | |
game[player_2_move_position[0]][player_2_move_position[1]] = player_2_data | |
game_board() | |
game_cal(player_1_data, player_2_data, player_1_name, player_2_name) | |
if Game_Finished: | |
replay_choice = input("\nDo You want to replay, Choose: Replay[Y] or Exit[N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
# break | |
elif len(moves_played) == 9: | |
print("\nNobody Won !!!, Please Replay.\n") | |
replay_choice = input("Do You want to replay, Choose: [Y], [N] >>> ") | |
player_1_move_made, player_2_move_made, moves_played = replay(replay_choice) | |
else: | |
print(f"\n{player_1_name} has already made this move, Try a diffrent move position !\n") | |
player_1_move_made, player_2_move_made = True, False | |
continue | |
if __name__ == "__main__": | |
game_play() # Play Game |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment