From ce9ded4064745aefa3f7877bc6c1a24a5d241c8a Mon Sep 17 00:00:00 2001 From: Emanuele Pireddu Date: Fri, 4 Jun 2021 13:29:33 +0200 Subject: [PATCH] Mastermind - Emanuele --- Kick-Off.md | 94 --- ... - Emanuele Pireddu-Copy1-checkpoint.ipynb | 639 +++++++++++++++++ ...d Game - Emanuele Pireddu-checkpoint.ipynb | 639 +++++++++++++++++ .../Mastermind Game - Emanuele Pireddu.ipynb | 657 ++++++++++++++++++ your-project/README.md | 34 - your-project/README.txt | 5 + 6 files changed, 1940 insertions(+), 128 deletions(-) delete mode 100644 Kick-Off.md create mode 100644 your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-Copy1-checkpoint.ipynb create mode 100644 your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-checkpoint.ipynb create mode 100644 your-project/Mastermind Game - Emanuele Pireddu.ipynb delete mode 100644 your-project/README.md create mode 100644 your-project/README.txt diff --git a/Kick-Off.md b/Kick-Off.md deleted file mode 100644 index f784834..0000000 --- a/Kick-Off.md +++ /dev/null @@ -1,94 +0,0 @@ -Ironhack Logo - -# Build your own game! - -## Content -- [Project Description](#project-description) -- [Games](#games) -- [Project Goals](#project-goals) -- [Requirements & Deliverables](#requirements-&-deliverables) -- [Mentoring](#mentoring) -- [Schedule](#schedule) -- [Presentation](#presentation) - -## Project Description -In this project, you will create your first software! -You are tasked with creating a **game** that interacts with the user, using functions and other basic python resources you have learned during the week. Your game will be able to take an input from the user and provide an output. - -You will also present your game to the class. - -## Games -You will be assigned one of the following games: - -* [Strong Random Password Generator](https://strongpasswordgenerator.com/) -* [Guess The Number](https://www.funbrain.com/games/guess-the-number) -* [Hangman](https://www.coolmathgames.com/0-hangman) -* [Message Encryption & Decryption](https://www.base64decode.org/) -* [Mastermind](https://www.webgamesonline.com/mastermind/) -* [Tic Tac Toe](https://www.google.com/search?q=tic+tac+toe&oq=tic+tac+toe&aqs=chrome..69i57j0l5.1876j0j4&sourceid=chrome&ie=UTF-8) -* [Blackjack](https://bicyclecards.com/how-to-play/blackjack/) -* [Soldier & Dice (Risk)](https://en.wikipedia.org/wiki/Risk_(game)#Attack_and_defense) - -## Project Goals -During this project you will: -* Fork and manage your own git repository. -* Build your own code from scratch. -* Put into practice the basic python concepts learned during the week. -* Get used to public presentations. - -## Requirements & Deliverables -The **mandatory** requirements that this project needs to satisfy are: -* The project must be planned. That is why creating a Kanban board is important. You can either do it manually (e.g. with post-its) or use an online tool like Trello. You can find a template for Trello [here](https://trello.com/b/pc2CNZTo/project-1-build-your-own-game). Remember that you **CAN'T CODE** until your project is planned. -* Your repository must be clean and organized; this means that it must include a *.gitignore* file and a README file and also have a functional file structure. You can easily create a *.gitignore* template [here](https://www.toptal.com/developers/gitignore). -* Your code must include at least one function. -* The project needs to be presented to your colleagues on the day of the presentation. - -The **mandatory** deliverables that you must turn in are: -* Link to the repository you used while working on your project. The repository must include all the files you used to build your game. Remember to commit often to avoid trouble in case you mess up: this means more than 1 commit! -* Link to Trello or picture of your Kanban Board. Include the link or the picture in the README file. - -The **deadline** to turn in the deliverables is right before the project presentations. - -## Mentoring -One of the TAs will be your mentor! - -Your mentor will: -* Keep track of your project in general terms. Your mentor will be the second person that knows more about the project, after you. -* Check if you are following your plan: are you keeping up with your tasks and deadlines? Do you have any obstacles blocking you? -* Help/support you with specific questions. - -Your mentor is **not** meant to: -* Know everything. -* Be your manager. You have to be responsible of your own tasks! - -## Schedule - -**Please note** that the following schedule is simply a guideline. Feel free to organize your work as you see fit. - -**Wednesday** -* Choose the game that you are going to code. -* Plan your project. Remember that we are providing you with a Trello [template](https://trello.com/b/pc2CNZTo/project-1-build-your-own-game). Remember that you **CAN'T CODE** until your project is planned. -* Fork the repository and edit the README overview. You can find a [template](https://github.com/ta-data-bcn/Project-Week-1-Build-Your-Own-Game/blob/master/your-project/README.md) for your README file in this repository. Remember to keep the README up-to-date. -* Once you finish, start coding! Remember to use functions. - -**Thursday** -* Finish the coding and possibly check for bugs in the program (e.g. check what happens if the player gives unexpected inputs to the program). -* In the evening, start preparing the slides for Friday's presentation. - -**Friday** -* Presentation time at **3PM**! There will be a 15-minute break during the presentations. - -## Presentation -You will have **3 minutes** to present your project to the class and then **2 minutes** for Q&A. - -The slides of your presentation must include the content listed below and a demo of your game: - -* Title of the project + Student name -* Description and rules of your game -* Workflow -* Challenges you encountered during the process -* Learnings -* Possible future improvements -* Demo of the game (remember to save time for it) - -Tip: you have only 3 minutes for this presentation so keep it simple! diff --git a/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-Copy1-checkpoint.ipynb b/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-Copy1-checkpoint.ipynb new file mode 100644 index 0000000..efc0011 --- /dev/null +++ b/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-Copy1-checkpoint.ipynb @@ -0,0 +1,639 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4867efce", + "metadata": {}, + "source": [ + "# Mastermind Game by Emthesda" + ] + }, + { + "cell_type": "markdown", + "id": "bf71ca21", + "metadata": {}, + "source": [ + "[How to play](#How-to-play)\n", + "\n", + "\n", + "[Play Game](#Play-Game)\n", + "\n", + "\n", + "[Code: _Variables_](#Variables)\n", + "\n", + "\n", + "[Code: _Functions_](#Functions)\n", + "\n", + "\n", + "[Issue Encoutered](#Issues-Encountered)\n", + "\n", + "[To Improve](#To-Improve)\n", + "\n", + "[Draft](#Draft)\n", + "\n", + "\n", + "[Any Questions?](#Any-Questions?)\n", + "\n", + "[Backup](#Backup)\n", + "\n", + "[Play Original Game](#Game)" + ] + }, + { + "cell_type": "markdown", + "id": "9f50cb02", + "metadata": {}, + "source": [ + "## How to play\n" + ] + }, + { + "cell_type": "markdown", + "id": "bab67939", + "metadata": {}, + "source": [ + "The purpose of the game is to _deduce_ the secret code, achieved through a selection of colored balls.\n", + "\n", + "The difficulty changes the length of the secret sequence.\n", + "\n", + "\n", + "***\n", + "**Easy** = 4 balls
\n", + "**Normal** = 6 balls
\n", + "**Hard** = 8 balls
\n", + "\n", + "***\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "After entering all needed values, the game gives out 2 hints:\n", + "* Number of balls answered that match the correct position.\n", + "* Number of balls answered that are actually in the secret sequence but are not in order." + ] + }, + { + "cell_type": "markdown", + "id": "aada583b", + "metadata": {}, + "source": [ + "## Issues Encountered\n", + "\n", + "\n", + "\n", + "Obviously... _None_ 😋\n", + "\n", + "Now seriously, please don't ask...\n", + "\n", + "Most common errors and problems:\n", + "* Index out of range\n", + "* blabla function is not defined\n", + "* Temptation to \"hard-code\" everything that doesn't work\n", + "* Counters/ Loops tend to infinity \n", + "* Last counter(Balls in answer) not correct\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "a600b255", + "metadata": {}, + "source": [ + "## To Improve\n", + "\n", + "_Well... So many things..._\n", + "\n", + "Things that I didnt use but maybe should've:\n", + "\n", + "* map()\n", + "* list comprehension\n", + "* input.upper so answers are robust but not case sensitive\n", + "* sets - just for the initial balls variable (possible choices are unique and this list shouldn't be modified)\n", + "* less usage of my bff _ctrl c_ & _ctrl v_ (Example: Getting while loop I did in prework to check input type)\n", + "* create a class 'balls' with var color and var position\n", + "* ~~using a victory = True like the snail problem~~ _Done_\n", + "* Use an Except X-Error\n", + "\n", + "Changes and different approaches related to the game itself:\n", + "\n", + "* Interface\n", + "* Theme\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "4ef8ed40", + "metadata": {}, + "source": [ + "## First Draft\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "raw", + "id": "3a141ad7", + "metadata": {}, + "source": [ + "# print? how to know what colour is every letter? as in: 'Please enter R for Red. G for Green ball, etc'\n", + "# 4 example: To enter Red write: R\n", + "R\n", + "R\n", + "\n", + "#First we need to create a list with all the possible options (colours of balls)\n", + "\n", + "balls = ['R', 'B', 'G', 'Y'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "\n", + "#inputs\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#Randomizer\n", + "import random as r\n", + "\n", + "x1 = r.randint(0, len(balls)-1)\n", + "x2 = r.randint(0, len(balls)-1)\n", + "x3 = r.randint(0, len(balls)-1)\n", + "x4 = r.randint(0, len(balls)-1)\n", + "\n", + "print(x1, x2, x3, x4)\n", + "ball1 = balls[x1]\n", + "ball2 = balls[x2]\n", + "ball3 = balls[x3]\n", + "ball4 = balls[x4]\n", + "print(ball1, ball2, ball3, ball4)\n", + "\n", + "print(type(correct_sequence))\n", + "correct_sequence.append(ball1) #or use insert?\n", + "print(correct_sequence)\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#if input is in list balls, continue with the functions created to check position and colour in correct sequence" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "4d2878e1", + "metadata": {}, + "outputs": [], + "source": [ + "#method 0\n", + "#method nº1 = do randomizer in range 0, difficulty_mode -1, 2\n", + "#method nº2 = for i in range 0, diff -1\n", + " #correct sequence[i] == method nº1" + ] + }, + { + "cell_type": "markdown", + "id": "670051a7", + "metadata": {}, + "source": [ + "## Variables\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8403ee70", + "metadata": {}, + "outputs": [], + "source": [ + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() " + ] + }, + { + "cell_type": "markdown", + "id": "f29e079f", + "metadata": {}, + "source": [ + "## Functions\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b30c7cac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if user_input[i] in correct_sequence and user_input[i] != correct_sequence[i]: # ATTENTION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " \n", + " counter_correct_color += 1\n", + "\n", + " \n", + "\n", + " # [R,Y,G,B]\n", + " # [G,Y,B,Y]\n", + " # Posibilidades:\n", + " # 0:Color no en la lista \n", + " # 1:Color en la lista pero no en su sitio\n", + " # 2:Color en su sitio\n", + " #[1,2,1,1]\n", + " \n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "a93a2397", + "metadata": {}, + "source": [ + "## Play Game\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a869a88", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 = Easy mode\n", + "2 = Normal mode\n", + "3 = Hard mode\n", + "Please select difficulty! (Enter 1, 2 or 3) 1\n", + "Easy mode selected\n", + "['R', 'B', 'B', 'B']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):r\n", + "Please enter a valid color:\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):O\n", + "Your choices are ['R', 'R', 'O']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):B\n", + "Your choices are ['R', 'R', 'O', 'B']\n", + "Wrong, please try again.\n", + "2 balls are in the correct position.\n", + "1 balls have been correctly chosen.\n" + ] + } + ], + "source": [ + "play_game()" + ] + }, + { + "cell_type": "markdown", + "id": "f5b41511", + "metadata": {}, + "source": [ + "## Any Questions?\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "19e74c5e", + "metadata": {}, + "source": [ + "## Game\n", + "[Original game example](https://www.webgamesonline.com/mastermind/index.php)\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "cc7db859", + "metadata": {}, + "source": [ + "## Backup\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42337be7", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() \n", + "\n", + "\n", + "\n", + "\n", + "######### End of assigning variables\n", + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + " #JUST ONE FUNCTION\n", + "\n", + "#LAST FUNCTION COMPARE LIST WITH ANOTHER (CORRECT SEQ WITH INPUT LIST) - WE MUST KNOW INTERSECTION OF THEM AND ORDER IF RIGHT = YOU WIN!\n", + " # !!!!! COUNTER WHERE WE KEEP TRACK OF ATTEMPTS (counter) and call again needed_inputs\n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if user_input[i] in correct_sequence: # ATTETION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " \n", + " counter_correct_color += 1\n", + " \n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " # print('CORRECT! YOU WON!')\n", + "\n", + " \n", + "#CREATE METHOD PLAY_GAME? PUT (FUNCTION_PLAYGAME) IN ANOTHER CELL\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + " ################################################# PRINT THIS TO CHECK ANSWER\n", + "#print(needed_inputs(correct_sequence, balls))\n", + "#print(input_list)\n", + "#WTF IS NAME '' IS NOT DEFINED\n", + "\n" + ] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-checkpoint.ipynb b/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-checkpoint.ipynb new file mode 100644 index 0000000..efc0011 --- /dev/null +++ b/your-project/.ipynb_checkpoints/Mastermind Game - Emanuele Pireddu-checkpoint.ipynb @@ -0,0 +1,639 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4867efce", + "metadata": {}, + "source": [ + "# Mastermind Game by Emthesda" + ] + }, + { + "cell_type": "markdown", + "id": "bf71ca21", + "metadata": {}, + "source": [ + "[How to play](#How-to-play)\n", + "\n", + "\n", + "[Play Game](#Play-Game)\n", + "\n", + "\n", + "[Code: _Variables_](#Variables)\n", + "\n", + "\n", + "[Code: _Functions_](#Functions)\n", + "\n", + "\n", + "[Issue Encoutered](#Issues-Encountered)\n", + "\n", + "[To Improve](#To-Improve)\n", + "\n", + "[Draft](#Draft)\n", + "\n", + "\n", + "[Any Questions?](#Any-Questions?)\n", + "\n", + "[Backup](#Backup)\n", + "\n", + "[Play Original Game](#Game)" + ] + }, + { + "cell_type": "markdown", + "id": "9f50cb02", + "metadata": {}, + "source": [ + "## How to play\n" + ] + }, + { + "cell_type": "markdown", + "id": "bab67939", + "metadata": {}, + "source": [ + "The purpose of the game is to _deduce_ the secret code, achieved through a selection of colored balls.\n", + "\n", + "The difficulty changes the length of the secret sequence.\n", + "\n", + "\n", + "***\n", + "**Easy** = 4 balls
\n", + "**Normal** = 6 balls
\n", + "**Hard** = 8 balls
\n", + "\n", + "***\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "After entering all needed values, the game gives out 2 hints:\n", + "* Number of balls answered that match the correct position.\n", + "* Number of balls answered that are actually in the secret sequence but are not in order." + ] + }, + { + "cell_type": "markdown", + "id": "aada583b", + "metadata": {}, + "source": [ + "## Issues Encountered\n", + "\n", + "\n", + "\n", + "Obviously... _None_ 😋\n", + "\n", + "Now seriously, please don't ask...\n", + "\n", + "Most common errors and problems:\n", + "* Index out of range\n", + "* blabla function is not defined\n", + "* Temptation to \"hard-code\" everything that doesn't work\n", + "* Counters/ Loops tend to infinity \n", + "* Last counter(Balls in answer) not correct\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "a600b255", + "metadata": {}, + "source": [ + "## To Improve\n", + "\n", + "_Well... So many things..._\n", + "\n", + "Things that I didnt use but maybe should've:\n", + "\n", + "* map()\n", + "* list comprehension\n", + "* input.upper so answers are robust but not case sensitive\n", + "* sets - just for the initial balls variable (possible choices are unique and this list shouldn't be modified)\n", + "* less usage of my bff _ctrl c_ & _ctrl v_ (Example: Getting while loop I did in prework to check input type)\n", + "* create a class 'balls' with var color and var position\n", + "* ~~using a victory = True like the snail problem~~ _Done_\n", + "* Use an Except X-Error\n", + "\n", + "Changes and different approaches related to the game itself:\n", + "\n", + "* Interface\n", + "* Theme\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "4ef8ed40", + "metadata": {}, + "source": [ + "## First Draft\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "raw", + "id": "3a141ad7", + "metadata": {}, + "source": [ + "# print? how to know what colour is every letter? as in: 'Please enter R for Red. G for Green ball, etc'\n", + "# 4 example: To enter Red write: R\n", + "R\n", + "R\n", + "\n", + "#First we need to create a list with all the possible options (colours of balls)\n", + "\n", + "balls = ['R', 'B', 'G', 'Y'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "\n", + "#inputs\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#Randomizer\n", + "import random as r\n", + "\n", + "x1 = r.randint(0, len(balls)-1)\n", + "x2 = r.randint(0, len(balls)-1)\n", + "x3 = r.randint(0, len(balls)-1)\n", + "x4 = r.randint(0, len(balls)-1)\n", + "\n", + "print(x1, x2, x3, x4)\n", + "ball1 = balls[x1]\n", + "ball2 = balls[x2]\n", + "ball3 = balls[x3]\n", + "ball4 = balls[x4]\n", + "print(ball1, ball2, ball3, ball4)\n", + "\n", + "print(type(correct_sequence))\n", + "correct_sequence.append(ball1) #or use insert?\n", + "print(correct_sequence)\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#if input is in list balls, continue with the functions created to check position and colour in correct sequence" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "4d2878e1", + "metadata": {}, + "outputs": [], + "source": [ + "#method 0\n", + "#method nº1 = do randomizer in range 0, difficulty_mode -1, 2\n", + "#method nº2 = for i in range 0, diff -1\n", + " #correct sequence[i] == method nº1" + ] + }, + { + "cell_type": "markdown", + "id": "670051a7", + "metadata": {}, + "source": [ + "## Variables\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8403ee70", + "metadata": {}, + "outputs": [], + "source": [ + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() " + ] + }, + { + "cell_type": "markdown", + "id": "f29e079f", + "metadata": {}, + "source": [ + "## Functions\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b30c7cac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if user_input[i] in correct_sequence and user_input[i] != correct_sequence[i]: # ATTENTION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " \n", + " counter_correct_color += 1\n", + "\n", + " \n", + "\n", + " # [R,Y,G,B]\n", + " # [G,Y,B,Y]\n", + " # Posibilidades:\n", + " # 0:Color no en la lista \n", + " # 1:Color en la lista pero no en su sitio\n", + " # 2:Color en su sitio\n", + " #[1,2,1,1]\n", + " \n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "a93a2397", + "metadata": {}, + "source": [ + "## Play Game\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a869a88", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 = Easy mode\n", + "2 = Normal mode\n", + "3 = Hard mode\n", + "Please select difficulty! (Enter 1, 2 or 3) 1\n", + "Easy mode selected\n", + "['R', 'B', 'B', 'B']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):r\n", + "Please enter a valid color:\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):O\n", + "Your choices are ['R', 'R', 'O']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):B\n", + "Your choices are ['R', 'R', 'O', 'B']\n", + "Wrong, please try again.\n", + "2 balls are in the correct position.\n", + "1 balls have been correctly chosen.\n" + ] + } + ], + "source": [ + "play_game()" + ] + }, + { + "cell_type": "markdown", + "id": "f5b41511", + "metadata": {}, + "source": [ + "## Any Questions?\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "19e74c5e", + "metadata": {}, + "source": [ + "## Game\n", + "[Original game example](https://www.webgamesonline.com/mastermind/index.php)\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "cc7db859", + "metadata": {}, + "source": [ + "## Backup\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "42337be7", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() \n", + "\n", + "\n", + "\n", + "\n", + "######### End of assigning variables\n", + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + " #JUST ONE FUNCTION\n", + "\n", + "#LAST FUNCTION COMPARE LIST WITH ANOTHER (CORRECT SEQ WITH INPUT LIST) - WE MUST KNOW INTERSECTION OF THEM AND ORDER IF RIGHT = YOU WIN!\n", + " # !!!!! COUNTER WHERE WE KEEP TRACK OF ATTEMPTS (counter) and call again needed_inputs\n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if user_input[i] in correct_sequence: # ATTETION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " \n", + " counter_correct_color += 1\n", + " \n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " # print('CORRECT! YOU WON!')\n", + "\n", + " \n", + "#CREATE METHOD PLAY_GAME? PUT (FUNCTION_PLAYGAME) IN ANOTHER CELL\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + " ################################################# PRINT THIS TO CHECK ANSWER\n", + "#print(needed_inputs(correct_sequence, balls))\n", + "#print(input_list)\n", + "#WTF IS NAME '' IS NOT DEFINED\n", + "\n" + ] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/your-project/Mastermind Game - Emanuele Pireddu.ipynb b/your-project/Mastermind Game - Emanuele Pireddu.ipynb new file mode 100644 index 0000000..5a14f62 --- /dev/null +++ b/your-project/Mastermind Game - Emanuele Pireddu.ipynb @@ -0,0 +1,657 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4867efce", + "metadata": {}, + "source": [ + "# Mastermind Game by Emthesda" + ] + }, + { + "cell_type": "markdown", + "id": "574777e4", + "metadata": {}, + "source": [ + "[How to play](#How-to-play)\n", + "\n", + "\n", + "[Play Game](#Play-Game)\n", + "\n", + "\n", + "[Code: _Variables_](#Variables)\n", + "\n", + "\n", + "[Code: _Functions_](#Functions)\n", + "\n", + "\n", + "[Issue Encoutered](#Issues-Encountered)\n", + "\n", + "[To Improve](#To-Improve)\n", + "\n", + "[Draft](#Draft)\n", + "\n", + "\n", + "[Any Questions?](#Any-Questions?)\n", + "\n", + "[Backup](#Backup)\n", + "\n", + "[Play Original Game](#Game)" + ] + }, + { + "cell_type": "markdown", + "id": "9f50cb02", + "metadata": {}, + "source": [ + "## How to play\n" + ] + }, + { + "cell_type": "markdown", + "id": "bab67939", + "metadata": {}, + "source": [ + "The purpose of the game is to _deduce_ the secret code, achieved through a selection of colored balls.\n", + "\n", + "The difficulty changes the length of the secret sequence.\n", + "\n", + "\n", + "***\n", + "**Easy** = 4 balls
\n", + "**Normal** = 6 balls
\n", + "**Hard** = 8 balls
\n", + "\n", + "***\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "After entering all needed values, the game gives out 2 hints:\n", + "* Number of balls answered that match the correct position.\n", + "* Number of balls answered that are actually in the secret sequence but are not in order." + ] + }, + { + "cell_type": "markdown", + "id": "0dc7381d", + "metadata": {}, + "source": [ + "## Issues Encountered\n", + "\n", + "\n", + "\n", + "Obviously... _None_ 😋\n", + "\n", + "Now seriously, please don't ask...\n", + "\n", + "Most common errors and problems:\n", + "* Index out of range\n", + "* blabla function is not defined\n", + "* Temptation to \"hard-code\" everything that doesn't work\n", + "* Counters/ Loops tend to infinity \n", + "* Last counter(Balls in answer) not correct\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "049c90bf", + "metadata": {}, + "source": [ + "## To Improve\n", + "\n", + "_Well... So many things..._\n", + "* Allow or not allow duplicates in game\n", + "\n", + "Things that I didnt use but maybe should've:\n", + "\n", + "* map()\n", + "* list comprehension\n", + "* input.upper so answers are robust but not case sensitive\n", + "* sets - just for the initial balls variable (possible choices are unique and this list shouldn't be modified)\n", + "* less usage of my bff _ctrl c_ & _ctrl v_ (Example: Getting while loop I did in prework to check input type)\n", + "* create a class 'balls' with var color and var position\n", + "* ~~using a victory = True like the snail problem~~ _Done_\n", + "* Use an Except X-Error\n", + "\n", + "Changes and different approaches related to the game itself:\n", + "\n", + "* Interface\n", + "* Theme\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "e46c2943", + "metadata": {}, + "source": [ + "## First Draft\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "raw", + "id": "5da68530", + "metadata": {}, + "source": [ + "# print? how to know what colour is every letter? as in: 'Please enter R for Red. G for Green ball, etc'\n", + "# 4 example: To enter Red write: R\n", + "R\n", + "R\n", + "\n", + "#First we need to create a list with all the possible options (colours of balls)\n", + "\n", + "balls = ['R', 'B', 'G', 'Y'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "\n", + "#inputs\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#Randomizer\n", + "import random as r\n", + "\n", + "x1 = r.randint(0, len(balls)-1)\n", + "x2 = r.randint(0, len(balls)-1)\n", + "x3 = r.randint(0, len(balls)-1)\n", + "x4 = r.randint(0, len(balls)-1)\n", + "\n", + "print(x1, x2, x3, x4)\n", + "ball1 = balls[x1]\n", + "ball2 = balls[x2]\n", + "ball3 = balls[x3]\n", + "ball4 = balls[x4]\n", + "print(ball1, ball2, ball3, ball4)\n", + "\n", + "print(type(correct_sequence))\n", + "correct_sequence.append(ball1) #or use insert?\n", + "print(correct_sequence)\n", + "\n", + "#inputs = 1 per position in sequence\n", + "#input first ball\n", + "#input 2nd ball - etcetera\n", + "\n", + "#if input is in list balls, continue with the functions created to check position and colour in correct sequence" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "4d2878e1", + "metadata": {}, + "outputs": [], + "source": [ + "#method 0\n", + "#method nº1 = do randomizer in range 0, difficulty_mode -1, 2\n", + "#method nº2 = for i in range 0, diff -1\n", + " #correct sequence[i] == method nº1" + ] + }, + { + "cell_type": "markdown", + "id": "068e8309", + "metadata": {}, + "source": [ + "## Variables\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e198b554", + "metadata": {}, + "outputs": [], + "source": [ + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() " + ] + }, + { + "cell_type": "markdown", + "id": "3781c8d8", + "metadata": {}, + "source": [ + "## Functions\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "b30c7cac", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + "\n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if counter_correct_position < len(correct_sequence) and counter_correct_position != 0:#user_input[i] in correct_sequence: #and user_input[i] != correct_sequence[i]: # ATTENTION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " print('hola')\n", + " #counter_correct_color += 1\n", + "\n", + "\n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " #print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "0ec9ffec", + "metadata": {}, + "source": [ + "## Play Game\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ee238eff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 = Easy mode\n", + "2 = Normal mode\n", + "3 = Hard mode\n", + "Please select difficulty! (Enter 1, 2 or 3) 1\n", + "Easy mode selected\n", + "['O', 'W', 'O', 'Y']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):r\n", + "Please enter a valid color:\n", + "Your choices are []\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):r\n", + "Please enter a valid color:\n", + "Your choices are []\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):r\n", + "Please enter a valid color:\n", + "Your choices are []\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'R', 'R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'R', 'R', 'R']\n", + "Wrong, please try again.\n", + "0 balls are in the correct position.\n", + "0 balls have been correctly chosen.\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):W\n", + "Your choices are ['R', 'W']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'W', 'R']\n", + "\n", + "Please choose a ball color (R, B, G, Y, O, W, P, C):R\n", + "Your choices are ['R', 'W', 'R', 'R']\n", + "hola\n", + "hola\n", + "Wrong, please try again.\n", + "1 balls are in the correct position.\n", + "2 balls have been correctly chosen.\n" + ] + } + ], + "source": [ + "play_game()" + ] + }, + { + "cell_type": "markdown", + "id": "a74b2089", + "metadata": {}, + "source": [ + "## Any Questions?\n", + "\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "016f3eba", + "metadata": {}, + "source": [ + "## Game\n", + "[Original game example](https://www.webgamesonline.com/mastermind/index.php)\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)" + ] + }, + { + "cell_type": "markdown", + "id": "0bfe9a51", + "metadata": {}, + "source": [ + "## Backup\n", + "[Back to Top](#Mastermind-Game-by-Emthesda)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0f4a8f8", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import random as r\n", + "\n", + "balls = ['R', 'B', 'G', 'Y','O', 'W', 'P', 'C'] #List of possible balls\n", + "\n", + "correct_sequence = [] #Empty list that stores the right sequence\n", + "\n", + "difficulty = int() # Trying if assigning empty variable was needed\n", + "\n", + "balls_length = int() \n", + "\n", + "\n", + "\n", + "\n", + "######### End of assigning variables\n", + "\n", + "\n", + "\n", + "########## GAME METHODS\n", + "\n", + "def input_difficulty_check(): #Difficulty selector and checker\n", + " \n", + " while True: \n", + " dif = input('1 = Easy mode\\n2 = Normal mode\\n3 = Hard mode\\nPlease select difficulty! (Enter 1, 2 or 3) ' )\n", + "\n", + " try:\n", + " dif = int(dif) #Assign the type int to input i, although float should be an option too (?)\n", + " if dif == 1 or dif == 2 or dif == 3:\n", + " break\n", + "\n", + " else:\n", + " continue\n", + " except:\n", + " print('Please enter a valid number (1, 2 or 3)') \n", + " continue\n", + " break\n", + " \n", + " return dif\n", + " \n", + "def difficulty_selection(x):\n", + " if x == 1:\n", + " balls_length = 4 #instead of putting -1 in the for len() it is already the needed length\n", + " print('Easy mode selected')\n", + " elif x == 2:\n", + " balls_length = 6\n", + " print('Normal mode selected')\n", + " elif x == 3:\n", + " balls_length = 8\n", + " print('Hard mode selected')\n", + " \n", + " return balls_length\n", + "\n", + "\n", + "\n", + "def randomizer_sequence(number_balls, options_balls): #THE MOST DIFFICULT SO FAR\n", + " list1 = []\n", + " for i in range(0, number_balls):\n", + " list1.extend(options_balls[r.randint(0, len(options_balls) - 1 )]) #OMG THE PROBLEMS I HAD till... added - 1 to the len(opt balls) cuz everytime random was 8 out of index\n", + " \n", + " return list1\n", + "\n", + "\n", + "def needed_inputs(correct_sequence, balls): #nº of inputs = nº values in correct_sequence\n", + " input_list = [] #where we store the user's choices ATTENTION it must be len as len correct sequence in other words, code starts when all inputs are entered.\n", + " answer = ''\n", + " \n", + " while len(correct_sequence) != len(input_list) or answer not in balls:\n", + " \n", + " answer = input('\\nPlease choose a ball color (R, B, G, Y, O, W, P, C):')\n", + " \n", + " if answer in balls: #CHECK IF ANSWER IS IN BALLS (LIST) HOW TO COMPARE X WITH VALUES IN LIST\n", + " input_list.append(answer)\n", + " \n", + " else:\n", + " \n", + " print('Please enter a valid color:') \n", + " \n", + " print('Your choices are', input_list) \n", + " continue\n", + " return input_list \n", + " \n", + " \n", + " #JUST ONE FUNCTION\n", + "\n", + "#LAST FUNCTION COMPARE LIST WITH ANOTHER (CORRECT SEQ WITH INPUT LIST) - WE MUST KNOW INTERSECTION OF THEM AND ORDER IF RIGHT = YOU WIN!\n", + " # !!!!! COUNTER WHERE WE KEEP TRACK OF ATTEMPTS (counter) and call again needed_inputs\n", + "def check_answer(user_input, correct_sequence):\n", + " counter_correct_position = 0\n", + " counter_correct_color = 0\n", + " correct_answer = True\n", + " \n", + " for i in range(0, len(user_input)): ##### I believe that maybe with list comprehension this could be achieved in a faster/shorter way. (?)\n", + " if user_input[i] == correct_sequence[i]:\n", + " counter_correct_position += 1\n", + " else:\n", + " correct_answer = False\n", + " \n", + " if user_input[i] in correct_sequence: # ATTETION CANT ACTUALLY COUNT IF INPUT IS IN C_SEQUENCE BUT NOT REPEATING\n", + " \n", + " counter_correct_color += 1\n", + " \n", + "\n", + " if correct_answer:\n", + " print('You win!')\n", + " play_again = True\n", + " while play_again: \n", + " replay = input('Want to play again? Y/N:')\n", + "\n", + " try:\n", + " if replay == 'Y':\n", + " play_game()\n", + "\n", + " elif replay == 'N':\n", + " play_again = False \n", + " except:\n", + " break\n", + " else:\n", + " print('Wrong, please try again.')\n", + " print(counter_correct_position, \"balls are in the correct position.\") \n", + " print(counter_correct_color, \"balls have been correctly chosen.\")\n", + " \n", + " return correct_answer\n", + " \n", + "def play_game():\n", + " \n", + " ##### GAME INFO\n", + " difficulty = input_difficulty_check()\n", + " balls_length = difficulty_selection(difficulty)\n", + " correct_sequence = randomizer_sequence(balls_length, balls)\n", + " print(correct_sequence)\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " while not check_answer(user_input, correct_sequence):\n", + " user_input = needed_inputs(correct_sequence, balls)\n", + "\n", + " # print('CORRECT! YOU WON!')\n", + "\n", + " \n", + "#CREATE METHOD PLAY_GAME? PUT (FUNCTION_PLAYGAME) IN ANOTHER CELL\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + " ################################################# PRINT THIS TO CHECK ANSWER\n", + "#print(needed_inputs(correct_sequence, balls))\n", + "#print(input_list)\n", + "#WTF IS NAME '' IS NOT DEFINED\n", + "\n" + ] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/your-project/README.md b/your-project/README.md deleted file mode 100644 index 2a8493d..0000000 --- a/your-project/README.md +++ /dev/null @@ -1,34 +0,0 @@ -Ironhack Logo - -# Title of Your Project -*[Your Name]* - -*[Your Cohort, Campus & Date]* - -## Content -- [Project Description](#project-description) -- [Rules](#rules) -- [Workflow](#workflow) -- [Organization](#organization) -- [Links](#links) - -## Project Description -Write a short description of your project. Write 1-2 sentences about the game you chose to build and why. - -## Rules -Briefly describe the rules of the game. - -## Workflow -Outline the workflow you used in your project. What are the steps you went through? - -## Organization -How did you organize your work? Did you use any tools like a kanban board? - -What does your repository look like? Explain your folder and file structure. - -## Links -Include links to your repository, slides and kanban board. Feel free to include any other links associated with your project. - -[Repository](https://github.com/) -[Slides](https://slides.com/) -[Trello](https://trello.com/en) diff --git a/your-project/README.txt b/your-project/README.txt new file mode 100644 index 0000000..4a69a33 --- /dev/null +++ b/your-project/README.txt @@ -0,0 +1,5 @@ +MASTERMIND GAME +by Emanuele + +Game Goal: +Deduce the correct code with given colored balls. \ No newline at end of file