Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. JAVA
  2. package com.example.rockpaperscissors;
  3.  
  4. import androidx.appcompat.app.AppCompatActivity;
  5.  
  6. import android.os.Bundle;
  7. import android.service.autofill.TextValueSanitizer;
  8. import android.view.View;
  9. import android.widget.TextView;
  10.  
  11. import java.util.Random;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14. String[] thisArray = {"Rock", "Paper", "Scissors"};
  15. TextView PlayerOne = findViewById(R.id.textplayer1);
  16. TextView PlayerTwo = findViewById(R.id.textplayer2);
  17. TextView Result = findViewById(R.id.result);
  18.  
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. }
  25.  
  26. public void Randomize(View v){
  27. Random random = new Random();
  28. String RandomStrforP1 = thisArray[random.nextInt(thisArray.length)];
  29. String RandomStrforP2 = thisArray[random.nextInt(thisArray.length)];
  30. System.out.print(RandomStrforP1);
  31. PlayerOne.setText(RandomStrforP1);
  32. PlayerTwo.setText(RandomStrforP2);
  33.  
  34. if (RandomStrforP1 == RandomStrforP2){
  35. Result.setText("Draw");
  36. }
  37. //p1 all win
  38. else if (RandomStrforP1 == "Rock" && RandomStrforP2 == "Scissors"){
  39. Result.setText("Rock beats Scissors, Player 1 Wins");
  40. }
  41. else if (RandomStrforP1 == "Paper" && RandomStrforP2 == "Rock"){
  42. Result.setText("Paper beats Rock, Player 1 Wins");
  43. }
  44. else if (RandomStrforP1 == "Scissor" && RandomStrforP2 == "Paper"){
  45. Result.setText("Scissor beats Paper, Player 1 Wins");
  46. }
  47. //p2 all win
  48. else if (RandomStrforP1 == "Scissors" && RandomStrforP2 == "Rock"){
  49. Result.setText("Rock beats Scissors, Player 2 Wins");
  50. }
  51. else if (RandomStrforP1 == "Rock" && RandomStrforP2 == "Paper"){
  52. Result.setText("Paper beats Rock, Player 2 Wins");
  53. }
  54. else if (RandomStrforP1 == "Paper" && RandomStrforP2 == "Scissor"){
  55. Result.setText("Scissor beats Paper, Player 2 Wins");
  56. }
  57.  
  58. }
  59. }
  60.  
  61. XML
  62. <?xml version="1.0" encoding="utf-8"?>
  63. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  64. xmlns:app="http://schemas.android.com/apk/res-auto"
  65. xmlns:tools="http://schemas.android.com/tools"
  66. android:layout_width="match_parent"
  67. android:layout_height="match_parent"
  68. android:orientation="vertical"
  69. android:gravity="center"
  70. tools:context=".MainActivity">
  71.  
  72. <TextView
  73. android:layout_width="match_parent"
  74. android:layout_height="wrap_content"
  75. android:layout_marginBottom="25dp"
  76. android:textAlignment="center"
  77. android:text="Player 1"/>
  78.  
  79. <TextView
  80. android:id="@+id/textplayer1"
  81. android:layout_width="match_parent"
  82. android:layout_height="wrap_content"
  83. android:layout_marginBottom="25dp"
  84. android:textAlignment="center"
  85. android:text="Rock/Paper/Scissors"/>
  86.  
  87. <TextView
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:layout_marginBottom="25dp"
  91. android:textAlignment="center"
  92. android:text="Player 2"/>
  93.  
  94. <TextView
  95. android:id="@+id/textplayer2"
  96. android:layout_width="match_parent"
  97. android:layout_height="wrap_content"
  98. android:layout_marginBottom="25dp"
  99. android:textAlignment="center"
  100. android:text="Rock/Paper/Scissors"/>
  101.  
  102. <TextView
  103. android:layout_width="match_parent"
  104. android:layout_height="wrap_content"
  105. android:layout_marginBottom="25dp"
  106. android:textAlignment="center"
  107. android:text="Result"/>
  108.  
  109. <TextView
  110. android:id="@+id/result"
  111. android:layout_width="match_parent"
  112. android:layout_height="wrap_content"
  113. android:layout_marginBottom="25dp"
  114. android:textAlignment="center"
  115. android:text="Result Here"/>
  116.  
  117. <Button
  118. android:id="@+id/randomize"
  119. android:layout_width="match_parent"
  120. android:layout_height="wrap_content"
  121. android:layout_marginHorizontal="25dp"
  122. android:text="Random"
  123. android:onClick="Randomize"
  124. />
  125.  
  126.  
  127. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement