xxxxxxxxxx
309
let hole1, hole2, hole3, hole4, hole5, homeA, hole6, hole7, hole8, hole9, hole10, homeB;
let state = 0;
playerAturn = true;
let player;
let holes;
let oppositeHoles;
let holeNum;
let addShell;
let shells;
function setup() {
createCanvas(600, 300);
textSize(25);
textFont('Poppins');
//set up holes on board
hole1 = new System(410, 129, 56, 56, 7);
hole2 = new System(340, 129, 56, 56, 7);
hole3 = new System(272, 129, 56, 56, 7);
hole4 = new System(202, 129, 56, 56, 7);
hole5 = new System(133, 129, 56, 56, 7);
homeA = new System(73, 80, 43, 79, 0);
hole6 = new System(133, 53, 56, 56, 7);
hole7 = new System(202, 53, 56, 56, 7);
hole8 = new System(272, 53, 56, 56, 7);
hole9 = new System(340, 53, 56, 56, 7);
hole10 = new System(410, 53, 56, 56, 7);
homeB = new System(482, 80, 43, 79, 0);
}
function mousePressed() {
//switch values of arrays depending on which player's turn it is
//player a's turn
if (playerAturn) {
//holes player can click
player = [hole1, hole2, hole3, hole4, hole5];
//holes where shells are distributed
holes = [hole1, hole2, hole3, hole4, hole5, homeA, hole6, hole7, hole8, hole9, hole10];
//opposite holes (for checking the last shell)
oppositeHoles = [hole10, hole9, hole8, hole7, hole6, homeA, hole5, hole4, hole3, hole2, hole1];
//if player b's turn
} else {
player = [hole6, hole7, hole8, hole9, hole10];
holes = [hole6, hole7, hole8, hole9, hole10, homeB, hole1, hole2, hole3, hole4, hole5];
oppositeHoles = [hole5, hole4, hole3, hole2, hole1, homeB, hole10, hole9, hole8, hole7, hole6];
}
//detect mouse click and whether it's in the player's hole and if it's not empty
for (let i = 0; i < player.length; i++) {
if (player[i].clicked(mouseX, mouseY) && player[i].total > 0) {
//change to animate state (state 1)
state = 1;
//declare values for distribution loop in state 1
shells = holes[i].total;
holeNum = i;
addShell = i;
}
}
}
function draw() {
//state 0 is waiting for click state
if (state == 0){
drawAllHoles();
}
//state 1 is where shells are distributed/animated
else if (state==1){
//loop for the number of shells in the selected hole
for (let j = 1; j < shells + 1; j++) {
//remove shell from selected hole
holes[holeNum].remove();
//to determine which hole to add the next shell to
//if the end of the holes array is reached, go back to position 0
if (addShell > holes.length - 2) {
addShell = 0;
//else increment by 1
} else {
addShell = addShell + 1;
}
//add shell to new hole
holes[addShell].add();
//fade(holes[addShell].s);
}
//for testing purposes, change player at the end of the turn
changePlayer();
//for the real thing, check where the last shell lands
//checkLastHole(addShell, holes, oppositeHoles);
//return to state 0 (waiting)
state = 0;
}
}
// function fade(s){
// print("I'm changing");
// if (s.color != 100) {
// s.color = s.color - 100;
// }
// s.show();
// }
//draw all the holes
function drawAllHoles() {
let drawHoles = [hole1, hole2, hole3, hole4, hole5, homeA, hole6, hole7, hole8, hole9, hole10, homeB];
background(0);
for (let i = 0; i < drawHoles.length; i++) {
drawHoles[i].show();
}
drawText();
}
//change player
function changePlayer() {
if (playerAturn) {
playerAturn = false;
} else {
playerAturn = true;
}
}
//check last hole to determine next behavior
function checkLastHole(lastHole, holes, oppositeHoles) {
//if last hole lands in the player's home, do not change player
if (lastHole == 5) {
print("Landed in home. Player's turn again!");
}
else {
//if the last hole is not empty, continue distributing the shells in that hole
if (holes[lastHole].total > 1) {
print("I will continue distributing these shells");
let shells = holes[lastHole].total;
let holeNum = lastHole;
for (let j = 1; j < shells + 1; j++) {
holes[lastHole].remove();
if (holeNum > holes.length - 2) {
holeNum = 0;
} else {
holeNum = holeNum + 1;
}
holes[holeNum].add();
}
print("I am done distributing these shells.");
//check last hole again (recursion!)
checkLastHole(holeNum, holes, oppositeHoles);
}
//if last hole is empty...
else {
//and the opposite hole has shells, transfer all the shells from the opposite hole to this hole
if (oppositeHoles[lastHole].total > 0) {
print("I will transfer the shells from oppsite hole to this hole.")
shells = oppositeHoles[lastHole].total;
for (let i = 0; i < shells; i++) {
oppositeHoles[lastHole].remove();
holes[lastHole].add();
}
print("Shells transfered");
}
//else, do nothing
//end turn and change player
changePlayer();
}
}
}
//draw counter's and text on board
function drawText() {
let playerA = [hole1, hole2, hole3, hole4, hole5];
let playerB = [hole6, hole7, hole8, hole9, hole10];
let homes = [homeA, homeB];
textAlign(CENTER, CENTER)
noStroke();
fill(0, 255, 255);
text("A", homeA.x + (homeA.w / 2), homeA.y - 20);
text(homeA.total, homeA.x + (homeA.w / 2), homeA.y + homeA.h + 25);
for (let i = 0; i < playerA.length; i++) {
text(playerA[i].total, playerA[i].x + (playerA[i].w / 2), playerA[i].y + playerA[i].h + 25);
}
fill(255, 255, 0);
text("B", homeB.x + (homeB.w / 2), homeB.y - 20);
text(homeB.total, homeB.x + (homeB.w / 2), homeB.y + homeB.h + 25);
for (let i = 0; i < playerB.length; i++) {
text(playerB[i].total, playerB[i].x + (playerB[i].w / 2), playerB[i].y - 20);
}
if (playerAturn) {
fill(0, 255, 255);
text("Player A's Turn", width / 2, height - 40);
} else {
fill(255, 255, 0);
text("Player B's Turn", width / 2, height - 40);
}
}
//define hole class
class System {
//construct hole
constructor(x, y, w, h, total) {
this.hole = [];
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.total = total; //total number of shells in hole
//random positioning of shells within hole
for (let i = 0; i < this.total; i++) {
this.posx = int(random(this.x + 10, this.w + this.x - 10));
this.posy = int(random(this.y + 10, this.h + this.y - 10));
this.hole[i] = new Particle(this.posx, this.posy, 100);
}
}
//detect whether click was within the hole
clicked(px, py) {
let d = dist(px, py, this.x + (this.w / 2), this.y + (this.h / 2));
if (d < this.w / 2) {
return true;
} else {
return false;
}
}
//display hole
show() {
noFill();
strokeWeight(2);
stroke(255, 255, 255);
rect(this.x, this.y, this.w, this.h);
for (let shell of this.hole) {
shell.show();
shell.fade();
}
// console.log('show 263');
}
//remove shell from hole
remove() {
this.hole.pop();
this.total = this.total - 1;
}
//add shell to hole
add() {
let s = new Particle(int(random(this.x + 10, this.w + this.x - 10)), int(random(this.y + 10, this.h + this.y - 10)), 300);
this.hole.push(s);
this.total = this.total + 1;
// fade(s);
}
}
//define shell class
class Particle {
//construct particle
constructor(x, y, color) {
this.x = x;
this.y = y;
this.color = color;
}
fade() {
print("I'm changing");
if (this.color >= 100) {
this.color = this.color - 1;
}
this.show();
}
//show
show() {
fill(this.color);
strokeWeight(1);
stroke(255)
circle(this.x, this.y, 5);
}
}