<html>
<head>
<meta charset=utf-8 />
<title>Project_Mastermind</title>
<script>
var colors = ['yellow', 'green', 'red', 'blue', 'orange'];
var arrayLength = colors.length; // WARN: var arrayLengte = colors.length;
function randomArrayItem(array) {
// ERR: Math.random(kleuren)
var index = Math.floor(Math.random() * array.length);
return array[index];
}
function randomColor() {
return randomArrayItem(colors);
}
function randomColorComb(number) {
var result = [];
// WARN: i < aantal;
for (var i = 0; i < number; i++) {
result.push(randomColor());
}
return result;
}
function arrayPlayer() {
var player = [];
// WARN: i < aantal;
for (var i = 0; i < arrayLength; i++) {
player[i] = prompt("Please fill in a color between ' '", "''");
}
return player;
}
function comparison(computer, player) {
console.log(computer);
console.log(player);
var arrayComparison = [];
for(var i = 0; i < arrayLength; i++) {
// ERR?
if (computer[i] === player[i]) {
arrayComparison.push("1");
}
else {
arrayComparison.push("0");
}
}
console.log(arrayComparison);
return arrayComparison;
}
//
function correctAnswers(array) {
//var array = comparison(); ERR
var correctCounter = 0;
for (var i = 0; i < arrayLength; i++) {
// ERR
if (parseInt(array[i]) === 1) {
alert("Color number " + i + " is correct");
correctCounter += 1;
}
else {
alert("Color number " + i + " is not correct");
}
}
return correctCounter;
}
function startGame()
{
var code = randomColorComb(arrayLength);
console.log(code);
//var aantalpogingen = true;
var gameDone = false;
var attempCounter = 0;
var maxNumOfAttempts = 2; // 15
var comparedArray;
// ERR || to &&
while (gameDone !== true && attempCounter < maxNumOfAttempts) {
var a = arrayPlayer();
console.log("a: " + a);
console.log(typeof code);
console.log(typeof a);
comparedArray = comparison(code, a);
console.log(comparedArray);
//var correctAnswerPlayer = correctAnswers();
var correctAnswerPlayer = correctAnswers(comparedArray);
console.log("Number of correct answers: " + correctAnswerPlayer);
if (correctAnswerPlayer !== arrayLength) {
attempCounter += 1;
gameDone = false;
}
else {
gameDone = true;
}
}
}
function questionAgain() {
var x;
var r = confirm("Press OK if you want to play again.");
// WAR: just return r
if (r === true) {
x = true;
}
else {
x = false;
}
return x;
}
function startNewGame()//
{
var newGame = true;
while(newGame !== false) {
startGame(); //Dit werkt nog niet goed. Ik heb geen idee waarom ik geen prompts krijg van --> arraySpeler.
newGame = questionAgain();
}
}
function startMasterMind() {
startNewGame();
}
</script>
</head>
<body>
<button onclick="startMasterMind()">start mastermind</button>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |