diff --git a/Project_01_Result_File.txt b/Project_01_Result_File.txt new file mode 100644 index 0000000..a4fa7f7 --- /dev/null +++ b/Project_01_Result_File.txt @@ -0,0 +1,2 @@ +RESULT CHART: +Game over! Winner is Player 1! \ No newline at end of file diff --git a/Project_01_Tic-Tac-Toe.ipynb b/Project_01_Tic-Tac-Toe.ipynb new file mode 100644 index 0000000..c89b8a6 --- /dev/null +++ b/Project_01_Tic-Tac-Toe.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "id": "suburban-marijuana", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1|2|3\n", + "-----\n", + "4|5|6\n", + "-----\n", + "7|8|9\n", + "Select option (X,O):\n", + "Player 1: A\n", + "Please enter valid input: (X,O)\n", + "Select option (X,O):\n", + "Player 1: X\n", + "Player 2: O\n", + "Enter position (Player 1): 1\n", + "X|2|3\n", + "-----\n", + "4|5|6\n", + "-----\n", + "7|8|9\n", + "Enter position (Player 2): 2\n", + "X|O|3\n", + "-----\n", + "4|5|6\n", + "-----\n", + "7|8|9\n", + "Enter position (Player 1): 4\n", + "X|O|3\n", + "-----\n", + "X|5|6\n", + "-----\n", + "7|8|9\n", + "Enter position (Player 2): 5\n", + "X|O|3\n", + "-----\n", + "X|O|6\n", + "-----\n", + "7|8|9\n", + "Enter position (Player 1): 7\n", + "X|O|3\n", + "-----\n", + "X|O|6\n", + "-----\n", + "X|8|9\n", + "Game over! Winner is Player 1!\n", + "RESULT CHART:\n", + "\n", + "Game over! Winner is Player 1!\n", + "Would you like to play again? (Yes/No): No\n", + "Game stopped as per you request!\n" + ] + } + ], + "source": [ + "board=[1,2,3,4,5,6,7,8,9]\n", + "player1=''\n", + "player2=''\n", + "player_flag=True\n", + "\n", + "#Display the Tic-Tac-Toe board\n", + "def board_display():\n", + " print(str(board[0])+'|'+str(board[1])+'|'+str(board[2]))\n", + " print('-----')\n", + " print(str(board[3])+'|'+str(board[4])+'|'+str(board[5]))\n", + " print('-----')\n", + " print(str(board[6])+'|'+str(board[7])+'|'+str(board[8]))\n", + "\n", + "#Players to select options(X,O)\n", + "def player_select():\n", + " global player1\n", + " global player2\n", + " check_player_input=True\n", + " while check_player_input: \n", + " print('Select option (X,O):')\n", + " player1=input('Player 1: ')\n", + " if player1=='X':\n", + " player2='O'\n", + " print('Player 2: {}'.format(player2))\n", + " check_player_input=False\n", + " elif player1=='O':\n", + " player2='X'\n", + " print('Player 2: {}'.format(player2))\n", + " check_player_input=False\n", + " else: \n", + " print('Please enter valid input: (X,O)')\n", + " \n", + "\n", + "#Enter options on the positions\n", + "def enter_option():\n", + " global player_flag\n", + " check_position=True\n", + " while check_position:\n", + " try:\n", + " if player_flag==True:\n", + " position=int(input('Enter position (Player 1): '))\n", + " check_position=position_check(position,player1) \n", + " else:\n", + " position=int(input('Enter position (Player 2): ')) \n", + " check_position=position_check(position,player2)\n", + " except:\n", + " print('Incorrect Input. Re-enter position.')\n", + " board_display()\n", + "\n", + "#Checks for - the position on the board is occupied or not, incorrect position entered. Also,update position if all is ok.\n", + "def position_check(position,player):\n", + " global player_flag\n", + " if position>=10 and position<0:\n", + " print('Incorrect position. It should be within 1 and 9. Re-enter position.')\n", + " return True\n", + " elif board[position-1] in ['X','O']:\n", + " print('Position already occupied. Re-enter position.')\n", + " return True\n", + " else:\n", + " board[position-1]=player\n", + " player_flag=not player_flag \n", + " return False \n", + "\n", + " \n", + "#Update options on the board\n", + "def board_update():\n", + " length=9\n", + " winner_found=False\n", + " result=''\n", + " while length>0:\n", + " enter_option()\n", + " if game_winner(player1):\n", + " result='Game over! Winner is Player 1!'\n", + " print(result)\n", + " length=0\n", + " winner_found=True\n", + " elif game_winner(player2):\n", + " result='Game over! Winner is Player 2!'\n", + " print(result)\n", + " length=0\n", + " winner_found=True\n", + " else:\n", + " length=length-1\n", + " if winner_found==False:\n", + " result='Game draw!'\n", + " print(result)\n", + " \n", + " result_file(result) \n", + " play_again()\n", + "\n", + "#Returns result if game is over and there is a winner\n", + "def game_winner(player):\n", + " return ((board[0]==board[1]==board[2]==player) or (board[0]==board[3]==board[6]==player) or \n", + " (board[0]==board[4]==board[8]==player) or (board[3]==board[4]==board[5]==player) or \n", + " (board[6]==board[7]==board[8]==player) or (board[1]==board[4]==board[7]==player) or \n", + " (board[2]==board[5]==board[8]==player) or (board[6]==board[4]==board[2]==player))\n", + " \n", + "#Ask users if they wish to continue the game or stop.\n", + "def play_again():\n", + " play_again_input=True\n", + " while play_again_input: \n", + " replay=input('Would you like to play again? (Yes/No): ')\n", + " if replay=='Yes':\n", + " global board\n", + " board=[1,2,3,4,5,6,7,8,9]\n", + " global player1\n", + " player1=''\n", + " global player2\n", + " player2=''\n", + " global player_flag\n", + " player_flag=True\n", + " play_game_start()\n", + " play_again_input=False\n", + " elif replay=='No':\n", + " print('Game stopped as per you request!')\n", + " play_again_input=False\n", + " else:\n", + " print('Unable to understand the input. Please re-enter.')\n", + "\n", + "#Save the result of the game in a file\n", + "def result_file(result):\n", + " game_result_file=open('Project_01_Result_File.txt',mode='a')\n", + " game_result_file.write('\\n'+result)\n", + " game_result_file.close()\n", + " \n", + " game_result_file=open('Project_01_Result_File.txt',mode='r')\n", + " print(game_result_file.read())\n", + " game_result_file.close()\n", + " \n", + "#Start game \n", + "def play_game_start():\n", + " board_display()\n", + " player_select()\n", + " board_update()\n", + "\n", + "play_game_start()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bigger-apparatus", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "mental-pattern", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}