# Untitled

Jul 31st, 2021
742
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from classes import player
2. from classes import bcolors
3.
4. print('''
5.    1 | 2 | 3
6.    ---------
7.    4 | 5 | 6
8.    ---------
9.    7 | 8 | 9
10.
11. Give these values, as input where
12. you want to mark your territory.
13. ''')
14.
15. grid = [
16.     [0, 0, 0],
17.     [0, 0, 0],
18.     [0, 0, 0]
19. ]
20.
21.
22. def print_grid():
23.     print('\n-------------')
24.     for x in grid:
25.         print("| ", end='')
26.         for y in x:
27.             if y == 0:
28.                 print('- | ', end='')
29.             else:
30.                 print(str(y) + ' | ', end='')
31.         print('\n-------------')
32.
33.
34. print_grid()
35. name1 = input('Player 1: ')
36. name2 = input('Player 2: ')
37. player1 = player(name1, 'X')
38. player2 = player(name2, 'O')
39.
40. players = [player1, player2]
41.
42. # control the flow of the game
43. running = True
44.
45.
46. def take_input():
47.     chance = int(input(player.get_name() +
48.                        ', please specify where you want to mark: '))
49.     print(player.get_name()+' has chosen input as ' + str(chance))
50.
51.     if chance == 1:
52.         if grid[0][0] != 0:
53.             print(bcolors.FAIL + bcolors.BOLD +
54.                   '\nSorry, already taken!\n'+bcolors.ENDC)
55.             take_input()
56.         else:
57.             grid[0][0] = player.get_symbol()
58.
59.     elif chance == 2:
60.         if grid[0][1] != 0:
61.             print(bcolors.FAIL + bcolors.BOLD +
62.                   '\nSorry, already taken!\n'+bcolors.ENDC)
63.             take_input()
64.         else:
65.             grid[0][1] = player.get_symbol()
66.
67.     elif chance == 3:
68.         if grid[0][2] != 0:
69.             print(bcolors.FAIL + bcolors.BOLD +
70.                   '\nSorry, already taken!\n'+bcolors.ENDC)
71.             take_input()
72.         else:
73.             grid[0][2] = player.get_symbol()
74.
75.     elif chance == 4:
76.         if grid[1][0] != 0:
77.             print(bcolors.FAIL + bcolors.BOLD +
78.                   '\nSorry, already taken!\n'+bcolors.ENDC)
79.             take_input()
80.         else:
81.             grid[1][0] = player.get_symbol()
82.
83.     elif chance == 5:
84.         if grid[1][1] != 0:
85.             print(bcolors.FAIL + bcolors.BOLD +
86.                   '\nSorry, already taken!\n'+bcolors.ENDC)
87.             take_input()
88.         else:
89.             grid[1][1] = player.get_symbol()
90.
91.     elif chance == 6:
92.         if grid[1][2] != 0:
93.             print(bcolors.FAIL + bcolors.BOLD +
94.                   '\nSorry, already taken!\n'+bcolors.ENDC)
95.             take_input()
96.         else:
97.             grid[1][2] = player.get_symbol()
98.
99.     elif chance == 7:
100.         if grid[2][0] != 0:
101.             print(bcolors.FAIL + bcolors.BOLD +
102.                   '\nSorry, already taken!\n'+bcolors.ENDC)
103.             take_input()
104.         else:
105.             grid[2][0] = player.get_symbol()
106.
107.     elif chance == 8:
108.         if grid[2][1] != 0:
109.             print(bcolors.FAIL + bcolors.BOLD +
110.                   '\nSorry, already taken!\n'+bcolors.ENDC)
111.             take_input()
112.         else:
113.             grid[2][1] = player.get_symbol()
114.
115.     elif chance == 9:
116.         if grid[2][2] != 0:
117.             print(bcolors.FAIL + bcolors.BOLD +
118.                   '\nSorry, already taken!\n'+bcolors.ENDC)
119.             take_input()
120.         else:
121.             grid[2][2] = player.get_symbol()
122.
123.
124. while running:
125.     for player in players:
126.
127.         # if any player wins, the flag turns true
128.         flag = False
129.
130.         # checks
131.         flag2 = False
132.         for i in grid:
133.             for j in i:
134.                 if j == 0:
135.                     flag2 = True
136.
137.         if flag2 == False:
138.             print(bcolors.WARNING + 'This game ends in a draw.'+bcolors.ENDC)
139.             running = False
140.             break
141.
142.         # horizontal check
143.         for i in range(2):
144.             if grid[i][0] == grid[i][1] == grid[i][2]:
145.                 flag = True
146.                 if grid[i][0] == player1.get_symbol():
147.                     print(bcolors.OKGREEN + bcolors.BOLD +
148.                           player1.get_name() + ' won the game!'+bcolors.ENDC)
149.                 elif grid[i][0] == player2.get_symbol():
150.                     print(bcolors.OKGREEN + bcolors.BOLD +
151.                           player2.get_name() + ' won the game!'+bcolors.ENDC)
152.
153.         # Vertical check
154.         for i in range(2):
155.             if grid[0][i] == grid[1][i] == grid[2][i]:
156.                 flag = True
157.                 if grid[0][i] == player1.get_symbol():
158.                     print(bcolors.OKGREEN + bcolors.BOLD +
159.                           player1.get_name() + ' won the game!'+bcolors.ENDC)
160.                 elif grid[0][i] == player2.get_symbol():
161.                     print(bcolors.OKGREEN + bcolors.BOLD +
162.                           player2.get_name() + ' won the game!'+bcolors.ENDC)
163.
164.         # Diagonal checks
165.         if grid[0][0] == grid[1][1] == grid[2][2] or grid[0][2] == grid[1][1] == grid[2][0]:
166.             flag = True
167.             if grid[1][1] == player1.get_symbol():
168.                 print(bcolors.OKGREEN + bcolors.BOLD +
169.                       player1.get_name() + ' won the game!'+bcolors.ENDC)
170.             elif grid[1][1] == player2.get_symbol():
171.                 print(bcolors.OKGREEN + bcolors.BOLD +
172.                       player2.get_name() + ' won the game!'+bcolors.ENDC)
173.
174.         if flag == True:
175.             running = False
176.             break
177.
178.         take_input()
179.         print_grid()
180.
RAW Paste Data