xxxxxxxxxx
377
var code=[]; // Secret Code
var colCount=[0,0,0,0,0,0] // Number of each color in code
var guess=[]; // Current Player guess of code
var prevGuess=[]; // Array of previous guesses
//var guessArr=[];
//([pos1, pos2, pos3, pos4, keyC1, keyC2, pos1, pos 2...)
var keyC=[0,0]; // Current keyCpeg arrangment
var score=0; // Player Score
var numGuess=0; // Num of guesses player made
var win=false; // Flag for if player has won.
var gameover=false; // Flag for losing
var cells=[]; // Array of Cells for display
var test; // Used for Testing
var sub; // Used for testing
var temp=[0,0,0,0,0,0]; // Used for temp colorcount
var cols=['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
var limit=10; // Used for guess limit
var xarr=[]; // Array of color positions
var yarr=[]; // Array of color position
var subx; // XPos of Submit Button
var suby; // Ypos of Submit button
var subsize; // Size of button.
var clearx; // XPos of clear Button
var cleary; // Ypos of clear button
var clearsize; // Size of button.
var newx; // xPos of NewGame Button
var newy; // yPos of NewGame Buttom
var newsize; // size of button
var winx;
var winy;
var winsize;
function setup() {
createCanvas(400, 605);
//test=createInput();
//test.position(0, height+10);
//sub=createButton("CHECK GUESS");
//sub.position(175, height+10);
//sub.mousePressed(checkGuess);
subx=width-75;
suby=height-25;
subsize=30;
clearx=75;
cleary=height-25;
clearsize=30;
newx=31;
newy=31;
newsize=60;
winsize=60;
winx=(width-95)+winsize/1.5;
winy=(height/2)+50;
createCode();
//checkGuess();
}
function draw() {
background(220);
showBoard();
}
function createCode(){
//randomSeed(1);
code.length=0;
colCount=[0,0,0,0,0,0];
for(var i=0; i<4; i++)
{
var num=floor(random(6));
colCount[num]++;
code.push(num);
}
// console.log("Code: " + code);
}
function checkGuess(){
//guess=test.value();
var tguess=[];
var found=false;
for(var i=0; i<4; i++)
{
tguess[i]=guess[i];
}
//temp.copyWithin(colCount);
for(var i=0; i<colCount.length; i++)
{
temp[i]=colCount[i];
}
//console.log("TEMP: " + temp);
//console.log("Guess: " +guess);
keyC=[0,0];
// Check for perfect matches first.
for(var i=0; i<4; i++)
{
if(tguess[i]==code[i])
{
keyC[0]++;
colCount[code[i]]--;
tguess[i]=-1;
}
}
// Check for correct colors, wrong position
for(var i=0; i<4; i++)
{
found=false;
for(var j=0; j<4 && !found; j++)
{
if((tguess[i]==code[j]) && (i!=j) && (colCount[tguess[i]]>0))
{
keyC[1]++;
colCount[guess[i]]--;
found=true;
}
}
}
if(keyC[0]==4)
{
win=true;
score++;
}
// Load Guess and keyCs into Previous Guess Array
// Increment Guess Count
for(var i=0; i<4; i++)
{
//console.log(guess[i]);
prevGuess.push(guess[i])
}
prevGuess.push(keyC[0],keyC[1]);
for(var i=0; i<colCount.length; i++)
{
colCount[i]=temp[i];
}
// Push the previous guess to the display array
numGuess++;
cells.push(new Cell ((width/2)-100, 2+(numGuess *50), guess[0], guess[1], guess[2], guess[3], keyC[0], keyC[1]))
// If we have more guesses than can be displayed get rid of the oldest one
if(cells.length>10)
{
for(var i=cells.length-1; i>0; i--)
{
cells[i].y=cells[i-1].y;
}
cells.splice(0,1);
}
guess.length=0
//cells.push(new Cell (10, 100, 1,2,3,4, 1,1));
//console.log("KEY: " +cells[cells.length-1].key);
//console.log(colCount);
//console.log(prevGuess);
}
function showBoard()
{
// Shows Guesses
for(var i=0; i<cells.length; i++)
{
cells[i].show();
}
// If win or game over show the code.
// Otherwise "cover" it.
if(keyC[0]==4)
{
win=true;
}
var codewidth=180;
var codex=width/2 - (codewidth/2);
var codey=0;
if((!win) && (!gameover))
{
fill(0);
rect(codex, 0, codewidth, 50)
}
else
{
fill(100,100,100);
rect(codex, codey, codewidth, 50)
// Show guess pegs
for(var i=0; i<4; i++)
{
fill(cols[this.code[i]])
circle(10 + ((codex+20)+(i*40)),25, 20);
}
}
// Show submit button
fill('green');
circle(subx, suby, subsize);
stroke(0);
fill(0);
text("SUBMIT", subx-subsize/1.5, suby-subsize/1.5);
// Clear Button
fill('red');
circle(clearx, cleary, clearsize);
stroke(0);
fill(0);
text("Clear", clearx-subsize/1.5, cleary-clearsize/1.5);
// New Game Button
fill('green');
circle(newx, newy, newsize);
fill(0);
text("New Game", newx-newsize/2.5, newy+newsize/1.5);
// Show Score
fill(0);
text("SCORE: " + score, 5, 100);
// Show next level Button
if(win)
{
text("Guesses " + '\n' +"this round: " + numGuess, width-95, height/2);
fill(255);
circle(winx, winy, winsize);
fill(0);
text("Next level", winx-(winsize/2)+3, winy);
}
playerGuess();
}
function playerGuess()
{
var guesswidth=180;
var guessx=width/2 - (guesswidth/2)
var guessy= height - 50;
fill(100,100,100);
rect(guessx, guessy, guesswidth, 50)
// Show guess pegs
if(guess.length)
{
for(var i=0; i<guess.length; i++)
{
fill(cols[guess[i]])
circle(10 + ((guessx+20)+(i*40)),height-25, 20);
}
}
{
fill(100,100,100);
stroke(0);
for(var i=guess.length; i<4; i++)
{
circle(10 + ((guessx+20)+(i*40)),height-25, 20);
}
}
for(var i=0; i<cols.length; i++)
{
fill(cols[i]);
circle(25, (height/1.5) + (i*35), 25);
if(xarr.length<cols.length)
{
xarr.push(25);
yarr.push((height/1.5) + (i*35))
}
}
}
function mousePressed()
{
if((!win) && (!gameover))
{
// Check for clicking of colors first
for(var i=0; i<cols.length; i++)
{
if(dist(mouseX, mouseY, xarr[i], yarr[i]) < 25/2)
{
if(guess.length<4)
{
guess.push(i);
}
}
}
if(dist(mouseX, mouseY, subx, suby)<subsize/2)
{
if(guess.length==4)
checkGuess();
}
if(dist(mouseX, mouseY, clearx, cleary) <clearsize/2)
{
guess.length=0;
}
}
if(dist(mouseX, mouseY, newx, newy) <newsize/2)
{
win=false;
gameover=false;
score=0;
guess.length=0;
cells.length=0;
numGuess=0;
createCode();
keyC=[0,0];
}
if(dist(mouseX, mouseY, winx, winy) <winsize/2)
{
win=false;
guess.length=0;
cells.length=0;
numGuess=0;
keyC=[0,0];
createCode();
}
}