Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /***********************************************************
- * WordGuess game is a Hangman like game for two (or more)
- * players. This is a two player version
- *
- * @author Nick Bennett
- * @version V1 Shell to get you started
- ***********************************************************/
- public class WordGuess
- {
- private Scanner keyboard; // use for all keyboard entryo
- private WordBank wordList; // the dictionary to use
- /* alternate instance variable for the players
- deactivate the individual players if you choose to
- use this version. */
- private Player[] players;
- /*********************************************************
- * Explicit value constructor that builds a random dictioinary
- *
- * @param player1 The name of player1
- * @param player2 The name of player2
- ********************************************************/
- public WordGuess(String player1, String player2)
- {
- this.wordList = new WordBank();
- this.players = new Player[2];
- players[0] = new Player(player1);
- players[1] = new Player(player2);
- }
- /*********************************************************
- * Explicit value constructor that builds a seeded dictioinary
- *
- * @param player1 The name of player1
- * @param player2 The name of player2
- * @param seed The seed to pass to WordBank constructor
- ********************************************************/
- public WordGuess(String player1, String player2, long seed)
- {
- this.wordList = new WordBank(seed);
- players[0] = new Player(player1);
- players[1] = new Player(player2);
- }
- /*********************************************************
- * play game plays the game until one player reaches 10 wins
- *********************************************************/
- public void playGame()
- {
- /*
- keyboard = new Scanner(System.in);
- int numPlayers = 2;
- int score = 0;
- int guess;
- int loop = 0;
- int counter = 0;
- String pGuess = "";
- String word = wordList.getWord();
- char[] usedLetters = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ',
- ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
- */
- System.out.printf("Welcome to Word Guess %s and %s\n\n", players[0].getName(), players[1].getName());
- /////////////////////////////////////////////////////////////////////////////////////////
- keyboard = new Scanner(System.in);
- String word = wordList.getWord();
- String pGuess = ""; // User input read into here
- char[] usedLetters;
- int i; // General counter we will use in all for() loops
- int compareflag1 = 0; // Flag to see if we should run the checking loop (did the user lose?)
- int compareflag2 = 0
- int guessvalue; // to determine if we should switch players or not
- int counter = 0; // keep track of last array element in usedLetters[]
- // we can start a do-loop right before the prompting
- // for user to guess a letter. we can check the do-loop
- // against a desired score value, and everything from this point
- // will loop and shouldn't break:
- // do {
- System.out.print(players[loop % 1].getName() + ", guess a letter.\n");
- pGuess = keyboard.nextLine(); // Get guess from user (character + newline)
- for(i = 0; i < usedLetters.length; i++) // Loop 26 times compare guess to each element in usedLetters[]
- {
- if(pGuess.charAt(0) == usedLetters[i]) // If guess = usedLetters[] tell player he loses, switch players and break out
- {
- System.out.print(pGuess + " is already guessed. " + players[loop % 1].getName() + ", you lose your turn.\n");
- loop++;
- compareflag1 = 1;
- break;
- }
- }
- // If we are here that means our guessed letter is not part
- // of usedLetters[] meaning it has never been guessed before.
- // This would be a good time to call our function that now takes
- // the users guess and does everything with it. That way we can
- // just loop this until completion.
- if(compareflag1 == 0) // compareflag only changes when letter is found in for
- { // loop above in which case we shouldn't do whats below
- if(pGuess.equalsIgnoreCase("a") || pGuess.equalsIgnoreCase("b") ||pGuess.equalsIgnoreCase("c") ||
- pGuess.equalsIgnoreCase("d") || pGuess.equalsIgnoreCase("e") || pGuess.equalsIgnoreCase("f") ||
- pGuess.equalsIgnoreCase("g") || pGuess.equalsIgnoreCase("h") || pGuess.equalsIgnoreCase("i") ||
- pGuess.equalsIgnoreCase("j") || pGuess.equalsIgnoreCase("k") || pGuess.equalsIgnoreCase("l") ||
- pGuess.equalsIgnoreCase("m") || pGuess.equalsIgnoreCase("n") || pGuess.equalsIgnoreCase("o") ||
- pGuess.equalsIgnoreCase("p") || pGuess.equalsIgnoreCase("q") || pGuess.equalsIgnoreCase("r") ||
- pGuess.equalsIgnoreCase("s") || pGuess.equalsIgnoreCase("t") || pGuess.equalsIgnoreCase("u") ||
- pGuess.equalsIgnoreCase("v") || pGuess.equalsIgnoreCase("w") || pGuess.equalsIgnoreCase("x") ||
- pGuess.equalsIgnoreCase("y") || pGuess.equalsIgnoreCase("z"))
- {
- // What happens when your word is 'abacus' and it finds the first 'a',
- // add to usedLetters[] increase the counter, then goes to the second
- // 'a', does the same thing. Is this what we really want?
- // Let's break after first instance so stop that from happening
- for(i = 0; i < word.length(); i++) // loop through length of word we are guessing
- {
- // compare individual character of word to player guess
- if(word.substring(i).equalsIgnoreCase(pGuess))
- {
- // Add score to this player somewhere here, before breaking
- // set compareflag2 so we know that at least 1 match was made
- System.out.print("Correct: " + players[loop % 1].getName() + " go again.\n");
- compareflag2 = 1;
- break;
- }
- }
- // if compareflag2 = 0 means that no matches were made, so switch players
- if(compareflag2 == 0)
- {
- System.out.print("Incorrect. " + players[loop % 1].getName() + ", you lose your turn.\n");
- loop++;
- }
- // reset compareflag2, add guess to usedLetters, increase its counter
- compareflag2 = 0;
- usedLetters[counter] = pGuess.charAt(0);
- counter++;
- }
- }
- compareflag1 = 0; // . . . and then we run again
- // we print the guessed word so far
- // also print the letters we used
- printWord(word, pGuess, usedLetters);
- Arrays.sort(usedLetters);
- System.out.print("\nUsed letters: ");
- for(i = 0; i < usedLetters.length; i++)
- {
- if(usedLetters[i] != ' ')
- System.out.print(usedLetters[i]);
- }
- System.out.print("\n");
- // below add a conditional if usedLetters == word
- // then you know the user won, and you can then
- // break out of the do{}while loop if that's true
- // =======
- // =======
- // this will be very hard to do!
- // that do { loop we started a while ago,
- // it should end here with a condition after
- // it so we know how many times it should run:
- // } while(score < 5);
- } // end of playGame() function
- /////////////////////////////////////////////////////////////////////////////////////////
- // Junk yard
- // do {
- /*
- System.out.print("\n");
- */
- // } while (pGuess.equalsIgnoreCase(word)); // (closes do-loop) you're comparing pGuess and word? why?
- /*
- if (usedLetters.equals(word)) // the letters you used equal the word? you sort the letters used!
- {
- System.out.print("Correct. " + players[loop % 1].getName() + " you win a point!\n");
- score++;
- System.out.println(word);
- System.out.println(players[loop % 1].getName() + ": " + score + "\t" + players[loop % 1].getName() + ": " + score);
- loop++;
- }
- */
- // } while (score < 5);
- // System.out.printf("Game over. " + players[loop % 1].getName() + " wins!");
- // You may (and should) add additional methods below. They should
- // all be private to this game
- private void printWord(String word, String pGuess, char[] usedLetters)
- {
- int ii;
- for (ii = 0; ii < word.length(); ii++)
- {
- if (word.substring(ii, ii + 1).equalsIgnoreCase(pGuess))
- {
- System.out.print(pGuess);
- }
- else
- System.out.print("-");
- }
- }
- }
Add Comment
Please, Sign In to add comment