• API
• FAQ
• Tools
• Archive
daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import random
2.
3. player_won = 0
4. computer_won = 0
5. draws = 0
6. who_begins = 2
7. whose_turn = 2
8. all_pos = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
9. player_choices = []
10. computer_choices = []
11. edges = [(0, 0), (0, 2), (2, 0), (2, 2)]
12. left_edges = [(0, 0), (2, 0)]
13. right_edges = [(0, 2), (2, 2)]
14. non_edges = [(0, 1), (1, 0), (1, 2), (2, 1)]
15. print("Player begins and uses <X>, Computer uses <O>.")
16.
17.
18. def print_board(b):
19.     s = ""
20.     for i in range(4):
21.         s += "\n --- --- ---\n"
22.         if i < 3:
23.             for j in range(3):
24.                 if b[i][j] == 1:
25.                     s += "| X "
26.                 elif b[i][j] == 2:
27.                     s += "| O "
28.                 else:
29.                     s += "|   "
30.             s += "|"
31.     print(s)
32.
33.
34. def who_won(b):
35.     mid = b[1][1]
36.     if mid != 0:
37.         if (b[0][0] == mid == b[2][2]) or (b[2][0] == mid == b[0][2]):
38.             return mid
39.     for i in range(3):
40.         elem = b[i][i]
41.         if elem != 0:
42.             if (b[i][(i-1) % 3] == elem == b[i][(i+1) % 3]) or (b[(i-1) % 3][i] == elem == b[(i+1) % 3][i]):
43.                 return elem
44.     return 0
45.
46.
47. def player_move(b):
48.     global all_pos
49.     global player_choices
50.     x, y = [(int(z.strip())-1) for z in input("Player, your turn: ").split(",")]
51.     while (x, y) not in all_pos:
52.         x,  y = [(int(z.strip())-1) for z in input("Wrong input, try again: ").split(",")]
53.     b[x][y] = 1
54.     all_pos.remove((x, y))
55.     player_choices.append((x, y))
56.
57.
58. def is_neighbor(x, y):
59.     i, j = x
60.     k, l = y
61.     if i == k:
62.         return abs(j-l) == 1
63.     elif j == l:
64.         return abs(i-k) == 1
65.
66.
67. def same_row(x, y):
68.     i, j = x
69.     k, l = y
70.     return i == k
71.
72.
73. def same_column(x, y):
74.     i, j = x
75.     k, l = y
76.     return j == l
77.
78.
79. def computer_starter(b, free):
80.     global edges
81.     global right_edges
82.     global left_edges
83.     global non_edges
84.     global all_pos
85.     global player_choices
86.     global computer_choices
87.     x = y = -1
89.     if free == 9:
90.         x, y = random.choice(edges + [(1, 1)])
91.     elif free == 7:
92.         if computer_choices[0] == (1, 1):
93.             if player_choices[0] in non_edges:
94.                 i, j = player_choices[0]
95.                 if j == 0:
96.                     x, y = (0, 2)
97.                 elif j == 2:
98.                     x, y = (0, 0)
99.                 else:
100.                     x, y = random.choice(edges)
101.             else:
102.                 x, y = random.choice(all_pos)
103.         else:
104.             if player_choices[0] == (1, 1):
105.                 i, j = computer_choices[0]
106.                 x, y = (2-i, 2-j)
107.             else:
108.                 comp_tup = computer_choices[0]
109.                 player_tup = player_choices[0]
110.                 i, j = computer_choices[0]
111.                 if is_neighbor(comp_tup, player_tup):
112.                     x, y = (1, 1)
113.                 elif same_row(comp_tup, player_tup) or same_column(comp_tup, player_tup):
114.                     x, y = (2-i, 2-j)
115.                 else:
116.                     x, y = (1, 1)
117.     elif free == 5:
118.         choice = search_choice(b)
119.         if choice:
120.             x, y = choice
121.         else:
122.             if computer_choices[0] == (1, 1):
123.                 i, j = computer_choices[1]
124.                 x, y = (2-i, j)
125.             else:
126.                 if is_neighbor(computer_choices[0], player_choices[0]):
127.                     i, j = player_choices[1]
128.                     if same_row(computer_choices[0], player_choices[0]):
129.                         x, y = (i, 2-j)
130.                     elif same_column(computer_choices[0], player_choices[0]):
131.                         x, y = (2-i, j)
132.                 else:
133.                     x, y = random.choice(all_pos)
134.     elif free == 3:
135.         choice = search_choice(b)
136.         if choice:
137.             x, y = choice
138.         else:
139.             x, y = random.choice(all_pos)
140.     elif free == 1:
141.         x, y = all_pos[0]
142.     b[x][y] = 2
143.     all_pos.remove((x, y))
144.     computer_choices.append((x, y))
145.
146.
147. def player_starter(b, free):
148.     return 0
149.
150.
151. def search_choice(b):
152.     choice = []
153.     mid = b[1][1]
154.     if mid != 0:
155.         for i in range(3):
156.             for j in range(3):
157.                 if b[i][j] == mid and b[2-i][2-j] == 0:
158.                     pos = [2-i, 2-j]
159.                     if mid == 2:
160.                         print("a")
161.                         return pos
162.                     else:
163.                         choice = pos
164.     if 0 != b[0][0] == b[2][2]:
165.         if b[0][0] == 2:
166.             return [1, 1]
167.         else:
168.             choice = [1, 1]
169.     elif 0 != b[0][2] == b[2][0]:
170.         if b[0][2] == 2:
171.             return [1, 1]
172.         else:
173.             choice = [1, 1]
174.     for i in range(3):
175.         row = b[i]
176.         if row.count(2) == 2 and row.count(0) == 1:
177.             return [i, row.index(0)]
178.         elif row.count(1) == 2 and row.count(0) == 1:
179.             choice = [i, row.index(0)]
180.     for j in range(3):
181.         column = []
182.         for i in range(3):
183.             column.append(b[i][j])
184.         if column.count(2) == 2 and column.count(0) == 1:
185.             return [column.index(0), j]
186.         elif column.count(1) == 2 and column.count(0) == 1:
187.             choice = [column.index(0), j]
188.     return choice
189.
190.
191. def play_game():
192.     b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
193.     global all_pos
194.     all_pos = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
195.     global player_choices
196.     player_choices = []
197.     global computer_choices
198.     computer_choices = []
199.     free_fields = 9
200.     winner = 0
201.     global who_begins
202.
203.     while winner == 0 and free_fields > 0:
204.         print_board(b)
205.         global whose_turn
206.         if whose_turn == 1:
207.             whose_turn = 2
208.             player_move(b)
209.         else:
210.             whose_turn = 1
211.             if who_begins == 2:
212.                 computer_starter(b, free_fields)
213.             else:
214.                 player_starter(b, free_fields)
215.         free_fields -= 1
216.         winner = who_won(b)
217.     print_board(b)
218.     if winner == 1:
219.         print("The Game is over. The Player won.")
220.         global player_won
221.         player_won += 1
222.     elif winner == 2:
223.         print("The Game is over. The Computer won.")
224.         global computer_won
225.         computer_won += 1
226.     else:
227.         print("The Game is over. Nobody won.")
228.         global draws
229.         draws += 1
230.     s = input("Do you want to play again? Press yes!").upper()
231.     if s == "Y" or s == "YE" or s == "YES" or s == "J" or s == "JA" or s == "1":
232.         if who_begins == 1:
233.             who_begins = 2
234.         else:
235.             who_begins = 1
236.         play_game()
237.     else:
238.         print("The Result: %d %s -- %d %s -- %d %s"
239.               % (player_won, "Win" if player_won == 1 else "Wins", computer_won,
240.                  "Loss" if computer_won == 1 else "Losses", draws, "Draw" if draws == 1 else "Draws"))
241.
242.
243. play_game()
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