Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JAVA
- package com.example.rockpaperscissors;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.service.autofill.TextValueSanitizer;
- import android.view.View;
- import android.widget.TextView;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- String[] thisArray = {"Rock", "Paper", "Scissors"};
- TextView PlayerOne = findViewById(R.id.textplayer1);
- TextView PlayerTwo = findViewById(R.id.textplayer2);
- TextView Result = findViewById(R.id.result);
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void Randomize(View v){
- Random random = new Random();
- String RandomStrforP1 = thisArray[random.nextInt(thisArray.length)];
- String RandomStrforP2 = thisArray[random.nextInt(thisArray.length)];
- System.out.print(RandomStrforP1);
- PlayerOne.setText(RandomStrforP1);
- PlayerTwo.setText(RandomStrforP2);
- if (RandomStrforP1 == RandomStrforP2){
- Result.setText("Draw");
- }
- //p1 all win
- else if (RandomStrforP1 == "Rock" && RandomStrforP2 == "Scissors"){
- Result.setText("Rock beats Scissors, Player 1 Wins");
- }
- else if (RandomStrforP1 == "Paper" && RandomStrforP2 == "Rock"){
- Result.setText("Paper beats Rock, Player 1 Wins");
- }
- else if (RandomStrforP1 == "Scissor" && RandomStrforP2 == "Paper"){
- Result.setText("Scissor beats Paper, Player 1 Wins");
- }
- //p2 all win
- else if (RandomStrforP1 == "Scissors" && RandomStrforP2 == "Rock"){
- Result.setText("Rock beats Scissors, Player 2 Wins");
- }
- else if (RandomStrforP1 == "Rock" && RandomStrforP2 == "Paper"){
- Result.setText("Paper beats Rock, Player 2 Wins");
- }
- else if (RandomStrforP1 == "Paper" && RandomStrforP2 == "Scissor"){
- Result.setText("Scissor beats Paper, Player 2 Wins");
- }
- }
- }
- XML
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout 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:orientation="vertical"
- android:gravity="center"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Player 1"/>
- <TextView
- android:id="@+id/textplayer1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Rock/Paper/Scissors"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Player 2"/>
- <TextView
- android:id="@+id/textplayer2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Rock/Paper/Scissors"/>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Result"/>
- <TextView
- android:id="@+id/result"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="25dp"
- android:textAlignment="center"
- android:text="Result Here"/>
- <Button
- android:id="@+id/randomize"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginHorizontal="25dp"
- android:text="Random"
- android:onClick="Randomize"
- />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement