forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tac-Toe_human_vs_human.py
99 lines (99 loc) · 3.61 KB
/
Tic-Tac-Toe_human_vs_human.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
author: shreyansh kushwaha
made on : 08.09.2020
"""
board = {'1' : ' ','2' : ' ','3' : ' ','4' : ' ','5' : ' ','6' : ' ','7' : ' ','8' : ' ','9' : ' '}
import random, os, check, time
p1 = input("Enter your name player 1 (symbol X):\n")
p2 = input("Enter your name player 2 (symbol O):\n")
score1, score2, score_tie = [], [], []
total_moves =0
player = random.randint(1, 2)
time.sleep(2.6)
os.system("cls")
if player == 1:
print(f" {p1} won the toss.")
else:
print(f" {p2} won the toss")
time.sleep(3)
print(" Let us begin")
time.sleep(2)
def toggaleplayer(player):
if player == 1:
player = 2
elif player == 2:
player = 1
def playagain():
inp = input("Do you want to play again??(Y/N)\n")
if inp.upper() == "Y":
a = toggaleplayer(player)
restart(a)
elif inp.upper() == "N":
os.system("cls")
print("Thanks for playing")
print(f"Number of times {p1} won : {len(score1)}.")
print(f"Number of times {p2} won : {len(score2)}.")
print(f"Number of ties: {len(score_tie)}.")
abc = input()
quit()
else:
print("Invalid input")
quit()
def restart(a):
total_moves, board =0, {'1' : ' ','2' : ' ','3' : ' ','4' : ' ','5' : ' ','6' : ' ','7' : ' ','8' : ' ','9' : ' '}
while True:
os.system("cls")
print(board['1'] + '|' + board['2'] + '|' + board['3'] )
print('-+-+-')
print(board['4'] + '|' + board['5'] + '|' + board['6'] )
print('-+-+-')
print(board['7'] + '|' + board['8'] + '|' + board['9'] )
check.check(total_moves,score1, score2, score_tie, playagain, board, p1, p2)
while True:
if a == 1:
p1_input = input(f"Its {p1}'s chance..\nwhere do you want to place your move:")
if p1_input.upper() in board and board[p1_input.upper()] == " ":
board[p1_input.upper()] = 'X'
a = 2
break
else: # on wrong input
print("Invalid input \n Enter again. ")
continue
else:
p2_input = input(f"Its {p2}'s chance..\nwhere do you want to place your move:")
if p2_input.upper() in board and board[p2_input.upper()] == " ":
board[p2_input.upper()] = 'O'
a = 1
break
else:
print("Invalid Input")
continue
total_moves += 1
while True:
os.system("cls")
print(board['1'] + '|' + board['2'] + '|' + board['3'] )
print('-+-+-')
print(board['4'] + '|' + board['5'] + '|' + board['6'] )
print('-+-+-')
print(board['7'] + '|' + board['8'] + '|' + board['9'] )
check.check(total_moves,score1, score2, score_tie, playagain, board, p1, p2)
while True:
if player == 1:
p1_input = input(f"Its {p1}'s chance..\nwhere do you want to place your move:")
if p1_input.upper() in board and board[p1_input.upper()] == " ":
board[p1_input.upper()] = 'X'
player = 2
break
else: # on wrong input
print("Invalid input ")
continue
else:
p2_input = input(f"Its {p2}'s chance..\nwhere do you want to place your move:")
if p2_input.upper() in board and board[p2_input.upper()] == " ":
board[p2_input.upper()] = 'O'
player = 1
break
else:
print("Invalid Input")
continue
total_moves += 1