Can anyone help with Hangman Code?

boolean newWord = true ; 
String w; 
char letter = '?';
char a, b, c;
int currentScreen=0;

void keyTyped()
{
  letter = key;
}

void intro()
{
  fill(0);
  textSize(26);
  text("HANGMAN", 195,50);
  strokeWeight(3);
   line(200,200,320,200);
   line(260,200,260,100);
   line(260,100,290,100);
   line(290,100,290,117);
   noFill(); 
   ellipse(290,125,10,10);
   line(290,133,290,170);
   line(290,150,285,145);
   line(290,150,295,145);
   line(290,170,285,175);
   line(290,170,295,175);
  textSize(20);
  rect(190,230,125,25);
  text("Instructions", 195,250);
  rect(190,280,125,25);
  text("1 Player", 195, 300);
  rect(190,330,125,25);
  text("2 Players", 195, 350);
  rect(190,380,125,25);
  text("3 Players", 195, 400);
  rect(190,430,125,25);
  text("4 Players", 195, 450);
}

void base ()
{
  line(350,125,450,125);
  line(400,125,400,25);
  line(400,25,425,25);
  line(425,25,425,40);
}

void guessBox()
{
  text("INCORRECT GUESSES", 25, 20);
  rect(25,25,200,100);
}

void randThreeWords ()
{
  line(190,300,220,300);
  line(240,300,270,300);
  line(290,300,320,300);
  text("LEVEL ONE", 200,200);
  String[] threeLWords = {"ace", "act", "air", "bad", "bed", "box", "did", "day", "cup", "eat", "fun", "got"};
  int index = int(random(threeLWords.length)); 
  if (newWord)
    {
      w = (threeLWords[index]);
      newWord = false;
      a = w.charAt(0);
      b = w.charAt(1);
      c = w.charAt(2);
    }
  text(w, 350,100); 
}

void check()
{
  if (letter == a)
    {
      text (a,200,290);
    }
  else if (letter == b)
    {
      text (b,250,290);
    }
    else if (letter == c)
    {
      text (c,300,290);
    }

}
  
void instructions ()
{
  textSize(15);
  text("INSTRUCTIONS", 195,50);
  text("BACK", 20,20);
  rect(15,5,50,20);
  text(" - Each player will have a turn to guess a letter", 30,100);
  text(" - Each incorrect guess adds another part to the hangman", 30,125);
  text(" - Guess one letter at a time to reveal the random word", 30, 150); 
  text(" - The game will begin at level one, with a 3 letter word", 30, 175);
  text(" - After each successful level, the hangman will reset",30,200);
  text(" - There will be 6 levels, the word length increases by 1 letter ",30,225);
  text("    each level (level 6 has 8 letter words)", 30, 250); 
  text(" - For a multiplayer game, the player who guesses the most", 30,275);
  text("    words at the end of the game wins, if the entire game is won", 30,300);
  text(" - The player/players win if they complete level 6", 30,325);
}

void onePlayer()
{
  base();
  guessBox();
  randThreeWords(); 
  if (letter != '?')
   text("Your Guess is " + letter, 175,150);
  else
   text("Enter Your Guess",175, 150);
  check();
}

void setup()
{
  size(500,500);
  smooth();
}

void draw()
{  
  background(255);
  if (currentScreen == 0)
    intro();
  else if (currentScreen == 1)
    instructions(); 
  else if (currentScreen == 2)
    onePlayer(); 
}

void mouseReleased()
{
  if (currentScreen == -1)
    currentScreen = 0;
  else if ( mouseX>190 && mouseX<190+125 && mouseY>230 && mouseY<230+25)
    currentScreen = 1;
  else if ( mouseX>15 && mouseX<15+50 && mouseY>5 && mouseY<5+20)
    currentScreen = 0;
  else if (currentScreen != 1 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
    currentScreen = 2;
  else if (currentScreen !=1 && currentScreen !=2 &&  mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
    currentScreen = 3;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && mouseX>190 && mouseX<190+125 && mouseY>330 && mouseY<330+25)
    currentScreen = 4;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && mouseX>190 && mouseX<190+125 && mouseY>380 && mouseY<380+25)
    currentScreen = 5;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && currentScreen !=5 && mouseX>190 && mouseX<190+125 && mouseY>430 && mouseY<430+25)
    currentScreen = 6;
}

Hi,

Welcome to the community! :slight_smile:

Please read the guidelines on how to format your code and how to post a question on this forum :

https://discourse.processing.org/faq

Please provide a description of your problem (what is Hangman Code?), say hi when you start a message so people can start to help you!

josephh idk what I did wrong I just need help coding the whole project

Could you describe what is not working in your code?

boolean newWord = true ;
int currentScreen=0;
String w;
char a,b,c;
char letter = ‘?’;
boolean nextLevel = false;
boolean letterOne = false;
boolean letterTwo = false;
boolean letterThree = false;
boolean wrongLetter = false;
boolean numOfGuesses = false;
int guesses = 0;

/*** keyTyped *************************

  • Checks to see what the user typed *
    ***************************************/
    void keyTyped()
    {
    letter = key;
    }

/*** intro **********************************

  • Creates the opening title game page. *
    *********************************************/
    void intro()
    {
    fill(0);
    textSize(26);
    text(“HANGMAN”, 195,50);
    strokeWeight(3);
    line(200,200,320,200);
    line(260,200,260,100);
    line(260,100,290,100);
    line(290,100,290,117);
    noFill();
    ellipse(290,125,10,10);
    line(290,133,290,170);
    line(290,150,285,145);
    line(290,150,295,145);
    line(290,170,285,175);
    line(290,170,295,175);
    textSize(20);
    rect(190,230,125,25);
    text(“Instructions”, 195,250);
    rect(190,280,125,25);
    text(“1 Player”, 195, 300);
    rect(190,330,125,25);
    text(“2 Players”, 195, 350);
    rect(190,380,125,25);
    text(“3 Players”, 195, 400);
    rect(190,430,125,25);
    text(“4 Players”, 195, 450);
    }

/**base **********************************

  • to draw the base of the hangman statue *
    ******************************************/
    void base ()
    {
    line(350,125,450,125);
    line(400,125,400,25);
    line(400,25,425,25);
    line(425,25,425,40);
    }

/*** guessBox ***********************************

  • Creates a box to display the incorrect guess *
    ************************************************/
    void guessBox()
    {
    text(“INCORRECT GUESS”, 34, 20);
    rect(25,25,200,100);
    }

void randThreeWords ()
{
line(190,300,220,300);
line(240,300,270,300);
line(290,300,320,300);
text(“LEVEL ONE”, 200,200);
String[] threeLWords = {“ace”, “act”, “air”, “bad”, “bed”, “box”, “dot”, “day”, “cup”, “eat”, “fun”, “got”};
int index = int(random(threeLWords.length));
if (newWord)
{
w = (threeLWords[index]);
newWord = false;
a = w.charAt(0);
b = w.charAt(1);
c = w.charAt(2);
}
text(w, 350,100);
}

void check()
{
if (letter == a)
{
text (a,200,290);
letterOne = true;
}
if (letter != a && letter == b)
{
text (b,250,290);
letterTwo = true;
}
if (letter != a && letter != b && letter == c)
{
text (c,300,290);
letterThree = true;
}
if (letter != a && letter != b && letter != c && letter != ‘?’)
{
text (letter, 118,80);
wrongLetter = true;
guesses += 1;
}
}

void pass()
{
if (letterOne == true && letterTwo == true && letterThree == true)
{
nextLevel = true;
background(255);
text(“YOU PASSED, GOOD LUCK IN THE NEXT LEVEL!”, 200,250);
}
}

