diff --git a/Python Solution - TicTacToe/Tic Tac Toe.ipynb b/Python Solution - TicTacToe/Tic Tac Toe.ipynb new file mode 100644 index 0000000..2008e87 --- /dev/null +++ b/Python Solution - TicTacToe/Tic Tac Toe.ipynb @@ -0,0 +1,408 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "marker = input(\"Choose Marker X or O: \").upper()" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import clear_output\n", + "clear_output()" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " X | O | X \n", + "-----------------\n", + " O | X | O \n", + "-----------------\n", + " X | O | X \n" + ] + } + ], + "source": [ + "test_board = ['#','X','O','X','O','X','O','X','O','X']\n", + "def display_board(board):\n", + " clear_output()\n", + " print(f\" {board[7]} | {board[8]} | {board[9]} \")\n", + " print(\"-----------------\")\n", + " print(f\" {board[4]} | {board[5]} | {board[6]} \")\n", + " print(\"-----------------\")\n", + " print(f\" {board[1]} | {board[2]} | {board[3]} \")\n", + " \n", + "\n", + "display_board(test_board)" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": {}, + "outputs": [], + "source": [ + "def player_input():\n", + " marker = '9'\n", + " while(marker not in ['X', 'O']):\n", + "\n", + " marker = input(\"Player 1: Choose Marker X or O: \")\n", + "\n", + " if marker.upper() in ['X', 'O']:\n", + " if marker.upper() == 'X':\n", + " return ('X','O')\n", + " return ('O','X')\n", + " \n", + " elif marker.isdigit():\n", + " print(\"Don't Put a number\")\n", + "\n", + " elif marker not in ['X','O']:\n", + " print(\"Please select a valid marker.\") \n", + " \n", + "\n", + "player1_marker, player2_marker = player_input()" + ] + }, + { + "cell_type": "code", + "execution_count": 161, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "O X\n" + ] + } + ], + "source": [ + "print(player1_marker, player2_marker)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " X | O | X \n", + "-----------------\n", + " O | X | O \n", + "-----------------\n", + " X | O | X \n" + ] + } + ], + "source": [ + "def place_marker(board, marker, position):\n", + " board[position] = marker\n", + "\n", + "place_marker(test_board,'X',5)\n", + "display_board(test_board)" + ] + }, + { + "cell_type": "code", + "execution_count": 169, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 169, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def win_check(board, mark):\n", + " for i in range(1,len(board)):\n", + "\n", + " #in a row marker check\n", + " if board[i:i+3] == [mark,mark, mark]:\n", + " return True\n", + " \n", + " else:\n", + " return ((board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle\n", + " (board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle\n", + " (board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side\n", + " (board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal\n", + " (board[9] == mark and board[5] == mark and board[1] == mark))# diagonal\n", + "\n", + "\n", + " \n", + "\n", + "win_check(test_board, 'O')" + ] + }, + { + "cell_type": "code", + "execution_count": 214, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player 2\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "def choose_first():\n", + " if random.randint(1,2) == 1:\n", + " return 'Player 1'\n", + " return 'Player 2'\n", + "\n", + "print(choose_first())" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 202, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def space_check(board, position):\n", + " return board[position] == ' '\n", + " \n", + "\n", + "space_check(test_board, 9)" + ] + }, + { + "cell_type": "code", + "execution_count": 208, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 208, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def full_board_check(board):\n", + " for i in range(1,10):\n", + " if space_check(board,i):\n", + " return False\n", + " return True\n", + "\n", + "full_board_check(test_board)" + ] + }, + { + "cell_type": "code", + "execution_count": 144, + "metadata": {}, + "outputs": [], + "source": [ + "def player_choice(board):\n", + " position = 0\n", + " while (position not in range(1,10) or not space_check(board, position)):\n", + " \n", + " position = input(\"Enter Choice between (1-9): \")\n", + " \n", + " if position.isalpha():\n", + " print(\"Please enter a number!\")\n", + " \n", + " elif int(position) not in range(1,10):\n", + " print(\"Enter value between 1 to 9\")\n", + "\n", + " elif int(position) in range(1,10):\n", + " position = int(position)\n", + " \n", + " if space_check(board, position):\n", + " return position\n", + " return False\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 146, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please enter Y/N\n", + "Please enter Y/N\n", + "Don't put a number\n", + "Don't put a number\n" + ] + }, + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 146, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def replay():\n", + " choice = '9'\n", + " while(choice not in ['Y','N']):\n", + " choice = input(\"Do you want to play again Y/N?\")\n", + "\n", + " if choice.isdigit():\n", + " print(\"Don't put a number\")\n", + "\n", + " elif choice.upper() not in ['Y','N']:\n", + " print(\"Please enter Y/N\")\n", + "\n", + " elif choice.upper() in ['Y','N']:\n", + " choice = choice.upper()\n", + " if choice == 'Y':\n", + " return True\n", + " return False\n", + "\n", + "replay()" + ] + }, + { + "cell_type": "code", + "execution_count": 220, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " O | O | O \n", + "-----------------\n", + " X | X | O \n", + "-----------------\n", + " X | O | X \n", + "The game is a draw!\n", + "Please enter Y/N\n" + ] + } + ], + "source": [ + "print('Welcome to Tic Tac Toe!')\n", + "\n", + "while True:\n", + " # Reset the board\n", + " theBoard = [' '] * 10\n", + " player1_marker, player2_marker = player_input()\n", + " turn = choose_first()\n", + " print(turn + ' will go first.')\n", + " \n", + " play_game = input('Are you ready to play? Enter Yes or No.')\n", + " \n", + " if play_game.lower()[0] == 'y':\n", + " game_on = True\n", + " else:\n", + " game_on = False\n", + "\n", + " while game_on:\n", + " if turn == 'Player 1':\n", + " # Player1's turn.\n", + " \n", + " display_board(theBoard)\n", + " position = player_choice(theBoard)\n", + " place_marker(theBoard, player1_marker, position)\n", + "\n", + " if win_check(theBoard, player1_marker):\n", + " display_board(theBoard)\n", + " print('Congratulations! Player 1 has won the game!')\n", + " game_on = False\n", + " else:\n", + " if full_board_check(theBoard):\n", + " display_board(theBoard)\n", + " print('The game is a draw!')\n", + " break\n", + " else:\n", + " turn = 'Player 2'\n", + "\n", + " else:\n", + " # Player2's turn.\n", + " \n", + " display_board(theBoard)\n", + " position = player_choice(theBoard)\n", + " place_marker(theBoard, player2_marker, position)\n", + "\n", + " if win_check(theBoard, player2_marker):\n", + " display_board(theBoard)\n", + " print('Player 2 has won!')\n", + " game_on = False\n", + " else:\n", + " if full_board_check(theBoard):\n", + " display_board(theBoard)\n", + " print('The game is a draw!')\n", + " break\n", + " else:\n", + " turn = 'Player 1'\n", + "\n", + " if not replay():\n", + " break\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.10.7 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.10.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "e8899eb02dfbc033aab5733bdae1bd213fa031d40331094008e8673d99ebab63" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}