Share your repls and programming experiences

← Back to all posts

# Mastermind!

This is a game I wrote not too long ago, but never finished.

Mastermind 2 is an online version of the board game Mastermind.

### UPDATE

The winning combination bug is fixed (@Sethster1)

Here's the game instructions from the page:

## How to Play

-- Scroll to bottom for TL;DR --

Mastermind is a guessing game. The goal is to reveal a combination of colors that the Codemaker creates.

In this game, you are the Codebreaker, the one who finds the combination, and the computer is the Codemaker. There are four positions (0,0,0,0) and there are six possible colors, lime, red, yellow, black, white, and cyan. Colors can be stacked!

To make it easier to guess the code, the Codemaker gives you Hints after each guess. These hints come in four smaller pegs.

To avoid directly giving away the code, the place that each peg is placed in does not matter.

A RED peg indicates that ONE peg in your guess is NOT included in the code, and is NOT in the right position.

A WHITE peg indicates that ONE of your pegs IS a color in the Codemaker's combination, but is NOT in the right position.

An EMPTY/BLACK peg means that you have gotten ONE peg to match a color in the Codemaker's combination, and is in the CORRECT position. You do not need to move or change this peg in subsequent rounds.

This is the entire game. Have fun playing!

TL;DR: Guess a color combination, Codemaker gives response, RED peg response = wrong color, wrong position, WHITE peg response = right color, wrong position, NO peg = right color, right position.

Fun Fact: There are 1296 possible combinations, and colors can be repeated!

## Note!

As far as I know, the game logic should be accurate.
However, if you notice any flaws in my code, let me know in the comments! :)

One bug:
1. For some reason, `[0, 0, 0, 0]` != `[0,0,0,0]` which would tell you won, but it doesn't (??) fixed, thanks @Sethster1 !
1. The computer treats multiple pegs of the same color as stackable when grading - so for example:
Winning Combination: [BLACK,RED,WHITE,YELLOW] and your input as
[RED, RED, RED, RED] would result in a response of:
BLACK [correct], WHITE, WHITE, WHITE [correct, wrong position]
because technically, the other red pegs are the right color, but in the wrong spot.

Have fun!

hotnewtop
[deleted]

OMG THIS IS SOOO COOL. I'VE NEVER SEEN A GAME LIKE THIS OMG SOO FUN

[deleted]

@tussiez NUUUUUUUUU problem lol

firefish (948)

@daddie never seen a game like this? cough 1970's board game

FraserNeilson (1)

this game is bugged af
the colors change even when i put in the same 4 pegs

tussiez (1669)

@FraserNeilson They are placed in random order to make the game more difficult.

P0GCHAMPB0i (54)

mhm i get it now

# EXELLENT GAEME!

P0GCHAMPB0i (54)

The code. You want to fix the thing in the file "script.js"

P0GCHAMPB0i (54)

@tussiez Yea you gotta becarful with that

P0GCHAMPB0i (54)

Cool. How long did this take you to make?

ch1ck3n (2077)

69th upodot yeet

RahulChoubey1 (173)

I've played the original game before. Red pegs mean correct color in correct position, white peg means correct color in WRONG position, and no peg means wrong.

tussiez (1669)

@RahulChoubey1 Hmm
It appears there are multiple variations of the game.

(in my clone, black is correct color/correct position)

Gunaalan10 (0)

i got the correct combo on my last try but it still said that I was wrong

Gunaalan10 (0)

shit did not see th comments the prob was already [email protected]

tussiez (1669)

@Gunaalan10 Lol, no need to curse yourself

infinity21 (0)

there's a bug where if you guess it correctly at the last line, it says "you weren't able to guess" and displays the correct answer despite having answered correctly. Great game though

tussiez (1669)

