├── .gitignore ├── README.md └── code.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tic-Tac-Toe Game 2 | 3 | A simple CLI-based game for fun ! 4 | 5 | ## Try it out ! 6 | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/divyakelaskar/Tic-Tac-Toe/blob/master/code.py) 7 | -------------------------------------------------------------------------------- /code.py: -------------------------------------------------------------------------------- 1 | # global variables 2 | board = ["-", "-", "-", 3 | "-", "-", "-", 4 | "-", "-", "-"] 5 | game_still_going = True 6 | winner = None 7 | current_player = "X" 8 | 9 | # the function below is the step by step execution of all the functions defined below. 10 | def play_game(): 11 | print("\n X - O G A M E\n") 12 | print("Note : Use the numpad to enter X or O in respective fields.\n") 13 | display_board() 14 | while game_still_going: 15 | handle_turn(current_player) 16 | check_if_game_over() 17 | flip_player() 18 | if winner == "X" or winner == "O": 19 | print("PLAYER",winner,"IS THE WINNER !!.") 20 | elif winner == None: 21 | print("Tie.") 22 | print("\nT H A N K S F O R P L A Y I N G ! ! ! ! !") 23 | 24 | # shows the game board with the help of the list named 'board'(line 2) 25 | def display_board(): 26 | print(board[6] + " | " + board[7] + " | " + board[8] + " 7 | 8 | 9\n") 27 | print(board[3] + " | " + board[4] + " | " + board[5] + " 4 | 5 | 6\n") 28 | print(board[0] + " | " + board[1] + " | " + board[2] + " 1 | 2 | 3\n") 29 | 30 | # manages the turn of single player at a time 31 | def handle_turn(player): 32 | print("Player",player + "'s turn.") 33 | position = input("Choose a position from 1-9: ") 34 | valid = False 35 | while not valid: 36 | while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]: 37 | position = input("Choose a position from 1-9: ") 38 | position = int(position) - 1 39 | if board[position] == "-": 40 | valid = True 41 | else: 42 | print("You can't go there. Go again.") 43 | board[position] = player 44 | display_board() 45 | 46 | # verifies if the game is completed 47 | def check_if_game_over(): 48 | check_for_winner() 49 | check_for_tie() 50 | 51 | # finds if any player is the winner 52 | def check_for_winner(): 53 | global winner 54 | row_winner = check_rows() 55 | column_winner = check_columns() 56 | diagonal_winner = check_diagonals() 57 | if row_winner: 58 | winner = row_winner 59 | elif column_winner: 60 | winner = column_winner 61 | elif diagonal_winner: 62 | winner = diagonal_winner 63 | else: 64 | winner = None 65 | 66 | # checks if any horizontal x/o pattern is present 67 | def check_rows(): 68 | global game_still_going 69 | row_1 = board[0] == board[1] == board[2] != "-" 70 | row_2 = board[3] == board[4] == board[5] != "-" 71 | row_3 = board[6] == board[7] == board[8] != "-" 72 | if row_1 or row_2 or row_3: 73 | game_still_going = False 74 | if row_1: 75 | return board[0] 76 | elif row_2: 77 | return board[3] 78 | elif row_3: 79 | return board[6] 80 | else: 81 | return None 82 | 83 | # checks if any vertical x/o pattern is present 84 | def check_columns(): 85 | global game_still_going 86 | column_1 = board[0] == board[3] == board[6] != "-" 87 | column_2 = board[1] == board[4] == board[7] != "-" 88 | column_3 = board[2] == board[5] == board[8] != "-" 89 | if column_1 or column_2 or column_3: 90 | game_still_going = False 91 | if column_1: 92 | return board[0] 93 | elif column_2: 94 | return board[1] 95 | elif column_3: 96 | return board[2] 97 | else: 98 | return None 99 | 100 | # checks if any x/o pattern is present diagonally on the board 101 | def check_diagonals(): 102 | global game_still_going 103 | diagonal_1 = board[0] == board[4] == board[8] != "-" 104 | diagonal_2 = board[2] == board[4] == board[6] != "-" 105 | if diagonal_1 or diagonal_2: 106 | game_still_going = False 107 | if diagonal_1: 108 | return board[0] 109 | elif diagonal_2: 110 | return board[2] 111 | else: 112 | return None 113 | 114 | # checks if the board is completely filled or not 115 | def check_for_tie(): 116 | global game_still_going 117 | if "-" not in board: 118 | game_still_going = False 119 | return True 120 | else: 121 | return False 122 | 123 | # swaps the players 124 | def flip_player(): 125 | global current_player 126 | if current_player == "X": 127 | current_player = "O" 128 | elif current_player == "O": 129 | current_player = "X" 130 | 131 | # call the function 132 | play_game() 133 | --------------------------------------------------------------------------------