0% found this document useful (0 votes)
18 views7 pages

AP-CSP Create Task

The document contains a Python implementation of a Tic-Tac-Toe game, featuring classes and methods for creating a game board, displaying it, and handling player interactions. It supports both single-player and two-player modes, including functionality for checking wins, validating moves, and resetting the board. The game can be played repeatedly based on user input.

Uploaded by

byjuaditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

AP-CSP Create Task

The document contains a Python implementation of a Tic-Tac-Toe game, featuring classes and methods for creating a game board, displaying it, and handling player interactions. It supports both single-player and two-player modes, including functionality for checking wins, validating moves, and resetting the board. The game can be played repeatedly based on user input.

Uploaded by

byjuaditya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/22/23, 1:45 PM [Link]

io/codePrint/

1 # AP CSP Create Task


2 # Tic-Tac-Toe
3
4 import random
5
6 class Tic_Tac_Toe():
7 def __init__(self):
8 [Link] = []
9
10 # Creates the board
11 def create_board(self):
12 # Creates 3 rows
13 for x in range(3):
14 row = []
15 # Makes the rows like ---
16 for i in range(3):
17 [Link]("-")
18 # This will append each row to the board
19 [Link](row)
20
21 # Displays the board
22 def display_board(self):
23 num = 1
24 # Prints the horiziontal coordinates
25 print(" 1 2 3")
26 print(" -------------")
27 for row in [Link]:
28 # Prints the vertical coordinates
29 print(num, end=' ')
30 print("|", end = ' ')
31 # Prints out board
32 for x in row:
33 # Prints the board out without the brackets and commas
34 print(x, end=' ')
35 print("|", end= ' ')
36 # Prints a new line for every row
37 print()
38 print(" -------------", end = '')
print()
[Link] 1/7
2/22/23, 1:45 PM [Link]

39 num += 1
40
41 # Creates a 50/50 random
42 # This will be used in determining a random first player (X/O)
43 def random(self):
44 return [Link](0, 1)
45
46 # This will swap the player
47 def swap_player(self):
48 if [Link] == 'X':
49 [Link] = 'O'
50 else:
51 [Link] = 'X'
52
53 # Places player on the board on the coordinates of user input
54 def place_player(self, row, col):
55 [Link][row-1][col-1] = [Link]
56
57
58 # Makes sure the user inputted coordinates don't already have something there and are on the board.
59 def check_spot_valid(self, row, col):
60 valid = False
61 while valid != True:
62 if row > 3 or row < 1:
63 print("That space is off the board!")
64 elif col > 3 or col < 1:
65 print("That space is off the board!")
66 else:
67 if [Link][row-1][col-1] != '-':
68 print(f"There is already a {[Link][row-1][col-1]} there!")
69 else:
70 valid = True
71 break
72
73 row, col = list(map(int, input(f"Player {[Link]} pick another spot (row,column). ").split(',')))
74
75 return row, col
76
77
78 # Checks if a player has won
79 def check_win(self):
80 board_length = len([Link])

[Link] 2/7
2/22/23, 1:45 PM [Link]

81
82 # Checks rows
83 for row in range(board_length):
84 win = True
85 for col in range(board_length):
86 if [Link][row][col] != [Link]:
87 win = False
88 break
89 # Works because when win is true it will return true but when win is false it will do nothing and won.
90 if win:
91 return win
92
93 # Checks columns
94 for row in range(board_length):
95 win = True
96 for col in range(board_length):
97 if [Link][col][row] != [Link]:
98 win = False
99 break
100 if win:
101 return win
102
103 # Checks top left to bottom right diagonal
104 win = True
105 for x in range(board_length):
106 if [Link][x][x] != [Link]:
107 win = False
108 break
109 if win:
110 return win
111
112 # Checks top right to bottom left diagonal
113 win = True
114 for x in range(board_length):
115 if [Link][x][2-x] != [Link]:
116 win = False
117 break
118 if win:
119 return win
120
121 # If the checks pass through everything above, then no one has won so return false
122 return False

[Link] 3/7
2/22/23, 1:45 PM [Link]

123
124
125 # Resets the board, used for playing again
126 def reset_board(self):
127 [Link] = []
128
129
130 # Checks if the board is full and game is a tie
131 def check_full_board(self):
132 board_length = len([Link])
133 for row in range(board_length):
134 for col in range(board_length):
135 if [Link][row][col] == '-':
136 return False
137 break
138 return True
139
140
141 # Used so computer can place random player
142 def computer_placer(self):
143 true = True
144 while true == True:
145 random_row = [Link](0, 2)
146 random_col = [Link](0, 2)
147
148 if [Link][random_row][random_col] == '-':
149 [Link][random_row][random_col] = [Link]
150 true = False
151 else:
152 true = True
153
154
155 # Starts the game
156 def pvp_start(self):
157 self.create_board()
158 self.display_board()
159
160 if [Link]() == 1:
161 [Link] = 'X'
162 else:
163 [Link] = 'O'
164

[Link] 4/7
2/22/23, 1:45 PM [Link]

165 while True:


166 # User input that turns the input into a list and uses python method to sepearte it into the desired row and column variable
167 # Rescources for learning how to do map and split are [Link]
168 # and [Link]
169 # User input will be row,col
170 row, col = list(map(int, input(f"Player {[Link]} pick a spot (row,column). ").split(',')))
171
172
173 row, col = self.check_spot_valid(row, col)
174
175
176 self.place_player(row, col)
177 self.display_board()
178
179 if self.check_win():
180 print(f"Congratulations player {[Link]}! You won!")
181 return False
182 if self.check_full_board():
183 print("Tie! Nice try player X and player O!")
184 return False
185
186 self.swap_player()
187
188
189 def one_player_start(self):
190 self.create_board()
191 self.display_board()
192
193 if [Link]() == 1:
194 [Link] = 'X'
195 [Link] = 'O'
196 else:
197 [Link] = 'O'
198 [Link] = 'X'
199
200 while True:
201 row, col = list(map(int, input(f"Player {[Link]} pick a spot (row,column). ").split(',')))
202
203 row, col = self.check_spot_valid(row, col)
204
205 self.place_player(row, col)
206

[Link] 5/7
2/22/23, 1:45 PM [Link]

207 if self.check_win():
208 self.display_board()
209 print(f"Congratulations player {[Link]}! You won!")
210 return False
211
212 self.display_board()
213 print("Computer placing...")
214
215 self.computer_placer()
216
217 # Swaps the player to the computer player and then checks win and then swaps the player back.
218 self.swap_player()
219 if self.check_win():
220 self.display_board()
221 print(f"Nice try! Player {[Link]} won!")
222 return False
223 self.swap_player()
224
225
226 if self.check_full_board():
227 print(f"Tie! Nice try player {[Link]}!")
228 return False
229
230 self.display_board()
231
232
233
234 print("Hello! This is a Tic_Tac_Toe Game!")
235 game_choice = input("Would you like to play 1 player or 2 player (1/2). ")
236
237 game = Tic_Tac_Toe()
238
239 # Gets what user wants to play and does according version of game
240 if game_choice == '2':
241 game.pvp_start()
242 else:
243 game.one_player_start()
244
245
246 # Play again
247 play_again = input("Do you want to play again? (y/n). ")
248

[Link] 6/7
2/22/23, 1:45 PM [Link]

249 while play_again == 'y':


250 game.reset_board()
251 game_choice = input("Would you like to play 1 player or 2 player (1/2). ")
252 if game_choice == '2':
253 game.pvp_start()
254 else:
255 game.one_player_start()
256 play_again = input("Do you want to play again? (y/n). ")
257 else:
258 print("Thanks for playing!")
259 exit()
260
261

PDF document made with CodePrint using Prism

[Link] 7/7

You might also like