@infinity21 See post, I dunno how to fix this bug :(
Thanks!

Sethster1 (1)

@tussiez I figured out a fix for checking if they inputted the correct answer. TL;DR at the bottom if you don't want to read all of this. Changing `if (ans === [0,0,0,0])` to `if (ans[0] == 0 && ans[1] == 0 && ans[2] == 0 && ans[3] == 0)` triggers the alert when the combination inputted is correct. It kind of looks a bit sloppy and inefficient, but it works. I'm pretty sure when you use comparison operators on lists, it checks if the lists are the same by reference, not by value. For example:

``````let x = [0,0,0,0];
let y = x;
print(x === y);
// should return true because both lists are referencing
// the exact same thing
// this is why if I did:
x.push(0); // x is now [0,0,0,0,0]
// y would now be [0,0,0,0,0] because it is referencing x or referencing what x is``````
``````let x = [0,0,0,0];
let y = [0,0,0,0];
print(x === y);
// should return false because although the values of
// the lists are the same and at the same index, the
// lists don't reference the exact same thing, so they
// aren't the "same"
// this is why if I did:
x.push(0); // x is now [0,0,0,0,0]
// y would still be [0,0,0,0] because it isn't referencing x or referencing what x is``````

Sorry if I kinda wrote a lot, I wanted to explain it thoroughly. I realize that I put "print" and "array.push()" in the code explanation, instead of what it would be in regular javascript to output in the console (is it like console.log() or something? I remember seeing that on a mozilla tutorial page for some of the array functions when I was learning them) or for adding to the end of an array/list (dunno what that is in regular javascript). I'm currently learning p5js, so I was just used to that. One last thing I will say, sorry if my markdown stuff is terrible, I'm not good with it. Hope this helped!

TL;DR: changing

``if (ans === [0,0,0,0])``

to

``if (ans[0] == 0 && ans[1] == 0 && ans[2] == 0 && ans[3] == 0)``

fixes the problem with it not triggering the alert when the answer given by the player matches the mastermind's code

edit: this doesn't work if the code is 4 of the same color though, it gives 3 black pegs and one white peg as the reply when that happens, though that is probably something with the code that checks if the color is correct at its position, not checking if the entire guess is correct

tussiez (1669)

@Sethster1 Great explanation!

Using your code, I was able to write `match`:

``````let x = [0,0,0,0]; // 4 black pegs
let y = [0,0,0,0]; // grading

const match = (a,b) => {
let c = true; // does match

a.forEach((ele,idx) =>{if(b[idx]!=ele) c = false});
// Loop over each element in array A, condition: ele != b[idx] ? [c = false]

return c;
}

if(match(x,y) === true) {
}``````
Sethster1 (1)

@tussiez Awesome! Glad I was able to help! :D Though one question: why use the `const` there and not just define match as a function? As you can probably guess, I am inexperienced with Javascript as a whole, so I'm curious

tussiez (1669)

@Sethster1
Arrow functions (`=>`) pass the variable scope into the function, while `function` creates a new scope.
@Baconman321 has a good tutorial on JavaScript scope on Talk :)

For example:

``````let globalVariable = 'ABC';

const arrowFunc = () => {
this.globalVariable = 'DEF';
}
arrowFunc();
console.log(globalVariable); // ==> logs 'DEF'

function anotherFunc() {
this.globalVariable = 'GHI'; // Different scope
}
anotherFunc();
console.log(globalVariable); // ==> still logs 'DEF'``````
NoahJospeh (29)

You should make another version where the roles are switched for the player and computer or some online multiplayer version.

EmmaGao8 (33)

https://replit.com/@EmmaGao8/Codecracker#main.py Mastermind is very hard to perfect. Even mine has unoticable bugs

EmmaGao8 (33)

Instead of comparing the entire codemakers list to a single element, you can just compare one single element to the corrsesponding element in the list.

tussiez (1669)

@EmmaGao8 This works, but makes grading with white pegs (right color, wrong position) more difficult

LaneMartin (114)

Well well well, look who we have here! It's @tussiez and he's back again with another game!

codingjlu (240)

tussiez (1669)

@codingjlu

# :)

InvisibleOne (2996)

Nice, I just played the board game version of this with my brother a week ago and was thinking I should make one in HTML, you beat me to it, great job!

NoahJospeh (29)

Look at the 6th row and the 8th row of my screenshot. How can there be two answers?

EmmaGao8 (33)

can combination have two reds or three greens?

JeffreyChen13 (44)

@tussiez, all good! I like the game, pretty creative! There's only one part I'm concerned about, and it is the styling. I think you can design and style it better so it looks more entertaining and fun.

tussiez (1669)

@JeffreyChen13 Thanks! It's primarily a logic-based game, but I'll try to make it a little nicer :)

JeffreyChen13 (44)

@tussiez Thanks! I would like to see the latest version of it!

FlaminHotValdez (694)

win mechanic is busted I got the right combo but wouldn't let me win, I entered the winning combo 2 more times and at the end it said "you didn't find the winning combo"

FlaminHotValdez (694)

another issue: if you lose and they show the winning combo, you can click advance again and it will show the winning combo again

FlaminHotValdez (694)

nice ss of 2 different bugs

tussiez (1669)

@FlaminHotValdez
1. This mechanic: `[0,0,0,0] != [0,0,0,0]`
2. 1
3.yay

FlaminHotValdez (694)

stackable grading makes this a pain

tussiez (1669)

@FlaminHotValdez True, but nobody's given me a solution to this

CodingCactus (4367)

yeeeaaahhhh that's not possible

@CodingCactus It is possible. Just there is only 1 black in the code(if you input two of the same color but there is only one, and one is in the correct spot, one of them is black, but since there is only 1, the other will be white as it is in the wrong spot.

NoahJospeh (29)

This bug made me so confused. First, it's telling yellow is a color but not in the right spot, then that yellow actually isn't a color at all. Next it's telling me that the colors, white, lime, and black are correct and in the right spots. Next, I'm being told that red and lime are incorrect. On the 5th row, I'm being told that white and yellow are incorrect, and the 6th row finally tells me that black is incorrect. In the 7th row, I'm being told that the first two whites are correct, but the 2nd two aren't in the right spots. It's the same with the 8th row, except its two whites and two limes. The 9th row tells me that only the first cyan is correct, and the other colors aren't in the right spots. The Codemaker is supposed to get you close to figuring out the code, but I just keep getting farther and farther away from the answer. If I am understanding this wrong, please explain this to me.

OldWizard209 (1544)

Bruhhh. The answer that tells you if it is right, wrong etc, is not in the correct order. It is random. @NoahJospeh

firefish (948)

@OldWizard209 It should be blacks first but anyway

tussiez (1669)

@firefish No lol, this is part of the game.If the stacking issue was fixed this can be won easily

NoahJospeh (29)

@tussiez Thats the whole point I thought. I mean sure you shouldn't be able to win easily, but you should still not have a super hard time like I did. The codemaker tells me, it's the correct color, then I'm told it's still correct but in the wrong spot, then why I try the other three spots with that color, I'm told that color doesn't belong in the code at all. In this game, the codemaker is supposed to always correctly tell me if my code is correct. If my code isn't correct, I should be given the correct colored pegs to show which colors are right or wrong. I don't think the codemaker should be lying to me.

FlaminHotValdez (694)

@NoahJospeh 1st hint doesn't always correspond to 1st peg, 2nd hint 2nd peg, so on.

world345 (8)

Good but maybe add a reset button

LavernieChen (3)

This game is a bit like the logic game @JeffreyChen13 posted. But nice job @tussiez !!

JeffreyChen13 (44)

@LavernieChen Yes! Four digits, A=right position and number, B=right number wrong position. I like it though, doing that not in numbers, but in colors.

firefish (948)

@JeffreyChen13 yes, this was a very original idea that @tussiez totally thought of himself, totally not copied directly from a 1970's board game

FlaminHotValdez (694)

@JeffreyChen13 actually if you read the description at all you would know that this is actually a computer version of a board game called "Mastermind"

JeffreyChen13 (44)

@firefish Mhm, yes, you are quite right

JeffreyChen13 (44)

@FlaminHotValdez I searched it up, thanks. I had no idea LOL

Bookie0 (6297)

Cheating doesn't make the game any fun! :)

:)

firefish (948)

Colours can repeat? That just defeats the whole purpose of the game! I mean my strategy relies on the fact that colours don't repeat... awwww

JeffreyChen13 (44)

@tussiez Yes, same. The logic game I created with @LavernieChen only allowed numbers that did not repeat. By the way, it is SOO HARD! LOL!