// 1. deposit money // 2. determine number of lines to bet on // 3. collect a bet amount // 4. spin the slot machine // 5. check if the user won or lost // 6. give the winnings or take the money // 7. play again const prompt = require("prompt-sync")(); // 4. spin the slot machine const ROWS = 3; const COLS = 3; const SYMBOL_COUNT = { //THIS IS KNOWN AS AN OBJECT A:2, B:4, C:6, D:8 }; const SYMBOL_VALUES = { A:5, B:4, C:3, D:2 }; // 1. deposit money function deposit () { // this is anothert way of making functions using es6 while(true) { // this is to make sure that the user enters a valid amount const depositAmount = prompt("Enter your deposit amount: "); const numberDepositAmount = parseFloat(depositAmount); // this will turn a string into an integer if (isNaN(numberDepositAmount)/* this chcks if something is not a number*/ || numberDepositAmount <= 0){ console.log('Invalid deposit amount, please try again'); } else{ return numberDepositAmount; // this will break the while loop } } }; // 2. determine number of lines to bet on function getNumberOfLines () { while(true) { // this is to make sure that the user enters a valid amount const lines = prompt("Enter the number of lines (1-3): "); const numberOfLines = parseFloat(lines); // this will turn a string into an integer if (isNaN(numberOfLines)/* this chcks if something is not a number*/ || numberOfLines <= 0 || numberOfLines > 3){ console.log('Invalid amount, please try again'); } else{ return numberOfLines; // this will break the while loop } } } // 3. collect a bet amount function getBet (balance,lines)/* it is going to be based on the balance*/ { while(true) { // this is to make sure that the user enters a valid amount const bet = prompt("Enter the bet per line: "); const totalBet = parseFloat(bet); // this will turn a string into an integer if (isNaN(totalBet)/* this chcks if something is not a number*/ || totalBet <= 0 || totalBet > balance / numberOfLines)/* this will check if there is enough*/{ console.log('Invalid amount, please try again'); } else{ return totalBet; // this will break the while loop } } } // 4. spin the slot machine function spin() { const symbols = [];// this is known as an array for (const[symbol, count] of Object.entries(SYMBOL_COUNT)){ for (let i = 0; i < count; i++){ symbols.push(symbol); } } const reels = []; // this represents the coloumns for (let i = 0; i < COLS; i++){ reels.push([]); // this is made so that there is no erro is the no. cols is more than 3 const reelSymbols = [...symbols]; for(let j = 0; j < ROWS; j++){ const randomIndex = Math.floor(Math.random() * reelSymbols.length); const selectedSymbol = reelSymbols[randomIndex]; reels[i].push(selectedSymbol); //index i is gonna represent thge reel we are working on reelSymbols.splice(randomIndex, 1); // the one here is to remove one element } } return reels; }; function convertCols(reels){ const rows = []; for(let i = 0; i < ROWS; i++){ // now for every single row, it will loof through every single coloumn rows.push([]); for(let j = 0; j < COLS; j++){ rows[i].push(reels[j][i]); // here ew are accessing each coulomn then we are getting the elements in the rows of the coloumns } } return rows; } function printRows (rows){ for (row of rows){ let rowString = ""; for(const [i, symbol] of row.entries()){ rowString += row; if( i != row.length - 1){ rowString += " | "; } } console.log(rowString); } } let balance = deposit(); // its not a constant bcz later the balance will adjusted based on what they are betting const numberOfLines = getNumberOfLines(); const betAmount = getBet(balance, numberOfLines); const reels = spin(); const convert_cols = convertCols(reels); const print_rows = printRows(rows); // 5. check if the user won or lost
ReferenceError: rows is not defined at Object.<anonymous> (D:\slot-machine\script.js:131:31) at Module._compile (node:internal/modules/cjs/loader:1119:14) at Module._extensions..js (node:internal/modules/cjs/loader:1173:10) at Module.load (node:internal/modules/cjs/loader:997:32) at Module._load (node:internal/modules/cjs/loader:838:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:18:47 Node.js v18.9.0
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)