Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.93 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. /***********************************************************
  5.  * WordGuess game is a Hangman like game for two (or more)
  6.  * players. This is a two player version
  7.  *
  8.  * @author Nancy Harris & <<<< >>>>
  9.  * @version V1 Shell to get you started
  10.  ***********************************************************/
  11.  public class WordGuess
  12.  {
  13.     private static int      arrayNumber;
  14.     private static boolean  roundOver; 
  15.    
  16.    private Scanner   kb;       // use for all keyboard entry
  17.    private Player    player1;  // first player
  18.    private Player    player2;  // second player
  19.    private WordBank  wordList; // the dictionary to use
  20.    
  21.     /* alternate instance variable for the players
  22.        deactivate the individual players if you choose to
  23.         use this version. */
  24.     // private Player[] players;
  25.        
  26.    /*********************************************************
  27.     * Explicit value constructor that builds a random dictioinary
  28.     *
  29.     * @param player1 The name of player1
  30.     * @param player2 The name of player2
  31.     ********************************************************/
  32.     public WordGuess(String player1, String player2)
  33.     {
  34.         System.out.printf("Welcome to Word Guess %s and %s\n\n", player1, player2);
  35.         this.player1 = new Player(player1);
  36.         this.player2 = new Player(player2);
  37.         wordList = new WordBank();
  38.     }
  39.    
  40.     /*********************************************************
  41.     * Explicit value constructor that builds a seeded dictioinary
  42.     *
  43.     * @param player1 The name of player1
  44.     * @param player2 The name of player2
  45.     * @param seed    The seed to pass to WordBank constructor
  46.     ********************************************************/
  47.     public WordGuess(String player1, String player2, long seed)
  48.     {
  49.         System.out.printf("Welcome to Word Guess %s and %s\n\n", player1, player2);
  50.         this.player1 = new Player(player1);
  51.         this.player2 = new Player(player2);
  52.         wordList = new WordBank(seed);
  53.     }
  54.    
  55.     /*********************************************************
  56.      * play game plays the game until one player reaches 10 wins
  57.      *********************************************************/
  58.     public void playGame()
  59.     {
  60.         boolean player1Playing;
  61.         boolean player2Playing;
  62.         boolean round;
  63.         boolean roundOver;
  64.         String chosenWord;
  65.         String hyphenWord;
  66.         String input;
  67.         String lastTurn;
  68.         char[] letters;
  69.         int arrayNumber;
  70.         int turn;
  71.        
  72.         turn = 0;
  73.         arrayNumber = 0;
  74.         letters = new char[26];
  75.         chosenWord = wordList.getWord();
  76.         hyphenWord = wordHyphen(chosenWord);
  77.         lastTurn = "ERROR";
  78.         kb = new Scanner(System.in);
  79.         round = true;
  80.        
  81.         while(round == true)
  82.         {
  83.            
  84.                 do
  85.                 {
  86.                         lastTurn = hyphenWord;
  87.                        
  88.                         hyphenWord = fillBlank(hyphenWord, chosenWord, letters);
  89.                        
  90.                         System.out.println(hyphenWord);
  91.                        
  92.                         printLetters(letters);
  93.                        
  94.                         letters = fillLetter (player1, kb, letters);
  95.                        
  96.                         lastTurn = hyphenWord;
  97.                        
  98.                         hyphenWord = fillBlank(hyphenWord, chosenWord, letters);
  99.    
  100.                         player1Playing = displayOutput(player1, lastTurn, hyphenWord, chosenWord);
  101.                    
  102.                 } while(player1Playing == true);
  103.                
  104.             if(this.roundOver == false)
  105.             {
  106.                 do
  107.                 {
  108.                         lastTurn = hyphenWord;
  109.                        
  110.                         hyphenWord = fillBlank(hyphenWord, chosenWord, letters);
  111.                        
  112.                         System.out.println(hyphenWord);
  113.                        
  114.                         printLetters(letters);
  115.                        
  116.                         letters = fillLetter (player2, kb, letters);
  117.                        
  118.                         lastTurn = hyphenWord;
  119.                        
  120.                         hyphenWord = fillBlank(hyphenWord, chosenWord, letters);
  121.    
  122.                         player2Playing = displayOutput(player2, lastTurn, hyphenWord, chosenWord);
  123.                    
  124.                 } while(player2Playing == true);
  125.             }
  126.            
  127.             else
  128.             {
  129.                 player1.toString();
  130.                 player2.toString();
  131.             }
  132.         }
  133.     }
  134.    
  135.      // You may (and should) add additional methods below. They should
  136.     // all be private to this game
  137.      
  138.      private String wordHyphen(String chosenWord)
  139.      {
  140.         String hyphen;
  141.         hyphen = "";
  142.        
  143.         for(int i = 0; i < chosenWord.length(); i++)
  144.         {
  145.             hyphen += '-';
  146.         }
  147.        
  148.         return hyphen;
  149.      }
  150.      
  151.      private String fillBlank(String hyphen, String chosenWord, char[] guessedLetters)
  152.      {
  153.         String filledWord;
  154.         boolean foundMatch;
  155.         int  numberGuessed;
  156.  
  157.         filledWord = "";
  158.         numberGuessed = guessedLetters.length; 
  159.         foundMatch = false;
  160.        
  161.         for(int i = 0; i < chosenWord.length(); i++)
  162.         {
  163.        
  164.             if(hyphen.charAt(i) == '-') //Seeing if there is a blank that can be filled
  165.             {
  166.                 for(int n = 0; n < numberGuessed; n++) //Loops through the String of letters guessed
  167.                 {
  168.                     if(chosenWord.charAt(i) == guessedLetters[n]) //If the word at the same place as the hyphen
  169.                                                                                           //then it adds that character to the String
  170.                     {
  171.                         filledWord = filledWord + guessedLetters[n];
  172.                         foundMatch = true;
  173.                     }
  174.                 }
  175.                
  176.                 if(foundMatch == false)
  177.                 {
  178.                     filledWord = filledWord + hyphen.charAt(i);
  179.                 }
  180.                
  181.                 else
  182.                 {
  183.                     foundMatch = false;
  184.                 }
  185.             }
  186.            
  187.             else //If there is already a letter
  188.             {
  189.                 for(int n = 0; n < numberGuessed; n++)
  190.                 {
  191.                     if(hyphen.charAt(i) == guessedLetters[n])
  192.                     {
  193.                         filledWord = filledWord + guessedLetters[n];
  194.                     }
  195.                 }
  196.             }
  197.            
  198.         }
  199.        
  200.         return filledWord;
  201.        
  202.      }
  203.      
  204.      private void listLetters(char[] letters)
  205.      {
  206.         for(int i = 0; i < letters.length; i++)
  207.         {
  208.             if(letters[i] != '\u0000')
  209.             {
  210.                 System.out.print(letters[i] + " ");
  211.             }
  212.         }
  213.      }
  214.      
  215.      private char[] fillLetter (Player player1, Scanner kb, char[] letters)
  216.      {
  217.         String input;
  218.         boolean correctInput;
  219.        
  220.         System.out.println();
  221.         System.out.println();
  222.         System.out.printf("%s, guess a letter. ", player1.getName());
  223.  
  224.         correctInput = kb.hasNext("[A-Za-z]+");
  225.         System.out.println();
  226.        
  227.         input = kb.next();
  228.        
  229.         for(int i = 0; i < letters.length; i++)
  230.         {
  231.             if(letters[i] == (input.charAt(0)))
  232.             {
  233.                 correctInput = false;
  234.             }
  235.         }
  236.        
  237.         if(correctInput == true)
  238.         {
  239.             letters[arrayNumber] = input.charAt(0);
  240.             arrayNumber++;
  241.         }
  242.        
  243.         return letters;
  244.      }
  245.      
  246.      private boolean displayOutput (Player player1, String lastTurn, String hyphenWord, String chosenWord)
  247.      {
  248.         boolean playing;
  249.         playing = true;
  250.        
  251.         if(lastTurn.equals(hyphenWord))
  252.         {
  253.             System.out.printf("Incorrect. %s, you lose your turn.\n", player1.getName());
  254.             playing = false;
  255.         }
  256.                    
  257.         else if(hyphenWord.equals(chosenWord))
  258.         {
  259.             System.out.printf("Correct. %s you win a point!\n", player1.getName());
  260.             playing = false;
  261.             roundOver = true;
  262.         }
  263.                    
  264.         else if(lastTurn != hyphenWord)
  265.         {
  266.             System.out.printf("Correct: %s go again.\n", player1.getName());
  267.         }
  268.        
  269.         return playing;
  270.     }
  271.    
  272.     private void printLetters(char[] letters)
  273.     {
  274.         Arrays.sort(letters);
  275.         System.out.print("Used letters: ");
  276.         for(int i = 0; i < letters.length; i++)
  277.         {
  278.             if(letters[i] != '\u0000')
  279.             {
  280.                 System.out.print(letters[i] + " ");
  281.             }
  282.         }
  283.     }
  284. }
Add Comment
Please, Sign In to add comment