title: Keeping a total score in Java hangman game
body: <pre><code>import java.util.Scanner;
  import javax.swing.JOptionPane;
  public class Hangman {
  
      public static void main(String[] args) {
          String playAgainMsg = "Would you like to play again?";
          String pickCategoryMsg = "You've tried all the words in this category!\nWould you like to choose another category?";
          int winCounter = 0, loseCounter = 0, score = 0;
          String[] words;
          int attempts = 0;
          String wordToGuess;
          boolean playCategory = true, playGame = true;
          int totalCounter = 0, counter;
          while (playCategory &amp;&amp; playGame)
          {
          while (playCategory &amp;&amp; playGame) {
              words = getWords();
              counter = 0;
              while (playGame &amp;&amp; counter &lt; words.length) {
  
                  wordToGuess = words[counter++];
                  if (playHangman(wordToGuess)) {
  
                      winCounter++;
                      System.out.println("You win! You have won " + winCounter + " game(s)." + " You have lost " + loseCounter + " game(s).");
  
                  } else {
                      loseCounter++;
                      System.out.println("You lose! You have lost " + loseCounter + " game(s)." + " You have won " + winCounter + " game(s).");
  
                  }
  
                  if (counter &lt; words.length) playGame = askYesNoQuestion(playAgainMsg);
              }
              if (playGame) playCategory = askYesNoQuestion(pickCategoryMsg);
          }
      }
      }
  
  
      public static boolean playHangman(String wordToGuess) {
          String[] computerWord = new String[wordToGuess.length()];
          String[] wordWithDashes = new String[wordToGuess.length()];
          for (int i = 0; i &lt; computerWord.length; i++) {
              computerWord[i] = wordToGuess.substring(i, i+1);
              wordWithDashes[i] = "_";
          }
  
          Scanner in = new Scanner(System.in);
          int attempts = 0, maxAttempts = 7;
          boolean won = false;
          int points = 0;
          while (attempts &lt; maxAttempts &amp;&amp; !won) {
              String displayWord = "";
              for (String s : wordWithDashes) displayWord += " " + s;
              System.out.println("\nWord is:" + displayWord);
              System.out.print("\nEnter a letter or guess the whole word: ");
              String guess = in.nextLine().toLowerCase();
              if (guess.length() &gt; 1 &amp;&amp; guess.equals(wordToGuess)) {
                  won = true;
              } else if (wordToGuess.indexOf(guess) != -1) {
                  boolean dashes = false;
                  for (int i = 0; i &lt; computerWord.length; i++) {
                      if (computerWord[i].equals(guess)) wordWithDashes[i] = guess;
                      else if (wordWithDashes[i].equals("_")) dashes = true;
                  }
                  won = !dashes; // If there are no dashes left, the whole word has been guessed
              } else {
                  drawHangmanDiagram(attempts);
                  System.out.println("You've used " + ++attempts + " out of " + maxAttempts + " attempts.");
              }
  
          }
          int score = 0;
          score = scoreGame(attempts);
          System.out.println("Your score is: " + score);
          return won;
  
      }
  
      //should take in a failure int from the main method that increments after every failed attempt
      public static void drawHangmanDiagram(int failure)
      {
          if (failure == 0)
              System.out.println("\t+--+\n\t|  |\n\t|\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
          else if (failure == 1)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t|\n\t|\n\t|\n\t|\n\t|\n\t+--");
          else if (failure == 2)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /\n\t|\n\t|\n\t|\n\t|\n\t+--");
          else if (failure == 3)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| / \\\n\t|\n\t|\n\t|\n\t|\n\t+--");
          else if (failure == 4)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t|\n\t|\n\t|\n\t+--");
          else if (failure == 5)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t| /\n\t|\n\t|\n\t+--");
          else if (failure == 6)
              System.out.println("\t+--+\n\t|  |\n\t|  @\n\t| /|\\\n\t|  |\n\t| / \\\n\t|\n\t|\n\t+--");
      }
  
      // Asks user a yes/no question, ensures valid input
      public static boolean askYesNoQuestion(String message) {
          Scanner in = new Scanner(System.in);
          boolean validAnswer = false;
          String answer;
          do {
              System.out.println(message + " (Y/N)");
              answer = in.nextLine().toLowerCase();
              if (answer.matches("[yn]")) validAnswer = true;
              else System.out.println("Invalid input! Enter 'Y' or 'N'.");
          } while (!validAnswer);
          return answer.equals("y");
      }
  
      public static boolean askForCategory(int category) {
          Scanner in = new Scanner(System.in);
          boolean validAnswer = false;
          String answer;
          do {
              System.out.println("\nWould you like to play again? (Y/N)");
              answer = in.nextLine().toLowerCase();
              if (answer.matches("[yn]")) validAnswer = true;
              else System.out.println("Invalid input! Enter 'Y' or 'N'.");
          } while (!validAnswer);
          return answer.equals("y");
      }
  
      // Asks the user to pick a category
      public static String[] getWords() {
          String[] programming = {"java", "pascal", "python", "javascript", "fortran", "cobol"};
          String[] sports = {"gymnastics", "badminton", "athletics", "soccer", "curling", "snooker", "hurling", "gaelic", "football", "darts"};
          String[] result = {""};
  
          Scanner in = new Scanner(System.in);
          boolean validAnswer = false;
          String answer;
          do {
              System.out.println("Pick a category:\n1. Programming\n2. Sports");
              answer = in.nextLine().toLowerCase();
              if (answer.matches("[1-2]")) validAnswer = true;
              else System.out.println("Invalid input! Enter the number of the category you want.");
          } while (!validAnswer);
  
          int selection = Integer.parseInt(answer);
          switch (selection) {
              case 1: result = randomOrder(programming); break;
              case 2: result = randomOrder(sports); break;
          }
          return result;
      }
  
      // Sorts a String array in random order
      public static String[] randomOrder(String[] array) {
          int[] order = uniqueRandoms(array.length);
          String[] result = new String[array.length];
          for (int i = 0; i &lt; order.length; i++) {
              result[i] = array[order[i]];
          }
          return result;
      }
  
      // Generates an array of n random numbers from 0 to n-1
      public static int[] uniqueRandoms(int n) {
          int[] array = new int[n];
          int random, duplicateIndex;
          for (int i = 0; i &lt; n; ) {
              random = (int) (Math.random() * n);
              array[i] = random;
              for (duplicateIndex = 0; array[duplicateIndex] != random; duplicateIndex++);
              if (duplicateIndex == i) i++;
          }
          return array;
      }
  
      public static int scoreGame(int attempts)
      {
          int score = 0;
          switch (attempts)
          {
              case 0: score = 70; break;
              case 1: score = 60; break;   
              case 2: score = 50; break;
              case 3: score = 40; break;
              case 4: score = 30; break;
              case 5: score = 20; break;
              case 6: score = 10; break;
              case 7: score = 0;  break;
  
          }
          return score;
      }
  }
  </code></pre>
  
  <p>I have got it working so that it keeps count of the games won and lost, as well as assigning a score based on the amount of attempts/lives saved but I haven't been able to find a way to get it to keep a total score for all of the games played. Each game unfortunately has a seperate score. If anyone can advise me on a way of doing this, it would be greatly appreciated.</p>