After completing this project, please fill out the survey found here
-
Write a guessing game where the user has to guess a secret number you will hard code as another data member (there is a challenge at the end to make this random). After every guess the program tells the user whether their number was too large or too small.
-
Must include these methods
a.fun checkGuess(guess: Int): Int
i. check to see if the number is correct and return the result as an integer valueb.
fun updateUI(result: Int)
i. this will perform all the tasks necessary to update the display once the result has been determined
ii. use a switch statement inside of this method to determine what to display to the user based on the provided result.
- Create a new Android Studio project
- Name the project "Guessing Game"
- Make sure that the target API level is below that of your testing environment.
- Open the app's activity_main.xml file.
- Change the parent viewgroup to a linear layout
Set the
orientation
attribute tovertical
to best utilize the screen real estate
- Add
TextViews
as desired to communicate necessary information to the user - Add an
EditText
view to get input from the user - Add a
Button
so the user can submit their guess - Add IDs to all the views that will need to be accessed in the code
- Create a listener and event handler for the submit button in MainActivity.java
- In the click event method, retreive the number guessed by the user
int guess = Integer.parseInt(editEntry.getText().toString());
- Create the methods above to process the user's guess and show them the result
- Call the
checkGuess
method and pass it's return value into theupdateUI
method.
Use the following constant values to communicate the results between methods
static final int CORRECT = 0;
static final int LOW = 1;
static final int HIGH = 2;
static final int RESET = 3;
Send your completed app to your Project Manager.
Randomly generate a secret number.
use
Random.nextInt(MAX_VALUE); // where MAX_VALUE < the highest number you want to generate
You'll need to import thekotlin.random.Random
library for this to work Documentation located here
When the user has correctly guessed the number, allow them to reset and allow the user to try again.
Again, experiment with attributes of your textViews and other GUI components to improve the look of your app.