-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tac-Toe.py
70 lines (63 loc) · 2.47 KB
/
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
import random
# Game Board
board=[0,1,2,3,4,5,6,7,8]
board_input=["","","","","O","","","",""]
def show():
print("\n",board_input[0],"|",board_input[1],"|",board_input[2],"\n ---------- \n",board_input[3],"|",board_input[4],"|",board_input[5],"\n ---------- \n",board_input[6],"|",board_input[7],"|",board_input[8])
#Displaying Inputs
print("\t ### Tic-Tac-Toe ### \n You are Playing as X \n Enter the respective number for position of X")
print("\n",board[0],"|",board[1],"|",board[2],"\n ---------- \n",board[3],"|",board[4],"|",board[5],"\n ---------- \n",board[6],"|",board[7],"|",board[8])
#Taking Input from Player
for i in range(4):
player=int(input("Play your turn-->"))
if player not in range(0,9):
print("INVALID INPUT")
elif player==4 :
print("Already Occupied")
continue
opponent=random.randrange(0,9,1) #Input from Computer
if opponent==player :
pass
elif opponent==4 :
pass
continue
board_input[player]="X"
board_input[opponent]="O"
show()
#Conditions for Winning and Losing
if board_input[0]==board_input[1]==board_input[2]=="X":
print("You Won the Game")
break
elif board_input[6]==board_input[7]==board_input[8]=="X":
print("You Won the Game")
break
elif board_input[0]==board_input[3]==board_input[6]=="X":
print("You Won the Game")
break
elif board_input[2]==board_input[5]==board_input[8]=="X":
print("You Won the Game")
break
elif board_input[0]==board_input[1]==board_input[2]=="O":
print("You Lost the Game")
break
elif board_input[6]==board_input[7]==board_input[8]=="O":
print("You Lost the Game")
break
elif board_input[0]==board_input[3]==board_input[6]=="O":
print("You Lost the Game")
break
elif board_input[2]==board_input[5]==board_input[8]=="O":
print("You Lost the Game")
break
elif board_input[0]==board_input[4]==board_input[8]=="O":
print("You Lost the Game")
break
elif board_input[2]==board_input[4]==board_input[6]=="O":
print("You Lost the Game")
break
elif board_input[1]==board_input[4]==board_input[7]=="O":
print("You Lost the Game")
break
elif board_input[3]==board_input[4]==board_input[5]=="O":
print("You Lost the Game")
break