• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 18th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def print_board(p=None, c=None, s2=None):
2.     s = s2
3.     s1 = s2
4.     if p == 1:
5.         s1 = s[0:12] + c + s[13:18]
6.     elif p == 2:
7.         s1 = s[0:14] + c + s[15:18]
8.     elif p == 3:
9.         s1 = s[0:16] + c + s[17:18]
10.     elif p == 4:
11.         s1 = s[0:6] + c + s[7:18]
12.     elif p == 5:
13.         s1 = s[0:8] + c + s[9:18]
14.     elif p == 6:
15.         s1 = s[0:10] + c + s[11:18]
16.     elif p == 7:
17.         s1 = c + s[1:18]
18.     elif p == 8:
19.         s1 = s[0:2] + c + s[3:18]
20.     elif p == 9:
21.         s1 = s[0:4] + c + s[5:18]
22.     elif p is None and c is None:
23.         s1 = s
24.
25.     return s1
26.
27.
28. def is_win(board_string):
29.     # To make easy we need to assign board_string to small name srring
30.     s = board_string
31.
32.     # Checking Horizontals
33.     def hr1():
34.         if s[0] == s[2]:
35.             if s[2] == s[4]:
36.                 return True
37.             else:
38.                 return False
39.         else:
40.             return False
41.
42.     def hr2():
43.         if s[6] == s[8]:
44.             if s[8] == s[10]:
45.                 return True
46.             else:
47.                 return False
48.         else:
49.             return False
50.
51.     def hr3():
52.         if s[12] == s[14]:
53.             if s[14] == s[16]:
54.                 return True
55.             else:
56.                 return False
57.         else:
58.             return False
59.
60.     # Checking Verticals
61.     def vc1():
62.         if s[0] == s[6]:
63.             if s[6] == s[12]:
64.                 return True
65.             else:
66.                 return False
67.         else:
68.             return False
69.
70.     def vc2():
71.         if s[2] == s[8]:
72.             if s[8] == s[14]:
73.                 return True
74.             else:
75.                 return False
76.         else:
77.             return False
78.
79.     def vc3():
80.         if s[4] == s[10]:
81.             if s[10] == s[16]:
82.                 return True
83.             else:
84.                 return False
85.         else:
86.             return False
87.
88.     # Checking Diagonals
89.     def dg1():
90.         if s[0] == s[8]:
91.             if s[8] == s[16]:
92.                 return True
93.             else:
94.                 return False
95.         else:
96.             return False
97.
98.     def dg2():
99.         if s[4] == s[8]:
100.             if s[8] == s[12]:
101.                 return True
102.             else:
103.                 return False
104.         else:
105.             return False
106.
107.     if hr1():
108.         return True
109.     elif hr2():
110.         return True
111.     elif hr3():
112.         return True
113.     elif vc1():
114.         return True
115.     elif vc2():
116.         return True
117.     elif vc3():
118.         return True
119.     elif dg1():
120.         return True
121.     elif dg2():
122.         return True
123.     else:
124.         return False
125.
126.
127. def play_ttt():
128.     board_str = '7|8|9\n4|5|6\n1|2|3\n'
129.     player1list = [1, 3, 5, 7, 9]
130.     p1 = input('Which one do you want? x or o  ')
131.
132.     if p1 == 'x':
133.         p2 = 'o'
134.     elif p1 == 'o':
135.         p2 = 'x'
136.     else:
138.
139.     print('Player 1 {}\nPlayer 2 {}'.format(p1, p2))
140.     print("\nLet's Start\n")
141.     print(board_str)
142.
143.     for i in range(1, 10):
144.         if i in player1list:
145.             print('Player 1 Enter Your Position')
146.             p = int(input())
147.             board_str = print_board(p, p1, board_str)
148.             print(board_str)
149.             if i > 2:
150.                 if is_win(board_str):
151.                     print('Congratulations To Player 1 You Won')
152.                     break
153.         else:
154.             print('Player 2 Enter Your Position')
155.             p = int(input())
156.             board_str = print_board(p, p2, board_str)
157.             print(board_str)
158.             if i > 2:
159.                 if is_win(board_str):
160.                     print('Congratulations To Player 2 You Won')
161.                     break
162.
163.
164. play_ttt()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top