Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions Tic Tac Toe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Welcome to the Tic Tac Toe Game!\n",
"\n",
"User is Player 1. User goes first!\n",
"\n",
" | 0 | 1 | 2 | \n",
" | 3 | 4 | 5 | \n",
" | 6 | 7 | 8 | \n",
"\n",
"Above you can see the positions you may choose to select your move. Good Luck!\n",
"\n",
" | - | - | - | \n",
" | - | - | - | \n",
" | - | - | - | \n",
"User, choose a number between 0-8 to select the spot you want to mark: 3\n",
" | - | - | - | \n",
" | X | - | - | \n",
" | - | - | - | \n",
"numbers selected by user: [3]\n",
"Computer chooses 8\n",
"numbers selected by computer: [8]\n",
" | - | - | - | \n",
" | X | - | - | \n",
" | - | - | O | \n",
"No Winner!!!\n",
"User, choose a number between 0-8 to select the spot you want to mark: 1\n",
" | - | X | - | \n",
" | X | - | - | \n",
" | - | - | O | \n",
"numbers selected by user: [1, 3]\n",
"Computer chooses 7\n",
"numbers selected by computer: [7, 8]\n",
" | - | X | - | \n",
" | X | - | - | \n",
" | - | O | O | \n",
"No Winner!!!\n",
"User, choose a number between 0-8 to select the spot you want to mark: 5\n",
" | - | X | - | \n",
" | X | - | X | \n",
" | - | O | O | \n",
"numbers selected by user: [1, 3, 5]\n",
"Computer chooses 4\n",
"numbers selected by computer: [4, 7, 8]\n",
" | - | X | - | \n",
" | X | O | X | \n",
" | - | O | O | \n",
"No Winner!!!\n",
"User, choose a number between 0-8 to select the spot you want to mark: 0\n",
" | X | X | - | \n",
" | X | O | X | \n",
" | - | O | O | \n",
"numbers selected by user: [0, 1, 3, 5]\n",
"Computer chooses 2\n",
"numbers selected by computer: [2, 4, 7, 8]\n",
" | X | X | O | \n",
" | X | O | X | \n",
" | - | O | O | \n",
"No Winner!!!\n",
"User, choose a number between 0-8 to select the spot you want to mark: 6\n",
" | X | X | O | \n",
" | X | O | X | \n",
" | X | O | O | \n",
"numbers selected by user: [0, 1, 3, 5, 6]\n"
]
},
{
"ename": "IndexError",
"evalue": "Cannot choose from an empty sequence",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-1-2d20385e5893>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 85\u001b[0m \u001b[0mthe_winner\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"computer wins!\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 86\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mthe_winner\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 87\u001b[1;33m \u001b[0mgame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-1-2d20385e5893>\u001b[0m in \u001b[0;36mgame\u001b[1;34m()\u001b[0m\n\u001b[0;32m 66\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 67\u001b[0m \u001b[1;31m#Process by which the Computer Choice is assigned to a specific position of the game\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 68\u001b[1;33m \u001b[0mcc\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchoice\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnum_selected\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 69\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Computer chooses\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 70\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m~\\Anaconda3\\lib\\random.py\u001b[0m in \u001b[0;36mchoice\u001b[1;34m(self, seq)\u001b[0m\n\u001b[0;32m 259\u001b[0m \u001b[0mi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_randbelow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseq\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 260\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 261\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Cannot choose from an empty sequence'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 262\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mseq\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 263\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mIndexError\u001b[0m: Cannot choose from an empty sequence"
]
}
],
"source": [
"## Tic Tac Toe Game\n",
"import random\n",
"print(\"\\n\")\n",
"print(\"Welcome to the Tic Tac Toe Game!\\n\")\n",
"print(\"User is Player 1. User goes first!\\n\")\n",
"\n",
"#Player 1 will go as \"X\" and computer will go as \"O\"\n",
"user_turn=\"X\"\n",
"cpu_turn=\"O\"\n",
"\n",
"#A locked Board will print as follows: a number will be positionated at the exact sport where Player 1 or computer want to go.\n",
"print(\" | \" + \"0\" + \" | \" + \"1\" + \" | \" + \"2\" + \" | \")\n",
"print(\" | \" + \"3\" + \" | \" + \"4\" + \" | \" + \"5\" + \" | \")\n",
"print(\" | \" + \"6\" + \" | \" + \"7\" + \" | \" + \"8\" + \" | \\n\")\n",
"print(\"Above you can see the positions you may choose to select your move. Good Luck!\\n\")\n",
"\n",
"#The \"Playable\" board will be filled with \"-\" on empty positions within the board\n",
"board_list=[\"-\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\",\"-\"]\n",
"def board():\n",
" print(\" | \" + board_list[0] + \" | \" + board_list[1] + \" | \" + board_list[2] + \" | \")\n",
" print(\" | \" + board_list[3] + \" | \" + board_list[4] + \" | \" + board_list[5] + \" | \")\n",
" print(\" | \" + board_list[6] + \" | \" + board_list[7] + \" | \" + board_list[8] + \" | \")\n",
"\n",
"#This are the numbers that the Player 1 or the computer can chose from: 0-8 in locked board\n",
"num_selected=[0,1,2,3,4,5,6,7,8]\n",
"\n",
"#When Players selects a number, it will pop from the num_selected list and will be append to uc_score or cc_score. \n",
"#Below we have all possible combination for winning.\n",
"winning_combo=[[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [2,4,6], [0,4,8]]\n",
"uc_score=[]\n",
"cc_score=[]\n",
"\n",
"#Actual function of the game\n",
"def game():\n",
" #First thing is to show the board where the game will be played\n",
" board()\n",
" \n",
" #Nº of max rounds the game can last = 9\n",
" maximum_rounds = 9\n",
" \n",
" #Process in which the game knows if there is a winner in the game\n",
" the_winner = \"No Winner!!!\"\n",
" winners_list = [\"computer wins\", \"User Wins\"]\n",
" while the_winner not in winners_list:\n",
" \n",
" #rounds counter\n",
" maximum_rounds=maximum_rounds-1\n",
" \n",
" #User Choice to be played\n",
" uc=int(input(\"User, choose a number between 0-8 to select the spot you want to mark: \"))\n",
" \n",
" #while loop for the user to choose a number between 0-9\n",
" while uc not in num_selected:\n",
" print(\"Number selected already chosen, please try with another one\")\n",
" uc=int(input(\"User, choose a number between 0-8 to select the spot you want to mark: \"))\n",
"\n",
" #Process by which the User Choice is assigned to a specific position of the game \n",
" board_list[uc]=user_turn\n",
" board()\n",
" \n",
" #Process by which the User Choice is removed from num_selec and added to a list of User Choices \n",
" uc_score.append(uc)\n",
" uc_score.sort()\n",
" print(\"numbers selected by user:\",uc_score)\n",
" num_selected.remove(uc) \n",
" \n",
" #Process by which the Computer Choice is assigned to a specific position of the game \n",
" cc=random.choice(num_selected) \n",
" print(\"Computer chooses\",cc)\n",
" \n",
" #Process by which the Computer Choice is removed from num_selec and added to a list of Computer Choices\n",
" cc_score.append(cc)\n",
" cc_score.sort()\n",
" print(\"numbers selected by computer:\",cc_score)\n",
" board_list[cc]=cpu_turn\n",
" board()\n",
" num_selected.remove(cc)\n",
" \n",
" #Process to verify if either the User or Computer has any combination of choices to win the game\n",
" if len(uc_score) >= 3:\n",
" for combo in winning_combo:\n",
" if set(combo).issubset(set(uc_score)):\n",
" the_winner = \"User Wins!\"\n",
" if set(combo).issubset(set(cc_score)):\n",
" the_winner = \"computer wins!\"\n",
" print(the_winner)\n",
"game()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}