sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Game Logic issue Can't get roll function to roll again and change it's value #230

Closed F2easy closed 9 months ago

F2easy commented 9 months ago

What's the problem you're trying to solve?

got carried away coding and not testing and lost myself. I'm trying to work on my game's algorithim

Post any code you think might be relevant (one fenced block per file)

 function movePlayer(player,roll){
        let currentPosition =Array.from(gameBoard).findIndex(element =>
            element.textContent === player);
       let newPosition = currentPostion += roll;
        if(newPosition >= board.length){
            newPosition -= board.length;
        }
        let current = gameBoard[newPosition]    ///this function rolls the dice and moves players that is selected
    current.append(player);              /// working backwards from the array starting at 0
    console.log(currentPostion)
            console.log()
    }

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

What is your best guess as to the source of the problem?

I just need help debugging and working through what i wrote

What things have you already tried to solve the problem?

Paste a link to your repository here

nayaba commented 9 months ago

I'd start with, what happens when you click on your start button? Is it what's supposed to happen? Do you have a detailed game plan of what's supposed to happen? If not, I would break this down with some pseudo code.

F2easy commented 9 months ago

I have some pseudocode written even though I haven't coded the start button yet, when the start button is clicked it is supposed to make turn go from null to 1 which indicates player1's turn and it starts the game. I figured what was wrong with that function I was missing a parameter lol. I still do need some assistance though

nayaba commented 9 months ago

What else is going on?

F2easy commented 9 months ago

I'm having a reference error with my position variable and can't find the issue

nayaba commented 9 months ago

Could you post the error?

F2easy commented 9 months ago

let currentPostion = 0;

function checkTrap(player){ if(currentPostion == 5){ return true && currentPosition + 7 } } // this function checks if player landed in lucky spot and awards them with another roll function checkLuck(player){
if(currentPostion == 6 && turn != null){ return true && currentPostion - 6
roll() } } // will putting these functions in the movePlayer function simulate the whole game ??

function progress() { let rollDice = diceRoll(); let newPosition = currentPostion + diceRoll let newSpot return newSpot = board[newPosition] }

nayaba commented 9 months ago

I'm sorry, what exactly is the error? I know you said you were having a reference error, but how do you know that? Was there an error message in the console, and if so, could you please post it?

F2easy commented 9 months ago

raceMe.js:144 Uncaught ReferenceError: Cannot access 'currentPostion' before initialization at raceMe.js:144:20 (anonymous) @ raceMe.js:144

nayaba commented 9 months ago

Cannot access 'currentPostion' before initialization tells me that somewhere you're code is looking for either const or let currentPosition, but it's either not there or below where you're using currentPosition

F2easy commented 9 months ago

I resolved the original issue but here is my new one:

function startGame() { if(!gameStarted){ gameStarted = true; turn = 1 console.log("player "+ turn + " turn") console.log(gameStarted) }}

function changeCurrentPlayer(){ turn = turn === 1 ? 2 : 1; console.log(turn) if(turn === 1){ currentPlayer = player1 } else{ currentPlayer = player2 } }

function diceRoll(){ console.log("player " + turn + " turn") /// roles the dice so players can progress //attach to roll the dice button // checking whether resulr is 5 or 3 so we can start game if (gameStarted === true){

// displays result in results element
//display the dice results in the display element HTML
    console.log(roll)
resultDisplay.textContent = ("Result of Roll: " + roll);
    if(roll === 5 || roll === 3){
        if(currentPlayer.canPlay==false){
        currentPlayer.canPlay = true
        return
        } 
        movePlayer(roll)
        console.log("is roll always 5 or 3 " + roll )
        gameStarted = true;
        } else { 

    console.log(currentPlayer + "rolled " + roll)

        } 
        // not going to execute unless gameStarte == false
    } else {
        // checks if player landed in trapspot and adjust player piece then prints message
        if (checkTrap()){
        resultDisplay.textContent="Yikes you landed in the Trap Spot ";
        } else{
        //checks if player landed in luckspot and adjust player piece then prints message
            if (checkLuck()){
            resultDisplay.textContent("Nice You landed in the Lucky Spot ")
            }
    }

}}

function movePlayer(roll){
   // let currentPosition = board.indexOf(`sp${currentPlayer.position}`);
    //console.log(currentPosition)
    let newPosition = currentPlayer.currentPostion + roll;
    if(newPosition >= board.length){
        newPosition -= board.length;
    }

    let current = gameBoard[newPosition]    ///this function rolls the dice and moves players that is selected

current.append(currentPlayer.element); /// working backwards from the array starting at 0 }

F2easy commented 9 months ago

so the issue I am having here is that once the player rolls the result of that roll is stuck to both players. I am not getting a new roll. I'm not getting a new roll also the player play 2x instead of going one time each

F2easy commented 9 months ago

Well in this code I've figured out that roll is not changing from 5 or 3 after the if statement that checks whether 5 || 3 was rolled to start the game.

timmshinbone commented 9 months ago

roll function was fixed by changing the variable from const in the global scope to let, and moving the roll functionality into the roll function.