//Created by Paul McKee for CA318
//Original and new to Ancient Brain
const ROWS = 6;
const COLS = 7;
const SIZE = 100;
const RADIUS = SIZE * 0.4;
let board = [];
let currentPlayer = 1;
let gameOver = false;
let restartButton;
let winner = 0; // Initialise the winner variable
let winStartCol, winStartRow, winEndCol, winEndRow;
let animationFrame = 0;
let isTimeFrozen = false;
function setup() {
let canvasX = (windowWidth - COLS * SIZE) / 2;
let canvasY = (windowHeight - (ROWS + 1) * SIZE) / 2; //
createCanvas(COLS * SIZE, (ROWS + 1) * SIZE).position(canvasX, canvasY);
for (let row = 0; row < ROWS; row++) {
board.push(Array.from({ length: COLS }, () => 0));
}
gameStartTime = millis();
}
function draw() {
background(0, 0, 255); // Change grid color to blue
translate((width - COLS * SIZE) / 2, (height - (ROWS + 1) * SIZE) / 2);
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
fill(255);
stroke(0);
ellipse(col * SIZE + SIZE / 2, (row + 1) * SIZE + SIZE / 2, RADIUS * 2);
if (board[row][col] === 1) {
fill(255, 0, 0);
ellipse(col * SIZE + SIZE / 2, (row + 1) * SIZE + SIZE / 2, RADIUS * 2);
} else if (board[row][col] === 2) {
fill(255, 255, 0);
ellipse(col * SIZE + SIZE / 2, (row + 1) * SIZE + SIZE / 2, RADIUS * 2);
}
}
}
if (winner !== 0) {
fill(0);
textSize(30);
textAlign(RIGHT, CENTER);
text("Player " + winner + " wins!", width - 2, 80);
}
if (!isTimeFrozen) {
gameTimeElapsed = (millis() - gameStartTime) / 1000;
}
fill(0);
textSize(20);
textAlign(RIGHT, CENTER);
text("Time Elapsed: " + nf(gameTimeElapsed, 0, 2) + "s", width - 10, 40);
// Display who turn it is
textSize(20);
textAlign(CENTER, CENTER);
let currentPlayerText = "";
if (currentPlayer === 1) {
fill(255, 0, 0); // Red for Player 1
currentPlayerText = "Player 1's Turn";
} else {
fill(255, 255, 0); // Yellow for Player 2
currentPlayerText = "Player 2's Turn";
}
text(currentPlayerText, width / 6, height - 650);
fill(255);
textSize(32);
textAlign(CENTER, CENTER);
text("CONNECT 4", width / 2, 40);
fill(255, 0, 0);
textSize(20);
textAlign(CENTER, CENTER);
text("Press R to restart", width / 2, 80);
}
let clickSound;
function preload() {
clickSound = loadSound('uploads/paulmckee/Click.mp3'); // load in click sound
}
function mousePressed() {
if (gameOver) return;
const col = floor(mouseX / SIZE);
if (col >= 0 && col < COLS) {
for (let row = ROWS - 1; row >= 0; row--) {
if (board[row][col] === 0) {
board[row][col] = currentPlayer;
currentPlayer = 3 - currentPlayer; // Switch player (1 to 2 or 2 to 1)
clickSound.play(); // Play the click sound
winner = checkWinner();
if (winner !== 0) {
gameOver = true;
}
if (winner !== 0) {
if (!isTimeFrozen) {
isTimeFrozen = true; // Freeze the time counter
}
}
break;
}
}
}
winner = checkWinner();
if (winner !== 0) {
// animation variables
winStartCol = col;
winStartRow = row;
winEndCol = col + 3; // Assuming a horizontal win
winEndRow = row;
animationFrame = 0;
gameOver = true;
}
}
function keyPressed() {
if (key === 'r' || key === 'R') {
restartGame();
}
}
function checkWinner() {
// Check horizontally
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col <= COLS - 4; col++) {
if (board[row][col] !== 0 &&
board[row][col] === board[row][col + 1] &&
board[row][col] === board[row][col + 2] &&
board[row][col] === board[row][col + 3]) {
return board[row][col];
}
}
}
// Check vertically
for (let col = 0; col < COLS; col++) {
for (let row = 0; row <= ROWS - 4; row++) {
if (board[row][col] !== 0 &&
board[row][col] === board[row + 1][col] &&
board[row][col] === board[row + 2][col] &&
board[row][col] === board[row + 3][col]) {
return board[row][col];
}
}
}
// Check diagonals (top-left to bottom-right)
for (let row = 0; row <= ROWS - 4; row++) {
for (let col = 0; col <= COLS - 4; col++) {
if (board[row][col] !== 0 &&
board[row][col] === board[row + 1][col + 1] &&
board[row][col] === board[row + 2][col + 2] &&
board[row][col] === board[row + 3][col + 3]) {
return board[row][col];
}
}
}
// Check diagonals (top-right to bottom-left)
for (let row = 0; row <= ROWS - 4; row++) {
for (let col = 3; col < COLS; col++) {
if (board[row][col] !== 0 &&
board[row][col] === board[row + 1][col - 1] &&
board[row][col] === board[row + 2][col - 2] &&
board[row][col] === board[row + 3][col - 3]) {
return board[row][col];
}
}
}
return 0; // No winner yet
}
function restartGame() {
console.log('Button clicked') // Could not get restart button to work hence I was checking for the function being called in console.
window.location.reload(); // Instead I mapped the R key to reload the page to overcome this problem.
}