Skip to content
Snippets Groups Projects
Commit f02f42b8 authored by Jonathan Vitale's avatar Jonathan Vitale
Browse files

Split the game logic so to fit the user interactions

parent e766f0ba
No related branches found
Tags tutorial-7
No related merge requests found
......@@ -10,10 +10,10 @@ function collectBet(){
let isValidBet = false;
//let counter = 0;
do {
console.log("please provide your bet")
currentBet = 10; // mocking up with a value
console.log(`You provided the bet being ${currentBet}`);
if (currentBet <= 0 || currentBet > gameState.playerWallet){
console.log("please provide your bet");
let inputBetElement = document.getElementById('player-bet-input');
currentBet = Number(inputBetElement.value);
if (isNaN(currentBet) || currentBet <= 0 || currentBet > gameState.playerWallet){
console.log('Your bet is not valid!');
//counter++;
} else {
......@@ -55,6 +55,66 @@ function handValue(cards){
return sum;
}
function resetGameState(){
gameState.currentBet = null;
gameState.cards = [[], []];
gameState.hiddenCard = null;
}
function newGame(){
resetGameState();
let inputBetElement = document.getElementById('player-bet-input');
inputBetElement.disabled = false;
let buttonBetElement = document.getElementById('player-bet-button');
buttonBetElement.disabled = false;
let walletCellElement = document.getElementById('player-wallet');
walletCellElement.innerHTML = gameState.playerWallet;
let buttonRunElement = document.getElementById('button-run');
buttonRunElement.disabled = true;
}
function collectBetAndDealCards(){
gameState.currentBet = collectBet();
let inputBetElement = document.getElementById('player-bet-input');
inputBetElement.disabled = true;
let buttonBetElement = document.getElementById('player-bet-button');
buttonBetElement.disabled = true;
// we deal the first 2 cards for the player
// we deal the first 2 cards for the dealer
// one is hidden
for(let playerIndex = 0; playerIndex < gameState.cards.length; playerIndex++){
for(let cardIndex = 0; cardIndex < 2; cardIndex++){
if (cardIndex === 1 && playerIndex === 1){
gameState.hiddenCard = drawCard();
} else {
gameState.cards[playerIndex][cardIndex] = drawCard();
}
}
}
let curPlayerScore = handValue(gameState.cards[0]);
let curDealerScore = handValue(gameState.cards[1]);
console.log(`Player score is ${curPlayerScore}, Dealer score is ${curDealerScore}`);
let playerScoreElement = document.getElementById('player-score');
let dealerScoreElement = document.getElementById('dealer-score');
playerScoreElement.innerHTML = curPlayerScore;
dealerScoreElement.innerHTML = curDealerScore;
// check immediately that the player has blackjack
// if so, we need to pay 2.5 the bet -> end the game
if (curPlayerScore === 21){
console.log('Player has blackjack!');
gameState.playerWallet += gameState.currentBet*2.5;
return true;
}
}
function blackjack(){
// decide the bet, collect it
gameState.currentBet = collectBet();
......@@ -165,4 +225,22 @@ function blackjack(){
}
blackjack();
\ No newline at end of file
function addEventsListeners(){
let buttonRunElement = document.getElementById('button-run');
buttonRunElement.addEventListener(
'click',
(event) => {
newGame();
}
);
let buttonBetElement = document.getElementById('player-bet-button');
buttonBetElement.addEventListener(
'click',
(event) => {
collectBetAndDealCards();
}
);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment