-
Notifications
You must be signed in to change notification settings - Fork 1
Milestone projects #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| RESULT CHART: | ||
| Game over! Winner is Player 1! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. player_flag is a boolean. Not need to compare it to True or False. condition "if payer_flag:" is enough. |
||
| " 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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can change the while condition as "length>0 and not winner_found". In that case no need to do length=0 after winner is found. |
||
| " 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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. play_again() should not be called here. It is not responsibility of the board_update function. It can be called at the end of play_game_start function. |
||
| "\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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these can be pulled out into a function called "reset_game" or "reset_board". |
||
| " 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", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should name the method based on action they perform. Like "save_result" or "save_result_to_file" or "write_to_file". Its more readable and makes sense about what the function is doing. |
||
| " 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()" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would write this as follows def replay_game(): yes = 'Yes' print('Game stopped as per you request!') |
||
| ] | ||
| }, | ||
| { | ||
| "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 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: This while loop can easily become infinite loop if the user keep entering wrong input. There should be limited number of attempts.