Coderslang_Master
**CONSTANTS**
import terminalKit from 'terminal-kit';
export const term = terminalKit.terminal;
export const config = {
gold: 0,
productionRate: 0,
producers:
[
{id: 1, title: 'Miner', cost: 10, growthRate: 1.13, baseProduction: 0.1, count: 0},
{ id: 2, title: 'Adventurer', cost: 100, growthRate: 1.17, baseProduction: 1, count: 0 },
{ id: 3, title: 'Professional', cost: 1200, growthRate: 1.14, baseProduction: 9, count: 0 }
],
isInitCompleted: false,
isProducerListUpdated: true
}
**FUNCTIONS**
export const init = (term) => {
term.clear();
term.hideCursor();
term.grabInput();
term.bold.green('Welcome to the mining game!')
}
export const updateGold = (term, state) => {
term.moveTo(25, 2)
term.bold.yellow(state.gold.toFixed(2) + ' ')
term.eraseLineAfter()
state.gold += state.productionRate
}
export const checkInitCompleted = (term, state) => {
state.isInitCompleted = true
state.isProducerListUpdated = false
term.moveTo(1, 1)
term.eraseLineAfter()
term('You can purchase producers by clicking the number button (1, 2, 3, ...)')
term.moveTo(0, 2)
term.bold.yellow(`GOLD:`)
term.moveTo(0, 3)
term.bold.green(`PRODUCTION RATE: `)
}
export const updateProducerList = (term, state) => {
term.moveTo(1, 5)
for (const j in state.producers) {
if (state.producers[j].count >= 0) {
term( `${state.producers[j].title}: ${state.producers[j].count} | Production per second: ${state.producers[j].baseProduction.toFixed(1)} | Cost: ${state.producers[j].cost.toFixed(1)} ` )
} else {
term(`${state.producers[j].title}: ${state.producers[j].count} | Production per second: ${state.producers[j].baseProduction.toFixed(1)} | Cost: ${state.producers[j].cost.toFixed(1)} `)
}
}
state.isProducerListUpdated = true
}
**GAME ENGINE**
import { init } from './functions.js'
import { handleKeyPress, handleStateChange} from './handlers.js'
export const startMiningGame = (term, state) => {
init(term)
term.on('key', handleKeyPress(term, state))
setInterval(handleStateChange(term, state), 1000);
}
**SOLUTION**
import { startMiningGame } from './gameEngine.js'
import { term, config } from './constants.js'
startMiningGame(term, config)