Keeping a total score in Java hangman game -


import java.util.scanner; import javax.swing.joptionpane; public class hangman {      public static void main(string[] args) {         string playagainmsg = "would play again?";         string pickcategorymsg = "you've tried words in category!\nwould choose 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 && playgame)         {         while (playcategory && playgame) {             words = getwords();             counter = 0;             while (playgame && counter < words.length) {                  wordtoguess = words[counter++];                 if (playhangman(wordtoguess)) {                      wincounter++;                     system.out.println("you win! have won " + wincounter + " game(s)." + " have lost " + losecounter + " game(s).");                  } else {                     losecounter++;                     system.out.println("you lose! have lost " + losecounter + " game(s)." + " have won " + wincounter + " game(s).");                  }                  if (counter < 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()];         (int = 0; < 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 < maxattempts && !won) {             string displayword = "";             (string s : wordwithdashes) displayword += " " + s;             system.out.println("\nword is:" + displayword);             system.out.print("\nenter letter or guess whole word: ");             string guess = in.nextline().tolowercase();             if (guess.length() > 1 && guess.equals(wordtoguess)) {                 won = true;             } else if (wordtoguess.indexof(guess) != -1) {                 boolean dashes = false;                 (int = 0; < computerword.length; i++) {                     if (computerword[i].equals(guess)) wordwithdashes[i] = guess;                     else if (wordwithdashes[i].equals("_")) dashes = true;                 }                 won = !dashes; // if there no dashes left, 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 failure int main method 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 yes/no question, ensures valid input     public static boolean askyesnoquestion(string message) {         scanner in = new scanner(system.in);         boolean validanswer = false;         string answer;         {             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;         {             system.out.println("\nwould 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 user pick 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;         {             system.out.println("pick category:\n1. programming\n2. sports");             answer = in.nextline().tolowercase();             if (answer.matches("[1-2]")) validanswer = true;             else system.out.println("invalid input! enter number of category 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 string array in random order     public static string[] randomorder(string[] array) {         int[] order = uniquerandoms(array.length);         string[] result = new string[array.length];         (int = 0; < order.length; i++) {             result[i] = array[order[i]];         }         return result;     }      // generates array of n random numbers 0 n-1     public static int[] uniquerandoms(int n) {         int[] array = new int[n];         int random, duplicateindex;         (int = 0; < n; ) {             random = (int) (math.random() * n);             array[i] = random;             (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;     } } 

i have got working keeps count of games won , lost, assigning score based on amount of attempts/lives saved haven't been able find way keep total score of games played. each game unfortunately has seperate score. if can advise me on way of doing this, appreciated.

create int totalscore variable wincounter, losecounter , score defined. increment after each call scoregame()

score = scoregame(attempts); totalscore += score; system.out.println("your score is: " + score); 

if want permanently save statistics between sessions it's whole nother story. need write scores file after each round , start program reading score file. it's hardly impossible, requires bit more code.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -