0% found this document useful (0 votes)
38 views

GameActivity Code

This document contains code for a number guessing game Android application. It defines a GameActivity class that extends AppCompatActivity and handles the game logic. On launch, it generates a random number within a given range. It tracks the user's guesses, remaining attempts, and provides feedback on whether guesses are too high or low. When the user wins or loses all attempts, it displays a dialog to play again or exit.

Uploaded by

Juan Dela Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

GameActivity Code

This document contains code for a number guessing game Android application. It defines a GameActivity class that extends AppCompatActivity and handles the game logic. On launch, it generates a random number within a given range. It tracks the user's guesses, remaining attempts, and provides feedback on whether guesses are too high or low. When the user wins or loses all attempts, it displays a dialog to play again or exit.

Uploaded by

Juan Dela Cruz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

package com.example.

mynumberguessinggame;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

public class GameActivity extends AppCompatActivity {

private TextView textViewLast, textViewRight, textViewHint;


private Button buttonConfirm;
private EditText editTextGuess;

boolean twoDigits, threeDigits, fourDigits;

Random r = new Random();


int random;
int remainingRight = 10;

ArrayList<Integer> guessesList = new ArrayList<>();


int userAttempts = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);

textViewLast = findViewById(R.id.textViewLast);
textViewRight = findViewById(R.id.textViewRight);
textViewHint = findViewById(R.id.textViewHint);

buttonConfirm = findViewById(R.id.buttonConfirm);

editTextGuess = findViewById(R.id.editTextGuess);

twoDigits = getIntent().getBooleanExtra("two",false);
threeDigits = getIntent().getBooleanExtra("three",false);
fourDigits = getIntent().getBooleanExtra("four",false);
if (twoDigits){
random = r.nextInt(90)+10;
}

if (threeDigits){
random = r.nextInt(900)+100;
}

if (fourDigits){
random = r.nextInt(9000)+1000;
}

buttonConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String guess = editTextGuess.getText().toString();

if (guess.equals("")){
AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
builder.setTitle("Number Guessing Game");
builder.setCancelable(false);
builder.setMessage("Congratulations. My guess was " + random
+ "\n\n You know my number in " + userAttempts
+ " attempts \n\nYour guesses: "
+ guessesList + "\n\n Would you like to play again?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(GameActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
builder.create().show();

}
else{
textViewLast.setVisibility(View.VISIBLE);
textViewRight.setVisibility(View.VISIBLE);
textViewHint.setVisibility(View.VISIBLE);

userAttempts++;
remainingRight--;

textViewLast.setText("You last guess: "+ guess);


textViewRight.setText("Your remaining right "+remainingRight);

int userGuess = Integer.parseInt(guess);


guessesList.add(userGuess);

if (random == userGuess){
textViewHint.setText("Your guess is right");
}
if (random < userGuess ){
textViewHint.setText("Decrease your guess");
}
if (random > userGuess ){
textViewHint.setText("Increase your guess");
}

if (remainingRight == 0){
AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity.this);
builder.setTitle("Number Guessing Game");
builder.setCancelable(false);
builder.setMessage("Sorry your right to guess is over. "
+ "\n\n My guess was " + random
+ " attempts \n\nYour guesses: "
+ guessesList + "\n\n Would you like to play again?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(GameActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
builder.create().show();
}
editTextGuess.setText("");
}
}
});

}
}

You might also like