Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android Rock Paper Scissors Game Tutorial

Android Rock Paper Scissors Game Tutorial
Hey, Developers! Hope you are having a great time learning with us. In this tutorial, we will learn how to create a Android Rock Paper Scissors Game in Android. Android Rock Paper Scissor Game is a Hand game usually played between two people. Here your win or loss is determined by the other player’s choice. There are 3 options each player can choose from. It can be a Rock, a paper or a Scissor. In this tutorial, we will make an app where the First choice is made by the user and the another choice is made by the App. The Result will then be displayed whether the user lost or won.

Demo Android Rock Paper Scissors Game Tutorial

Download full code here:

This is how the UI will appear

The Rules of the Game

The Rules of this game are pretty simple. User and Computer both can choose from 3 options. It can be a Rock(represented by a Fist),   paper (represented by a flat hand) or scissors (represented by middle and index fingers forming a horizontal V shape). The Shapes of hand will be represented in this project as an image displayed in an ImageView.
A Rock will win against Scissors, A Paper will win against Rock and Scissors will win against Paper. When both choices are same it’s a tie.

Creating a New Project – RockPaperScissors

  1. Open your Android Studio & create a new Project named RockPaperScissors, we used androidtutorial.com as our domain name and have taken Blank Activity for this project.
  2. The name for activity is by default MainActivity, leave all the things by default and click finish.

A new project will be created and gradle will resolve all the dependencies.

Layout of Android Rock Paper Scissors

We have used three Buttons to make User able, to choose from three possible options. Also, we used two ImageView, When the user chooses a button, the corresponding image will be displayed in the First ImageView, and after the code execution completes the choice made by the app will be represented in the second ImageView. All the code is self-explanatory but you may feel free to ask questions.

Include following code in activity_main.xml.

activity_main.xml



 

 
 

Working of Android Rock Paper Scissors

MainActivity.java

package com.androidtutorialpoint.rockpaperscissors;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView input, output;
    Button rock, paper, scissors;
    int[] images = new int[]{
            R.mipmap.rock,
            R.mipmap.paper,
            R.mipmap.scissors
    };
    int userinput = 0;

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

        input = (ImageView) findViewById(R.id.iv_input);
        output = (ImageView) findViewById(R.id.iv_output);
        rock = (Button) findViewById(R.id.btn_rock);
        paper = (Button) findViewById(R.id.btn_paper);
        scissors = (Button) findViewById(R.id.btn_scissors);

        rock.setOnClickListener(this);
        paper.setOnClickListener(this);
        scissors.setOnClickListener(this);
    }

    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_rock:
                userinput = 1;
                input.setBackgroundResource(R.mipmap.rock);
                setOutput();
                break;
            case R.id.btn_paper:
                userinput = 2;
                input.setBackgroundResource(R.mipmap.paper);
                setOutput();
                break;
            case R.id.btn_scissors:
                userinput = 3;
                input.setBackgroundResource(R.mipmap.scissors);
                setOutput();
                break;
        }
    }

    private void setOutput() {
        int imageId = (int) (Math.random() * images.length);
        output.setBackgroundResource(images[imageId]);
        checkresult(imageId);
    }

    private void checkresult(int imageId) {
        if (userinput == 1 && imageId == 0) {     //User choose Rock,Computer choose Rock
            showresult(2);
        } else if (userinput == 1 && imageId == 1) { //User choose Rock,Computer choose Paper
            showresult(0);
        } else if (userinput == 1 && imageId == 2) { //User choose Rock,Computer choose Scissors
            showresult(1);
        } else if (userinput == 2 && imageId == 0) { //User choose Paper,Computer choose Rock
            showresult(1);
        } else if (userinput == 2 && imageId == 1) { //User choose Paper,Computer choose Paper
            showresult(2);
        } else if (userinput == 2 && imageId == 2) { //User choose Paper,Computer choose Scissors
            showresult(0);
        } else if (userinput == 3 && imageId == 0) {//User choose Scissors,Computer choose Rock
            showresult(0);
        } else if (userinput == 3 && imageId == 1) { //User choose Scissors,Computer choose Paper
            showresult(1);
        } else if (userinput == 3 && imageId == 2) { //User choose Scissors,Computer choose Scissors
            showresult(2);
        }
    }

    private void showresult(int result) {
        if (result == 0) {
            Toast.makeText(getApplicationContext(), "Oh! You Lost :(", Toast.LENGTH_SHORT).show();
        } else if (result == 1)
            Toast.makeText(getApplicationContext(), "You Won! Yeah! :)", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(getApplicationContext(), "OOPS! It's a Tie! :P", Toast.LENGTH_SHORT).show();
    }
}

When User clicks on any button then we have called the onClick(View v). Make sure that your class implements View.OnClickListener in order to access this method. Now, in OnClick, we will do Five things on each button click.

    1. To Know what option user has selected we will change the value of the userinput(a global variable) accordingly. We will set 1 if user choose Rock, 2 if user choose Paper and therefore 3 for Scissors
    2. Next, on the Basis of User’s choice, we will set Image to the Top ImageView to show what user has chosen. So we use input.setBackgroundResource(R.mipmap.rock); to set rock image(in this case) to the Top ImageView programmatically. Make sure you have added images for Rock, Paper, and Scissors in the mipmap folder.
    3. Now we will call the setOutput() where a random imageId is obtained which is considered as computer’s choice. That imageId will be sent to the checkresult() . (Math.random() * images.length) will give as a random number of integer type.
    4. The checkresult() checks if the user has lost, won or it’s a tie. Based on that checks performed using if-else statements the message is displayed using showresult()
    5. showresult() prompts the user with the output using Toast.

We have used 3 methods to execute the logic for Android Rock Paper Scissors Game in this Projects:

  1. setOutput() – This method returns a random integer number so random image/option can be chosen as Computer’s choice
  2. checkresult(int imageId) – This method uses values of imageId and userinput to determine the result.
  3. showresult(int result) – This method is responsible for displaying the result to the user using Toast.

You may also download our Android Rock Paper Scissors project from the link given below:

The post Android Rock Paper Scissors Game Tutorial appeared first on Android tutorials for hassle-free android development and programming.



This post first appeared on Android Tutorial Point, please read the originial post: here

Share the post

Android Rock Paper Scissors Game Tutorial

×

Subscribe to Android Tutorial Point

Get updates delivered right to your inbox!

Thank you for your subscription

×