CVS: TapestryBook/hangman1/src/hangman1 Guess.java,1.3,1.4 Lose.java,1.3,1.4 Win.java,1.1,1.2 Game.java,1.2,1.3 Visit.java,1.2,1.3

Howard Lewis Ship <[email protected]>
Newsgroups gmane.comp.java.tapestry.cvs
Message-ID <[email protected]>
Update of /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1
In directory sc8-pr-cvs1:/tmp/cvs-serv8664/hangman1/src/hangman1

Modified Files:
	Guess.java Lose.java Win.java Game.java Visit.java 
Log Message:
Finish off chapter two.

Index: Guess.java
===================================================================
RCS file: /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1/Guess.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Guess.java	14 Jan 2003 16:44:44 -0000	1.3
--- Guess.java	19 Jan 2003 13:26:04 -0000	1.4
***************
*** 1,7 ****
  package hangman1;
  
- import java.util.ArrayList;
- import java.util.List;
- 
  import net.sf.tapestry.IAsset;
  import net.sf.tapestry.IRequestCycle;
--- 1,4 ----
***************
*** 46,91 ****
      }
  
! 	/**
! 	 *  Returns the asset for an image corresponding
! 	 *  to the number.
! 	 * 
! 	 **/
! 	 
!     public IAsset getDigitImage(int digit)
!     {
!         return getAsset("digit" + digit);
!     }
  
- 	/**
- 	 *  Returns an asset for an image used to present the
- 	 *  scaffold, showing the indicating number
- 	 *  of remaining misses.
- 	 * 
- 	 **/
- 	
-     public IAsset getScaffoldImage(int missesLeft)
-     {
-         return getAsset("scaffold" + missesLeft);
-     }
-   
-   	/**
-   	 *  The {@link Game} stores the letters of the target word as lower case,
-   	 *  but the UI looks better if the labels on the images are in upper case.
-   	 * 
-   	 **/
-   	
      public String getLetterLabel()
      {
!         char upper = Character.toUpperCase(_letter);
! 
!         return new Character(upper).toString();
      }
  
! 	/**
! 	 *  Returns the image for the current letter (which may be an underscore
! 	 *  as well).
! 	 * 
! 	 **/
! 	
      public IAsset getLetterImage()
      {
--- 43,63 ----
      }
  
!     /**
!      *  The {@link Game} stores the letters of the target word as lower case,
!      *  but the UI looks better if the labels on the images are in upper case.
!      * 
!      **/
  
      public String getLetterLabel()
      {
!     	return ("" + _letter).toUpperCase();
      }
  
!     /**
!      *  Returns the image for the current letter (which may be an underscore
!      *  as well).
!      * 
!      **/
! 
      public IAsset getLetterImage()
      {
***************
*** 93,97 ****
              return getAsset("dash");
  
!         return getAsset(new Character(_letter).toString());
      }
  
--- 65,69 ----
              return getAsset("dash");
  
!         return getAsset("" + _letter);
      }
  
***************
*** 142,146 ****
              return getAsset("space");
  
!         String name = new Character(getLetterForGuessIndex()).toString();
  
          return getAsset(name);
--- 114,118 ----
              return getAsset("space");
  
!         String name = "" + getLetterForGuessIndex();
  
          return getAsset(name);
***************
*** 183,206 ****
      public void makeGuess(IRequestCycle cycle)
      {
!     	// Java wraps the char as an instance of Character
!     	
          Character guess = (Character) cycle.getServiceParameters()[0];
  
          char ch = guess.charValue();
  
! 		// Get the Visit and cast it to our application-specific
! 		// class.
! 		
!         Visit visit = (Visit) getVisit();
! 
!         Game game = visit.getGame();
! 
!         // If this return true, then stay on this page at let
!         // user keep guessing.
  
!         if (game.makeGuess(ch))
!             return;
  
!         cycle.setPage(game.isWin() ? "Win" : "Lose");
      }
  }
--- 155,170 ----
      public void makeGuess(IRequestCycle cycle)
      {
!         // Java wraps the char as an instance of Character
! 
          Character guess = (Character) cycle.getServiceParameters()[0];
  
          char ch = guess.charValue();
  
!         // Get the Visit and cast it to our application-specific
!         // class.
  
!         Visit visit = (Visit) getVisit();
  
!         visit.makeGuess(cycle, ch);
      }
  }

