Creating the ‘High and Low’ Game app

CHiPSET
2 min readFeb 7, 2020

The High and Low game is a game where the player tries to guess the target number. After each incorrect guess, the player is informed if the target number is higher or lower than their current guess. This continues until the target number is guessed or the player gives up. while developing this application we will develop both the frontend and the backend.

Backend:

The backend of the application of the game High and Low is developed as follows:

1. The textbox “enter the number” is created and also the random library is imported.

public class MainActivity extends AppCompatActivity {    int randomNumber;    public void generateRandomNumber() {        Random rand = new Random();        randomNumber = rand.nextInt(20) + 1;    }

2. Here the ‘submit’ button is created and based upon the entered value, the desired options are displayed in the toast. The logic applied is that if the number guessed by the user is less than the targeted number then it advises the user to guess a higher number but if the number guessed by the user is higher than the targeted number then it advises the user to guess a lower number.

public void guess(View view) {

EditText UserNumber = findViewById(R.id.number);

int guessValue = Integer.parseInt(UserNumber.getText().toString());

String message;

if (guessValue > randomNumber) {

message = "Go Lower!";

} else if (guessValue < randomNumber) {

message = "Go Higher!";

} else {

message = "That's right !! Wanna try again ?";

generateRandomNumber();

}

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

Log.i("Entered Value", UserNumber.getText().toString());

Log.i("Random Number", Integer.toString(randomNumber));
}

Frontend:

The frontend of the application of the game High and Low is developed as follows:

  1. To add an image.
<ImageView
android:id="@+id/imageView"
android:layout_width="339dp"
android:layout_height="182dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.636"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/image" />

2. To add text.

<TextView
android:id="@+id/textView2"
android:layout_width="339dp"
android:layout_height="56dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="28dp"
android:gravity="center"
android:text="Can You Guess it?"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="@android:color/white"
app:fontFamily="sans-serif-black"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />

<TextView
android:id="@+id/textView3"
android:layout_width="303dp"
android:layout_height="34dp"
android:layout_marginStart="52dp"
android:layout_marginLeft="52dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:layout_marginBottom="49dp"
android:gravity="center"
android:text="The number lies between 0 to 20"
android:textColor="@android:color/white"
app:layout_constraintBottom_toTopOf="@+id/number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

3. To edit text.

<EditText
android:id="@+id/number"
android:layout_width="167dp"
android:layout_height="42dp"
android:layout_marginStart="115dp"
android:layout_marginLeft="115dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="115dp"
android:layout_marginRight="115dp"
android:layout_marginBottom="204dp"
android:ems="10"
android:gravity="center"
android:hint="Enter Number"
android:inputType="number"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

4. To add button.

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="45dp"
android:layout_marginEnd="163dp"
android:layout_marginRight="163dp"
android:layout_marginBottom="92dp"
android:gravity="center"
android:onClick="guess"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/number" />

</androidx.constraintlayout.widget.ConstraintLayout>

Background colour code — #969184

Icon

Image

--

--