184 lines
3.9 KiB
JavaScript
184 lines
3.9 KiB
JavaScript
|
const fs = require("fs");
|
||
|
const { exit } = require("process");
|
||
|
const filePath = "data.txt";
|
||
|
/*
|
||
|
BINGO!
|
||
|
*/
|
||
|
let randomBingoNumbers;
|
||
|
let BingoCards= [{table: [], CardIsFinished :false}];
|
||
|
|
||
|
|
||
|
|
||
|
fs.readFile(filePath, "utf-8", (err, data)=>{
|
||
|
if( err ){
|
||
|
console.log(`ERROR! ${err.message} (code: ${err.code})`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
|
||
|
ParseFile(data);
|
||
|
|
||
|
|
||
|
|
||
|
// Draw numbers
|
||
|
for ( let i = 0 ; i < randomBingoNumbers.length ; i++ ){
|
||
|
|
||
|
console.log(`Number ${randomBingoNumbers[i]} drawn!`);
|
||
|
|
||
|
for ( let j = 0 ; j < BingoCards.length; j ++){
|
||
|
// console.log(`Card ${j}:`);
|
||
|
// console.log(BingoCards[j].rows);
|
||
|
if(BingoCards[j].CardIsFinished){
|
||
|
continue;
|
||
|
}
|
||
|
MarkNumberIfOnCard(j, randomBingoNumbers[i]);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// console.log("Bingo Cards:");
|
||
|
// console.log(BingoCards);
|
||
|
|
||
|
|
||
|
|
||
|
});
|
||
|
|
||
|
|
||
|
function MarkNumberIfOnCard(CardNumber, numberToSearch){
|
||
|
let Card = BingoCards[CardNumber];
|
||
|
|
||
|
for ( let i = 0; i < Card.table.length; i++){
|
||
|
for(let j = 0; j < Card.table[i].length; j++){
|
||
|
if( Card.table[i][j].value == numberToSearch ){
|
||
|
Card.table[i][j].IsChecked = true;
|
||
|
if( IsCardFull(i,j , Card))
|
||
|
{
|
||
|
console.log("Card is full!");
|
||
|
printBingoCard(Card);
|
||
|
|
||
|
let sum = sumUnmarkedNumbers(Card);
|
||
|
console.log(`Card ${CardNumber}`);
|
||
|
Card.CardIsFinished = true;
|
||
|
console.log(`Winning Calculation: ${sum } * ${numberToSearch}`);
|
||
|
console.log(`Final number: ${sum * numberToSearch}`);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
function sumUnmarkedNumbers(Card){
|
||
|
let result = 0;
|
||
|
for ( let i = 0; i < Card.table.length; i++){
|
||
|
for(let j = 0; j < Card.table[i].length; j++){
|
||
|
result += Card.table[i][j].IsChecked? 0: Card.table[i][j].value;
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
|
||
|
}
|
||
|
|
||
|
function printBingoCard (Card){
|
||
|
for ( let i = 0; i < Card.table.length; i++){
|
||
|
let consoleLine = '';
|
||
|
for(let j = 0; j < Card.table[i].length; j++){
|
||
|
consoleLine += `[${Card.table[i][j].value} ${Card.table[i][j].IsChecked ? 'X': ''}] `.padEnd(5);
|
||
|
}
|
||
|
console.log(consoleLine);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
function IsCardFull(row, column , Card)
|
||
|
{
|
||
|
let isComplete = true;
|
||
|
// Check if all in column are checked ;
|
||
|
for(let i = 0; i < 5; i++){
|
||
|
if (Card.table[row][i].IsChecked == false){
|
||
|
isComplete = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
if( isComplete) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
isComplete =true;
|
||
|
|
||
|
// Check if all in row are checked;
|
||
|
for(let i = 0; i < 5; i++){
|
||
|
if (Card.table[i][column].IsChecked == false){
|
||
|
isComplete = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return isComplete;
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
function ParseFile(data){
|
||
|
|
||
|
// First line is the random bingo numbers
|
||
|
randomBingoNumbers= data.split('\n')[0].split(',');
|
||
|
|
||
|
let StartindexBingoCards = 2;
|
||
|
let bingoCard = 0;
|
||
|
let linesOfData = data.split('\r\n');
|
||
|
|
||
|
for ( let i = StartindexBingoCards; i < linesOfData.length ; i++)
|
||
|
{
|
||
|
|
||
|
|
||
|
let rowOfNumbers = linesOfData[i].split(' ');
|
||
|
rowOfNumbers = rowOfNumbers.filter( (value, index)=> {
|
||
|
return value != '' && value != ' ';
|
||
|
});
|
||
|
|
||
|
|
||
|
// console.log(rowOfNumbers);
|
||
|
|
||
|
|
||
|
|
||
|
if(rowOfNumbers.length == 0){
|
||
|
bingoCard++;
|
||
|
BingoCards.push( {table:[]});
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
BingoCards[bingoCard].table.push(rowOfNumbers.map((value)=> ({value: Number(value), IsChecked: false}) ));
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/*
|
||
|
|
||
|
rowOfNumbers.filter((value, index )=>{
|
||
|
return value.length < 1 ;
|
||
|
});
|
||
|
|
||
|
console.log(rowOfNumbers);
|
||
|
*/
|
||
|
|
||
|
|
||
|
/*
|
||
|
|
||
|
|
||
|
|
||
|
BingoCards[bingoCard].table.push(bingoCardsRows) ;
|
||
|
*/
|