homework2/main.js
2025-03-10 14:36:34 +09:00

143 lines
2.8 KiB
JavaScript

/*
DI311 Homework 2
Author: Your name (KAIST ID)
*/
// Constants
const FONT_SIZE = 20;
const DEFAULT_BOARD_DIMENSION = 10;
const REFRESH_RATE = 100;
// The Board
const board = {
cells: [], // cells are an array like [true, false, true, true, ...]
size: 10, // the dimension of the board, like 10x10
totAlive: 0, // number of cells alive
days: 0, // days since we started teh colony
editable: false, // can we modify the board?
cellProperties: {
color: '#98E024', // the color of the alive cells
size: 0, // the size in pixels of the cells
},
};
// YOU CAN CHANGE ANYTHING AFTER THIS LINE
// =======================================================
// Program's entry point
function setup() {
createCanvas(800, 800);
// Initialize the baord for the first time
initBoard(DEFAULT_BOARD_DIMENSION);
setInterval(updateBoard, REFRESH_RATE);
}
function draw() {
background(0);
// Draw the baord with the cells
drawBoard(board);
// Draw the head up display
drawLifeCounter(board.days);
}
// ------------------------------------------------- //
// Some functions definitions to get you started
// Access to cells
/**
* Initialize a new Board with a given dimension N
* @param {number} boardSize - N is a positive number to determine how many cells NxN
*/
function initBoard(boardSize) {
if (boardSize < 0) throw new Error('Invalid board size');
// PLACEHOLDER
console.log('Initialize the board :' + board);
}
/**
*
* @param {Array} cells - the array of cells
* @param {number} r - row
* @param {number} c - column
* @param {boolean} value - setting the cell value true or false
*/
function setCell(cells, r, c, value) {
// PLACEHOLDER}
}
/**
*
* @param {Array} cells - the array of cells
* @param {number} r - row
* @param {number} c - column
* @returns true or false
*/
function getCell(cells, r, c) {
// PLACEHOLDER
}
// Draw and update logic
/**
* draw the board
*/
function drawBoard() {
// PLACEHOLDER
background(0);
}
/**
* draw the text "Life: days"
*/
function drawLifeCounter(days) {
// PLACEHOLDER: Draw the text
}
/**
* Update the board state, including the cells
*/
function updateBoard() {
// PLACEHOLDER
console.log('Updating the baord');
}
// User Input
/**
* When the user press a key
*/
function keyPressed() {
// PLACEHOLDER
console.log(`key ${key} is pressed`);
}
/**
* When the mouse wheel is spun
* @param {event} event - event.delta shows the amount of scross
*/
function mouseWheel(event) {
// PLACEHOLDER
console.log('mouse wheel: ', event.delta);
}
/**
* When the mouse is clicked
*/
function mousePressed() {
// PLACEHOLDER
console.log('mouse pressed');
}
/**
* When the mouse is dragged (after being pressed)
*/
function mouseDragged() {
// PLACEHOLDER
console.log('mouse dragged');
}
// FEEL FREE TO ADD MORE FUNCTIONS AND TO MODIFY THE ONE ABOVE...