-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anti Tic-Tac-Toe.py
134 lines (118 loc) · 4.32 KB
/
Anti Tic-Tac-Toe.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
print(' *-*-*-RULES-*-*-* ')
print()
print('Introducing NEW ANTI TIC-TAC-TOE -(note the keyword *ANTI*)-')
print('Now play NOT to WIN but to LOSE.')
print('The rules are simple:)')
print('All you need to do is select a spot from the given number grid.')
print('The grid is numbered from 0-8, from which you can choose a spot- by selecting a number.')
print('You will be play against the computer and the first one to get 3 in a row LOSES.')
print('And you are x & get to play first, whereas the computer is o.')
print()
import random
#The Game board
board = [0,1,2,
3,4,5,
6,7,8]
def show():
print( board[0],'|',board[1],'|',board[2])
print('---------')
print( board[3],'|',board[4],'|',board[5])
print('---------')
print( board[6],'|',board[7],'|',board[8])
def checkline(char,spot1,spot2,spot3):
if board[spot1] == char and board[spot2] == char and board[spot3]==char:
return(True)
def checkall(char):
if checkline(char, 0, 1, 2):
return(True)
if checkline(char, 3, 4, 5):
return(True)
if checkline(char, 6, 7, 8):
return(True)
if checkline(char, 0, 3, 6):
return(True)
if checkline(char, 1, 4, 7):
return(True)
if checkline(char, 2, 5, 8):
return(True)
if checkline(char, 2, 4, 6):
return(True)
if checkline(char, 0, 4, 8):
return(True)
def resetBoard(char) :
# declaring the Game board variable as global before resetting the board
global board
print() # print newline before the board display
show() # shows the board before resetting it
board = [0, 1, 2,
3, 4, 5,
6, 7, 8]
if char == 'x' :
print()
print (':( Computer Won :(')
elif char == 'o' :
print()
print (':) You Won :)')
else :
print()
print ('!!! Draw !!!')
print()
print ('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-')
print (' :) New Game Board :) ')
print ('*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-')
num = 0 # global varibale to track the number of times user has drawn (Max 5)
while True:
# Before starting the new game, if previous game resulted
# in (Won, Lost, Draw)
if checkall('x') == True :
num = 0
resetBoard('x')
continue
if checkall('o') == True :
num = 0
resetBoard('o')
continue
# Declare as Draw as 5th iteration of the user1 is already done
# and no one Won:(
if num == 5 :
num = 0
resetBoard('')
continue
if num == 0 :
# to take inputs from the user to continue or quit the Game
Play = (input('Do you Wanna Play, (Y/N) '))
if Play == 'Y' or Play == 'y':
show() # to show the Game board at the start of the game
elif Play == 'N' or Play == 'n':
break
else :
continue
s = int(input('Select your spot : '))
if s > 8 or s < 0:
print ('Invalid input :(')
continue
num +=1 # increment the number of moves from the user
if board[s]!= 'x' and board[s]!= 'o':
board[s] = 'x' # puts the user1 input as 'x' on the place with index's'
if checkall('x') == True:
num = 0
resetBoard('x')
continue
if num < 5 :
while True:
random.seed() #Generates a random number
opponent = random.randint(0,8)
if board[opponent]!= 'x' and board[opponent]!= 'o':
board[opponent] = 'o'
if checkall('o') == True:
num = 0
resetBoard('o')
break
else:
print('This spot is already taken') # error from the user
# It was incremented earlier now decrement it to keep it logical
num -= 1
if num != 0 and num < 5:
# show the board after every move played from computer
# for all except last move
show()