void instructions ()
{
textSize(15);
text(“INSTRUCTIONS”, 195,50);
text(“BACK”, 20,20);
rect(15,5,50,20);
text(" - Each player will have a turn to guess a letter", 30,100);
text(" - Each incorrect guess adds another part to the hangman", 30,125);
text(" - Guess one letter at a time to reveal the random word", 30, 150);
text(" - The game will begin at level one, with a 3 letter word", 30, 175);
text(" - After each successful level, the hangman will reset",30,200);
text(" - There will be 6 levels, the word length increases by 1 letter “,30,225);
text(” each level (level 6 has 8 letter words)", 30, 250);
text(" - For a multiplayer game, the player who guesses the most", 30,275);
text(" words at the end of the game wins, if the entire game is won", 30,300);
text(" - The player/players win if they complete level 6", 30,325);
}

void onePlayer()
{
base();
guessBox();
randThreeWords();
text("Guesses: "+guesses,350,450);
if (letter != ‘?’)
text("You guess is " + letter, 180,150);
else
text(“Enter your guess”,180, 150);
check();
if (letterOne)
text (a,200,290);
if (letterTwo)
text(b,250,290);
if (letterThree)
text(c,300,290);
if (nextLevel == true)
pass();
}

void setup()
{
size(500,500);
smooth();
}

void draw()
{
background(255);
if (currentScreen == 0)
intro();
else if (currentScreen == 1)
instructions();
else if (currentScreen == 2)
onePlayer();
}

void mouseReleased()
{
if (currentScreen == -1)
currentScreen = 0;
else if ( mouseX>190 && mouseX<190+125 && mouseY>230 && mouseY<230+25)
currentScreen = 1;
else if ( mouseX>15 && mouseX<15+50 && mouseY>5 && mouseY<5+20)
currentScreen = 0;
else if (currentScreen != 1 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
currentScreen = 2;
else if (currentScreen !=1 && currentScreen !=2 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
currentScreen = 3;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && mouseX>190 && mouseX<190+125 && mouseY>330 && mouseY<330+25)
currentScreen = 4;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && mouseX>190 && mouseX<190+125 && mouseY>380 && mouseY<380+25)
currentScreen = 5;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && currentScreen !=5 && mouseX>190 && mouseX<190+125 && mouseY>430 && mouseY<430+25)
currentScreen = 6;
}

The number of guesses keeps getting repeated, but we only want it to increase by 1 after each incorrect guess

It is hard to tell what is going on, please format your code. (cntrl+shift+c)

Guessing you are calling check() twice, again it’s difficult to tell atm.

I can tell you could benefit from the use of a switch statement.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

boolean newWord = true ;
int currentScreen=0;
String w;
char a,b,c;
char letter = ‘?’;
boolean win = false;
boolean letterOne = false;
boolean letterTwo = false;
boolean letterThree = false;
boolean wrongLetter = false;
boolean numOfGuesses = false;
int guesses = 0;

/*** keyTyped *************************

  • Checks to see what the user typed *
    ***************************************/
    void keyTyped()
    {
    letter = key;
    }

/*** intro **********************************

  • Creates the opening title game page. *
    *********************************************/
    void intro()
    {
    fill(0);
    textSize(26);
    text(“HANGMAN”, 195,50);
    strokeWeight(3);
    line(200,200,320,200);
    line(260,200,260,100);
    line(260,100,290,100);
    line(290,100,290,117);
    noFill();
    ellipse(290,125,10,10);
    line(290,133,290,170);
    line(290,150,285,145);
    line(290,150,295,145);
    line(290,170,285,175);
    line(290,170,295,175);
    textSize(20);
    rect(190,230,125,25);
    text(“Instructions”, 195,250);
    rect(190,280,125,25);
    text(“1 Player”, 195, 300);
    rect(190,330,125,25);
    text(“2 Players”, 195, 350);
    rect(190,380,125,25);
    text(“3 Players”, 195, 400);
    rect(190,430,125,25);
    text(“4 Players”, 195, 450);
    }

/**base **********************************

  • to draw the base of the hangman statue *
    ******************************************/
    void base ()
    {
    line(350,125,450,125);
    line(400,125,400,25);
    line(400,25,425,25);
    line(425,25,425,40);
    }

/*** guessBox ***********************************

  • Creates a box to display the incorrect guess *
    ************************************************/
    void guessBox()
    {
    text(“INCORRECT GUESS”, 34, 20);
    rect(25,25,200,100);
    }

/*** randThreeWords ******************************************

  • Picks one of the random three letter words for level one *
    **************************************************************/
    void randThreeWords ()
    {
    line(190,300,220,300);
    line(240,300,270,300);
    line(290,300,320,300);
    text(“LEVEL ONE”, 200,200);
    String[] threeLWords = {“ace”, “act”, “air”, “bad”, “bed”, “box”, “dot”, “day”, “cup”, “eat”, “fun”, “got”};
    int index = int(random(threeLWords.length));
    if (newWord)
    {
    w = (threeLWords[index]);
    newWord = false;
    a = w.charAt(0);
    b = w.charAt(1);
    c = w.charAt(2);
    }
    text(w, 350,100);
    }

/*** check ************************

  • Check if the guess is correct *
    *********************************/
    void check()
    {
    if (letter == a)
    {
    text (a,200,290);
    letterOne = true;
    }
    if (letter != a && letter == b)
    {
    text (b,250,290);
    letterTwo = true;
    }
    if (letter != a && letter != b && letter == c)
    {
    text (c,300,290);
    letterThree = true;
    }
    if (letter != a && letter != b && letter != c && letter != ‘?’)
    {
    text (letter, 118,80);
    wrongLetter = true;
    guesses += 1;
    }
    }

void pass()
{
if (letterOne == true && letterTwo == true && letterThree == true)
{
win = true;
background(255);
text(“YOU WIN!”, 200,250);
}
}

void bodyParts ()
{
if (guesses >= 1)
ellipse(425,53,20,20);
if (guesses >= 2)
line(425,63,425,93);
if (guesses >= 3)
line(425,80,410,70);
if (guesses >= 4)
line(425,80,440,70);
if (guesses >= 5)
line(425,93,410,103);
if (guesses >= 6)
{
line(425,93,440,103);
win = false;
background(255);
text(“SORRY BUT YOU LOST, TRY AGAIN NEXT TIME:(”, 25,250);
}
}

void instructions ()
{
textSize(15);
text(“INSTRUCTIONS”, 195,50);
text(“BACK”, 20,20);
rect(15,5,50,20);
text(" - Each player will have a turn to guess a letter", 30,100);
text(" - Each incorrect guess adds another part to the hangman", 30,125);
text(" - Guess one letter at a time to reveal the random word", 30, 150);
text(" - The game will begin at level one, with a 3 letter word", 30, 175);
text(" - After each successful level, the hangman will reset",30,200);
text(" - There will be 6 levels, the word length increases by 1 letter “,30,225);
text(” each level (level 6 has 8 letter words)", 30, 250);
text(" - For a multiplayer game, the player who guesses the most", 30,275);
text(" words at the end of the game wins, if the entire game is won", 30,300);
text(" - The player/players win if they complete level 6", 30,325);
}

void onePlayer()
{
base();
guessBox();
randThreeWords();
check();
text("Guesses: "+guesses,350,450);
if (letter != ‘?’)
text("You guess is " + letter, 180,150);
else
text(“Enter your guess”,180, 150);
if (letterOne)
text (a,200,290);
if (letterTwo)
text(b,250,290);
if (letterThree)
text(c,300,290);
pass();
bodyParts();
}

void setup()
{
size(500,500);
smooth();
}

void draw()
{
background(255);
if (currentScreen == 0)
intro();
else if (currentScreen == 1)
instructions();
else if (currentScreen == 2)
onePlayer();
}

void mouseReleased()
{
if (currentScreen == -1)
currentScreen = 0;
else if ( mouseX>190 && mouseX<190+125 && mouseY>230 && mouseY<230+25)
currentScreen = 1;
else if ( mouseX>15 && mouseX<15+50 && mouseY>5 && mouseY<5+20)
currentScreen = 0;
else if (currentScreen != 1 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
currentScreen = 2;
else if (currentScreen !=1 && currentScreen !=2 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)
currentScreen = 3;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && mouseX>190 && mouseX<190+125 && mouseY>330 && mouseY<330+25)
currentScreen = 4;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && mouseX>190 && mouseX<190+125 && mouseY>380 && mouseY<380+25)
currentScreen = 5;
else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && currentScreen !=5 && mouseX>190 && mouseX<190+125 && mouseY>430 && mouseY<430+25)
currentScreen = 6;
}

Sorry, I am what I am doing, but I think I reformatted it? Where are we calling check twice? and where should we put the switch statement? Also, a sample of the switch statement would be very appreciated.
Thank you so much!

@cammy please! I’m sure you can.

I shared a link to the documentation which includes an example.

sorry ive never been on this website before. I just cant get the number of incorrect guesses to work properly

If you are unable to figure out how to format the code, you can host it on openprocessing.org and share the link.

The issue with unformatted code is not only that it is unreadable at times, but it cannot be copy/pasted into the PDE and run. Try it and you will see …

When you reply … just above … you will see </>, either click that or hover to see the shortcut. Or you can post your code in between 2 sets of 3 backticks.

You will need to format your code in PDE first (cntrl-t), then copy/paste in here.

1 Like

I dug into your code … you will find it formatted in the summary below.

Summary
int currentScreen=0, guesses = 0;
String w;
char a, b, c, 
  letter = '?';
boolean nextLevel = false, 
  letterOne = false, 
  letterTwo = false, 
  letterThree = false, 
  wrongLetter = false, 
  numOfGuesses = false, 
  newWord = true;

/*** keyTyped *************************
 
 Checks to see what the user typed *
 ***************************************/
void keyTyped() {
  letter = key;
}

/*** intro **********************************
 
 Creates the opening title game page. *
 *********************************************/
void intro() {
  fill(0);
  textSize(26);
  text("HANGMAN", 195, 50);
  strokeWeight(3);
  line(200, 200, 320, 200);
  line(260, 200, 260, 100);
  line(260, 100, 290, 100);
  line(290, 100, 290, 117);
  noFill();
  ellipse(290, 125, 10, 10);
  line(290, 133, 290, 170);
  line(290, 150, 285, 145);
  line(290, 150, 295, 145);
  line(290, 170, 285, 175);
  line(290, 170, 295, 175);
  textSize(20);
  rect(190, 230, 125, 25);
  text("Instructions", 195, 250);
  rect(190, 280, 125, 25);
  text("1 Player", 195, 300);
  rect(190, 330, 125, 25);
  text("2 Players", 195, 350);
  rect(190, 380, 125, 25);
  text("3 Players", 195, 400);
  rect(190, 430, 125, 25);
  text("4 Players", 195, 450);
}

/**base **********************************
 
 to draw the base of the hangman statue *
 ******************************************/
void base() {
  line(350, 125, 450, 125);
  line(400, 125, 400, 25);
  line(400, 25, 425, 25);
  line(425, 25, 425, 40);
}

/*** guessBox ***********************************
 
 Creates a box to display the incorrect guess *
 ************************************************/
void guessBox() {
  text("INCORRECT GUESS", 34, 20);
  rect(25, 25, 200, 100);
}

void randThreeWords() {
  line(190, 300, 220, 300);
  line(240, 300, 270, 300);
  line(290, 300, 320, 300);
  text("LEVEL ONE", 200, 200);
  String[] threeLWords = {"ace", "act", "air", "bad", "bed", "box", "dot", "day", "cup", "eat", "fun", "got"};
  int index = int(random(threeLWords.length));
  
  if (newWord) {
    w = (threeLWords[index]);
    newWord = false;
    a = w.charAt(0);
    b = w.charAt(1);
    c = w.charAt(2);
  }
  text(w, 350, 100);
}

void check() {
  if (letter == a) {
    text (a, 200, 290);
    letterOne = true;
  }

  if (letter != a && letter == b) {
    text (b, 250, 290);
    letterTwo = true;
  }

  if (letter != a && letter != b && letter == c) {
    text (c, 300, 290);
    letterThree = true;
  }

  if (letter != a && letter != b && letter != c && letter != '?') {
    text (letter, 118, 80);
    wrongLetter = true;
    guesses++;
  }
}

void pass() {
  if (letterOne && letterTwo && letterThree) {
    nextLevel = true;
    background(255);
    text("YOU PASSED, GOOD LUCK IN THE NEXT LEVEL!", 200, 250);
  }
}

void instructions () {
  textSize(15);
  text("INSTRUCTIONS", 195, 50);
  text("BACK", 20, 20);
  rect(15, 5, 50, 20);
  text(" - Each player will have a turn to guess a letter", 30, 100);
  text(" - Each incorrect guess adds another part to the hangman", 30, 125);
  text(" - Guess one letter at a time to reveal the random word", 30, 150);
  text(" - The game will begin at level one, with a 3 letter word", 30, 175);
  text(" - After each successful level, the hangman will reset", 30, 200);
  text(" - There will be 6 levels, the word length increases by 1 letter ", 30, 225);
  text(" each level (level 6 has 8 letter words)", 30, 250);
  text(" - For a multiplayer game, the player who guesses the most", 30, 275);
  text(" words at the end of the game wins, if the entire game is won", 30, 300);
  text(" - The player/players win if they complete level 6", 30, 325);
}

void onePlayer() {
  base();
  guessBox();
  randThreeWords();
  text("Guesses: " + guesses, 350, 450);
  
  if (letter != '?') text("You guess is " + letter, 180, 150);
  else               text("Enter your guess", 180, 150);
  
  check();
  
  if (letterOne)   text (a, 200, 290);
  if (letterTwo)   text(b, 250, 290);
  if (letterThree) text(c, 300, 290);
  if (nextLevel)   pass();
}

void setup() {
  size(500, 500);
  smooth();
}

void draw() {
  background(255);
  if (currentScreen == 0)      intro();
  else if (currentScreen == 1) instructions();
  else if (currentScreen == 2) onePlayer();
}

void mouseReleased() {
  if (currentScreen == -1)                                                                                                                                                       currentScreen = 0;
  else if ( mouseX>190 && mouseX<190+125 && mouseY>230 && mouseY<230+25)                                                                                                         currentScreen = 1;
  else if ( mouseX>15 && mouseX<15+50 && mouseY>5 && mouseY<5+20)                                                                                                                currentScreen = 0;
  else if (currentScreen != 1 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)                                                                                    currentScreen = 2;
  else if (currentScreen !=1 && currentScreen !=2 && mouseX>190 && mouseX<190+125 && mouseY>280 && mouseY<280+25)                                                                currentScreen = 3;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && mouseX>190 && mouseX<190+125 && mouseY>330 && mouseY<330+25)                                           currentScreen = 4;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && mouseX>190 && mouseX<190+125 && mouseY>380 && mouseY<380+25)                      currentScreen = 5;
  else if (currentScreen !=1 && currentScreen !=2 && currentScreen !=3 && currentScreen !=4 && currentScreen !=5 && mouseX>190 && mouseX<190+125 && mouseY>430 && mouseY<430+25) currentScreen = 6;
}

this is where your issue is … once this becomes true it keeps incrementing guesses, you need an additional check to avoid that being called every frame.

if (letter != a && letter != b && letter != c && letter != '?') {
    text (letter, 118, 80);
    wrongLetter = true;
    guesses++;
  }
1 Like