Skip to content

Instantly share code, notes, and snippets.

@DenverCoder1
Created July 22, 2022 22:36
Show Gist options
  • Save DenverCoder1/7c14515e1f09fca72e49486a1a1d2c37 to your computer and use it in GitHub Desktop.
Save DenverCoder1/7c14515e1f09fca72e49486a1a1d2c37 to your computer and use it in GitHub Desktop.
Codenames Key Board Generator
/**
* Codenames.gs - JavaScript Codenames Key Board Generator
*
* This script was created for use in a Google Spreadsheet. See this link below
* for a printable version of the game and spreadsheets that generate the key boards.
*
* https://drive.google.com/drive/folders/1KHwA2W96sawlWOQq8Hl3FiUygB6PxG8o
*
* (c) 2020-2022, Jonah Lawrence
*
* This script is licensed under the MIT license.
*
* This script is not affiliated with or endorsed by Codenames or CGE.
*/
/**
* Shuffles an array in place.
* @param arr {Array} The array to shuffle.
* @returns {Array} The shuffled array.
*/
function shuffle(arr) {
var j, x, i;
for (i = arr.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = arr[i];
arr[i] = arr[j];
arr[j] = x;
}
return arr;
}
/**
* Convert a 1-dimensional array to a 2-dimensional array.
* @param list {Array} The list to convert.
* @param elementsPerSubArray {Number} The number of elements per row.
* @returns {Array} The 2-dimensional array.
*/
function listToMatrix(list, elementsPerSubArray) {
var matrix = [], i, k;
for (i = 0, k = -1; i < list.length; i++) {
if (i % elementsPerSubArray === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(list[i]);
}
return matrix;
}
/* CODENAMES CLASSIC */
/**
* Create a classic Codenames board with red as the first player.
* @returns {Array} The board for the game.
*/
function boardRed() {
var board = ["×", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "○", "○", "○", "○", "○", "○", "○", "○", " ", " ", " ", " ", " ", " ", " "];
return listToMatrix(shuffle(board), 5);
}
/**
* Create a classic Codenames board with blue as the first player.
* @returns {Array} The board for the game.
*/
function boardBlue () {
var board = ["×", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "○", "○", "○", "○", "○", "○", "○", "○", "○", " ", " ", " ", " ", " ", " ", " "];
return listToMatrix(shuffle(board), 5);
}
/* CODENAMES PICTURES */
/**
* Create a Codenames Pictures board with red as the first player.
* @returns {Array} The board for the game.
*/
function boardRedPictures() {
var board = ["×", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "○", "○", "○", "○", "○", "○", "○", " ", " ", " ", " "];
return listToMatrix(shuffle(board), 5);
}
/**
* Create a Codenames Pictures board with blue as the first player.
* @returns {Array} The board for the game.
*/
function boardBluePictures() {
var board = ["×", "◇", "◇", "◇", "◇", "◇", "◇", "◇", "○", "○", "○", "○", "○", "○", "○", "○", " ", " ", " ", " "];
return listToMatrix(shuffle(board), 5);
}
/* CODENAMES DUET */
/**
* Create a pair of Codenames Duet boards with with spacing in between, foldable across the vertical axis.
* @returns {Array} The boards for the game.
*/
function boardDuet() {
var boardPairs = shuffle([
["×", "×"],
["×", "★"],
["×", " "],
["★", "★"],
["★", "★"],
["★", "★"],
["★", " "],
["★", " "],
["★", " "],
["★", " "],
["★", " "],
["★", "×"],
[" ", "★"],
[" ", "★"],
[" ", "★"],
[" ", "★"],
[" ", "★"],
[" ", "×"],
[" ", " "],
[" ", " "],
[" ", " "],
[" ", " "],
[" ", " "],
[" ", " "],
[" ", " "],
]);
var board1 = listToMatrix(boardPairs.map(function (pair) { return pair[0]; }), 5);
var board2 = listToMatrix(boardPairs.map(function (pair) { return pair[1]; }), 5);
var output = [];
for (var i = 0; i < board1.length; ++i) {
output.push(board1[i].concat([" ", " ", " ", " ", " "]).concat(board2[i]));
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment