Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Android Hi, I have this assignment that needs a score recorded. Rock, paper scissors. I have all of the objects done and declaring a
Java Android
Hi, I have this assignment that needs a score recorded. Rock, paper scissors. I have all of the objects done and declaring a winner. Not sure how to add a score if the player won or the computer won.
Here is what I have so far this is done with Android Stidio. I also have the GUI done and ready to go.
import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { Button btnRock, btnPaper, btnScissors; TextView txtHumanChoice, txtComputerChoice, txtWinner, txtScore; enum Choice {ROCK, PAPER, SCISSORS}; enum Winner {PLAYER, COMPUTER, TIE}; Random random = new Random(); int playerScore = 0; int computerScore = 0; int numberOfGames = 0; int Player; int Computer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar=( Toolbar ) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab=( FloatingActionButton ) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); btnRock = (Button) findViewById(R.id.btnRock); btnPaper = (Button) findViewById(R.id.btnPaper); btnScissors = (Button) findViewById(R.id.btnScissors); txtHumanChoice = (TextView) findViewById(R.id.txtHumanChoice); txtComputerChoice = (TextView) findViewById(R.id.txtComputerChoice); txtWinner = (TextView) findViewById(R.id.txtWinner); txtScore = (TextView) findViewById(R.id.txtScore); btnRock.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MyMsg", "Rock button pressed"); PlayGame(Choice.ROCK); } }); btnPaper.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MyMsg", "Paper button pressed"); PlayGame(Choice.PAPER); } }); btnScissors.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MyMsg", "Scissors button pressed"); PlayGame(Choice.SCISSORS); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id=item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //This is where the game logic goes. void PlayGame(Choice humanChoice) { Choice computerChoice; Winner gameResult = Winner.TIE; String stringWinner = ""; String stringHumanChoice = ""; String stringComputerChoice = ""; // Score stringScore =""; int number = random.nextInt(3); computerChoice = Choice.values()[number]; Log.d("MyMsg", "Computer Choice = " + number + computerChoice); switch (humanChoice) { case ROCK: stringHumanChoice = "Hard Rock"; break; case PAPER: stringHumanChoice = "Paper"; break; case SCISSORS: stringHumanChoice = "Sharp Scissors"; break; default: stringHumanChoice = "Error in humanChoice switch"; } switch (computerChoice) { case ROCK: stringComputerChoice = "Hard Rock"; break; case PAPER: stringComputerChoice = "Paper"; break; case SCISSORS: stringComputerChoice = "Sharp Scissors"; break; default: stringComputerChoice = "Error in humanChoice switch"; } if(humanChoice == computerChoice) gameResult = Winner.TIE; if(humanChoice == Choice.ROCK && computerChoice == Choice.PAPER) gameResult = Winner.COMPUTER; if(humanChoice == Choice.ROCK && computerChoice == Choice.SCISSORS) gameResult = Winner.PLAYER; if(humanChoice == Choice.PAPER && computerChoice == Choice.ROCK) gameResult = Winner.PLAYER; if(humanChoice == Choice.PAPER && computerChoice == Choice.SCISSORS) gameResult = Winner.COMPUTER; if(humanChoice == Choice.SCISSORS && computerChoice == Choice.PAPER) gameResult = Winner.PLAYER; if(humanChoice == Choice.SCISSORS && computerChoice == Choice.ROCK) gameResult = Winner.COMPUTER; { } switch (gameResult) { case TIE: stringWinner="It's A Tie"; break; case PLAYER: stringWinner="Player Wins <3"; break; case COMPUTER: stringWinner="Computer Wins"; break; default: stringWinner="Error in gameResult switch"; } txtHumanChoice.setText("Player: " + stringHumanChoice); txtComputerChoice.setText("Computer: " + stringComputerChoice); txtWinner.setText(stringWinner); { System.out.println("Number of games played is "+ numberOfGames); System.out.println("Player's Score " + playerScore); System.out.println("Computers score " + computerScore); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started