sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Game project issue #247

Closed F2easy closed 9 months ago

F2easy commented 9 months ago

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

Questions on Current Position feature that is supposed to track the players location. Current Position is going up to 40 at times when the board only has 13 slots. This also affects other features such as the Trap, Luck and Check winner functions because they refer to Current Position.

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

const player1 = {
  canPlay: false,
  currentPosition: 0,
  element: document.getElementById("player1"),
};
const player2 = {
  canPlay: false,
  currentPosition: 0,
  element: document.getElementById("player2"),
};

  if (currentPlayer.currentPosition  === 5) {
    console.log(currentPlayer.currentPosition);
    currentPlayer.currentPosition = 0;
    console.log("Check Trap was hit");
    turnEl.innerText = "TRAPP SPOT TO THE BACK YOU GO !"
  }
}
// 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;
    console.log("CHECK LUCK WAS HIT !!!");
    roll();
  }
} // will putting these functions in the movePlayer function simulate the whole game ??

\
function checkWinner() {
  if (player1.currentPostion == 13) {
    winner == 1 ;
  } else if (player2.currentPosition == 13) {
    winner == 2;
  }

}

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?

Referencing

What things have you already tried to solve the problem?

What I attempted to do was create a function that limits currentPosition to the amount of spaces on the board (13)

Paste a link to your repository here[

](https://github.com/F2easy/Project-1-Race-Me)

timmshinbone commented 9 months ago

Sounds like an if statement would solve this, something ilke:

if(currentPosition + roll > 13) {
   //dont allow move
} else {
  //allow move, or do whatever it's supposed to do
}
F2easy commented 9 months ago
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) {
    roll = Math.floor(Math.random() * 6) + 1;
    // displays result in results element
    //display the dice results in the display element HTML
    console.log("roll result ", roll);
    resultDisplay.textContent = "Result of Roll: " + roll;
    if (roll === 5 || roll === 3) {
      if (currentPlayer.canPlay === false) {
        currentPlayer.canPlay = true;
      }else {
        movePlayer(roll)
        console.log(turn + "rolled " + roll);
      }
    } else if (currentPlayer.canPlay === true){
        if(currentPlayer.currentPosition + roll > 13) {         <---------
        }
      movePlayer(roll);
    console.log("current player can play is True")
    }
timmshinbone commented 9 months ago

Ok, you should add some comments on each line of that function relevant to moving items, explain the conditions of your if statements and the steps your game takes to figure our if/how they should move. Once you have added those comments, share the updated code. Make sure you add a 'js' to the end of the opening backtics for the codeblock so that the code looks like javascript

F2easy commented 9 months ago

Im on it chief

F2easy commented 9 months ago

let roll;
function diceRoll() {
  console.log("player's " + 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 is true then the game can officially start and players can progress
  if (gameStarted === true) {
    // roll simulates the dice rolling and finds a random number 1-6
    roll = Math.floor(Math.random() * 6) + 1;
    console.log("roll result ", roll);
    // will print a message on the screen of the result of the most previous roll
    resultDisplay.textContent = "Result of Roll: " + roll;
    // checks if the player rolling rolled a 5 or a 3 so they can advance on their following turn
    if (roll === 5 || roll === 3) {
      // if player rolled 5 or 3 set can play from false to true now allowing progression
      if (currentPlayer.canPlay === false) {
        currentPlayer.canPlay = true;
      }else {
        // once canPlay is set to true move player function is called with the roll results inside of it telling the player how many spots to move
        movePlayer(roll)
        console.log(turn + "rolled " + roll); //prints the turn p1 =1 and p2 =1 followed by the number they rolled
      }
    } else if (currentPlayer.canPlay === true){
      // checking if player can move and if  they can checks if player's roll is less then 13 which is the lenth of the board
        if(currentPlayer.currentPosition + roll > 13) {
          console.log("invalid Move ")
        }else
        // if move is valid so the result of the proggresion is less then the size of the board allow player to move
      movePlayer(roll);
    }
    // not going to execute unless gameStarte == false
  } else {
    // checks if player landed in trapspot and adjust player piece then prints message, NOTE move this to the move player function
    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 ");
      }
    }
  }
  changeCurrentPlayer();
}

function movePlayer(roll) { // this function is supposed to allow players to progress adding where they are currently at with the result of the dice roll
  console.log(currentPlayer +"= player and position = " + currentPlayer.currentPosition)
  //  takes the player's current postion and increments it by the roll they got 
  currentPlayer.currentPosition += roll 

  console.log(" currentPlayer.CurrentPostion: " + currentPlayer.currentPosition);
// checks if player's move is outside of the scope of the board
  if (currentPlayer.currentPosition + roll > board.length) {
    console.log(board.length)
    // NOTE IF PLAYER GOES OFF THE BOARD MOVE DOES NOT COUNT
    console.log("Player went off the board");
    return //exits if statement checking scope of the board
  }
  // checks where the player is reffering it  to the gameboard array
  let current = gameBoard[currentPlayer.currentPosition];
  console.log("gameboard ", gameBoard); 
  current.append(currentPlayer.element);
  console.log("currentPlayer: " + current);
  checkTrap()
  checkWinner()

} //However right now it only moves p1 as listed I want to make this function able to move whatever piece it is attached to

// getWinner --> checks to see if we have a winner / if player lands on final spot
function renderMessage() {
  if (winner == 1) {
    messageEl.innerText = " Tiger Wins the Race !!";
  } else if (winner === 2) {
    messageEl.innerText = " Bear Wins the Race !!";
  } else if (winner === null) messageEl.innerText = " Who is going to win ? !!";
}
if (turn === 1) {
  turnEl.innerText = "Tiger's Turn";
} else if (turn === 2) {
  turnEl.innerText = "Bear's Turn";
} else {
  turnEl.innerText = "Start Game !!";
}

function renderControls() {
  if (winner !== null) {
    restartButton.style.visibility = "visible";
  } else {
    restartButton.style.visibility = "hidden";
  }
} // why isn't this working ??

//              Game Logic      ///
// -StarGame button starts the game
// - each player is allowed to rolldice until one of them rolls a 5 or 3
// -whcihever player rolls a 5 or 3 will get to roll again but this time to advance
// -depending what # a  player rolls determines the # of spots they get to advance
// -if a player lands on the trapspot they will have to start over fromm the starting spot
// - if a player lands on lucky spot they get another turn, rolling again
// - if players happen to land on the same spot whichever player landed there second gets to decide whether the other player
// starts over or if they get to roll again
// -which ever player makes it to final square is deemed winner

// this function checks if player landed in trap spot and sends them back to starting spot

function checkTrap(player) {
  // this function is supposed to check if they player landed at the trapspot then send them to the beggining spot of the game
  if (currentPlayer.currentPosition  === 5) {  // checking i
    console.log(currentPlayer.currentPosition);
    currentPlayer.currentPosition = 0; // this is setting the current postion equal to 0 sending the player back to the starting spot
    console.log("Check Trap was hit");
    turnEl.innerText = "TRAP WAS HIT SPOT TO THE BACK YOU GO !" // changing text letting player know they hit the booby trap
  } ///once player starts back I want the message to go away
}
// this function checks if player landed in lucky spot and awards them with another roll
function checkLuck(player) {
  if (currentPlayer.currentPostion === 6 && turn != null) {  //checking if player hit the lucky spot
    console.log("CHECK LUCK WAS HIT !!!");
    roll(), console.log("re-roll"); // roll was awarded to player who hit lucky spot might want to ad a delya
  }
} // will putting these functions in the movePlayer function simulate the whole game ?? ```
timmshinbone commented 9 months ago

lookin good, before I read through it, did adding those comments reveal anything that might point out where the issue is?

F2easy commented 9 months ago

yeah I don't think the board array that represents the board and player.position that represents player location are linked which affect the other functions. It is also probably why player.position can even go over 13. How to fix it ?
¯_(ツ)_/¯