-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
63 lines (55 loc) · 1.93 KB
/
TicTacToe.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
lists = [' ']*10
# lists[0] = '#'
def board():
print(lists[1]+ '|' + lists[2] + '|' + lists[3])
print('---------')
print(lists[4] + '|' + lists[5] + '|' + lists[6])
print('----------')
print(lists[7]+'|' +lists[8] + ' |' + lists[9])
def win(mark):
return ((lists[1] == lists[2] == lists[3] == mark) or (lists[4] == lists[5] == lists[6] == mark)
or (lists[7] == lists[8] == lists[9] == mark) or (lists[1] == lists[5] == lists[9]== mark)or (lists[3] == lists[5] == lists[7] == mark))
def blank(position):
return lists[position] == ' '
def game():
choose = input("enter ur choice 'X' or 'O' ").upper()
turn = "first"
while(1):
if(' ' not in lists and (turn == "first" or turn =="second")):
print('Draw')
break
elif (turn == "first"):
print("player 1 turn ")
position = int(input("enter ur postiion "))
if(blank(position)):
lists[position] = 'X'
board()
else:
print("Give correct position")
position = int(input("enter ur position "))
if(win('X')):
print("player 1 wins")
break
else:
turn = "second"
elif (turn == "second"):
print("player 2 turn")
position = int(input("enter ur position "))
if(blank(position)):
lists[position] = "O"
board()
else:
print("Give correct position")
position = int(input("enter ur position "))
if(win('O')):
print("player 2 wins")
break
else:
turn = "first"
game()
lists = [' ']*10
replay = input("Replay 'Y' of 'N'").upper()
if(replay == 'Y'):
game()
else:
print("thank you")