xxxxxxxxxx
215
class Player {
constructor(id) {
this.id = id; // Player ID
this.pits = this.createPits(); // Array of pits belonging to the player
this.potWidth = floor(boardW / 9); // Width of each pit on the game board
this.currentPit = 0; // Index of the currently selected pit
this.conquered = false; // Flag indicating if the player has conquered a pit
}
// Create and initialize the pits for the player
createPits() {
let pits = [];
for (let i = 0; i < 9; i++) {
pits.push(new Pit(i, 9, this.id));
}
return pits;
}
// Fill the bottom pit with pebbles - PLAYER 2
fillBottomPot(x, y, balls) {
let ballSpacing = 2; // space between each pebble
let numRows = 10; // max num of rows
let numCols = 3; // max num of columns
for (let row = 0; row < numRows && balls > 0; row++) {
for (let col = 0; col < numCols && balls > 0; col++) {
let ballX = x + col * (ballR + ballSpacing); // locate X for the pebble
let ballY = y - row * (ballR + ballSpacing * 2.5); // locate Y for the pebble
image(pebble_img, ballX + ballR, ballY - ballR, ballR, ballR);
balls--; // decriment the number of pebbles to be drawn
}
}
}
// Fill the bottom storehouse with pebbles - PLAYER 2
fillBottomStorehouse(x, y, balls) {
let ballSpacing = 2; // space between each pebble
let numRows = 2; // max num of rows
let numCols = 43; // max num of columns
for (let row = 0; row < numRows && balls > 0; row++) {
for (let col = 0; col < numCols && balls > 0; col++) {
let ballX = x + col * (ballR + ballSpacing); // locate X for the pebble
let ballY = y - row * (ballR + ballSpacing * 2.5); // locate Y for the pebble
image(pebble_img, ballX + ballR, ballY - ballR, ballR, ballR);
balls--; // decriment the number of pebbles to be drawn
}
}
}
// Fill the top pit with pebbles - PLAYER 1
fillTopPot(x, y, balls) {
let ballSpacing = 2; // space between each pebble
let numRows = 10; // max num of rows
let numCols = 3; // max num of columns
for (let row = 0; row < numRows && balls > 0; row++) {
for (let col = 0; col < numCols && balls > 0; col++) {
let ballX = x + col * (ballR + ballSpacing); // locate X for the pebble
let ballY = y + row * (ballR + ballSpacing * 2.5); // locate Y for the pebble
image(pebble_img, ballX + ballR, ballY + ballR, ballR, ballR);
balls--; // decriment the number of pebbles to be drawn
}
}
}
// Fill the top storehouse with pebbles - PLAYER 1
fillTopStorehouse(x, y, balls) {
let ballSpacing = 2; // space between each pebble
let numRows = 2; // max num of rows
let numCols = 43; // max num of columns
for (let row = 0; row < numRows && balls > 0; row++) {
for (let col = 0; col < numCols && balls > 0; col++) {
let ballX = x + col * (ballR + ballSpacing); // locate X for the pebble
let ballY = y + row * (ballR + ballSpacing * 2.5); // locate Y for the pebble
image(pebble_img, ballX + ballR, ballY + ballR, ballR, ballR);
balls--; // decriment the number of pebbles to be drawn
}
}
}
// Check if the current pit has no pebbles
notEmpty() {
while (this.pits[this.currentPit].pebbles == 0) {
// Move to the next pit
this.currentPit += 1;
if (this.currentPit == 9) this.currentPit = 0;
}
}
// Move pit selection window
moveCurrentPit(dir) {
// Check if moving in the specified direction would be out of bounds
if (this.currentPit + dir < 0 || this.currentPit + dir >= 9) {
return; // Return if out of bounds
}
// Move the current pit in the specified direction
this.currentPit += dir;
}
//
//DISPLAY OF EVERYTHING IN THE GAMe
//
display() {
this.pits.forEach((pot, i) => {
let check = this.id == 1 ? 8 - i : i;
let x = this.potWidth * check;
let y = this.id == 1 ? 20 : boardH - margin * 22.25;
if (i == this.currentPit && this.id == game.currentPlayer) {
//HIGHLIGHT SELECTED PIT
fill(0, 244, 20);
if (game.currentPlayer == 1) {
rect(x + margin, y - margin / 2, pitW, pitH / 12);
} else {
rect(x + margin, y + margin * 17.5, pitW, pitH / 12);
}
}
//
//PLAYER 1
//
//DISPLAY UPPER PART OF THE BOARD
if (this.id == 1) {
//PIT
image(player1_pit_img, x + margin, y - margin * 2, pitW, pitH);
//PIT NUMBER
fill(240, 232, 26);
textSize(generalFS);
textFont("depixelklein");
text(i + 1, x + margin / 2 + pitW / 2, y + margin * 1.5);
//PEBBLES
// add pebbles to the pits
this.fillTopPot(x + margin, y + margin * 1.5, pot.pebbles);
//DRAW ONE OF
if (i == 8) {
//STOREHOUSE - PLAYER 1
image(
player1_store_img,
x + margin * 2.75 + pitW,
y + pitH - margin,
storehouseW,
storehouseH
);
// fill the storehouse
this.fillTopStorehouse(
x + margin * 2.75 + pitW,
y + pitH - margin * 2.25,
game.storehouses[0]
);
//SCORE BOX - PLAYER 1
image(
player1_score_img,
x + margin,
y + pitH - margin,
scoreW,
scoreH
);
// score number
text(
game.storehouses[0],
x + scoreW / 2,
y + pitH - margin + scoreH / 1.5
);
}
}
//
//PLAYER 2
//
//DISPLAY BOTTOM PART OF THE BOARD
else if (this.id == 2) {
//PIT
image(player2_pit_img, x + margin, y - margin * 4, pitW, pitH);
//PIT NUMBER
fill(240, 232, 26);
textSize(generalFS);
textFont("depixelklein");
text(i + 1, x + margin / 2 + pitW / 2, y - storehouseH + pitH + margin);
//PEBBLES
// add pebbles to the pits
this.fillBottomPot(x + margin, y + margin * 15, pot.pebbles);
//DRAW ONE OF
if (i == 0) {
//STOREHOUSE - PLAYER 2
image(
player2_store_img,
x + margin,
y + margin * 13.5 - pitH,
storehouseW,
storehouseH
);
// fill the storehouse
this.fillBottomStorehouse(
x + margin,
y + margin * 13.5 - pitH + margin * 5.5,
game.storehouses[1]
);
//SCORE BOX - PLAYER 2
image(
player2_score_img,
x + margin * 2.75 + storehouseW,
y + margin * 13.5 - pitH,
scoreW,
scoreH
);
// score number
text(
game.storehouses[1],
x + storehouseW + scoreW / 1.5,
y + margin * 13.5 - pitH + scoreH / 1.5
);
}
}
});
}
}