Added initial kinds of valid path

This commit is contained in:
Rob Pearce 2016-08-18 16:18:35 +10:00
parent 08b65688ca
commit a73ff6ce6d
2 changed files with 149 additions and 43 deletions

189
cat.html
View File

@ -17,6 +17,7 @@ var things = [];
var myScore; var myScore;
var curpath = []; var curpath = [];
var pathvalid = false;
var MAXDIRS = 4; var MAXDIRS = 4;
var DIRXMOD = [ 0, 1, 0, -1 ]; var DIRXMOD = [ 0, 1, 0, -1 ];
@ -52,6 +53,26 @@ function getthingxy(x, y) {
return null; return null;
} }
function clearpath() {
curpath = [];
pathvalid = false;
}
function addtopath(what) {
console.log("addpath() " + what.name);
dumppath("addpath pre: ", curpath);
curpath.push(what);
pathvalid = true;
if (curpath.length == 1) {
console.log("Starting path with " + what.name);
} else {
dumppath("Cur path is: ",curpath);
}
}
function pathcontains(what) { function pathcontains(what) {
var i; var i;
if (curpath == undefined) return false; if (curpath == undefined) return false;
@ -64,6 +85,54 @@ function pathcontains(what) {
return false; return false;
} }
function isvalidpath(mypath) {
var valid = true;
for (i = 0; i < mypath.length - 1; i++) {
var thisone,nextone;
thisone = mypath[i];
/*
if (i == mypath.length - 1) { // last one
nextone = null;
} else {
nextone = mypath[i+1];
}
*/
nextone = mypath[i+1];
if ((thisone.type == "cat") && nextone.type == "cat") {
// lines of cats are ok
} else if ((i == 0) && (thisone.type == "cat") && (nextone.type == "food")) {
// first cat -> food is ok
} else {
// not ok
valid = false;
break;
}
}
//var pr = "isvalidpath()? " + valid + " ";
//dumppath(pr, mypath);
return valid;
}
// would adding 'overthing' to our existing path result in a valid path?
function canextendpath(overthing) {
if (!overthing) return false;
if (overthing &&
isadjacent(overthing, curpath[curpath.length-1]) && // adjacent to last thing in path?
!pathcontains(overthing) ) { // path doesn't already contain this?
// create a fake new path containing this.
var fakepath = curpath.slice();
fakepath.push(overthing);
if (isvalidpath(fakepath)) {
return true;
}
}
return false;
}
function isadjacent(thing1, thing2) { function isadjacent(thing1, thing2) {
// is thing1 adjacent to thing2? // is thing1 adjacent to thing2?
var newgridx,newgridy; var newgridx,newgridy;
@ -82,28 +151,22 @@ function isadjacent(thing1, thing2) {
function startGame() { function startGame() {
var x,y; var x,y;
/*
myGamePiece = new thing(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new thing("30px", "Consolas", "black", 280, 40, "text");
*/
for (y = 0; y < GRIDH; y++) { for (y = 0; y < GRIDH; y++) {
for (x = 0; x < GRIDW; x++) { for (x = 0; x < GRIDW; x++) {
things.push(new thing(x, y, getrandomname())); things.push(new thing(x, y, "random"));
} }
} }
myGameArea.start(); myGameArea.start();
} }
function dumppath() { function dumppath(prefix,arr) {
var str; var str;
str = ""; str = "";
for (i = 0; i < curpath.length; i++) { for (i = 0; i < arr.length; i++) {
str = str + " " + curpath[i].name; str = str + " " + arr[i].name;
} }
console.log("Cur path is: " + str); console.log(prefix + str);
} }
function thingsmoving() { function thingsmoving() {
@ -164,14 +227,14 @@ var myGameArea = {
// make sure nothing is moving // make sure nothing is moving
if (thingsmoving()) return; if (thingsmoving()) return;
// clear current path // clear existing path
curpath = []; clearpath();
// did you click on an object? // did you click on an object?
var onthing = getthingxy(event.pageX,event.pageY); var onthing = getthingxy(event.pageX,event.pageY);
if (onthing) { if (onthing) {
curpath.push(onthing); console.log("Initial click on " + onthing.name);
console.log("Starting path: " + onthing.name); addtopath(onthing);
} }
}, },
@ -182,27 +245,49 @@ var myGameArea = {
// existing path? // existing path?
if (curpath != undefined && curpath.length > 0) { if (curpath != undefined && curpath.length > 0) {
var overthing = getthingxy(event.pageX,event.pageY); var overthing = getthingxy(event.pageX,event.pageY);
var lastinpath;
if (curpath == undefined) {
lastinpath = null;
} else {
lastinpath = curpath[curpath.length-1];
}
// is overthing adjacent to the last thing in the path? if (canextendpath(overthing)) {
if (overthing && isadjacent(overthing, curpath[curpath.length-1])) {
// add it to the path // add it to the path
curpath.push(overthing); addtopath(overthing);
console.log("Adding to path: " + overthing.name); } else if (pathcontains(overthing)) {
dumppath(); pathvalid = true;
} else {
pathvalid = false;
} }
} }
}, },
handlemouseup : function(event) { handlemouseup : function(event) {
// make sure nothing is moving var ok = true;
if (thingsmoving()) return; var overthing = getthingxy(event.pageX,event.pageY);
if (thingsmoving()) {
console.log("mouseup() - things are moving");
ok = false;
} else if (!isvalidpath(curpath)) {
console.log("mouseup() - path isn't valid");
ok = false;
} else if ((overthing == undefined) || !pathcontains(overthing)) {
console.log("mouseup() - not over something in path");
ok = false;
}
if (!ok) {
clearpath();
return;
}
// kill all in path // kill all in path
while (curpath != undefined && curpath.length > 0) { while (curpath != undefined && curpath.length > 0) {
curpath[0].kill(); curpath[0].kill();
curpath.splice(0, 1); curpath.splice(0, 1);
} }
curpath = null; clearpath();
}, },
} }
@ -218,19 +303,42 @@ function getrandomcolour() {
function getrandomname() { function getrandomname() {
var letters = '0123456789'; var letters = '0123456789';
var name = "cat-"; var name = "";
for (var i = 0; i < 3; i++ ) { for (var i = 0; i < 3; i++ ) {
name += letters[Math.floor(Math.random() * 10)]; name += letters[Math.floor(Math.random() * 10)];
} }
return name; return name;
} }
function thing(gridx, gridy, name) { function getrandomtype() {
this.name = name; var pct,type;
pct = Math.random() * 100;
if ( pct < 50) {
type = "cat";
} else {
type = "food";
}
return type;
}
function thing(gridx, gridy, type) {
this.width = THINGSIZE; this.width = THINGSIZE;
this.height = THINGSIZE; this.height = THINGSIZE;
this.color = getrandomcolour();
if (type == "random") {
type = getrandomtype();
}
this.type = type;
if (this.type == "cat") {
this.color = "#b5dea8";
} else if (type == "food") {
this.color = "#d8db03";
} else {
this.color = getrandomcolour();
}
this.name = type + "-" + getrandomname();
this.gridx = gridx; this.gridx = gridx;
if (gridy == "top") { if (gridy == "top") {
@ -275,13 +383,23 @@ function thing(gridx, gridy, name) {
if (inpath) { if (inpath) {
ctx.beginPath(); ctx.beginPath();
ctx.fillText("PATH", this.x + 10, this.y + (THINGSIZE/2) + 20); ctx.fillText("PATH", this.x + 10, this.y + (THINGSIZE/2) + 20);
ctx.strokeStyle = "red"; if (pathvalid) {
ctx.strokeStyle = "green";
} else {
ctx.strokeStyle = "red";
}
ctx.rect(this.x, this.y, this.width, this.height); ctx.rect(this.x, this.y, this.width, this.height);
ctx.stroke(); ctx.stroke();
} }
} }
this.issametype = function(otherthing) {
if (otherthing == undefined) return false;
if (otherthing.type == this.type) return true;
return false;
}
this.snaptogrid = function() { this.snaptogrid = function() {
if (this.y % GRIDSIZE != 0) { if (this.y % GRIDSIZE != 0) {
this.y = this.gridy * GRIDSIZE; this.y = this.gridy * GRIDSIZE;
@ -300,7 +418,7 @@ function thing(gridx, gridy, name) {
this.kill = function() { this.kill = function() {
// add a new cat above us // add a new cat above us
things.push(new thing(this.gridx, "top", getrandomname())); things.push(new thing(this.gridx, "top", "random"));
// kill ourselves // kill ourselves
var idx = things.indexOf(this); var idx = things.indexOf(this);
@ -361,19 +479,6 @@ function updateGameArea() {
myGameArea.clear(); myGameArea.clear();
myGameArea.draw(); myGameArea.draw();
myGameArea.frameNo += 1; myGameArea.frameNo += 1;
/*
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
things.push(new thing(10, height, "green", x, 0));
things.push(new thing(10, x - height - gap, "green", x, height + gap));
}
*/
for (i = 0; i < things.length; i += 1) { for (i = 0; i < things.length; i += 1) {
things[i].draw(); things[i].draw();
things[i].move(); things[i].move();

3
todo
View File

@ -78,6 +78,7 @@ start:
*drag across multi, all disappear, rest fall, new are generated *drag across multi, all disappear, rest fall, new are generated
*don't allow clicks until all grid locations are full *don't allow clicks until all grid locations are full
then change types of things *allow cat->food or cat->...->cat
cat eating food should take its place