can anyone spot what is causeing my problem.

i am working on a lab for class and got far but now i am hitting a major bump in the road. i already spoke with my professor but her time is limited. the objective of the sketch is to make a game that will have 6 click able die that correspond with each side of the die. then I have to use a loop to limit the amount of guesses. the game was working fin till i tried using the loop. i could click each box and it would tell me i won or chose again but would not display you lose because i didn't have a loop to track guesses. once i added the loop it now just shows a blank screen. below is the code with the loop and also i then commented the loop out to show the game working without a game over.

code not working with loop

int guess = 0;

//die
int die = 1;

//guesses


//bolleans for each die
boolean D1 = false;
boolean D2 = false;
boolean D3 = false;
boolean D4 = false;
boolean D5 = false;
boolean D6 = false;
boolean picked = false;

// dice variables
int diceX = 10;
int diceY = 10;
int diceW = 50;
int diceH = 50;

//dot variables
int dotX = 80;
int dotY = 20;
int dotW = 5;
int dotH = 5;

void setup()
{
  //screen size
  size(370,160);

  //rolling die
  //die = (int) random(1,7);
  println("die landed on " + die);

  //loop to make boxes for dice
  while(diceX < width)
  {
   rect(diceX,diceY,diceW,diceH);
   diceX = diceX + 60;
  }


 //dots on the dice
  while(dotX < width)
  {
    fill(0);
    ellipse(dotX,dotY,dotW,dotH);
    ellipse(dotX + 30,dotY + 30, dotW,dotH);
    ellipse(dotX + 150,dotY,dotW,dotH);
    ellipse(dotX + 120, dotY + 30, dotW,dotH);
    ellipse(35,35,5,5);
    ellipse(155,35,5,5);
    ellipse(275,35,5,5);
    ellipse(320,35,5,5);
    ellipse(350,35,5,5);
    dotX = dotX + 60;
  }
}

void draw()
{
  while (guess < 3 && (picked == false))
  {
 // if statment for when the die is 1
   println(D1);
   if (D1 && die == 1)
  {
    textBox("you win");
    picked = true;
  }
   //if statment for when the die is not 1
  else if (D1 && die != 1)
  {
    textBox("sorry chose again");
  }

    //DEBUG
    //if statment for when the die is not 2
   else if (D2 && die != 2)
  {
    textBox("sorry chose again");
  }

   //if statment for when the die is 2
   else if (D2 && die == 2)
  {
    textBox("you win");
    picked = true;
  }

   //if statment for when the die is not 3
   else if (D3 && die != 3)
  {
    textBox("sorry chose again");
  }

   // if statment for when the die is 3
   else if (D3 && die == 3)
  {
    textBox("you win");
    picked = true;
  }

   //if statment for when the die is not 4
   else if (D4 && die != 4 && guess < 3)
  {
    textBox("sorry chose again");

  }

  // if statment for when the die is 4
  else if (D4 && die == 4 && guess < 3)
  {
    textBox("you win");
    picked = true;
  }

   //if statment for when the die is not 5
   else if (D5 && die != 5 && guess < 3)
  {
    textBox("sorry chose again");
  }

   // if statment for when the die is 5
   else if (D5 && die == 5 && guess < 3)
  {
    textBox("you win");
    picked = true;
  }

   //if statment for when the die is not 6
   else if (D6 && die != 6 && guess < 3)
  {
    textBox("sorry chose again");
  }

  // if statment for when the die is 6
  else if (D6 && die == 6 && guess < 3)
  {
    textBox("you win");
    picked = true;
  }

   //if statment for game over  
   else if (guess > 3)
  {
    textBox("you lose");

  }

  else
  {
   textBox("Please select a number");
  }

  println(guess);
  }
noLoop();

}

void textBox(String words)
{
  stroke(255);
    fill(0);
    rect(100,110,160,40);
    fill(255);
    text(words,110,130);
}

void mousePressed()
{
  println("mouseX " +mouseX  +" mouseY " +   mouseY);
  guess = guess+1;
 println("inside " + guess);
  if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
  {
    D1 = true;
  }
  if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
  {
    D2 =!D2;
  }
  if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
  {
    D3 =!D3;
  }
  if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
  {
    D4 =!D4;
  }
  if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
  {
    D5 =!D5;
  }
  if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
  {
    D6 =!D6;
  }
}

This is the earlier version of the sketch that didn't have a loop.

void textBox()
{
  stroke(255);
    fill(0);
    rect(100,110,160,40);
    fill(255);
}

//die
int die = 0;

//guesses
int guess = 0;

//bolleans for each die
boolean D1 = false;
boolean D2 = false;
boolean D3 = false;
boolean D4 = false;
boolean D5 = false;
boolean D6 = false;

// dice variables
int diceX = 10;
int diceY = 10;
int diceW = 50;
int diceH = 50;

int dotX = 80;
int dotY = 20;
int dotW = 5;
int dotH = 5;

void setup()
{
  //screen size
  size(370,160);

  //rolling die
  die = (int) random(1,7);
  println("die landed on " + die);

  //loop to make boxes for dice
  while(diceX < width)
  {
   rect(diceX,diceY,diceW,diceH);
   diceX = diceX + 60;
  }


 //dots on the dice
  while(dotX < width)
  {
    fill(0);
    ellipse(dotX,dotY,dotW,dotH);
    ellipse(dotX + 30,dotY + 30, dotW,dotH);
    ellipse(dotX + 150,dotY,dotW,dotH);
    ellipse(dotX + 120, dotY + 30, dotW,dotH);
    ellipse(35,35,5,5);
    ellipse(155,35,5,5);
    ellipse(275,35,5,5);
    ellipse(320,35,5,5);
    ellipse(350,35,5,5);
    dotX = dotX + 60;
  }
}

void draw()
{
  //if statment for when the die is not 1
  if (D1 && die != 1 && guess < 3)
  {
    textBox();
    text("sorry chose again",110,130);
  }

   // if statment for when the die is 1
   else if (D1 && die == 1 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

    //if statment for when the die is not 2
   else if (D2 && die != 2 && guess < 3 )
  {
    textBox();
    text("sorry chose again",110,130);
  }

   //if statment for when the die is 2
   else if (D2 && die == 2 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

   //if statment for when the die is not 3
   else if (D3 && die != 3 && guess < 3)
  {
    textBox();
    text("sorry chose again",110,130);
  }

   // if statment for when the die is 3
   else if (D3 && die == 3 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

   //if statment for when the die is not 4
   else if (D4 && die != 4 && guess < 3)
  {
    textBox();
    text("sorry chose again",110,130);
  }

  // if statment for when the die is 4
  else if (D4 && die == 4 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

   //if statment for when the die is not 5
   else if (D5 && die != 5 && guess < 3)
  {
    textBox();
    text("sorry chose again",110,130);
  }

   // if statment for when the die is 5
   else if (D5 && die == 5 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

   //if statment for when the die is not 6
   else if (D6 && die != 6 && guess < 3)
  {
    textBox();
    text("sorry chose again",110,130);
  }

  // if statment for when the die is 6
  else if (D6 && die == 6 && guess < 3)
  {
    textBox();
    text("you win",110,130);
  }

   //if statment for game over  
   else if (guess > 3)
  {
    textBox();
    text("you lose",110,130);
  }

  else
  {
   textBox();
   text("Please select a number",110,130);
  }

}

void mousePressed()
{
  if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
  {
    D1 = !D1;
  }
  if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
  {
    D2 =!D2;
  }
  if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
  {
    D3 =!D3;
  }
  if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
  {
    D4 =!D4;
  }
  if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
  {
    D5 =!D5;
  }
  if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
  {
    D6 =!D6;
  }
}

i know this is a lot to look at but im at the end of my options at the moment.

Answers

  • edited October 2013

    It'd help a lot if you re-post it, highlight it and hit CTRL+K to format it! 8-X

  • edited October 2013 Answer ✓
    int guess = 0;
    
    //die
    int die = 1;
    
    //guesses
    
    
    //bolleans for each die
    boolean D1 = false;
    boolean D2 = false;
    boolean D3 = false;
    boolean D4 = false;
    boolean D5 = false;
    boolean D6 = false;
    boolean picked = false;
    
    // dice variables
    int diceX = 10;
    int diceY = 10;
    int diceW = 50;
    int diceH = 50;
    
    //dot variables
    int dotX = 80;
    int dotY = 20;
    int dotW = 5;
    int dotH = 5;
    
    void setup()
    {
      //screen size
      size(370,160);
    
      //rolling die
      die = (int) random(1,7);
      println("die landed on " + die);
    
      //loop to make boxes for dice
      while(diceX < width)
      {
       rect(diceX,diceY,diceW,diceH);
       diceX = diceX + 60;
      }
    
    
     //dots on the dice
      while(dotX < width)
      {
        fill(0);
        ellipse(dotX,dotY,dotW,dotH);
        ellipse(dotX + 30,dotY + 30, dotW,dotH);
        ellipse(dotX + 150,dotY,dotW,dotH);
        ellipse(dotX + 120, dotY + 30, dotW,dotH);
        ellipse(35,35,5,5);
        ellipse(155,35,5,5);
        ellipse(275,35,5,5);
        ellipse(320,35,5,5);
        ellipse(350,35,5,5);
        dotX = dotX + 60;
      }
    }
    
    void draw()
    {
    
       if (((D1 && die == 1) || (D2 && die == 2) || (D3 && die == 3) || (D4 && die == 4) || (D5 && die == 5) || (D6 && die == 6)) || picked)
       {
        textBox("you win");
        picked = true;
        return;
       }
      if ( (guess > 0 && guess < 3) )
      {
       if (!picked)
       {
        textBox("sorry chose again");
       }
      }
      else if (guess == 0)
      {
        textBox("Please select a number");
      }
      else
      {
        textBox("you lose");
      }
     // println(guess);
    
    //noLoop();
    
    }
    
    void textBox(String words)
    {
      stroke(255);
        fill(0);
        rect(100,110,160,40);
        fill(255);
        text(words,110,130);
    }
    
    void mousePressed()
    {
     // println("mouseX " +mouseX  +" mouseY " +   mouseY);
    // println("inside " + guess);
     if (guess < 3 && !picked)
    
     {
      if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
      {
        D1 = true;
        guess++;
      }
      if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
      {
        D2 = true;
        guess++;
      }
      if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
      {
        D3 = true;
        guess++;
      }
      if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
      {
        D4 = true;
        guess++;
      }
      if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
      {
        D5 = true;
        guess++;
      }
      if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
      {
        D6 = true;
        guess++;
      }
    
     }
    }
    
  • to Dareon thank you for your help. i cant hand this in as my own work now but it alt east gives me an example of how to rework it. also thank you for showing how to condense the if statements. to GoToLoop thanks for the tip. i knew it looked wrong when i posted it but i dint know what to do.

  • int guess = 0;
    
    //die
    int die = 1;
    
    //guesses
    
    
    //bolleans for each die
    boolean D1 = false;
    boolean D2 = false;
    boolean D3 = false;
    boolean D4 = false;
    boolean D5 = false;
    boolean D6 = false;
    boolean picked = false;
    boolean end = false;
    
    
    
    void setup()
    {
    
      //screen size
      size(370,160);
    
      //rolling die
      die = (int) random(1,7);
      println("die landed on " + die);
    
    dice();
    }
    
    void dice()
    {
      // dice variables
    int diceX = 10;
    int diceY = 10;
    int diceW = 50;
    int diceH = 50;
    
    //dot variables
    int dotX = 80;
    int dotY = 20;
    int dotW = 5;
    int dotH = 5;
    
    
      //loop to make boxes for dice
      while(diceX < width)
      {
       fill(255);
       rect(diceX,diceY,diceW,diceH);
       diceX = diceX + 60;
      }
    
    
     //dots on the dice
      while(dotX < width)
      {
        fill(0);
        ellipse(dotX,dotY,dotW,dotH);
        ellipse(dotX + 30,dotY + 30, dotW,dotH);
        ellipse(dotX + 150,dotY,dotW,dotH);
        ellipse(dotX + 120, dotY + 30, dotW,dotH);
        ellipse(35,35,5,5);
        ellipse(155,35,5,5);
        ellipse(275,35,5,5);
        ellipse(320,35,5,5);
        ellipse(350,35,5,5);
        dotX = dotX + 60;
      }
    }
    void draw()
    {
    
       if (end)
       {
          fill(200);
          rect(145, 80, 80, 20);
          fill(255);
          rect(150, 83, 70, 14);
          fill(0);
          text("Try Again?", 157, 95);
       }
    
    
       if (((D1 && die == 1) || (D2 && die == 2) || (D3 && die == 3) || (D4 && die == 4) || (D5 && die == 5) || (D6 && die == 6)) || picked)
       {
        textBox("you win");
        picked = true;
        end = true;
        return;
       }
      if ( (guess > 0 && guess < 3) )
      {
       if (!picked)
       {
        textBox("sorry chose again");
       }
      }
      else if (guess == 0)
      {
        textBox("Please select a number");
      }
      else
      {
        textBox("you lose");
        end = true;
      }
     // println(guess);
    
    //noLoop();
    
    }
    
    void textBox(String words)
    {
      stroke(255);
        fill(0);
        rect(100,110,160,40);
        fill(255);
        text(words,110,130);
    }
    
    void mousePressed()
    {
     // println("mouseX " +mouseX  +" mouseY " +   mouseY);
    // println("inside " + guess);
     if (guess < 3 && !picked)
    
     {
      if (mouseX > 10 && mouseX < 60 && mouseY > 10 && mouseY < 50)
      {
        D1 = true;
        guess++;
      }
      if (mouseX > 70 && mouseX < 120 && mouseY > 10 && mouseY < 50)
      {
        D2 = true;
        guess++;
      }
      if (mouseX > 130 && mouseX < 180 && mouseY > 10 && mouseY < 50)
      {
        D3 = true;
        guess++;
      }
      if (mouseX > 190 && mouseX < 240 && mouseY > 10 && mouseY < 50)
      {
        D4 = true;
        guess++;
      }
      if (mouseX > 250 && mouseX < 300 && mouseY > 10 && mouseY < 50)
      {
        D5 = true;
        guess++;
      }
      if (mouseX > 310 && mouseX < 360 && mouseY > 10 && mouseY < 50)
      {
        D6 = true;
        guess++;
      }
    
     }
     if ((mouseX > 150 && mouseX < 220 && mouseY > 83 && mouseY < 97) && end)
     {
       D1 = false;
       D2 = false;
       D3 = false;
       D4 = false;
       D5 = false;
       D6 = false;
       guess = 0;
       end = false;
       picked = false;
       die = (int) random(1,7);
       clear();
          stroke(1);
          background(200);
          dice();
    
     }
    }
    
  • edited October 2013

    Your method of keeping track of the guesses is the better in my opinion but I am still tinkering around with how to keep track of it with a loop. Loops seam to not like this sketch. Even with cleaning things up and changing a bit around it wont run if the loop is in the draw. You helped me to get it to work so i can get credit for the attempting the work at least.

Sign In or Register to comment.