Skip to content

Instantly share code, notes, and snippets.

@shabbir-hasan
Last active November 3, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabbir-hasan/09cd09a05bd4d7406bedaac04f96c3aa to your computer and use it in GitHub Desktop.
Save shabbir-hasan/09cd09a05bd4d7406bedaac04f96c3aa to your computer and use it in GitHub Desktop.
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
# 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:
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 = 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:
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
# 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 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 and item == {0} and player_1_data == 0:
Game_Finished = True
print("Player 1 Wins, Congratulations !!!")
elif len(item) == 1 and item == {1} and player_2_data == 1:
Game_Finished = True
print("Player 2 Wins, Congratulations !!!")
elif len(item) == 1 and item == {1} and player_1_data == 1:
Game_Finished = True
print("Player 1 Wins, Congratulations !!!")
elif len(item) == 1 and item == {0} and player_2_data == 0:
Game_Finished = True
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 game_play():
print("\n")
initiate_game()
print("\n")
game_board()
print("\n")
while not Game_Finished:
player_1_move = input("Player 1, Please Type Your Move Position >>> ")
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:
break
player_2_move = input("Player 2, Please Type Your Move Position >>> ")
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()
# return player_1_move_position, player_2_move_position
# def welcome():
_ = system('clear')
print("###############################")
print(" Welcome to Tic Tac Toe Game ")
print("###############################")
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
game_play() # Play Game
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment