Developing Rock-Paper-Scissor game in android

Photo by Aysha Begum on Unsplash

What’s better than creating something easy to use and fun to develop. So, the title speaks for itself! In this article, I will be creating a rock-paper-scissor game in android studio step by step.

I will not focus much on animation and stuff but will talk about the core logic our game is based on. Without much ado, let’s start the session.

I chose the rock-paper-scissor game for many reasons, like:

  1. It is an easy game
  2. We can cover multiples logic of android in a single game
  3. The game does not demand much animation, etc.

Our final result will look like this:

Let us start by creating a new project in android studio. After creating the project, we will change the default styles for activity. Go to:

res > values > styles.xml

Replace the text with code below and now style.xml should look like this:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

Let’s create UI of our game, the game screen will be very simple without any special animation or color. Go to:

res > layout > activity_main.xml

Replace the text with code below and now activity_main.xml should look like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#27A5A5A5"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="96dp"
android:text="Let us play"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#000"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Rock-Paper-Scissor"
android:textColor="@color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:text="Choose one out of the\n ROCK-PAPER-SCISSOR\nfrom below"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp" />

<Button
android:id="@+id/button_rock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:background="@color/colorPrimaryDark"
android:text="Rock"
android:textColor="#fff" />

<Button
android:id="@+id/button_paper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:background="@color/colorPrimary"
android:text="Paper"
android:textColor="#fff" />

<Button
android:id="@+id/button_scissor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:text="scissor"
android:textColor="#fff" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Our activity_main.xml will look like something like this in ‘preview’

Now, let’s start writing code for our game, we will create an enum which will have constant values for ROCK, PAPER and SCISSOR like this:

public enum Selection {
ROCK, PAPER, SCISSOR
}

Now, we are ready to write our code to respond to the user click and find the winner. Our game is a single-player game, here user will either win or lose depending on the selection amongst ROCK, PAPER or SCISSOR.

We will add some constant in MainActivity.java before onCreate(), they will be used to store user selection, CPU selection, winner/loser, etc.

//default value assigned to all constant
private static
Selection user_selection = Selection.SCISSOR;
private static Selection cpu_selection = Selection.ROCK;

private static boolean user_winner = true;
private static boolean match_draw = false;

Your MainActivity.java file will implement View.OnClickListener. Now edit onCreate() method of your MainActivity.java file and it should look like this:

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

button_rock = findViewById(R.id.button_rock);
button_paper = findViewById(R.id.button_paper);
button_scissor = findViewById(R.id.button_scissor);

button_rock.setOnClickListener(this);
button_paper.setOnClickListener(this);
button_scissor.setOnClickListener(this);
}

Since we implemented OnClickListener, we have to override onClick() and then we will perform our click task in it:

@Override
public void onClick(View v) {
switch (v.getId()){

case R.id.button_rock:
user_selection = Selection.ROCK;
break;

case R.id.button_paper:
user_selection = Selection.PAPER;
break;

case R.id.button_scissor:
user_selection = Selection.SCISSOR;
break;
}

proceed();
}

Here, proceed() method is responsible for calculating winner and display winner to the user in a dialog, our method will look like this:

private void proceed() {    //get random cpu selection
cpu_selection
= getRandomCPUSelection();
//get winner based on cpu_selection and user_selection
checkWinner();
//print result based on result
showWinner();
}

Now, let's work on creating random CPU selection, we will have a method getRandomCPUSelection() which will generate random CPU selection.

//function return random value
private
Selection getRandomCPUSelection() {
int random = new Random().nextInt(3);

switch (random){
case 0: return Selection.ROCK;
case 1: return Selection.PAPER;
case 2: return Selection.SCISSOR;
}
return Selection.ROCK;
}

Since we have user_selection and cpu_selection, now we can find out who has won the game. We will create checkWinner() method in this way:

private void checkWinner() {

if(user_selection == cpu_selection){
match_draw = true;
return;
}

//rock wins from scissor, loose from paper
if
(user_selection == Selection.ROCK){
if(cpu_selection == Selection.PAPER){
user_winner = false;
return;
}
else if(cpu_selection == Selection.SCISSOR){
user_winner = true;
return;
}
}

//paper wins from rock, loose from scissor
if
(user_selection == Selection.PAPER){
if(cpu_selection == Selection.SCISSOR){
user_winner = false;
return;
}
else if(cpu_selection == Selection.ROCK){
user_winner = true;
return;
}
}

//scissor wins from paper, loose from rock
if
(user_selection == Selection.SCISSOR){
if(cpu_selection == Selection.ROCK){
user_winner = false;
return;
}
else if(cpu_selection == Selection.PAPER){
user_winner = true;
return;
}
}

Log.e("cpu selected",cpu_selection.toString());
Log.e("user selected",user_selection.toString());
}

Now, we have calculated the winner based on user selection and randomly generated CPU selection, we will display the result to our user in a dialog with showWinner() method:

//show dialog to show result
private void
showWinner() {

String line1 = "USER selection : "+user_selection;
String line2 = "CPU selection : "+cpu_selection;
String result = getResultString();

String message = line1 + "\n" + line2 + "\nResult: " + result;

new AlertDialog.Builder(MainActivity.this)
.setTitle("Result")
.setMessage(message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close dialog
dialog.dismiss();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//reset values when dialog is dismissed
resetValues();
}
})
.show();
}
//return result string
private
String getResultString() {

String result;
if(match_draw == true){
result = "Match draw! Let's go again";
}
else{
if(user_winner)
result = "You win!";
else
result = "You loose!!!";
}

return result;
}
//reset values after the result is displayed to user
private void resetValues() {
user_selection = Selection.SCISSOR;
cpu_selection = Selection.ROCK;

user_winner = true;
match_draw = false;
Log.e(MainActivity.class.getName(), "values reset successful");
}

In this way, we have created our simple but addictive game, here is how our game look:

With this, we have reached the end of our article, the code is available on Github here. Do clap if you liked this.

Founder at Inside Android | Youtuber | App Developer