Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You need to complete it so that you keep score for both the Player and the computer, and also show how many tie games. Main

You need to complete it so that you keep score for both the Player and the computer, and also show how many tie games.

Main Activity:

package com.John.rockpaperscissors; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { Button btnRock, btnPaper, btnScissors; TextView txtPlayerChoice, txtComputerChoice, txtWinner, txtPlayerWins, txtComputerWins, txtTie; enum Choice {ROCK, PAPER, SCISSORS} enum Winner {PLAYER, COMPUTER, TIE} Random random = new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // This is where program execution starts... btnRock = findViewById(R.id.btnRock); btnPaper = findViewById(R.id.btnPaper); btnScissors = findViewById(R.id.btnScissors); txtPlayerChoice = findViewById(R.id.txtPlayerChoice); txtComputerChoice = findViewById(R.id.txtComputerChoice); txtWinner = findViewById(R.id.txtWinner); txtPlayerChoice.setText(""); txtComputerChoice.setText(""); txtWinner.setText(""); btnRock.setOnClickListener(view -> { Log.d("MyMsg", "Rock button pressed..."); PlayGame(Choice.ROCK); }); btnPaper.setOnClickListener(view -> { Log.d("MyMsg", "Paper button pressed..."); PlayGame(Choice.PAPER); }); btnScissors.setOnClickListener(view -> { Log.d("MyMsg", "Scissors button pressed..."); PlayGame(Choice.SCISSORS); }); } // This is where the game logic goes. void PlayGame(Choice playerChoice) { Log.d("MyMsg", "Play game. Player chose " + playerChoice); Log.d("MyMsg", "Player choice value = " + playerChoice.ordinal()); Choice computerChoice; Winner gameResult = Winner.TIE; // Has to be set to something String stringPlayerChoice = ""; String stringComputerChoice = ""; String stringWinner = ""; int number = random.nextInt(3); Log.d("MyMsg", "Computer chose " + number); computerChoice = Choice.values()[number]; Log.d("MyMsg", "Computer chose " + computerChoice); switch(playerChoice) { case ROCK: stringPlayerChoice = "Hard Rock"; break; case PAPER: stringPlayerChoice = "Paper"; break; case SCISSORS: stringPlayerChoice = "Scissors"; break; default: stringPlayerChoice = "Error in playerChoice switch"; } switch(computerChoice) { case ROCK: stringComputerChoice = "Hard Rock"; break; case PAPER: stringComputerChoice = "Paper"; break; case SCISSORS: stringComputerChoice = "Scissors"; break; default: stringComputerChoice = "Error in playerChoice switch"; } txtPlayerChoice.setText("You chose " + stringPlayerChoice); txtComputerChoice.setText("Computer chose " + stringComputerChoice); if (playerChoice == computerChoice) gameResult = Winner.TIE; if (playerChoice == Choice.ROCK && computerChoice == Choice.PAPER) gameResult = Winner.COMPUTER; if (playerChoice == Choice.ROCK && computerChoice == Choice.SCISSORS) gameResult = Winner.PLAYER; if (playerChoice == Choice.PAPER && computerChoice == Choice.ROCK) gameResult = Winner.PLAYER; if (playerChoice == Choice.PAPER && computerChoice == Choice.SCISSORS) gameResult = Winner.COMPUTER; if (playerChoice == Choice.SCISSORS && computerChoice == Choice.ROCK) gameResult = Winner.COMPUTER; if (playerChoice == Choice.SCISSORS && computerChoice == Choice.PAPER) gameResult = Winner.PLAYER; switch (gameResult) { case TIE: stringWinner = "Tie game."; txtWinner.setTextColor(Color.parseColor("#000000")); // Black break; case PLAYER: stringWinner = "You win!!!"; txtWinner.setTextColor(Color.parseColor("#008000")); // Dark green break; case COMPUTER: stringWinner = "Computer wins."; txtWinner.setTextColor(Color.parseColor("#FFA500")); // Orange break; default: stringWinner = "Error in gameResult switch"; } txtWinner.setText(stringWinner); } }

XML:

   

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions