0% found this document useful (0 votes)
6 views5 pages

CODEE

The document contains an Android application layout and code for a Rock-Paper-Scissors game. It includes a LinearLayout with buttons for user choices and a TextView to display the result. The MainActivity class handles the game logic, determining the winner based on the user's choice and a randomly generated computer choice.

Uploaded by

Geramier Apostol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

CODEE

The document contains an Android application layout and code for a Rock-Paper-Scissors game. It includes a LinearLayout with buttons for user choices and a TextView to display the result. The MainActivity class handles the game logic, determining the winner based on the user's choice and a randomly generated computer choice.

Uploaded by

Geramier Apostol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"

android:padding="16dp">

<TextView

android:id="@+id/textView_result"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Make Your Choice!"

android:textSize="24sp"

android:padding="20dp"

android:textAlignment="center" />

<Button

android:id="@+id/button_rock"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rock" />

<Button

android:id="@+id/button_paper"

android:layout_width="wrap_content"

android:layout_height="wrap_content"
android:text="Paper"

android:layout_marginTop="10dp" />

<Button

android:id="@+id/button_scissors"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Scissors"

android:layout_marginTop="10dp" />

</LinearLayout>

package com.example.rockpaperscissors;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private TextView resultTextView;

private String[] choices = {"Rock", "Paper", "Scissors"};

private Random random = new Random();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

resultTextView = findViewById(R.id.textView_result);

Button rockButton = findViewById(R.id.button_rock);

Button paperButton = findViewById(R.id.button_paper);

Button scissorsButton = findViewById(R.id.button_scissors);

rockButton.setOnClickListener(v -> playGame("Rock"));

paperButton.setOnClickListener(v -> playGame("Paper"));

scissorsButton.setOnClickListener(v -> playGame("Scissors"));

}
private void playGame(String userChoice) {

String computerChoice = choices[random.nextInt(choices.length)];

String result;

if (userChoice.equals(computerChoice)) {

result = "Draw! Both chose " + userChoice;

} else if ((userChoice.equals("Rock") &&


computerChoice.equals("Scissors")) ||

(userChoice.equals("Paper") && computerChoice.equals("Rock"))


||

(userChoice.equals("Scissors") &&
computerChoice.equals("Paper"))) {

result = "You Win! " + userChoice + " beats " + computerChoice;

} else {

result = "You Lose! " + computerChoice + " beats " + userChoice;

resultTextView.setText(result);

You might also like