<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
<!--
today = new Date();
y0 = today.getFullYear();
</script>
<style type="text/css">
.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalDialog:target {
opacity: 1;
pointer-events: auto;
}
.modalDialog > div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover {
background: #00d9ff;
}
</style>
<script type="text/javascript">
var r2pi = 2 * Math.PI
var row = 20;
var canvas;
var ctx;
var xmax;
var border = 10;
var xSpacing = 25;
var maxrows = 10;
var rowWidth = 40;
var ypx;
var radiusLg = 10;
var radiusSm = 5;
var smoffset = 6;
var curRow = 0;
var curRowMinX;
var curRowMaxX;
var curRowMinY;
var curRowMaxY;
var code = [];
var guess = [];
var score = [];
var colors = [];
var numOfColors = 6;
var colorColMinX;
var colorColMaxX;
var colorColMinY;
var colorColMaxY;
var currCenterX;
var currCenterY;
var pickedColorIdx = -1;
var right = 0;
var same = 0;
var iSkipRight = [];;
var iSkipSame = [];;
window.onload = init;
function init() {
colors = makeArray(10);
assignColors();
initArray(code, 4);
assignCode();
initArray(guess, 4);
initArray(score, 4);
initArray(iSkipRight, 4);
initArray(iSkipSame, 4);
canvas = document.getElementById("myCanvas");
canvas.addEventListener("mousedown", getPosition, false);
ctx = canvas.getContext("2d");
xmax = canvas.width;
ymax = canvas.height;
ctx.fillStyle = "tan"
ctx.fillRect(0, 0, xmax, ymax);
for (var col = 0; col < numOfColors; col++) {
var ypx = col * rowWidth + rowWidth / 2;
drawCircle(180, ypx, radiusLg, colors[col], "black");
}
colorColMinX = 180 - radiusLg
colorColMaxX = 180 + radiusLg;
colorColMinY = 0;
colorColMaxY = colors.length * rowWidth;
currColorCenterX = 0;
currColorCenterY = 0;
drawCodeRow();
drawRowStuff()
}
function assignColors() {
colors = new Array("Red", "Blue", "Yellow", "ForestGreen", "Orange", "Cyan", "Lime", "BlueViolet", "MidnightBlue", "DeepPink");
}
function assignCode() {
for (var i = 0; i < code.length; i++) {
code[i] = Math.floor(Math.random() * numOfColors);
}
}
function processKeyCode(event) {
switch (event.keyCode) {
case 72:
update();
break;
}
}
function makeArray(size) {
this.length = size;
for (var i = 0; i <= size; i++) {
this[i] = -1;
}
return this
}
function initArray(arr, size) {
for (var i = 0; i < size; i++) {
arr[i] = -1;
}
}
function drawCodeRow() {
ypx = ymax - rowWidth / 2;
for (idx = 0; idx < code.length; idx++) {
xval = border + idx * xSpacing + xSpacing / 2;
drawCircle(xval, ypx, radiusLg, "tan", "black");
}
}
function fillCodeRow() {
ypx = ymax - rowWidth / 2;
for (idx = 0; idx < code.length; idx++) {
xval = border + idx * xSpacing + xSpacing / 2;
drawCircle(xval, ypx, radiusLg, colors[code[idx]], "black");
}
}
function drawCircle(x, y, radius, clr, strokeColor) {
ctx.beginPath();
ctx.arc(x, y, radius, 0, r2pi);
if (clr.length > 0) {
ctx.fillStyle = clr;
ctx.fill();
}
ctx.lineWidth = "1.2";
ctx.strokeStyle = strokeColor;
ctx.stroke();
}
function update() {
DetermineScoring();
MarkScore();
if (right < 4) {
if (curRow >= 11) {
fillCodeRow();
alert("I'm sorry :-(( We ran out of rows. Please click Reset to play again :-)");
}
else {
curRow++;
drawRowStuff();
var button = document.getElementById("btnGuess");
button.disabled = "disabled";
for (var idx = 0; idx < guess.length; idx++) {
guess[idx] = -1;
}
}
}
else {
fillCodeRow();
alert('Congratulations - you cracked the code!');
}
}
function drawRowStuff() {
ypx = curRow * rowWidth + rowWidth / 2;
curRowMinX = border;
curRowMaxX = 95 + radiusLg;
curRowMinY = curRow * rowWidth;
curRowMaxY = (curRow + 1) * rowWidth;
for (idx = 0; idx < code.length; idx++) {
xval = border + idx * xSpacing + xSpacing / 2;
drawCircle(xval, ypx, radiusLg, "tan", "black");
}
drawCircle(120, ypx - (smoffset), radiusSm, "tan", "black");
drawCircle(140, ypx - (smoffset), radiusSm, "tan", "black");
drawCircle(120, ypx + (smoffset), radiusSm, "tan", "black");
drawCircle(140, ypx + (smoffset), radiusSm, "tan", "black");
}
function getPosition(event) {
var evt = event ? event : window.event;
var clickX = 0, clickY = 0;
canoffset = $(canvas).offset();
x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - Math.floor(canoffset.left);
y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop - Math.floor(canoffset.top) + 1;
var myX = x;
var myY = y;
if (myY >= curRowMinY && myY <= curRowMaxY && myX >= curRowMinX && myX <= curRowMaxX) {
var foundCell = false;
if (currColorCenterX > 0) {
for (var idx = 0; idx < code.length && !foundCell; idx++) {
var cellMinX = border + idx * xSpacing;
var cellMaxX = border + (idx + 1) * xSpacing;
if (myY >= curRowMinY && myY <= curRowMaxY && myX >= cellMinX && myX <= cellMaxX) {
currCenterX = border + idx * xSpacing + xSpacing / 2;
currCenterY = curRow * rowWidth + rowWidth / 2;
drawCircle(currCenterX, currCenterY, radiusLg, "", "white");
foundCell = true;
drawCircle(currCenterX, currCenterY, radiusLg, colors[pickedColorIdx], "black");
currCenterX = 0;
currCenterY = 0;
guess[idx] = pickedColorIdx;
CheckNumColored();
}
}
}
}
else if (myY >= colorColMinY && myY <= colorColMaxY && myX >= colorColMinX && myX <= colorColMaxX) {
pickedColorIdx = -1;
for (var idx = 0; idx < colors.length && pickedColorIdx < 0; idx++) {
var cellMinY = idx * rowWidth;
var cellMaxY = (idx + 1) * rowWidth;
if (myY >= cellMinY && myY <= cellMaxY && myX >= colorColMinX && myX <= colorColMaxX) {
if (currColorCenterX > 0) {
drawCircle(currColorCenterX, currColorCenterY, radiusLg, "", "black");
}
currColorCenterX = 180;
currColorCenterY = idx * rowWidth + rowWidth / 2;
drawCircle(currColorCenterX, currColorCenterY, radiusLg, "", "white");
pickedColorIdx = idx;
if (currCenterX > 0) {
drawCircle(currCenterX, currCenterY, radiusLg, "", "black");
currCenterX = 0;
currCenterY = 0;
}
}
}
}
}
function reset() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
curRow = 0;
init();
}
function CheckNumColored() {
var cntr = 0;
for (var idx = 0; idx < guess.length; idx++) {
if (guess[idx] >= 0) {
cntr++;
}
}
if (cntr == guess.length) {
var button = document.getElementById("btnGuess");
button.disabled = "";
}
}
function DetermineScoring() {
right = 0;
same = 0;
for (var i = 0; i < 4; i++) {
iSkipRight[i] = 0;
iSkipSame[i] = 0;
}
for (var i = 0; i < 4; i++) {
if (code[i] == guess[i]) {
right++;
iSkipRight[i] = 1;
iSkipSame[i] = 1;
}
}
for (var j = 0; j < 4; j++) {
if (iSkipRight[j] == 0) {
var NoMatch = true;
for (var k = 0; k < 4 & NoMatch; k++) {
if (iSkipSame[k] == 0) {
if (guess[j] == code[k]) {
same++;
iSkipSame[k] = 1;
NoMatch = false;
}
}
}
}
}
}
function MarkScore() {
var cntr = 0;
for (var i = 0; i < right; i++) {
score[cntr] = "black";
cntr++;
}
for (var i = 0; i < same; i++) {
score[cntr] = "white";
cntr++;
}
for (var i = cntr; i < score.length; i++) {
score[cntr] = 'tan';
cntr++;
}
ypx = curRow * rowWidth + rowWidth / 2;
drawCircle(120, ypx - (smoffset), radiusSm, score[0], "black");
drawCircle(140, ypx - (smoffset), radiusSm, score[1], "black");
drawCircle(120, ypx + (smoffset), radiusSm, score[2], "black");
drawCircle(140, ypx + (smoffset), radiusSm, score[3], "black");
}
</script>
<title>MasterMind in HTML5</title>
</head>
<body onload="init();" onkeydown="processKeyCode(event);"> <h1>MasterMind</h1>
Here we have an implementation of the classic code-breaking game - MasterMind H5.<br>
It has been written in HTML5, JavaScript with some jQuery, and CSS3.<br>
The game works well in FireFox, Chrome and Safari. Not so well in IE.
<br>
Comments and bug reports can be sent to me at: will.autio at gmail.com.<br>
<b>Enjoy!</b><br>
<br>
<canvas id="myCanvas" width="200" height="520" style="border: 1px solid #000000;">Your browser does not support the HTML5 canvas tag. Please try Chorme, FireFox or IE9.<br>
<br>
</canvas>
<input id="btnGuess" value=" Test Guess " disabled="disabled" onclick="update()" type="button">
<input id="btnReset" value=" Reset " onclick="reset()" type="button">
<a href="#openModal">Instructions</a>
<div id="openModal" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2>MasterMind Instructions:</h2>
<p>This is an HTML5 implementation of the classic MasterMind game. The computer has randomly created a code. </p>
<p>
Your mission is to determine the code by assigning a color to each of the circles on the first row (select a color from the column on
the right and then click the position to which you would like to assign that color.)
</p>
<p>
The colors do not have to be unique - [blue] [blue] [green] [green] is a valid combination.
<br>
When you are ready to see how you did at assigning colors, click Test Guess. Your score will appear in the
small circles to the right of the row.<br>
- A black score means that a color is correct and in the correct position.<br>
- A white score means that a color is correct, but in the wrong position.
</p>
</div>
</div>
<br>
<br>
Visit <a href="http://www.willautio.com">the rest of my site </a>for a look at other ways I have spent my time.
<br> <br>
Copyright © WillAutio.com 2011 -
<script type="text/javascript">
<!-- Hide from old browsers
document.write(y0);
</script>
</body>
</html>