parent
063ed5cad2
commit
bb5a9dbc52
|
@ -0,0 +1,493 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Milestone Project 1: Walkthrough Steps Workbook\n",
|
||||
"\n",
|
||||
"Below is a set of steps for you to follow to try to create the Tic Tac Toe Milestone Project game!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### Some suggested tools before you get started:\n",
|
||||
"To take input from a user:\n",
|
||||
"\n",
|
||||
" player1 = input(\"Please pick a marker 'X' or 'O'\")\n",
|
||||
" \n",
|
||||
"Note that input() takes in a string. If you need an integer value, use\n",
|
||||
"\n",
|
||||
" position = int(input('Please enter a number'))\n",
|
||||
" \n",
|
||||
"<br>To clear the screen between moves:\n",
|
||||
"\n",
|
||||
" from IPython.display import clear_output\n",
|
||||
" clear_output()\n",
|
||||
" \n",
|
||||
"Note that clear_output() will only work in jupyter. To clear the screen in other IDEs, consider:\n",
|
||||
"\n",
|
||||
" print('\\n'*100)\n",
|
||||
" \n",
|
||||
"This scrolls the previous board up out of view. Now on to the program!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from IPython.display import clear_output\n",
|
||||
"\n",
|
||||
"def display_board(board):\n",
|
||||
" \n",
|
||||
" print (board[1]+' | '+board[2]+' | '+board[3])\n",
|
||||
" print (board[4]+' | '+board[5]+' | '+board[6])\n",
|
||||
" print (board[7]+' | '+board[8]+' | '+board[9])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**TEST Step 1:** run your function on a test version of the board list, and make adjustments as necessary"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"X | O | X\n",
|
||||
"O | X | O\n",
|
||||
"X | O | X\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"test_board = ['#','X','O','X','O','X','O','X','O','X']\n",
|
||||
"display_board(test_board)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def player_input():\n",
|
||||
" player1 = False\n",
|
||||
" while not player1:\n",
|
||||
" player1 = input(\"Player 1 please pick a marker 'X' or 'O' \")\n",
|
||||
" if player1 not in ('X','O'):\n",
|
||||
" print('\\nIncorrect input provided.\\nPlease try again.\\n ')\n",
|
||||
" player1 = False\n",
|
||||
" return player1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**TEST Step 2:** run the function to make sure it returns the desired output"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Please pick a marker 'X' or 'O' t\n",
|
||||
"\n",
|
||||
"Incorrect input provided.\n",
|
||||
"Please try again.\n",
|
||||
" \n",
|
||||
"Please pick a marker 'X' or 'O' X\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'X'"
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"player_input()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def place_marker(board, marker, position):\n",
|
||||
" \n",
|
||||
" #marker = player_input()\n",
|
||||
" #position = 0\n",
|
||||
" #while not position:\n",
|
||||
" # position = int(input('Please enter a number (1-9): '))\n",
|
||||
" # if position not in range(1,10):\n",
|
||||
" # print('\\nPlease enter a valid input\\n ')\n",
|
||||
" # position = 0\n",
|
||||
" \n",
|
||||
" for index,value in enumerate(board):\n",
|
||||
" if index == position:\n",
|
||||
" board[index] = marker\n",
|
||||
" \n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**TEST Step 3:** run the place marker function using test parameters and display the modified board"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"X | O | X\n",
|
||||
"O | X | O\n",
|
||||
"X | $ | X\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"place_marker(test_board,'$',8)\n",
|
||||
"display_board(test_board)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Step 4: Write a function that takes in a board and a mark (X or O) and then checks to see if that mark has won. **"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def win_check(board, mark):\n",
|
||||
" checklist = []\n",
|
||||
" winning_seq = [{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}]\n",
|
||||
" \n",
|
||||
" for index,value in enumerate(board):\n",
|
||||
" if value == mark:\n",
|
||||
" checklist.append(index)\n",
|
||||
" \n",
|
||||
" for seq in winning_seq:\n",
|
||||
" if seq.issubset(set(checklist)):\n",
|
||||
" return True\n",
|
||||
" \n",
|
||||
" return False\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**TEST Step 4:** run the win_check function against our test_board - it should return True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"win_check(test_board,'X')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"\n",
|
||||
"def choose_first():\n",
|
||||
" player = random.randint(1,2)\n",
|
||||
" print(f'Player {player} goes first!\\n')\n",
|
||||
" return player"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Step 6: Write a function that returns a boolean indicating whether a space on the board is freely available.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def space_check(board, position):\n",
|
||||
" \n",
|
||||
" return board[position] not in ('X','O')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Step 7: Write a function that checks if the board is full and returns a boolean value. True if full, False otherwise.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def full_board_check(board):\n",
|
||||
" \n",
|
||||
" for i in range(1,10):\n",
|
||||
" if board[i] not in ('X','O'):\n",
|
||||
" return False\n",
|
||||
"\n",
|
||||
" return True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"test_board2 = ['X','O','X','O','X','O','X','O','O','X']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"full_board_check(test_board2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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 it's a free position. If it is, then return the position for later use.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def player_choice(board):\n",
|
||||
" \n",
|
||||
" position = int(input('Please enter a number '))\n",
|
||||
" \n",
|
||||
" free_position = space_check(board,position)\n",
|
||||
" if free_position:\n",
|
||||
" return position"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**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.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def replay():\n",
|
||||
" \n",
|
||||
" play = input('Do you want to play again? [Y|N] ')\n",
|
||||
" return True if play.upper() == 'Y' else False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"**Step 10: Here comes the hard part! Use while loops and the functions you've made to run the game!**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('Welcome to Tic Tac Toe!')\n",
|
||||
"\n",
|
||||
"while True:\n",
|
||||
" # Set the game up here\n",
|
||||
" board = ['X',' ',' ',' ',' ',' ',' ',' ',' ',' ']\n",
|
||||
" display_board(board)\n",
|
||||
" \n",
|
||||
" game_dict = {'X':1,'O':2}\n",
|
||||
" player_1 = player_input()\n",
|
||||
" if player_1 == 'X':\n",
|
||||
" player_2 = 'O'\n",
|
||||
" else:\n",
|
||||
" player_2 = 'X'\n",
|
||||
" game_dict[player_2] = 2\n",
|
||||
" game_dict[player_1] = 1\n",
|
||||
" \n",
|
||||
" player = choose_first()\n",
|
||||
" \n",
|
||||
" game_on = not full_board_check(board)\n",
|
||||
"\n",
|
||||
" while game_on:\n",
|
||||
" #Player 1 Turn\n",
|
||||
" if player == 1:\n",
|
||||
" position = player_choice(board)\n",
|
||||
" place_marker(board,player_1,position)\n",
|
||||
" display_board(board)\n",
|
||||
" if win_check(board,player_1):\n",
|
||||
" print(\"\\nYou've won Player 1! \")\n",
|
||||
" break\n",
|
||||
" player = 2\n",
|
||||
" \n",
|
||||
" # Player2's turn.\n",
|
||||
" if player == 2:\n",
|
||||
" position = player_choice(board)\n",
|
||||
" place_marker(board,player_2,position)\n",
|
||||
" display_board(board)\n",
|
||||
" if win_check(board,player_2):\n",
|
||||
" print(\"\\nYou've won Player 2! \")\n",
|
||||
" break\n",
|
||||
" player = 1\n",
|
||||
" \n",
|
||||
" game_on = not full_board_check(board)\n",
|
||||
"\n",
|
||||
" if not replay():\n",
|
||||
" clear_output()\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"## Good Job!"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
Loading…
Reference in New Issue