Skip to content

Commit c35f239

Browse files
Merge pull request geekcomputers#413 from rohitbabugaddeti/rohit
modified code for readability and security
2 parents b102658 + 11e49f5 commit c35f239

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

tik_tak.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
1+
#Tik-tak game
12

2-
l=["anything",1,2,3,4,5,6,7,8,9]
3-
i=0
3+
4+
board=["anything",1,2,3,4,5,6,7,8,9]
5+
switch="p1"
46
j=9
57
print("\n\t\t\tTIK-TAC-TOE")
6-
def board():
8+
def print_board():
79
#import os
810
#os.system('cls')
911
print("\n\n")
1012
print(" | |" )
11-
print("",l[1]," | ",l[2]," | ",l[3] )
13+
print("",board[1]," | ",board[2]," | ",board[3] )
1214
print("____|_____|____")
1315
print(" | |" )
14-
print("",l[4]," | ",l[5]," | ",l[6] )
16+
print("",board[4]," | ",board[5]," | ",board[6] )
1517
print("____|_____|____")
1618
print(" | |" )
17-
print("",l[7]," | ",l[8]," | ",l[9] )
19+
print("",board[7]," | ",board[8]," | ",board[9] )
1820
print(" | |" )
19-
def enter_number(p1,p2):
20-
global i
21+
def enter_number(p1_sign,p2_sign):
22+
global switch
2123
global j
2224
k=9
2325
while(j):
2426
if k==0:
2527
break
2628

27-
if i==0:
28-
x=int(input("\nplayer 1 :- "))
29-
if x<=0:
29+
if switch=="p1":
30+
p1_input=int(input("\nplayer 1 :- "))
31+
if p1_input<=0:
3032
print("chose number from given board")
3133
else:
3234
for e in range(1,10):
33-
if l[e]==x:
34-
l[e]=p1
35-
board()
35+
if board[e]==p1_input:
36+
board[e]=p1_sign
37+
print_board()
3638
c=checkwin()
3739
if c==1:
3840
print("\n\n Congratulation ! player 1 win ")
3941
return
4042

4143

42-
i=1
44+
switch="p2"
4345
j-=1
4446
k-=1
4547
if k==0:
@@ -50,59 +52,60 @@ def enter_number(p1,p2):
5052

5153
break
5254

53-
if i==1:
54-
y=int(input("\nplayer 2 :- "))
55-
if y<=0:
55+
if switch=="p2":
56+
p2_input=int(input("\nplayer 2 :- "))
57+
if p2_input<=0:
5658
print("chose number from given board")
5759
#return
5860
else:
5961
for e in range(1,10):
60-
if l[e]==y:
61-
l[e]=p2
62-
board()
62+
if board[e]==p2_input:
63+
board[e]=p2_sign
64+
print_board()
6365
w=checkwin()
6466
if w==1:
6567
print("\n\n Congratulation ! player 2 win")
6668
return
6769

68-
i=0
70+
switch="p1"
6971
j-=1
7072
k-=1
7173

7274

7375
def checkwin():
74-
if l[1]==l[2]==l[3]:
76+
if board[1]==board[2]==board[3]:
7577

7678
return 1
77-
elif l[4]==l[5]==l[6]:
79+
elif board[4]==board[5]==board[6]:
7880

7981
return 1
80-
elif l[7]==l[8]==l[9]:
82+
elif board[7]==board[8]==board[9]:
8183

8284
return 1
83-
elif l[1]==l[4]==l[7]:
85+
elif board[1]==board[4]==board[7]:
8486

8587
return 1
8688

87-
elif l[2]==l[5]==l[8]:
89+
elif board[2]==board[5]==board[8]:
8890

8991
return 1
90-
elif l[3]==l[6]==l[9]:
92+
elif board[3]==board[6]==board[9]:
9193

9294
return 1
93-
elif l[1]==l[5]==l[9]:
95+
elif board[1]==board[5]==board[9]:
9496

9597
return 1
96-
elif l[3]==l[5]==l[7]:
98+
elif board[3]==board[5]==board[7]:
9799

98100
return 1
99101
else:
100102
print("\n\nGame continue")
101103

102-
def main():
103-
board()
104-
p1=input("\n\nplayer 1 chose your sign [0/x] = ")
105-
p2=input("player 2 chose your sign [0/x] = ")
106-
enter_number(p1,p2)
104+
def play():
105+
print_board()
106+
p1_sign=input("\n\nplayer 1 chose your sign [0/x] = ")
107+
p2_sign=input("player 2 chose your sign [0/x] = ")
108+
enter_number(p1_sign,p2_sign)
107109
print("\n\n\t\t\tDeveloped By :- UTKARSH MATHUR")
108-
main()
110+
if __name__=="__main__":
111+
play()

0 commit comments

Comments
 (0)