Last active
March 15, 2023 21:54
-
-
Save terrettaz/c3ed42835bce99c5de7b86d097b79d54 to your computer and use it in GitHub Desktop.
Easy version of MasterMind developed with my sister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import Random | |
COLOURS = "ABCDE" | |
MAX_TRIES = 10 | |
def start(): | |
print(f""" | |
_ (_) | | | |
____ ____ ___| |_ ____ ____ ____ _ ____ _ | | | |
| \\ / _ |/___| _)/ _ )/ ___) | \\| | _ \\ / || | | |
| | | ( ( | |___ | |_( (/ /| | | | | | | | | ( (_| | | |
|_|_|_|\\_||_(___/ \\___\\____|_| |_|_|_|_|_| |_|\\____| | |
Welcome in the game! | |
You will need to guess a combination of n colours represented by the letters [{COLOURS}] | |
""") | |
def generate_code(num): | |
if num == 0: | |
return "" | |
return Random().choice(COLOURS) + generate_code(num - 1) | |
def remove_first_element(el, xs): | |
if len(xs) == 0: | |
return xs | |
head = xs[0] | |
tail = xs[1:] | |
if el == head: | |
return tail | |
return head + remove_first_element(el, tail) | |
def remove_found_letters(letters, bag): | |
if len(letters) == 0: | |
return bag | |
head = letters[0] | |
tail = letters[1:] | |
bag = remove_first_element(head, bag) | |
return remove_found_letters(tail, bag) | |
def num_of_correct_letters(answer, code): | |
letters_left = remove_found_letters(answer, code) | |
return len(code) - len(letters_left) | |
def num_of_good_position(answer, code): | |
if len(answer) == 0 or len(code) == 0: | |
return 0 | |
if answer[0] == code[0]: | |
return 1 + num_of_good_position(answer[1:], code[1:]) | |
return num_of_good_position(answer[1:], code[1:]) | |
def guess(code, tries=1): | |
answer = input("> ").upper() | |
if answer == code: | |
print(f"Well done, you found the code in {tries} tries !!") | |
return True | |
if tries == MAX_TRIES: | |
print(f"Well, you just lost the game, the combination was: '{code}'") | |
return False | |
correct_letters = num_of_correct_letters(answer, code) | |
print(f"There are {correct_letters} correct letters") | |
good_positions = num_of_good_position(answer, code) | |
print(f"There are {good_positions} letters well placed") | |
print(f"\n{(MAX_TRIES - tries)} tries left ") | |
return guess(code, tries + 1) | |
def main(): | |
start() | |
print("How many letter do you want to guess?") | |
num = int(input("> ") or 0) | |
if num < 1: | |
print("Bye!") | |
return | |
code = generate_code(num) | |
print("Secret combination generated!") | |
print(f"Let's start with your first guess, you have a maximum of {MAX_TRIES}") | |
if not guess(code): | |
print("G A M E O V E R") | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Random; | |
public class MasterMind { | |
private static final String COLOURS = "rvbjn"; | |
public static void main(String[] args) throws IOException { | |
System.out.printf("Bonjour, combien de pions parmis les couleurs %s? %n", Arrays.toString(COLOURS.toCharArray())); | |
var stream = new BufferedReader(new InputStreamReader(System.in)); | |
var num = readNumber(stream); | |
if (num == null) { | |
System.out.println("Bye!"); | |
return; | |
} | |
var code = generateCode(num, COLOURS); | |
var count = 0; | |
System.out.println("Essayez de deviner la combinaison"); | |
String input; | |
while ((input = prompt(stream)).length() > 0 && count++ < 10) { | |
if (input.equals(code)) { | |
System.out.printf("Bravo, réussi en %d essai(s) !!%n", count); | |
return; | |
} | |
List<Character> charList = new ArrayList<>(); | |
for (char c : code.toCharArray()) { | |
charList.add(c); | |
} | |
for (char c : input.toCharArray()) { | |
charList.remove(Character.valueOf(c)); | |
} | |
System.out.println("Il y a " + (code.length() - charList.size()) + " qui sont corrects"); | |
// Find characters well placed | |
var charactersWellPlaced = 0; | |
var minimum = Math.min(code.length(), input.length()); | |
for (int i = 0; i < minimum; i++) { | |
if (input.charAt(i) == code.charAt(i)) { | |
charactersWellPlaced++; | |
} | |
} | |
System.out.println("Il y a " + charactersWellPlaced + " qui sont à la bonne place"); | |
System.out.print("Essaie encore: "); | |
} | |
System.out.println("Perdu, le code était: " + code); | |
} | |
private static String prompt(BufferedReader stream) throws IOException { | |
System.out.print("> "); | |
return stream.readLine(); | |
} | |
private static Integer readNumber(BufferedReader stream) throws IOException { | |
var max = 10; | |
String line; | |
while((line = prompt(stream)).length() > 0) { | |
try { | |
var number = Integer.parseInt(line); | |
if (number < 1 || number > max) { | |
System.out.println("Le nombre doit etre compris entre 1 et " + max); | |
} | |
return number; | |
} catch (NumberFormatException e) { | |
System.out.println(line + " n'est pas un nombre"); | |
} | |
}; | |
return null; | |
} | |
private static String generateCode(int num, String colours) { | |
Random rand = new Random(); | |
var result = ""; | |
for (int i = 0; i < num; i++) { | |
var index = rand.nextInt(colours.length()); | |
var colour = colours.charAt(index); | |
result = result + colour; | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment