Problem on If-else statement

Hi All I am now doing a project on p5.js gaming. As I want to do random picking on CPU/Player playing first, I am using the concept of random( ) and if-else statement. However, it does not work. Would like to ask what is the problem? Also I would like to ask are there ways to track the value of ‘r’. Thanks!

https://editor.p5js.org/JustinChan/sketches/gdQ2nM9Ld

Hello, @JustinChan613, and welcome to the Processing Forum!

There are several problems. One of them is that names of variables in p5.js and many other programming languages are case-sensitive. For spellings of variables to refer to the same variable, the cases of all their letters must match.

Another problem is that you did not call your functions. See the following for corrections:

let humanFirstPlayer = false;
let humanCurrentPlayer = false;
let airFirstPlayer = false;

function setup() {
  createCanvas(400, 400);
  choosingPlayer();
  firstTurn1();
}

function choosingPlayer() {
  let players = ['0','1'];
  let r = random(players);
  if (r == '0'){
    humanFirstPlayer = true;
  } else if (r == '1') {
    airFirstPlayer = true;
  }
}
function firstTurn1(){
  if(humanFirstPlayer){
    humanCurrentPlayer = true;
  }
}

function draw() {
background(220);
  if(humanCurrentPlayer){
    text('human',50,50);
  }
}
1 Like

Thank you for that. Yes there were always typo in the capital letter. Btw would like to ask does it mean that I have to include every functions except function draw( ) in setup? Thanks a lot!

That is true of those functions that you would like to run once as your script begins its execution. Call your functions at whatever points within your script that you would like them to run.

1 Like

Thank you for that! It really helps. Thanks.

2 Likes