Sunday, November 4, 2012

Android: Rock, Paper, Scissors

I'm giving my first baby steps in the Android development world. In this simple application I exemplify the use of:

  • Buttons
  • Click events
  • Image Views
  • Alert dialogs
The application is a simple game of "Rock, Paper, Scissors". User clicks on an image button according to the selection, and immediately the Android makes a random selection. According to the universal very well known rules of this game, an alert dialog is shown with the game result.

So here's the basic layout of the game. The two interrogation images are for displaying the two selections. The three images below are for the user to select an option.



















This is the layout XML:

This is the code for the activity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.example.mytoolbox;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
 
public class RockPaperScissors extends Activity implements OnClickListener {
  
 public enum Option {
  ROCK, PAPER, SCISSORS
 }
  
 public enum Result {
  WIN, LOSS, DRAW 
 }
  
 private Option userSelection;
 private Result gameResult;
  
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rock_paper_scissors);
         
        Button buttonRock = (Button) findViewById(R.id.buttonRock);
        Button buttonpaper = (Button) findViewById(R.id.buttonPaper);
        Button buttonScissors = (Button) findViewById(R.id.buttonScissors);
        ImageButton buttonHome = (ImageButton) findViewById(R.id.imageButtonHome);
         
        // Set click listening event for all buttons.
        buttonRock.setOnClickListener(this);
        buttonpaper.setOnClickListener(this);
        buttonScissors.setOnClickListener(this);
        buttonHome.setOnClickListener(this);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_rock_paper_scissors, menu);
        return true;
    }
 
 @Override
 public void onClick(View v) {
   
  ImageView imageView = (ImageView) findViewById(R.id.imageViewAnswerUser);
  boolean play = true;
   
  switch (v.getId()) {
   case R.id.buttonRock:
    userSelection = Option.ROCK;
    imageView.setImageResource(R.drawable.rock);
    break;
   case R.id.buttonPaper:
    userSelection = Option.PAPER;
    imageView.setImageResource(R.drawable.paper);
    break;
   case R.id.buttonScissors:
    userSelection = Option.SCISSORS;
    imageView.setImageResource(R.drawable.scissors);
    break;
   case R.id.imageButtonHome:
    startActivity(new Intent(RockPaperScissors.this, ChooseActivity.class)); // To go home.
    play = false;
    break;
  }
   
  if(play) {
   play();
   showResults();
  }
 }
 
 private void showResults() {
  AlertDialog.Builder builder = new AlertDialog.Builder(RockPaperScissors.this);    
     builder.setCancelable(false);    
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // Do nothing  
   }
  });
      
     // Sets the right message according to result.
     if(gameResult == Result.LOSS) {
      builder.setMessage("You Loose!");
     } else if(gameResult == Result.WIN) {
      builder.setMessage("You Win!");
     } else if(gameResult == Result.DRAW) {
      builder.setMessage("It's a draw!");
     }
      
     AlertDialog alert = builder.create();
     alert.show(); 
    }
 
 private void play() {
  // Generates a random play.
  int rand =  ((int)(Math.random() * 10)) % 3;
  Option androidSelection = null;
  ImageView imageView = (ImageView) findViewById(R.id.ImageViewAnswerAndroid);
   
  // Sets the right image according to random selection.
  switch (rand) {
   case 0:
    androidSelection = Option.ROCK;
    imageView.setImageResource(R.drawable.rock);
    break;
   case 1:
    androidSelection = Option.PAPER;
    imageView.setImageResource(R.drawable.paper);
    break;
   case 2:
    androidSelection = Option.SCISSORS;
    imageView.setImageResource(R.drawable.scissors);
    break;
  }
  // Determine game result according to user selection and Android selection.
  if(androidSelection == userSelection) {
   gameResult = Result.DRAW;
  }
  else if(androidSelection == Option.ROCK && userSelection == Option.SCISSORS) {
   gameResult = Result.LOSS;
  }
  else if(androidSelection == Option.PAPER && userSelection == Option.ROCK) {
   gameResult = Result.LOSS;
  }
  else if(androidSelection == Option.SCISSORS && userSelection == Option.PAPER) {
   gameResult = Result.LOSS;
  } else {
   gameResult = Result.WIN;
  
 }   
}
Running app:


4 comments:

  1. Hi Gabriel,

    I'm new to app development and wanted to test run your code above. Can you please help explain some of the errors I'm receiving?

    **This one in particular ImageView imageView = (ImageView) findViewById(R.id.ImageViewAnswerAndroid); **

    Does R.id.ImageViewAnswerAndroid need to be made as a new class? I'm receiving error as: "ImageViewAnswerAndroid cannot be resolved or is not a field type"

    Thank You!

    Jesse L.

    ReplyDelete
  2. Hello fellow developer,

    It's been a while since I did something Android related. But if something it's not compiling, it might be because I didn't include steps to create the GUI elements. "ImageViewAnswerAndroid" must be a GUI element you need to add.

    Maybe later on I can give it a better check.

    Good luck in your learning.

    ReplyDelete
  3. Hello, would you be able to provide a step by step tutuorial, im currently studying and finding android studio to be a bit challenging at times :)

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete