Skip to content
← Back to Community
Mastermind Game! [Updated!]
Profile icon
tussiez

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!

image

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!

Voters
Profile icon
AustinK1219
Profile icon
FrancisPan
Profile icon
FlaminHotValdez
Profile icon
CyberPy
Profile icon
ch1ck3n
Profile icon
CampHelper1Help
Profile icon
ChopSueyMaster
Profile icon
dpryan
Profile icon
TryhardGamingGa
Profile icon
SYGT3M
Comments
hotnewtop
Profile icon
[deleted]

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

Profile icon
tussiez

@daddie Thanks!

Profile icon
[deleted]

@tussiez NUUUUUUUUU problem lol

Profile icon
firefish

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

Profile icon
ch1ck3n

69th upodot yeet

Profile icon
FraserNeilson

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

Profile icon
tussiez

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

Profile icon
P0GCHAMPB0i

mhm i get it now

EXELLENT GAEME!

Profile icon
tussiez
Profile icon
P0GCHAMPB0i
Profile icon
firefish

EXELLENT

@P0GCHAMPB0i *excellent

Profile icon
P0GCHAMPB0i

@firefish oh yeah hehe

Profile icon
P0GCHAMPB0i

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

Profile icon
tussiez
Profile icon
P0GCHAMPB0i

@tussiez Yea you gotta becarful with that

Profile icon
P0GCHAMPB0i

Cool. How long did this take you to make?

Profile icon
tussiez

@P0GCHAMPB0i Not too long

Profile icon
P0GCHAMPB0i

@tussiez oh ok

Profile icon
AntBerger

imma use your shit for a test grade homie :)

Profile icon
AntBerger
Profile icon
tussiez

@AntBerger
There is a serious flaw in the game.
Nah.. do it

Profile icon
MaycoOrellano
Profile icon
RahulChoubey1

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.

Profile icon
tussiez

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

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

Profile icon
Gunaalan10

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

Profile icon
Gunaalan10

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

Profile icon
tussiez

@Gunaalan10 Lol, no need to curse yourself

Profile icon
infinity21

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

Profile icon
tussiez

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

Profile icon
infinity21

@tussiez oh mb

Profile icon
Sethster1

@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

Profile icon
tussiez

@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) { alert("Congratulations! You cracked the code!"); }
Profile icon
Sethster1

@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

Profile icon
tussiez

@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'
Profile icon
Sethster1

@tussiez got it. Thanks!

Profile icon
TheMaster7661

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

Profile icon
amirking2013yy

kkkkkkkkkkkkkkkkkkk

Profile icon
EmmaGao8

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

Profile icon
tussiez

@EmmaGao8 Unnoticeable bugs?

Profile icon
firefish

@EmmaGao8 no advertising

Profile icon
EmmaGao8

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.

Profile icon
tussiez

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

Profile icon
LaneMartin

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

Profile icon
tussiez
Profile icon
codingjlu

niceeeeeeee

Profile icon
tussiez

@codingjlu

:)

Profile icon
InvisibleOne

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!

Profile icon
tussiez

@InvisibleOne Lol, thanks!

Profile icon
TheMaster7661

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

Profile icon
tussiez

@NoahJospeh See post, bug

Profile icon
EmmaGao8

can combination have two reds or three greens?

Profile icon
tussiez
Profile icon
CodingElf66

@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.

Profile icon
tussiez

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

Profile icon
CodingElf66

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

Profile icon
KurdMajid

testing

Profile icon
tussiez

@KurdMajid # Testing

Profile icon
FlaminHotValdez

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"

Screenshot from 2021-06-09 11-39-05

Profile icon
FlaminHotValdez

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

Profile icon
FlaminHotValdez

Screenshot from 2021-06-09 11-46-07

nice ss of 2 different bugs

Profile icon
tussiez

@FlaminHotValdez

  1. This mechanic: [0,0,0,0] != [0,0,0,0]
  2. 1
    3.yay
Profile icon
FlaminHotValdez

stackable grading makes this a pain

Profile icon
tussiez

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

Profile icon
candies

i like this

Profile icon
candies

its really nice. @candies

Profile icon
tussiez

@candies Thanks!

Profile icon
candies
Profile icon
CodingCactus

yeeeaaahhhh that's not possible
image

Profile icon
JadenTang1

@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.

Profile icon
TheMaster7661

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.
image

Profile icon
OldWizard209

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

Profile icon
firefish

@OldWizard209 It should be blacks first but anyway

Profile icon
tussiez

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

Profile icon
TheMaster7661

@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.

Profile icon
FlaminHotValdez

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

Profile icon
world345

Good but maybe add a reset button

Profile icon
tussiez
Profile icon
SilentFoxy78

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

Profile icon
CodingElf66

@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.

Profile icon
tussiez
Profile icon
firefish

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

Profile icon
FlaminHotValdez

@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"

Profile icon
tussiez

@firefish Definitely

Profile icon
CodingElf66

@firefish Mhm, yes, you are quite right

Profile icon
CodingElf66

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

Profile icon
Bookie0

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

:)

Profile icon
tussiez