Skip to content

Commit ad78656

Browse files
committed
milestone 1 updates
1 parent 5f1f249 commit ad78656

2 files changed

+722
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Milestone Project 1: Full Walkthrough Code Solution\n",
8+
"\n",
9+
"Below is the filled in code that goes along with the complete walkthrough video. Check out the corresponding lecture videos for more information on this code!"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 86,
15+
"metadata": {
16+
"collapsed": true
17+
},
18+
"outputs": [],
19+
"source": [
20+
"# For using the same code in either Python 2 or 3\n",
21+
"from __future__ import print_function \n",
22+
"\n",
23+
"## Note: Python 2 users, use raw_input() to get player input. Python 3 users, use input()"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"**Step 1: Write a function that can print out a board. Set up your board as a list, where each index 1-9 corresponds with a number on a number pad, so you get a 3 by 3 board representation.**"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 87,
36+
"metadata": {
37+
"collapsed": true
38+
},
39+
"outputs": [],
40+
"source": [
41+
"from IPython.display import clear_output\n",
42+
"def display_board(board):\n",
43+
" \n",
44+
" clear_output()\n",
45+
" print(' | |')\n",
46+
" print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])\n",
47+
" print(' | |')\n",
48+
" print('-----------')\n",
49+
" print(' | |')\n",
50+
" print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])\n",
51+
" print(' | |')\n",
52+
" print('-----------')\n",
53+
" print(' | |')\n",
54+
" print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])\n",
55+
" print(' | |')"
56+
]
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"**Step 2: Write a function that can take in a player input and assign their marker as 'X' or 'O'. Think about using *while* loops to continually ask until you get a correct answer.**"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 88,
68+
"metadata": {
69+
"collapsed": true
70+
},
71+
"outputs": [],
72+
"source": [
73+
"def player_input():\n",
74+
" \n",
75+
" marker = ''\n",
76+
" while not (marker == 'X' or marker == 'O'):\n",
77+
" marker = raw_input('Player 1: Do you want to be X or O?').upper()\n",
78+
"\n",
79+
" if marker == 'X':\n",
80+
" return ('X', 'O')\n",
81+
" else:\n",
82+
" return ('O', 'X')"
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"**Step 3: Write a function that takes, in the board list object, a marker ('X' or 'O'), and a desired position (number 1-9) and assigns it to the board.**"
90+
]
91+
},
92+
{
93+
"cell_type": "code",
94+
"execution_count": 89,
95+
"metadata": {
96+
"collapsed": true
97+
},
98+
"outputs": [],
99+
"source": [
100+
"def place_marker(board, marker, position):\n",
101+
" board[position] = marker"
102+
]
103+
},
104+
{
105+
"cell_type": "markdown",
106+
"metadata": {},
107+
"source": [
108+
"**Step 4: Write a function that takes in a board and checks to see if someone has won. **"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": 90,
114+
"metadata": {
115+
"collapsed": true
116+
},
117+
"outputs": [],
118+
"source": [
119+
"def win_check(board,mark):\n",
120+
" \n",
121+
" return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top\n",
122+
" (board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle\n",
123+
" (board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom\n",
124+
" (board[7] == mark and board[4] == mark and board[1] == mark) or # down themarkft side\n",
125+
" (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle\n",
126+
" (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side\n",
127+
" (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal\n",
128+
" (board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal"
129+
]
130+
},
131+
{
132+
"cell_type": "markdown",
133+
"metadata": {},
134+
"source": [
135+
"**Step 5: Write a function that uses the random module to randomly decide which player goes first. You may want to lookup random.randint() Return a string of which player went first.**"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 91,
141+
"metadata": {
142+
"collapsed": true
143+
},
144+
"outputs": [],
145+
"source": [
146+
"import random\n",
147+
"def choose_first():\n",
148+
" if random.randint(0, 1) == 0:\n",
149+
" return 'Player 2'\n",
150+
" else:\n",
151+
" return 'Player 1'"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"**Step 6: Write a function that returns a boolean indicating whether a space on the board is freely available.**"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 92,
164+
"metadata": {
165+
"collapsed": true
166+
},
167+
"outputs": [],
168+
"source": [
169+
"def space_check(board, position):\n",
170+
" \n",
171+
" return board[position] == ' '"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"**Step 7: Write a function that checks if the board is full and returns a boolean value. True if full, False otherwise.**"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 93,
184+
"metadata": {
185+
"collapsed": true
186+
},
187+
"outputs": [],
188+
"source": [
189+
"def full_board_check(board):\n",
190+
" \n",
191+
" if space_check(board, i):\n",
192+
" return False\n",
193+
" return True"
194+
]
195+
},
196+
{
197+
"cell_type": "markdown",
198+
"metadata": {},
199+
"source": [
200+
"**Step 8: Write a function that asks for a player's next position (as a number 1-9) and then uses the function from step 6 to check if its a free position. If it is, then return the position for later use. **"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": 94,
206+
"metadata": {
207+
"collapsed": true
208+
},
209+
"outputs": [],
210+
"source": [
211+
"def player_choice(board):\n",
212+
" \n",
213+
" position = ' '\n",
214+
" while position not in '1 2 3 4 5 6 7 8 9'.split() or not space_check(board, int(position)):\n",
215+
" \n",
216+
" position = raw_input('Choose your next position: (1-9) ')\n",
217+
" return int(position)"
218+
]
219+
},
220+
{
221+
"cell_type": "markdown",
222+
"metadata": {},
223+
"source": [
224+
"**Step 9: Write a function that asks the player if they want to play again and returns a boolean True if they do want to play again.**"
225+
]
226+
},
227+
{
228+
"cell_type": "code",
229+
"execution_count": 95,
230+
"metadata": {
231+
"collapsed": true
232+
},
233+
"outputs": [],
234+
"source": [
235+
"def replay():\n",
236+
" \n",
237+
" return raw_input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')"
238+
]
239+
},
240+
{
241+
"cell_type": "markdown",
242+
"metadata": {
243+
"collapsed": true
244+
},
245+
"source": [
246+
"**Step 10: Here comes the hard part! Use while loops and the functions you've made to run the game!**"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": 96,
252+
"metadata": {
253+
"collapsed": false
254+
},
255+
"outputs": [
256+
{
257+
"name": "stdout",
258+
"output_type": "stream",
259+
"text": [
260+
" | |\n",
261+
" X | | \n",
262+
" | |\n",
263+
"-----------\n",
264+
" | |\n",
265+
" O | X | O\n",
266+
" | |\n",
267+
"-----------\n",
268+
" | |\n",
269+
" | O | X\n",
270+
" | |\n",
271+
"Player 2 has won!\n",
272+
"Do you want to play again? Enter Yes or No: n\n"
273+
]
274+
}
275+
],
276+
"source": [
277+
"print('Welcome to Tic Tac Toe!')\n",
278+
"\n",
279+
"while True:\n",
280+
" # Reset the board\n",
281+
" theBoard = [' '] * 10\n",
282+
" player1_marker, player2_marker = player_input()\n",
283+
" turn = choose_first()\n",
284+
" print(turn + ' will go first.')\n",
285+
" game_on = True\n",
286+
"\n",
287+
" while game_on:\n",
288+
" if turn == 'Player 1':\n",
289+
" # Player1's turn.\n",
290+
" \n",
291+
" display_board(theBoard)\n",
292+
" position = player_choice(theBoard)\n",
293+
" place_marker(theBoard, player1_marker, position)\n",
294+
"\n",
295+
" if win_check(theBoard, player1_marker):\n",
296+
" display_board(theBoard)\n",
297+
" print('Congratualtions! You have won the game!')\n",
298+
" game_on = False\n",
299+
" else:\n",
300+
" if full_board_check(theBoard):\n",
301+
" display_board(theBoard)\n",
302+
" print('The game is a draw!')\n",
303+
" break\n",
304+
" else:\n",
305+
" turn = 'Player 2'\n",
306+
"\n",
307+
" else:\n",
308+
" # Player2's turn.\n",
309+
" \n",
310+
" display_board(theBoard)\n",
311+
" position = player_choice(theBoard)\n",
312+
" place_marker(theBoard, player2_marker, position)\n",
313+
"\n",
314+
" if win_check(theBoard, player2_marker):\n",
315+
" display_board(theBoard)\n",
316+
" print('Player 2 has won!')\n",
317+
" game_on = False\n",
318+
" else:\n",
319+
" if full_board_check(theBoard):\n",
320+
" display_board(theBoard)\n",
321+
" print('The game is a tie!')\n",
322+
" break\n",
323+
" else:\n",
324+
" turn = 'Player 1'\n",
325+
"\n",
326+
" if not replay():\n",
327+
" break"
328+
]
329+
},
330+
{
331+
"cell_type": "markdown",
332+
"metadata": {
333+
"collapsed": true
334+
},
335+
"source": [
336+
"## Good Job!"
337+
]
338+
}
339+
],
340+
"metadata": {
341+
"kernelspec": {
342+
"display_name": "Python 2",
343+
"language": "python",
344+
"name": "python2"
345+
},
346+
"language_info": {
347+
"codemirror_mode": {
348+
"name": "ipython",
349+
"version": 2
350+
},
351+
"file_extension": ".py",
352+
"mimetype": "text/x-python",
353+
"name": "python",
354+
"nbconvert_exporter": "python",
355+
"pygments_lexer": "ipython2",
356+
"version": "2.7.11"
357+
}
358+
},
359+
"nbformat": 4,
360+
"nbformat_minor": 0
361+
}

0 commit comments

Comments
 (0)