Skip to content

Instantly share code, notes, and snippets.

@akhildaphara
Created May 5, 2021 13:27
Show Gist options
  • Save akhildaphara/3a530b8d0366945c9315950182c17834 to your computer and use it in GitHub Desktop.
Save akhildaphara/3a530b8d0366945c9315950182c17834 to your computer and use it in GitHub Desktop.
TicTacToe.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"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.7.4"
},
"colab": {
"name": "TicTacToe.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/akhildaphara/3a530b8d0366945c9315950182c17834/tictactoe.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "TfY7ZhdBcCyH"
},
"source": [
"from IPython.display import clear_output\n",
"def display_board(board):\n",
" clear_output()\n",
" print(board[7]+\"|\"+board[8]+\"|\"+board[9])\n",
" print(board[4]+\"|\"+board[5]+\"|\"+board[6])\n",
" print(board[1]+\"|\"+board[2]+\"|\"+board[3])"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "0rCvVuMNcCyL",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "513d2be6-c784-4403-fed9-a4867ed5155e"
},
"source": [
"test_board=[' ']*10\n",
"display_board(test_board)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": [
" | | \n",
" | | \n",
" | | \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "HbRYZ2IDcCyO"
},
"source": [
"def player_input():\n",
" marker = ''\n",
" \n",
" while not (marker == 'X' or marker == 'O'):\n",
" marker = input('Player 1: Do you want to be X or O? ').upper()\n",
"\n",
" if marker == 'X':\n",
" return ('X', 'O')\n",
" else:\n",
" return ('O', 'X')"
],
"execution_count": 11,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "RGA8qY9dcCyR",
"collapsed": true
},
"source": [
"# player1_marker,player2_marker=player_input()"
],
"execution_count": 12,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8s84Me69cCyX"
},
"source": [
"def place_marker(board, marker, position):\n",
" board[position] = marker"
],
"execution_count": 14,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_q0-xZyQcCya"
},
"source": [
"place_marker(test_board,' ',2)"
],
"execution_count": 15,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "mOwYj06pcCyg"
},
"source": [
"def win_check(board,mark):\n",
" \n",
" return ((board[7] == mark and board[8] == mark and board[9] == mark) or \n",
" (board[4] == mark and board[5] == mark and board[6] == mark) or \n",
" (board[1] == mark and board[2] == mark and board[3] == mark) or \n",
" (board[7] == mark and board[4] == mark and board[1] == mark) or \n",
" (board[8] == mark and board[5] == mark and board[2] == mark) or \n",
" (board[9] == mark and board[6] == mark and board[3] == mark) or \n",
" (board[7] == mark and board[5] == mark and board[3] == mark) or \n",
" (board[9] == mark and board[5] == mark and board[1] == mark)) "
],
"execution_count": 17,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "8BcNSOGhcCyi",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "320f7c3e-774d-45a3-eba0-56cb8ee446b3"
},
"source": [
"win_check(test_board,' ')"
],
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"True"
]
},
"metadata": {
"tags": []
},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "5fGKHHPqcCyl"
},
"source": [
"import random\n",
"\n",
"def choose_first():\n",
" if random.randint(0,1) == 0:\n",
" return 'Player 2'\n",
" else:\n",
" return 'Player 1'"
],
"execution_count": 19,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "vcfKQIoTcCyt"
},
"source": [
"def space_check(board, position):\n",
" \n",
" return board[position] == ' '"
],
"execution_count": 21,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "WLDsILdWcCyw"
},
"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"
],
"execution_count": 22,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "kNJtr2PDcCyy"
},
"source": [
"def player_choice(board):\n",
" position = 0\n",
" \n",
" while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n",
" position = int(input('Choose your next position: (1-9) '))\n",
" \n",
" return position"
],
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9fMnNli4cCy1"
},
"source": [
"def replay():\n",
" \n",
" return input('Do you want to play again? Enter Yes or No: ').lower().startswith('y')"
],
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Zjvi_uwkcCy4",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "45e0b76b-3ade-44d5-b339-0e9f4ad14961"
},
"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! You have 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",
" \n",
" break\n",
" else:\n",
" turn = 'Player 1'\n",
"\n",
" if not replay():\n",
" break"
],
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": [
" | | \n",
"X|X| \n",
"O|O|O\n",
"Player 2 has won!\n",
"Do you want to play again? Enter Yes or No: no\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment