/* * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wso2.wsas.example; import java.util.Random; /** * WSO2GameService is a Simple Guessing Game that will check your ability to guess * and the knowledge of the future. At the start of the game there will be a number selected * by the service machineGuess and to win the game user have to guess * that number using his guessing power and with the minimum tries. User will be given * 10 maximum tries ( This can be configurable by calling startGame(int, int) ) to * guess. Game end either with, if user fails to guess it within the number of tries (user loosing) * or user was able to guess the number less than or equal number of maximum tries (user winning). *

* How to play. *

*
    * * * * * *
* * Game will report the results after each guess. * */ public class WSO2GameService { /** * Number to store the machine guess at the start of the game. */ private int machineGuess; /** * Maximum number of attempts that users can try the game */ private final int MAX_USER_ATTEMPTS = 10; /** * Counter initialized to zero at the start to counter user tries */ private int tryCounter = 0; /** * Default range for the number guess. * Can be override by the startGame(int, int) */ private final int[] defalutRange = {1,20}; /** * Start the WSO2GameService * This will initiate the game with default number range */ public String startGame() { String message; try { message = displayStartingMessage(); generateRandom(defalutRange[0], defalutRange[1]); } catch (Exception e) { return "Error, Not Started"; } return message; } /** * Start the WSO2GameService * This will initiate the game with custom number range * @param startNumber : starting number of the range * @param endNumber : ending number of the range */ public String startGameWithCustomRange(int startNumber, int endNumber ){ String message; try { message = displayStartingMessage(); generateRandom(startNumber, endNumber); } catch (Exception e) { return "Error, Not Started"; } return message; } /** * Match the user guess against the machine guess * @param guess : user guess * @return result of the try */ public String guess(int guess) { StringBuffer stringResult = new StringBuffer(); if(tryCounter == MAX_USER_ATTEMPTS){ stringResult.append(" User has exeed all Given tries !! You LOOSE"); stringResult.append(" The Machine guess was : "+ machineGuess); System.out.print(stringResult); } else if (guess == machineGuess) { stringResult.append("\nUser Guess : " + guess+ " Guess Succeed !! You WIN !!"); System.out.println(stringResult); }else{ stringResult.append("\nUser Guess : " + guess+" Bad Guess !! Try Again !!"); System.out.println(stringResult); tryCounter++; } return stringResult.toString(); } /** * End the game * This will clear the machine guess also */ public String endGame() { String message; try { tryCounter = 0; message = "\nWSO2GameService Terminated !! \n\n"; System.out.println(message); } catch (Exception e) { return "Error, Not Stopped"; } return message; } /** * Generate the random number as the machine guess * @param startValue : start value for the number range * @param range : number range */ private void generateRandom(int startValue, int range){ Random random = new Random(); machineGuess = random.nextInt(range) + startValue; } /** * Display starting message. */ private String displayStartingMessage() { return "Welcome to the WSO2 Game Service,\n Machine Guess Initiating... !!"; } }