5 |
6 |
7 |
8 | # Bulls and Cows with AI  
9 |
10 | In this class, you'll learn how to create **[Bulls and Cows](https://www.youtube.com/watch?v=r_dw8iV_52g&t=160s&ab_channel=GamesOnPaper)** with AI using python
11 |
12 |
13 |
14 |
15 |
16 |
17 | ## Prerequisites:
18 |
19 | 1. Python Basics
20 | 2. Python Advance
21 |
22 | ---
23 |
24 |
25 | ## Necessary Features:
26 |
27 | Adding a simple AI to the Bulls and Cows Game:
28 | 1. Player vs. AI (1 - player mode)
29 |
30 |
31 | ---
32 |
33 | ## Frequently asked questions ❔
34 |
35 | ### How can I thank you for writing and sharing this tutorial? 🌷
36 |
37 | You can and Starring and Forking is free for you, but it tells me and other people that it was helpful and you like this tutorial.
38 |
39 | Go [**`here`**](https://github.com/milaan9/92_Python_Games) if you aren't here already and click ➞ **`✰ Star`** and **`ⵖ Fork`** button in the top right corner. You'll be asked to create a GitHub account if you don't already have one.
40 |
41 | ---
42 |
43 | ### How can I read this tutorial without an Internet connection?
44 |
45 | 1. Go [**`here`**](https://github.com/milaan9/92_Python_Games) and click the big green ➞ **`Code`** button in the top right of the page, then click ➞ [**`Download ZIP`**](https://github.com/milaan9/92_Python_Games/archive/refs/heads/main.zip).
46 |
47 | 
48 |
49 | 3. Extract the ZIP and open it. Unfortunately I don't have any more specific instructions because how exactly this is done depends on which operating system you run.
50 |
51 | 4. Launch ipython notebook from the folder which contains the notebooks. Open each one of them
52 |
53 | `Kernel > Restart & Clear Output`
54 |
55 | This will clear all the outputs and now you can understand each statement and learn interactively.
56 |
57 | If you have git and you know how to use it, you can also clone the repository instead of downloading a zip and extracting it. An advantage with doing it this way is that you don't need to download the whole tutorial again to get the latest version of it, all you need to do is to pull with git and run ipython notebook again.
58 |
59 | ---
60 |
61 | ## Authors ✍️
62 |
63 | I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏
64 |
65 | See [github's contributors page](https://github.com/milaan9/91_Python_Mini_Projects/graphs/contributors) for details.
66 |
67 | If you have trouble with this tutorial please tell me about it by [Create an issue on GitHub](https://github.com/milaan9/91_Python_Mini_Projects/issues/new). and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one.
68 |
69 | If you like this tutorial, please [give it a ⭐ star](https://github.com/milaan9/91_Python_Mini_Projects).
70 |
71 | ---
72 |
73 | ## Licence 📜
74 |
75 | You may use this tutorial freely at your own risk. See [LICENSE](https://github.com/milaan9/91_Python_Mini_Projects/blob/main/LICENSE).
76 |
77 | Copyright (c) 2020 Dr. Milaan Parmar
78 |
79 | ---
80 |
81 |
82 |
Connect with me
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/001_Bulls_and_Cows_with_AI/img/bull.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/001_Bulls_and_Cows_with_AI/img/cow.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/001_Bulls_and_Cows_with_AI/img/output_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/milaan9/92_Python_Games/fd57f30c38f96d7d1419973c901143193f702f6d/001_Bulls_and_Cows_with_AI/img/output_1.png
--------------------------------------------------------------------------------
/001_Bulls_and_Cows_with_AI/img/output_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/milaan9/92_Python_Games/fd57f30c38f96d7d1419973c901143193f702f6d/001_Bulls_and_Cows_with_AI/img/output_2.png
--------------------------------------------------------------------------------
/002_Tic_Tac_Toe_with_AI/001_Tic_Tac_Toe_with_AI.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "\n",
8 | "All the IPython Notebooks in this **Python Games** series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/92_Python_Games)**\n",
9 | ""
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Tic-Tac-Toe with AI\n",
17 | "\n",
18 | "Adding a simple AI to the Tic-Tac-Toe Game\n",
19 | "\n",
20 | "**3 modes:**\n",
21 | "* 👱 🆚 🤖 (1 - player mode)\n",
22 | "* 👱 🆚 👱♀️ (2 - player mode)\n",
23 | "* 🤖 🆚 🤖 (for fun)"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 1,
29 | "metadata": {
30 | "ExecuteTime": {
31 | "end_time": "2021-08-10T09:46:38.923352Z",
32 | "start_time": "2021-08-10T09:45:56.091269Z"
33 | }
34 | },
35 | "outputs": [
36 | {
37 | "name": "stdout",
38 | "output_type": "stream",
39 | "text": [
40 | "Press ENTER↩ to start▶\n",
41 | "\n",
42 | "Welcome! Let's play▶ TIC-TAC-TOE\n",
43 | "\n",
44 | "The 3 x 3 board will look like below: ⤵\n",
45 | "\n",
46 | " 1 | 2 | 3 \n",
47 | "-----------\n",
48 | " 4 | 5 | 6 \n",
49 | "-----------\n",
50 | " 7 | 8 | 9 \n",
51 | "\n",
52 | "You just have to Enter↩ the position(1-9).\n",
53 | "\n",
54 | "0️⃣➡ 👱 🆚 🤖\n",
55 | "1️⃣➡ 👱 🆚 👱♀️\n",
56 | "2️⃣➡ 🤖 🆚 🤖\n",
57 | "\n",
58 | "Select↩ an option 0, 1, or 2: 0\n",
59 | "\n",
60 | "Enter↩ NAME of 👱 who will go against the 🤖: Milaan\n",
61 | "\n",
62 | "Milaan, Do you want to be ❌ or ⭕?: x\n",
63 | "\n",
64 | "👱Milaan: X\n",
65 | "🤖: O\n",
66 | "\n",
67 | "Milaan will go first!☝️\n",
68 | "\n",
69 | "Are you ready to play▶ the game? Enter↩ [Y]es or [N]o: y\n",
70 | " | | 1 | 2 | 3 \n",
71 | " ----------- -----------\n",
72 | " | | 4 | 5 | 6 \n",
73 | " ----------- -----------\n",
74 | " | | 7 | 8 | 9 \n",
75 | "\n",
76 | "Milaan (X), Choose your next position: (1-9): 9\n",
77 | "\n",
78 | "\n",
79 | " | | 1 | 2 | 3 \n",
80 | " ----------- -----------\n",
81 | " | | 4 | 5 | 6 \n",
82 | " ----------- -----------\n",
83 | " | | X 7 | 8 | \n",
84 | "\n",
85 | "🤖 (O) have chosen position: 3\n",
86 | "\n",
87 | " | | O 1 | 2 | \n",
88 | " ----------- -----------\n",
89 | " | | 4 | 5 | 6 \n",
90 | " ----------- -----------\n",
91 | " | | X 7 | 8 | \n",
92 | "\n",
93 | "Milaan (X), Choose your next position: (1-9): 7\n",
94 | "\n",
95 | "\n",
96 | " | | O 1 | 2 | \n",
97 | " ----------- -----------\n",
98 | " | | 4 | 5 | 6 \n",
99 | " ----------- -----------\n",
100 | " X | | X | 8 | \n",
101 | "\n",
102 | "🤖 (O) have chosen position: 8\n",
103 | "\n",
104 | " | | O 1 | 2 | \n",
105 | " ----------- -----------\n",
106 | " | | 4 | 5 | 6 \n",
107 | " ----------- -----------\n",
108 | " X | O | X | | \n",
109 | "\n",
110 | "Milaan (X), Choose your next position: (1-9): 1\n",
111 | "\n",
112 | "\n",
113 | " X | | O | 2 | \n",
114 | " ----------- -----------\n",
115 | " | | 4 | 5 | 6 \n",
116 | " ----------- -----------\n",
117 | " X | O | X | | \n",
118 | "\n",
119 | "🤖 (O) have chosen position: 4\n",
120 | "\n",
121 | " X | | O | 2 | \n",
122 | " ----------- -----------\n",
123 | " O | | | 5 | 6 \n",
124 | " ----------- -----------\n",
125 | " X | O | X | | \n",
126 | "\n",
127 | "Milaan (X), Choose your next position: (1-9): 5\n",
128 | "\n",
129 | "\n",
130 | " X | | O | 2 | \n",
131 | " ----------- -----------\n",
132 | " O | X | | | 6 \n",
133 | " ----------- -----------\n",
134 | " X | O | X | | \n",
135 | "\n",
136 | "************************************************\n",
137 | "\n",
138 | "CONGRATULATIONS Milaan! You've won the Game! 🏆\n",
139 | "\n",
140 | "\n",
141 | "Do you want to play again? 🔁 Enter↩ [Y]es or [N]o: n\n",
142 | "\n",
143 | "\n",
144 | "\t\t\t🏵THE END🏵\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "####\tTic-Tac-Toe with AI ####\n",
150 | "\n",
151 | "# Functions\n",
152 | "def default():\n",
153 | " # Display welcome message\n",
154 | " print(\"\\nWelcome! Let's play▶ TIC-TAC-TOE\\n\")\n",
155 | "\n",
156 | "def rules():\n",
157 | " print(\"The 3 x 3 board will look like below: ⤵\\n\")\n",
158 | " print(\" 1 | 2 | 3 \")\n",
159 | " print(\"-----------\")\n",
160 | " print(\" 4 | 5 | 6 \")\n",
161 | " print(\"-----------\")\n",
162 | " print(\" 7 | 8 | 9 \")\n",
163 | " #print(\"The positions of this 3 x 3 board is same as the right side of your key board.\\n\")\n",
164 | " print(\"\\nYou just have to Enter↩ the position(1-9).\")\n",
165 | "\n",
166 | "def play():\n",
167 | " # Asking if the user is ready\n",
168 | " return input(\"\\nAre you ready to play▶ the game? Enter↩ [Y]es or [N]o: \").upper().startswith('Y')\n",
169 | "\n",
170 | "def names():\n",
171 | " # Input players names\n",
172 | " p1_name=input(\"\\nEnter↩ NAME of 👱: \").capitalize()\n",
173 | " p2_name=input(\"Enter↩ NAME of 👱♀️: \").capitalize()\n",
174 | " return (p1_name, p2_name)\n",
175 | "\n",
176 | "def choice():\n",
177 | " # Input players choice\n",
178 | " p1_choice = \" \"\n",
179 | " p2_choice = \" \"\n",
180 | " \n",
181 | " # while loop: if the entered value isn't X or O\n",
182 | " while p1_choice != \"X\" or p1_choice != \"O\": \n",
183 | " # while loop body begins\n",
184 | " p1_choice = input(f\"\\n{p1_name}, Do you want to be ❌ or ⭕?: \")[0].upper()\n",
185 | " # The input above has [0].upper() in the end,\n",
186 | " # So even if user enters x, X, xxxx or XXX the input will always be taken as X.\n",
187 | " # Hence, increasing the user input window.\n",
188 | "\n",
189 | " if p1_choice == \"X\" or p1_choice == \"O\":\n",
190 | " # if entered value is X or O, end the loop.\n",
191 | " break\n",
192 | " print(\"INVALID🚫 INPUT! Please Try Again!\") \n",
193 | " # if the entered value isn't X or O, restart the while loop.\n",
194 | " # while loop body begins\n",
195 | " \n",
196 | " # Assigning the value to p2 and then diplaying the values\n",
197 | " if p1_choice == \"X\":\n",
198 | " p2_choice = \"O\"\n",
199 | " elif p1_choice == \"O\":\n",
200 | " p2_choice = \"X\" \n",
201 | " return (p1_choice, p2_choice)\n",
202 | "\n",
203 | "def first_player():\n",
204 | " # This function will randomly decide who will play first\n",
205 | " import random\n",
206 | " return random.choice((0, 1))\n",
207 | "\n",
208 | "def display_board(board, avail):\n",
209 | " print(\" \" + \" {} | {} | {} \".format(board[1],board[2],board[3]) + \" \" + \" {} | {} | {} \".format(avail[1],avail[2],avail[3]))\n",
210 | " print(\" \" + \"-----------\" + \" \" + \"-----------\")\n",
211 | " print(\" \" + \" {} | {} | {} \".format(board[4],board[5],board[6]) + \" \" + \" {} | {} | {} \".format(avail[4],avail[5],avail[6]))\n",
212 | " print(\" \" + \"-----------\" + \" \" + \"-----------\")\n",
213 | " print(\" \" + \" {} | {} | {} \".format(board[7],board[8],board[9]) + \" \" + \" {} | {} | {} \".format(avail[7],avail[8],avail[9]))\n",
214 | "\n",
215 | "\n",
216 | "def player_choice(board, name, choice):\n",
217 | " position = 0\n",
218 | " # Initialising position as 0^, so it passes through the while loop\n",
219 | " while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position):\n",
220 | " position = int(input(f\"\\n{name} ({choice}), Choose your next position: (1-9): \"))\n",
221 | " \n",
222 | " if position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, position) or position == \"\": \n",
223 | " # To check whether the given position is in the set [1-9] or whether it is empty or occupied\n",
224 | " print(f\"INVALID🚫 INPUT. Please Try Again!\\n\") \n",
225 | " print(\"\\n\") \n",
226 | " return position\n",
227 | "\n",
228 | "#####################################\n",
229 | "\n",
230 | "# Functions to add AI to game\n",
231 | "def CompAI(board, name, choice):\n",
232 | " position = 0\n",
233 | " possibilities = [x for x, letter in enumerate(board) if letter == \" \" and x != 0]\n",
234 | " \n",
235 | " # Including both X and O, since if AI will win, it will place a choice there, \n",
236 | " # but if the component will win ➡ we have to block that move\n",
237 | " for let in [\"O\", \"X\"]:\n",
238 | " for i in possibilities:\n",
239 | " # Creating a copy of the board everytime, placing the move and checking if it wins\n",
240 | " # Creating a copy like this and not this boardCopy = board, since changes to \n",
241 | " # boardCopy changes the original board\n",
242 | " boardCopy = board[:]\n",
243 | " boardCopy[i] = let\n",
244 | " if(win_check(boardCopy, let)):\n",
245 | " position = i\n",
246 | " return position\n",
247 | "\n",
248 | " openCorners = [x for x in possibilities if x in [1, 3, 7, 9]]\n",
249 | " \n",
250 | " if len(openCorners) > 0:\n",
251 | " position = selectRandom(openCorners)\n",
252 | " return position\n",
253 | "\n",
254 | " if 5 in possibilities:\n",
255 | " position = 5\n",
256 | " return position\n",
257 | "\n",
258 | " openEdges = [x for x in possibilities if x in [2, 4, 6, 8]]\n",
259 | " \n",
260 | " if len(openEdges) > 0:\n",
261 | " position = selectRandom(openEdges)\n",
262 | " return position\n",
263 | "\n",
264 | "def selectRandom(board):\n",
265 | " import random\n",
266 | " ln = len(board)\n",
267 | " r = random.randrange(0,ln)\n",
268 | " return board[r]\n",
269 | "\n",
270 | "def place_marker(board, avail, choice, position):\n",
271 | " # To mark/replace the position on the board list\n",
272 | " board[position] = choice\n",
273 | " avail[position] = \" \"\n",
274 | "\n",
275 | "def space_check(board, position):\n",
276 | " # To check whether the given position is empty or occupied\n",
277 | " return board[position] == \" \"\n",
278 | "\n",
279 | "def full_board_check(board):\n",
280 | " # To check if the board is full, then the game is a draw\n",
281 | " for i in range(1,10):\n",
282 | " if space_check(board, i):\n",
283 | " return False\n",
284 | " return True\n",
285 | "\n",
286 | "\n",
287 | "def win_check(board, choice):\n",
288 | " # To check if one of the following patterns are true and then the respective player has won!;\n",
289 | " \n",
290 | " # Horizontal Check\n",
291 | " return ( \n",
292 | " (board[1] == choice and board[2] == choice and board[3] == choice)\n",
293 | " or (board[4] == choice and board[5] == choice and board[6] == choice)\n",
294 | " or (board[7] == choice and board[8] == choice and board[9] == choice)\n",
295 | " # Vertical Check\n",
296 | " or (board[1] == choice and board[4] == choice and board[7] == choice)\n",
297 | " or (board[2] == choice and board[5] == choice and board[8] == choice)\n",
298 | " or (board[3] == choice and board[6] == choice and board[9] == choice)\n",
299 | " # Diagonal Check\n",
300 | " or (board[1] == choice and board[5] == choice and board[9] == choice)\n",
301 | " or (board[3] == choice and board[5] == choice and board[7] == choice) \n",
302 | " )\n",
303 | "\n",
304 | "def delay(mode):\n",
305 | " if mode == 2:\n",
306 | " import time\n",
307 | " time.sleep(2)\n",
308 | "\n",
309 | "def replay():\n",
310 | " # If the users want to play the game again?\n",
311 | " return input(\"\\nDo you want to play again? 🔁 Enter↩ [Y]es or [N]o: \").lower().startswith(\"y\")\n",
312 | "\n",
313 | "\n",
314 | "# Main Program begins\n",
315 | "input(\"Press ENTER↩ to start▶\")\n",
316 | "default()\n",
317 | "rules()\n",
318 | "\n",
319 | "#####################################\n",
320 | "\n",
321 | "while True:\n",
322 | " \n",
323 | " # Creating the board as a list to be kept replacing it with user input\n",
324 | " theBoard = [\" \"]*10\n",
325 | " \n",
326 | " # Creating the available options on the board\n",
327 | " available = [str(num) for num in range(0,10)] # a List Comprehension\n",
328 | " # available ➡ \"0 1 2 3 4 5 6 7 8 9\"\n",
329 | " \n",
330 | " print(\"\\n0️⃣➡ 👱 🆚 🤖\")\n",
331 | " print(\"1️⃣➡ 👱 🆚 👱♀️\")\n",
332 | " print(\"2️⃣➡ 🤖 🆚 🤖\")\n",
333 | " mode = int(input(\"\\nSelect↩ an option 0, 1, or 2: \"))\n",
334 | " \n",
335 | " # Mode for 👱 🆚 🤖\n",
336 | " if mode == 0: \n",
337 | " p1_name = input(\"\\nEnter↩ NAME of 👱 who will go against the 🤖: \").capitalize()\n",
338 | " p2_name = \"🤖\"\n",
339 | " # Asking Choices Printing choices➡ X or O\n",
340 | " p1_choice, p2_choice = choice()\n",
341 | " print(f\"\\n👱{p1_name}:\", p1_choice)\n",
342 | " print(f\"{p2_name}:\", p2_choice)\n",
343 | "\n",
344 | " # Mode for 👱 🆚 👱♀️\n",
345 | " elif mode == 1:\n",
346 | " # Asking Names\n",
347 | " p1_name, p2_name = names()\n",
348 | " # Asking Choices Printing choices➡ X or O\n",
349 | " p1_choice, p2_choice = choice()\n",
350 | " print(f\"\\n👱{p1_name}:\", p1_choice)\n",
351 | " print(f\"👱♀️{p2_name}:\", p2_choice)\n",
352 | " \n",
353 | " # Mode for 🤖 🆚 🤖\n",
354 | " else:\n",
355 | " p1_name = \"🤖1️⃣\"\n",
356 | " p2_name = \"🤖2️⃣\"\n",
357 | " p1_choice, p2_choice = \"X\", \"O\"\n",
358 | " print(f\"\\n{p1_name}:\", p1_choice)\n",
359 | " print(f\"\\n{p2_name}:\", p2_choice)\n",
360 | " \n",
361 | " # Printing randomly who will go first\n",
362 | " if first_player():\n",
363 | " turn = p2_name\n",
364 | " else:\n",
365 | " turn = p1_name\n",
366 | " print(f\"\\n{turn} will go first!☝️\")\n",
367 | " \n",
368 | " # The user, if ready to play the game, output will be True or False\n",
369 | " if(mode == 2):\n",
370 | " ent = input(\"\\nThis is going to be fast!⚡ Press Enter↩ for the battle⚔ to begin!\\n\")\n",
371 | " play_game = 1\n",
372 | " else:\n",
373 | " play_game = play() \n",
374 | " \n",
375 | " while play_game:\n",
376 | " \n",
377 | "#####################################\n",
378 | " # Player_1\n",
379 | " if turn == p1_name:\n",
380 | " \n",
381 | " # Displaying the board\n",
382 | " display_board(theBoard, available)\n",
383 | "\n",
384 | " # Position of the input\n",
385 | " if mode != 2:\n",
386 | " position = player_choice(theBoard, p1_name, p1_choice)\n",
387 | " else:\n",
388 | " position = CompAI(theBoard, p1_name, p1_choice)\n",
389 | " print(f\"\\n{p1_name} ({p1_choice}) have chosen position: {position}\\n\")\n",
390 | " \n",
391 | " # Replacing the \" \" at *position* to *p1_choice* in *theBoard* list\n",
392 | " place_marker(theBoard, available, p1_choice, position)\n",
393 | " \n",
394 | " # To check if Player_1 has won after the current input\n",
395 | " if win_check(theBoard, p1_choice):\n",
396 | " display_board(theBoard, available)\n",
397 | " print(\"\\n************************************************\")\n",
398 | " if(mode>=0):\n",
399 | " print(f\"\\nCONGRATULATIONS {p1_name}! You've won the Game! 🏆\\n\")\n",
400 | " play_game = False\n",
401 | " \n",
402 | " else:\n",
403 | " # To check if the board is full, if yes, the game is a draw\n",
404 | " if full_board_check(theBoard):\n",
405 | " display_board(theBoard, available)\n",
406 | " print(\"******************\")\n",
407 | " print(\"\\nThe game is a DRAW ⚖!\\n\")\n",
408 | " print(\"******************\")\n",
409 | " break\n",
410 | " # If none of the above is possible, next turn of Player_2 \n",
411 | " else:\n",
412 | " turn = p2_name\n",
413 | " \n",
414 | "#####################################\n",
415 | " # Player_2 \n",
416 | " elif turn == p2_name:\n",
417 | " \n",
418 | " # Displaying the board\n",
419 | " display_board(theBoard, available)\n",
420 | "\n",
421 | " # Position of the input\n",
422 | " if(mode == 1):\n",
423 | " position = player_choice(theBoard, p2_name, p2_choice)\n",
424 | " else:\n",
425 | " position = CompAI(theBoard, p2_name, p2_choice)\n",
426 | " print(f\"\\n{p2_name} ({p2_choice}) have chosen position: {position}\\n\")\n",
427 | " \n",
428 | " # Replacing the \" \" at *position* to *p2_choice* in *theBoard* list\n",
429 | " place_marker(theBoard, available, p2_choice, position)\n",
430 | " \n",
431 | " # To check if Player_2 has won after the current input\n",
432 | " if win_check(theBoard, p2_choice):\n",
433 | " display_board(theBoard, available)\n",
434 | " print(\"\\n************************************************\")\n",
435 | " if(mode):\n",
436 | " print(f\"\\nCONGRATULATIONS {p2_name}! You've won the Game! 🏆\\n\")\n",
437 | " else:\n",
438 | " print(\"\\nTHE 🤖 has won the Game! 🏆\\n\")\n",
439 | " print(\"************************************************\")\n",
440 | " play_game = False\n",
441 | " \n",
442 | " else:\n",
443 | " # To check if the board is full, if yes, the game is a draw\n",
444 | " if full_board_check(theBoard):\n",
445 | " display_board(theBoard, available)\n",
446 | " print(\"******************\")\n",
447 | " print(\"\\nThe game is a DRAW ⚖! 😑\\n\")\n",
448 | " print(\"******************\")\n",
449 | " break\n",
450 | " # If none of the above is possible, next turn of Player_2 \n",
451 | " else:\n",
452 | " turn = p1_name \n",
453 | " \n",
454 | " # If the users want to play the game again? \n",
455 | " if replay():\n",
456 | " # if Yes\n",
457 | " continue\n",
458 | " else:\n",
459 | " # if No\n",
460 | " break \n",
461 | "\n",
462 | "# Ending message\n",
463 | "print(\"\\n\\n\\t\\t\\t🏵THE END🏵\") "
464 | ]
465 | },
466 | {
467 | "cell_type": "code",
468 | "execution_count": null,
469 | "metadata": {},
470 | "outputs": [],
471 | "source": []
472 | }
473 | ],
474 | "metadata": {
475 | "hide_input": false,
476 | "kernelspec": {
477 | "display_name": "Python 3",
478 | "language": "python",
479 | "name": "python3"
480 | },
481 | "language_info": {
482 | "codemirror_mode": {
483 | "name": "ipython",
484 | "version": 3
485 | },
486 | "file_extension": ".py",
487 | "mimetype": "text/x-python",
488 | "name": "python",
489 | "nbconvert_exporter": "python",
490 | "pygments_lexer": "ipython3",
491 | "version": "3.8.8"
492 | },
493 | "toc": {
494 | "base_numbering": 1,
495 | "nav_menu": {},
496 | "number_sections": true,
497 | "sideBar": true,
498 | "skip_h1_title": false,
499 | "title_cell": "Table of Contents",
500 | "title_sidebar": "Contents",
501 | "toc_cell": false,
502 | "toc_position": {},
503 | "toc_section_display": true,
504 | "toc_window_display": false
505 | },
506 | "varInspector": {
507 | "cols": {
508 | "lenName": 16,
509 | "lenType": 16,
510 | "lenVar": 40
511 | },
512 | "kernels_config": {
513 | "python": {
514 | "delete_cmd_postfix": "",
515 | "delete_cmd_prefix": "del ",
516 | "library": "var_list.py",
517 | "varRefreshCmd": "print(var_dic_list())"
518 | },
519 | "r": {
520 | "delete_cmd_postfix": ") ",
521 | "delete_cmd_prefix": "rm(",
522 | "library": "var_list.r",
523 | "varRefreshCmd": "cat(var_dic_list()) "
524 | }
525 | },
526 | "types_to_exclude": [
527 | "module",
528 | "function",
529 | "builtin_function_or_method",
530 | "instance",
531 | "_Feature"
532 | ],
533 | "window_display": false
534 | }
535 | },
536 | "nbformat": 4,
537 | "nbformat_minor": 4
538 | }
539 |
--------------------------------------------------------------------------------
/002_Tic_Tac_Toe_with_AI/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | # Tic-Tac-Toe with AI
9 |
10 | In this class, you'll learn how to create Tic-Tac-Toe with AI using python
11 |
12 |
13 |
14 |
15 |
16 |
17 | ## Prerequisites:
18 |
19 | 1. Python Basics
20 | 2. Python Advance
21 |
22 | ---
23 |
24 |
25 | ## Necessary Features:
26 |
27 | Add simple AI with total 3 modes of play:
28 | 1. 👱 vs 👦 (2 - player mode)
29 | 2. 👱 vs 🤖 (1 - player mode)
30 | 3. 🤖 vs 🤖 (AI mode)
31 |
32 |
33 | ---
34 |
35 | ## Frequently asked questions ❔
36 |
37 | ### How can I thank you for writing and sharing this tutorial? 🌷
38 |
39 | You can and Starring and Forking is free for you, but it tells me and other people that it was helpful and you like this tutorial.
40 |
41 | Go [**`here`**](https://github.com/milaan9/92_Python_Games) if you aren't here already and click ➞ **`✰ Star`** and **`ⵖ Fork`** button in the top right corner. You'll be asked to create a GitHub account if you don't already have one.
42 |
43 | ---
44 |
45 | ### How can I read this tutorial without an Internet connection?
46 |
47 | 1. Go [**`here`**](https://github.com/milaan9/92_Python_Games) and click the big green ➞ **`Code`** button in the top right of the page, then click ➞ [**`Download ZIP`**](https://github.com/milaan9/92_Python_Games/archive/refs/heads/main.zip).
48 |
49 | 
50 |
51 | 3. Extract the ZIP and open it. Unfortunately I don't have any more specific instructions because how exactly this is done depends on which operating system you run.
52 |
53 | 4. Launch ipython notebook from the folder which contains the notebooks. Open each one of them
54 |
55 | `Kernel > Restart & Clear Output`
56 |
57 | This will clear all the outputs and now you can understand each statement and learn interactively.
58 |
59 | If you have git and you know how to use it, you can also clone the repository instead of downloading a zip and extracting it. An advantage with doing it this way is that you don't need to download the whole tutorial again to get the latest version of it, all you need to do is to pull with git and run ipython notebook again.
60 |
61 | ---
62 |
63 | ## Authors ✍️
64 |
65 | I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏
66 |
67 | See [github's contributors page](https://github.com/milaan9/91_Python_Mini_Projects/graphs/contributors) for details.
68 |
69 | If you have trouble with this tutorial please tell me about it by [Create an issue on GitHub](https://github.com/milaan9/91_Python_Mini_Projects/issues/new). and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one.
70 |
71 | If you like this tutorial, please [give it a ⭐ star](https://github.com/milaan9/91_Python_Mini_Projects).
72 |
73 | ---
74 |
75 | ## Licence 📜
76 |
77 | You may use this tutorial freely at your own risk. See [LICENSE](https://github.com/milaan9/91_Python_Mini_Projects/blob/main/LICENSE).
78 |
79 | Copyright (c) 2020 Dr. Milaan Parmar
80 |
81 | ---
82 |
83 |
84 |
Connect with me
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------
/002_Tic_Tac_Toe_with_AI/img/output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/milaan9/92_Python_Games/fd57f30c38f96d7d1419973c901143193f702f6d/002_Tic_Tac_Toe_with_AI/img/output.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 milaan9
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
21 |
22 | # 92_Python_Games 🎮
23 |
24 | ## Introduction 👋
25 |
26 | This repository contains Python games that I've worked on. You'll learn how to create python games with AI. I try to focus on creating board games without GUI in Jupyter-notebook.
27 |
28 | ---
29 |
30 | ## Table of contents 📋
31 |
32 | | **No.** | **Name** |
33 | | ------- | -------- |
34 | | 01 | **[Bulls and Cows with AI](https://github.com/milaan9/92_Python_Games/tree/main/001_Bulls_and_Cows_with_AI)** |
35 | | 02 | **[Tic-Tac-Toe with AI](https://github.com/milaan9/92_Python_Games/tree/main/002_Tic_Tac_Toe_with_AI)** |
36 |
37 |
38 | These are online **read-only** versions. However you can **`Run ▶`** all the codes **online** by clicking here ➞
39 |
40 | ---
41 |
42 | ## Frequently asked questions ❔
43 |
44 | ### How can I thank you for writing and sharing this tutorial? 🌷
45 |
46 | You can and Starring and Forking is free for you, but it tells me and other people that it was helpful and you like this tutorial.
47 |
48 | Go [**`here`**](https://github.com/milaan9/92_Python_Games) if you aren't here already and click ➞ **`✰ Star`** and **`ⵖ Fork`** button in the top right corner. You will be asked to create a GitHub account if you don't already have one.
49 |
50 | ---
51 |
52 | ### How can I read this tutorial without an Internet connection?
53 |
54 | 1. Go [**`here`**](https://github.com/milaan9/92_Python_Games) and click the big green ➞ **`Code`** button in the top right of the page, then click ➞ [**`Download ZIP`**](https://github.com/milaan9/92_Python_Games/archive/refs/heads/main.zip).
55 |
56 | 
57 |
58 | 2. Extract the ZIP and open it. Unfortunately I don't have any more specific instructions because how exactly this is done depends on which operating system you run.
59 |
60 | 3. Launch ipython notebook from the folder which contains the notebooks. Open each one of them
61 |
62 | **`Kernel > Restart & Clear Output`**
63 |
64 | This will clear all the outputs and now you can understand each statement and learn interactively.
65 |
66 | If you have git and you know how to use it, you can also clone the repository instead of downloading a zip and extracting it. An advantage with doing it this way is that you don't need to download the whole tutorial again to get the latest version of it, all you need to do is to pull with git and run ipython notebook again.
67 |
68 | ---
69 |
70 | ## Authors ✍️
71 |
72 | I'm Dr. Milaan Parmar and I have written this tutorial. If you think you can add/correct/edit and enhance this tutorial you are most welcome🙏
73 |
74 | See [github's contributors page](https://github.com/milaan9/92_Python_Games/graphs/contributors) for details.
75 |
76 | If you have trouble with this tutorial please tell me about it by [Create an issue on GitHub](https://github.com/milaan9/92_Python_Games/issues/new). and I'll make this tutorial better. This is probably the best choice if you had trouble following the tutorial, and something in it should be explained better. You will be asked to create a GitHub account if you don't already have one.
77 |
78 | If you like this tutorial, please [give it a ⭐ star](https://github.com/milaan9/92_Python_Games).
79 |
80 | ---
81 |
82 | ## Licence 📜
83 |
84 | You may use this tutorial freely at your own risk. See [LICENSE](./LICENSE).
85 |
--------------------------------------------------------------------------------
/img/dnld_rep.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/milaan9/92_Python_Games/fd57f30c38f96d7d1419973c901143193f702f6d/img/dnld_rep.png
--------------------------------------------------------------------------------