View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.numberguess;
18  
19  import java.io.Serializable;
20  
21  import javax.annotation.PostConstruct;
22  import javax.enterprise.context.SessionScoped;
23  import javax.enterprise.inject.Instance;
24  import javax.faces.application.FacesMessage;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIInput;
27  import javax.faces.context.FacesContext;
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  
31  /**
32   * <p>
33   * {@link Game} contains all the business logic for the application, and also serves as the controller for the JSF view.
34   * </p>
35   * <p>
36   * It contains properties for the <code>number</code> to be guessed, the current <code>guess</code>, the <code>smallest</code>
37   * and <code>biggest</code> numbers guessed so far (as this is a higher/lower game we can prevent them entering numbers that
38   * they should know are wrong), and the number of <code>remainingGuesses</code>.
39   * </p>
40   * <p>
41   * The {@link #check()} method, and {@link #reset()} methods provide the business logic whilst the
42   * {@link #validateNumberRange(FacesContext, UIComponent, Object)} method provides feedback to the user.
43   * </p>
44   * 
45   * @author Pete Muir
46   * 
47   */
48  @SuppressWarnings("serial")
49  @Named
50  @SessionScoped
51  public class Game implements Serializable {
52  
53      /**
54       * The number that the user needs to guess
55       */
56      private int number;
57  
58      /**
59       * The users latest guess
60       */
61      private int guess;
62  
63      /**
64       * The smallest number guessed so far (so we can track the valid guess range).
65       */
66      private int smallest;
67  
68      /**
69       * The largest number guessed so far
70       */
71      private int biggest;
72  
73      /**
74       * The number of guesses remaining
75       */
76      private int remainingGuesses;
77  
78      /**
79       * The maximum number we should ask them to guess
80       */
81      @Inject
82      @MaxNumber
83      private int maxNumber;
84  
85      /**
86       * The random number to guess
87       */
88      @Inject
89      @Random
90      Instance<Integer> randomNumber;
91  
92      public Game() {
93      }
94  
95      public int getNumber() {
96          return number;
97      }
98  
99      public int getGuess() {
100         return guess;
101     }
102 
103     public void setGuess(int guess) {
104         this.guess = guess;
105     }
106 
107     public int getSmallest() {
108         return smallest;
109     }
110 
111     public int getBiggest() {
112         return biggest;
113     }
114 
115     public int getRemainingGuesses() {
116         return remainingGuesses;
117     }
118 
119     /**
120      * Check whether the current guess is correct, and update the biggest/smallest guesses as needed. Give feedback to the user
121      * if they are correct.
122      */
123     public void check() {
124         if (guess > number) {
125             biggest = guess - 1;
126         } else if (guess < number) {
127             smallest = guess + 1;
128         } else if (guess == number) {
129             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Correct!"));
130         }
131         remainingGuesses--;
132     }
133 
134     /**
135      * Reset the game, by putting all values back to their defaults, and getting a new random number. We also call this method
136      * when the user starts playing for the first time using {@linkplain PostConstruct @PostConstruct} to set the initial
137      * values.
138      */
139     @PostConstruct
140     public void reset() {
141         this.smallest = 0;
142         this.guess = 0;
143         this.remainingGuesses = 10;
144         this.biggest = maxNumber;
145         this.number = randomNumber.get();
146     }
147 
148     /**
149      * A JSF validation method which checks whether the guess is valid. It might not be valid because there are no guesses left,
150      * or because the guess is not in range.
151      * 
152      */
153     public void validateNumberRange(FacesContext context, UIComponent toValidate, Object value) {
154         if (remainingGuesses <= 0) {
155             FacesMessage message = new FacesMessage("No guesses left!");
156             context.addMessage(toValidate.getClientId(context), message);
157             ((UIInput) toValidate).setValid(false);
158             return;
159         }
160         int input = (Integer) value;
161 
162         if (input < smallest || input > biggest) {
163             ((UIInput) toValidate).setValid(false);
164 
165             FacesMessage message = new FacesMessage("Invalid guess");
166             context.addMessage(toValidate.getClientId(context), message);
167         }
168     }
169 }