Two teams try and fill in any horizontal, vertical, or diagonal line on a bingo board by playing maps on osu! osu.bingo
osu

correctly check 3x3 and 7x7 boards

+23 -15
+23 -15
src/lib/bingo-helpers/check_win.ts
··· 24 24 export const checkWin = (game: Bingo.Card) => { 25 25 if (!game.squares) return null; 26 26 27 - // Arrange the board into a 2D array. 28 - const yMax = Math.max(...game.squares.map((x) => x.y_pos)); 29 - const xMax = Math.max(...game.squares.map((x) => x.x_pos)); 30 - 31 - const board: (string | null)[] = new Array((yMax + 1) * (xMax + 1)); 32 - for (const square of game.squares) { 33 - const index = square.y_pos * 5 + square.x_pos; 34 - board[index] = square.claimed_by?.team_name ?? null; 35 - } 36 - 37 - // Get lines from template 27 + // Get board and lines from template 38 28 let template: Template; 39 29 try { 40 30 template = JSON.parse(game.template.data); 41 31 } catch { 42 - return bingoCheck(board); 32 + throw "Template is invalid"; 43 33 } 44 - const { lines } = 45 - typeof template.setup.board == 'string' ? boards[template.setup.board] : template.setup.board; 46 34 47 - return bingoCheck(board, lines); 35 + // Get board and lines from template 36 + let board: Template.Board; 37 + if (typeof template.setup.board == 'string') { 38 + board = boards[template.setup.board]; 39 + } else { 40 + board = template.setup.board; 41 + } 42 + 43 + if (!board) { 44 + throw "Template does not have a valid board"; 45 + } 46 + 47 + const boardMarks: (string | null)[] = new Array(board.squares.length); 48 + for (let i = 0; i < board.squares.length; i++) { 49 + const coords = board.squares[i]; 50 + const square = game.squares.find((x) => ((x.x_pos == coords[1]) && (x.y_pos == coords[0]))) 51 + if (!square) continue; 52 + boardMarks[i] = square.claimed_by?.team_name?.toUpperCase() ?? null 53 + } 54 + 55 + return bingoCheck(boardMarks, board.lines); 48 56 }; 49 57 50 58 /**