From 2ffdf0d447433df9ae5b7331ac80156dae6adbe3 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <47217012+Saurabhsaha027@users.noreply.github.com> Date: Mon, 3 Aug 2020 01:35:20 +0530 Subject: [PATCH] Create Tic Tac Toe.py --- .../Tic Tac Toe.py | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 Tic tac Toe game with Computer and friend simple program/Tic Tac Toe.py diff --git a/Tic tac Toe game with Computer and friend simple program/Tic Tac Toe.py b/Tic tac Toe game with Computer and friend simple program/Tic Tac Toe.py new file mode 100644 index 0000000..43d9e3b --- /dev/null +++ b/Tic tac Toe game with Computer and friend simple program/Tic Tac Toe.py @@ -0,0 +1,109 @@ +import random +import time +def board(pName,pc,p): + print("\t\t\t : " + pName.upper() + " vs "+pc ) + print("\t\t\t _____________________________________") + print("\t\t\t | | | |") + print("\t\t\t | "+p[0]+ " | "+p[1]+" | "+p[2]+" |") + print("\t\t\t | | | |") + print("\t\t\t _____________________________________") + print("\t\t\t | | | |") + print("\t\t\t | "+p[3]+ " | "+p[4]+" | "+p[5]+" |") + print("\t\t\t | | | |") + print("\t\t\t _____________________________________") + print("\t\t\t | | | |") + print("\t\t\t | "+p[6]+ " | "+p[7]+" | "+p[8]+" |") + print("\t\t\t | | | |") + print("\t\t\t -------------------------------------") +def winner(p,t): + if((p[0] == t and p[1] ==t and p[2] == t) or + (p[3] == t and p[4] == t and p[5] == t) or + (p[6] == t and p[7] == t and p[8] == t) or + (p[0] == t and p[3] == t and p[6] == t) or + (p[1] == t and p[4] == t and p[7] == t) or + (p[2] == t and p[5] == t and p[8] == t) or + (p[0] == t and p[4] == t and p[8] == t) or + (p[2] == t and p[4] == t and p[6] == t)): + return True + else: + return False + + +if __name__ == '__main__': + again=1 + print('\n\n\n!!@saurabhsaha027-------------: PlAY ("_") TIC TAC TOE ("_") WITH COMPUTER/Friend :-----------!!') + print(" `````````````````````````````````````````````````````````````````````````````") + print("---------: MODE :---------\n 1 - COMPUTER :\n 2 - FRIEND:\n ") + ck = input("Enter Choice : ") + while (ck != "1" and ck != "2"): + print("------> Invalid Choice <-----") + ck = input("Enter Choice : ") + ck = ord(ck) - 48 + + pName = input("Enter Your Name :\t") + if (ck == 1): + pc = "COMPUTER" + else: + pc = input("Enter Friend's Name : ").upper() + p = ["1", "2", "3", "4", "5", "6", "7", "8", "9"] + cnt = 0 + ps = input("Choose Your Symbol (O , X) : ").upper() + while (ps != "O" and ps != "X"): + ps = input("Please choose either 'o' or 'x' :").upper() + if (ps == "X"): + cs = "O" + else: + cs = "X" + board(pName, pc, p) + while(again): + while (cnt < 9): + if (cnt % 2 == 0): + if (ck == 1): + choice = random.randint(1, 9) + else: + print(pc, "Turns ", end=" ") + choice = input(" Enter Choice :") + choice = ord(choice) - 48 + if (choice > 0 and choice < 10): + if (p[choice - 1] == cs or p[choice - 1] == ps): + print("------>Invalid Choice<-----") + continue + else: + print("------>Invalid Choice<-----") + continue + + if (p[choice - 1] != cs and p[choice - 1] != ps): + p[choice - 1] = cs + cnt += 1 + print("\t", pc, "turns processing....Wait please...??") + if (ck == 1): + time.sleep(1) + print("\t", pc, "TURNS :\t DONE: ") + board(pName, pc, p) + if (winner(p, cs)): + print(" !!!---->-->--->opps, Bad Luck --Better Luck Next Time-------!!!") + break + + else: + print(pName, "Turns ", end=" ") + choice = input("Turns Enter Choice :") + choice = ord(choice) - 48 + if (choice > 0 and choice < 10): + if (p[choice - 1] == cs or p[choice - 1] == ps): + print("------>Invalid Choice<-----") + else: + p[choice - 1] = ps + cnt += 1 + board(pName, pc, p) + if (winner(p, ps)): + print(" !!!->-->->-Congrats " + pName + ", YOU WON<--<-<- !!!-------!!!") + break; + else: + print("------>Invalid Choice<-----") + if (cnt > 8): + print(" ----->>--> No-One Wons ,<--<<-------") + print("Wanna play again: type 'yes': Otherwise click any key Except Enter") + sk=input().upper() + if(sk!="YES"): + again=0 + print("\t\t\t\t\t---->>>-->>>>--THANKU YOU--<<<<<<<<----------------")