Index: Lose.java
===================================================================
RCS file: /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1/Lose.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** Lose.java	14 Jan 2003 16:44:46 -0000	1.3
--- Lose.java	19 Jan 2003 13:26:04 -0000	1.4
***************
*** 7,12 ****
  /**
   *
!  *  Page displayed when the users misses too many guesses.
!  *  Displays the target word, and gives the user a chance
   *  to start a new game.
   *
--- 7,12 ----
  /**
   *
!  *  Page displayed when the player misses too many guesses.
!  *  Displays the target word, and gives the player a chance
   *  to start a new game.
   *
***************
*** 48,52 ****
      
      /**
!      *  Listener method; invokes {@link hangman1.Visit#startGame(IRequestCycle)}.
       * 
       **/
--- 48,53 ----
      
      /**
!      *  Listener method; invokes 
!      *  {@link hangman1.Visit#startGame(IRequestCycle)}.
       * 
       **/

Index: Win.java
===================================================================
RCS file: /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1/Win.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Win.java	14 Jan 2003 16:44:39 -0000	1.1
--- Win.java	19 Jan 2003 13:26:04 -0000	1.2
***************
*** 7,11 ****
  /**
   *
!  *  Page displayed when the user wins ... succesfully guesses
   *  all the letters in the word.
   *
--- 7,11 ----
  /**
   *
!  *  Page displayed when the player wins ... succesfully guesses
   *  all the letters in the word.
   *
***************
*** 37,50 ****
      {
          _letter = letter;
-     }
- 
-     public IAsset getDigitImage(int digit)
-     {
-         return getAsset("digit" + digit);
-     }
- 
-     public IAsset getScaffoldImage(int missesLeft)
-     {
-         return getAsset("scaffold" + missesLeft);
      }
  
--- 37,40 ----

Index: Game.java
===================================================================
RCS file: /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1/Game.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Game.java	14 Jan 2003 16:44:43 -0000	1.2
--- Game.java	19 Jan 2003 13:26:04 -0000	1.3
***************
*** 3,49 ****
  public class Game
  {
-     /**
-      *  The word being guessed.
-      * 
-      **/
- 
      private String _targetWord;
! 
!     /**
!      *  The number of remaining failures allowed.  This
!      *  is decremented with each invalid guess, and
!      *  ends the game when it reaches zero.
!      * 
!      **/
! 
!     private int _failuresLeft;
! 
!     /**
!      *  The letters of the word being guessed.  These initially are all the underscore
!      *  character (indicating unguessed positions), but are converted to real
!      *  letters when a succesful guess occurs.
!      * 
!      **/
! 
      private char[] _letters;
- 
-     /**
-      *  Indicates which letters the user has already guessed.  'A' is at index 0,
-      *  'B' at index 1, and so on, up to 'Z' at index 25.
-      * 
-      **/
- 
      private boolean[] _guessed = new boolean[26];
- 
-     /**
-      *  Set to true when the user succesfully guesses the word
-      *  (all slots in the letters are filled in).
-      * 
-      **/
- 
      private boolean _win;
  
      /**
!      *  Returns true if the user has guessed all letters in the word.
       * 
       **/
--- 3,14 ----
  public class Game
  {
      private String _targetWord;
!     private int _incorrectGuessesLeft;
      private char[] _letters;
      private boolean[] _guessed = new boolean[26];
      private boolean _win;
  
      /**
!      *  Returns true if the player has guessed all letters in the word.
       * 
       **/
***************
*** 55,60 ****
  
      /**
!      *  Returns an array of letters that have been guessed by the user, with
!      *  an underscore for each unguessed position.  Once the user loses,
       *  this returns the array of actual letters in the target word.
       * 
--- 20,25 ----
  
      /**
!      *  Returns an array of letters that have been guessed by the player, with
!      *  an underscore for each unguessed position.  Once the player loses,
       *  this returns the array of actual letters in the target word.
       * 
***************
*** 70,80 ****
  
      /**
!      *  Returns the number of failed guesses remaining.
       * 
       **/
  
!     public int getFailuresLeft()
      {
!         return _failuresLeft;
      }
  
--- 35,47 ----
  
      /**
!      *  Returns the number of incorrect guesses remaining.
!      *  An incorrect guess when this is already zero results
!      *  in a loss.
       * 
       **/
  
!     public int getIncorrectGuessesLeft()
      {
!         return _incorrectGuessesLeft;
      }
  
***************
*** 94,105 ****
  
      /**
!      *  Initializes the Game with a new target word.  This resets the failures and initializes the array of letters. 
       * 
       **/
  
      public void start(String word)
      {
          _targetWord = word;
!         _failuresLeft = 6;
          _win = false;
  
--- 61,75 ----
  
      /**
!      *  Initializes the Game with a new target word.  This resets the
!      *  incorrectGuessesLeft count, and initializes the array of
!      *  letters and letters guessed. 
       * 
       **/
  
+ 
      public void start(String word)
      {
          _targetWord = word;
!         _incorrectGuessesLeft = 5;
          _win = false;
  
***************
*** 114,127 ****
              _guessed[i] = false;
      }
! 
      /**
!      *  The user makes a guess.  If the letter has already been guessed, then no change occurs.
!      *  Otherwise, there's a check to see if the guess fills in any positions in the target word.
!      *  This may result in a win.  If the guess doesn't match any letter of the word, then
!      *  a failure occurs; when enough failures occur, there's a loss.
       * 
!      *  @return true if further guesses are allowed (this guess did not result in a win or a loss),
!      *  or false if further guesses are not allowed (the user guessed the word, or used up
!      *  all possible failures).
       * 
       **/
--- 84,100 ----
              _guessed[i] = false;
      }
! 		
      /**
!      *  The player makes a guess.  If the letter has already been 
!      *  guessed, then no change occurs.  Otherwise, there's a check 
!      *  to see if the guess fills in any positions in the target word.
!      *  This may result in a win.  If the guess doesn't match any
!      *  letter of the word, then a failure occurs; when enough 
!      *  failures occur, the game results in a loss.
       * 
!      *  @return true if further guesses are allowed (this guess did 
!      *  not result in a win or a loss), or false if further guesses 
!      *  are not allowed (the player guessed the word, or used up
!      *  all possible incorrect guesses).
       * 
       **/
***************
*** 136,140 ****
          int index = ch - 'a';
  
!         // If the user (somehow) guesses the same letter more than once, it does not affect
          // state of the game.
  
--- 109,113 ----
          int index = ch - 'a';
  
!         // If the player (somehow) guesses the same letter more than once, it does not affect
          // state of the game.
  
***************
*** 173,179 ****
          }
  
-         _failuresLeft--;
  
!         if (_failuresLeft <= 0)
          {
              // Replace the letters array with the solution
--- 146,151 ----
          }
  
  
!         if (_incorrectGuessesLeft == 0)
          {
              // Replace the letters array with the solution
***************
*** 184,195 ****
          }
  
          // Not a good guess, but not a loss yet
  
          return true;
      }
!     
      public String getTargetWord()
      {
!     	return _targetWord;
      }
  }
--- 156,174 ----
          }
  
+         _incorrectGuessesLeft--;
+ 
          // Not a good guess, but not a loss yet
  
          return true;
      }
! 
! 	/**
! 	 *  Returns the word the player is trying to guess.
! 	 * 
! 	 **/
! 	
      public String getTargetWord()
      {
!         return _targetWord;
      }
  }

Index: Visit.java
===================================================================
RCS file: /cvsroot/tapestry/TapestryBook/hangman1/src/hangman1/Visit.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** Visit.java	14 Jan 2003 16:44:43 -0000	1.2
--- Visit.java	19 Jan 2003 13:26:04 -0000	1.3
***************
*** 15,43 ****
  public class Visit
  {
!     private WordSource _wordSource;
!     private Game _game;
  
      public void startGame(IRequestCycle cycle)
      {
!         // In a real application, the word source would be shared between all sessions.  Here, we just allow
!         // each Visit to have its own instance.
  
!         if (_wordSource == null)
!             _wordSource = new WordSource();
  
!         // On the other hand, the Game is specifically for this
!         // Visit and only this Visit.
  
!         if (_game == null)
!             _game = new Game();
  
!         _game.start(_wordSource.nextWord());
  
!         // Now that the Game is initialized, we can go to the Guess page to
!         // allow the user to start making guesses.
  
!         cycle.setPage("Guess");
      }
!     
      /**
       *  Returns the {@link Game} instance for this Visit; this is used
--- 15,55 ----
  public class Visit
  {
!     // In a real application, the word source would be shared between all sessions.  
!     // Here, we just allow each Visit to have its own instance.
! 
!     private WordSource _wordSource = new WordSource();
! 
!     // On the other hand, the Game is specifically for this
!     // Visit and only this Visit.
! 
!     private Game _game = new Game();
  
      public void startGame(IRequestCycle cycle)
      {
!         _game.start(_wordSource.nextWord());
  
!         // Now that the Game is initialized, we can go to the Guess 
!         // page to allow the player to start making guesses.
  
!         cycle.setPage("Guess");
!     }
  
!     /**
!      *  Processes the player's guess, possibly updating the response
!      *  page to be "Win" or "Lose".
!      * 
!      **/
  
!     public void makeGuess(IRequestCycle cycle, char ch)
!     {
!         // If this return true, then stay on this page at let
!         // player keep guessing.
  
!         if (_game.makeGuess(ch))
!             return;
  
!         cycle.setPage(_game.isWin() ? "Win" : "Lose");
      }
! 
      /**
       *  Returns the {@link Game} instance for this Visit; this is used



-------------------------------------------------------
This SF.NET email is sponsored by: FREE  SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your  SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.