Rock Paper Scissors Compiler Error - java

I am trying to construct a rock paper scissors game that incorporates multiple classes together. I am struggling a lot with object oriented programming, and I cannot figure out how to correct my compiler error. Below is my code:
Main method:
import java.util.*;
public class RPSMain extends RPSPlayer{
RPSPlayer player = new RPSPlayer();
RPSGame gameObject = new RPSGame ();
public void main ()
{
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
System.out.print ("Number of Rounds: ");
int rounds = sc.nextInt();
//Call and process all of the methods found in RPSPlayer and RPSGame
for (int i = 0; i < rounds; i++){
int playerThrow = player.makeThrow();
int compThrow = gameObject.makeCompThrow();
int winner = gameObject.announceWinner (compThrow, playerThrow );
System.out.print (gameObject.bigWinner(winner, rounds));
}
//Final Output
System.out.print (gameObject.bigWinner(winner, rounds));
}
//accessor to return round to RPSGame
public int getRound (int round){
this.round = round;
return round;
}
}
Game method:
import java.util.*;
public class RPSGame extends RPSPlayer{
RPSPlayer player = new RPSPlayer();
RPSGame game = new RPSGame ();
RPSMain mainRPS = new mainRPS();
public void main (String args[]) {
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
int rounds = mainRPS.getRound(rounds);
}
//Random Throw Generator
public int makeCompThrow (){
int Max = 3;
int Min = 1;
int compThrow = Min + (int)(Math.random() * ((Max - Min) + 1));
return compThrow;
}
// Get the throw from the player in RPSPlayer
public int getPlayerThrow (){
RPSPlayer player = new RPSPlayer();
int getPThrow = player.makeThrow();
return getPThrow;
}
//Does all of the calculatoins and ouputs who threw what.
public int announceWinner (int compThrow, int getPThrow) {
int winner = 0;
if (getPThrow == 1){
System.out.println ("Player throws ROCK.");
}
else if (getPThrow == 2){
System.out.println ("Player throws PAPER.");
}
else if (getPThrow == 3){
System.out.println ("Player throws SCISSORS.");
}
if (compThrow == 1){
System.out.println ("Computer throws ROCK.");
}
else if (compThrow == 2){
System.out.println ("Computer throws PAPER.");
}
else if (compThrow == 3){
System.out.println ("Computer throws SCISSORS.");
}
if (getPThrow == compThrow){
winner = 3;
}
else if (getPThrow == 1 && compThrow == 3){
winner = 1;
}
else if (getPThrow == 1 && compThrow == 2){
winner = 2;
}
else if (getPThrow == 2 && compThrow == 1){
winner = 1;
}
else if (getPThrow == 2 && compThrow == 3){
winner = 2;
}
else if (getPThrow == 3 && compThrow == 1){
winner = 2;
}
else if (getPThrow == 3 && compThrow == 2){
winner = 1;
}
return winner;
}
//Final Output with imported values of 'rounds' and 'winner'
public int bigWinner (int winner, int rounds){
int tie = 0;
int playerWins = 0;
int compWins = 0;
if (winner == 1){
playerWins = playerWins + 1;
}
else if (winner == 0){
tie = tie + 1;
}
else if (winner == 3){
compWins = compWins + 1;
}
System.out.println ("You win " +playerWins+ ". Computer wins " +(compWins)+ ".");
if (playerWins > compWins){
System.out.print ("You win!");
}
if (playerWins < compWins){
System.out.print ("Computer wins!");
}
if (playerWins == compWins){
System.out.print ("It's a tie!");
}
return tie;
}
}
Player method:
import java.util.*;
public class RPSPlayer {
public void main (String args[]) {
Random generator = new Random ();
Scanner sc = new Scanner(System.in);
}
//This method gets the throw, and loops if throw is not within 1 and 3
public int makeThrow (){
Scanner sc = new Scanner (System.in);
int playerThrow;
do{
System.out.print ("Enter your throw (1=Rock, 2=Paper, 3=Scissors)");
playerThrow = sc.nextInt();
} while (playerThrow > 3 && playerThrow < 1);
return playerThrow;
}
//Accessor method
public int getThrow (int playerThrow){
this.playerThrow = playerThrow;
return playerThrow;
}
}
When I attempt to compile any of the classes, the error refers to code found in the RPSPlayer class:
cannot find symbol - variable playerThrow
As I said before, my knowledge of object-oriented program is very weak. I'm not really sure why I'm getting this error, as int playerThrow is defined right above it. I am also unsure if there are additional flaws or errors within my code. One thing I particularly struggle with is the concept of static vs. non-static code, and when to use which and what can be used within them.
Suggestions are greatly appreciated. Thank you in advance.

You'll need to define the member variable, before you can assign a value to it. That's mandatory (obviously) for every class and every member variable.
Please add this to your class:
private int playerThrow;
Also, the function getThrow makes no sense. Getters return an already set value; setters are the one who actually give a value to your member variables. Do this instead:
public int getThrow()
{
return playerThrow;
}
public void setThrow(int playerThrow)
{
this.playerThrow = playerThrow;
}
Please note that this.playerThrow and playerThrow are not related in any way. this.playerThrow is a member variable of your class (defined as shown above) and playerThrow is a variable given as an argument to your method.
EDIT:
I'm addressing some more of your errors.
The problem you are referring to in your comment is because you are calling a constructor mainRPS while your class is called RPSmain. That's located here RPSMain mainRPS = new mainRPS(); in the RPSGame class, in the start. Please fix this to RPSMain mainRPS = new RPSMain();.
Please define private int round in your RPSMain class. That's the same error as your first one addressed.
Also, define int winner outside the for loop because you're using it outside your loop, as well. When you define a value inside a loop, it will be local to that loop and will get destroyed when the program exits the loop.

Related

Best way to code my unintelligent Nim game?

This is my nim game (goal is dont be the last person to pick up marbles), can someone please guide me. In playing the game, I have just one question
How can I keep track of whose turn it is, I guess if I can keep track of that I can monitor my remainingMarbles. This is the crux of my code. Please help
public class Nim{
public static void main(String[] args)
{
System.out.println("************** Welcome to the game of Nim *******************");
System.out.println("The object of this game is to make me take the last marble off the table");
System.out.println("You must take at least 1 and you can take up to 3 marbles on your turn");
System.out.println("You can go first");
Scanner scan = new Scanner(System.in);
final int MARBLES = 13;
int remainingMarbles;
String input;
do {
//boolean whoseTurn = true;
remainingMarbles = MARBLES;
System.out.println("There are " + MARBLES + " marbles on the table");
while (remainingMarbles > 0){
remainingMarbles -= getUserSelection();
System.out.println("There are " + (remainingMarbles -= getComputerSelection()) + " marble(s) on the table.");
}
if (1 <= remainingMarbles && remainingMarbles <= 2 && remainingMarbles < 0) {
System.out.println("Congratulations! you won!");
System.out.println("Want to play again? y/n");
input = scan.nextLine();
input = input.toLowerCase();
} else
System.out.println("Hard luck you lose!");
System.out.println("Want to play again? y/n");
input = scan.nextLine();
input = input.toLowerCase();
}while (input.charAt(0) == 'y');
}
private static int getUserSelection()
{
Scanner scan = new Scanner(System.in);
do {
System.out.println("Enter 1, 2 or 3");
int userSelection = scan.nextInt();
if (isValidMove(userSelection)){
return userSelection;
}
else {
System.out.println("Not a valid entry");
}
}while (true);
}
private static boolean isValidMove(int input)
{
return 1 <= input && input <= 3;
}
private static int getComputerSelection ()
{
Random generator = new Random();
int computerSelection = 1 + generator.nextInt(3);
System.out.println("The computer chooses " + computerSelection);
return computerSelection;
}
}
First, make a boolean before the loop.
boolean whoseTurn = true;
When the variable is true it's the first player's turn, otherwise the second player's turn.
Now inside the while loop we change the variable at the end of every repetition:
whoseTurn = !whoseTurn;
(Which is a faster way of this)
if(whoseTurn) whoseTurn = false;
else whoseTurn = true;
To conclude, you should do something like that:
...
boolean whoseTurn = true;
while(remainingMarbles > 0){
remainingMarbles -= getUserSelection();
System.out.println("There are " + (remainingMarbles -= getComputerSelection()) + " marble(s) on the table.");
whoseTurn = !whoseTurn;
}
...

Java Shut the Box

Im writing a program similar to a game called shut the box. The game asks the player to roll 2 die, and then the player chooses to cover both numbers individually (in the boolean array), or the total of the two.
I'm having difficulty returning two values of the int method roll() which is supposed to roll two die.
Heres my code for the main class:
public class clacker {
private int play;
private int die1;
private int die2;
private boolean[] table;
private final int high = 6;
private final int low = 1;
private int range;
public clacker()
{
range = high-low+1;
table = new boolean[13];
play = 0;
}
public void roll()
{
die1 = (int)(Math.random()*range+low);
die2 = (int)(Math.random()*range+low);
}
public void value(char ch)
{
if(ch == 'i' || ch == 'I')
{
table[die1] = true;
table[die2] = true;
displayBoard();
}
else if(ch == 'T' || ch == 't')
{
table[die1+die2] = true;
displayBoard();
}
else
{
roll();
}
}
public void displayBoard()
{
for(int i = 1; i<table.length; i++)
{
if(table[i] == false)
{
System.out.print(" " + i + " ");
}
else
{
System.out.print(" / ");
}
}
}
}
and for the test class:
public class clackerTest {
public static void main(String[] args) {
clacker oo = new clacker();
EasyReader kboard = new EasyReader();
System.out.println("Press anything to roll the dice. ");
char roll= kboard.readChar();
int dice1 = oo.roll();
int dice2 = oo.roll();
System.out.println("You rolled " + dice1 + " and a " + dice2 + "!");
System.out.println("Cover Individual or Total: (enter i or t)");
char roll2 = kboard.readChar();
}
}
Why don't you just roll() twice? Modify roll() to return one int.
public int roll() {
return (int)(Math.random()*range+low);
}
And now, from within clackerTest (java conventions dictates you name this ClackerTest):
int dice1 = oo.roll();
int dice2 = oo.roll();

Rock, Paper, Scissors random match not working

How do you make the player play for a random number of match and then end the game with all the score?
I'm trying to do a loop that let a player play a number of matches and ask at the end if still want to play or not.
public class RockPaperScissors {
public static void main(String[] args) {
Random rand = new Random();
Scanner sc = new Scanner(System.in);
Scanner playername = new Scanner(System.in);
String name;
int playerChoose;
int aiChoose;
String match = "";
int matchCount = 1;
int matchLimit = 10;
boolean endMatch = true;
int Rock = 1, Paper = 2, Scissor = 3;
//Player choose how many round.
int max = 10;
int min = 1;
int randomNum = rand.nextInt((max - min) + 1) + 1;
// Computer random choose Rock, Paper or Scissor.
int maxme = 3;
int minme = 1;
aiChoose = rand.nextInt((maxme - minme) + 1) + 1;
System.out.println("Hi, what is your name?");
name = playername.next();
System.out.println("Hi, " + name);
System.out.println("Let's play Rock, Paper and Scissor!!");
System.out.println("How many round do you want to play?");
// loop match I'm doing something wrong
while (!match.equals(randomNum)) {
System.out.println(randomNum + " match");
if (randomNum == 0)
{
System.out.println("Chooses between Rock - 1, Paper - 2 and Scissor - 3? ");
}
playerChoose = sc.nextInt();
//Check Player choise for Rock Paper and Scissor
if (playerChoose < 1 || playerChoose > 3) {
System.out.println("Wrong Choise");
System.exit(0);
}
System.out.println(aiChoose);
if (playerChoose == aiChoose) {
System.out.println("We tie");
} else if ((playerChoose == Rock && aiChoose == Scissor) || (playerChoose == Scissor && aiChoose == Paper) || (playerChoose == Paper && aiChoose == Rock));
{
System.out.println("You win");
}
}
}
If you want to do a specific amount of round you can do it with a for loop:
for(int i = 0; i < numberOfMatches; i++){
}
If you want to let the user decide if he wants to play you could do something like this:
while(true){
//all the things before
System.out.println("Do you still want to play? ")
if(sc.next.equals("No")){
break;
}
}

Having trouble w/ returning and passing values; "counting"

Our task was to create a guessing game, where the computer would generate a number and the user was prompted to guess. We were to create a method to play only one game, and then create a while loop in the main to make the game playable again. In the end, we need to show statistics. I'm having trouble with showing the "best game." That is a game where the amount of guesses is the least.
Here is the code:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static final int MAX = 100;
// This is the main. Here we can see a do/while loop
// and a few variables that were created to compliment it.
public static void main(String[] args) {
Random rand = new Random();
Scanner console = new Scanner(System.in);
intro();
String s = "";
int totalGames = 0;
int totalGuess = 0;
do {
totalGuess = game(console);
System.out.print("Do you want to play again? ");
s = console.next();
totalGames++;
} while (s.equals("y") || s.equals("Y") || s.equals("Yes") ||
s.equals("yes") || s.equals("Yes"));
totalGuess = totalGuess;
statistics(totalGames, totalGuess);
}
// This method prints out the intro.
public static void intro() {
System.out.println("This program allows you to play a guessing
game.");
System.out.println("I will think of a number between 1 and");
System.out.println(MAX + " and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.\n ");
}
// This method plays the game only once. It's later used in the main.
// Returns the
// number of guesses for one game.
public static int game(Scanner console) {
Random rand = new Random();
int random = rand.nextInt(MAX) + 1;
System.out.println("I'm thinking of a number between 1 and " + MAX + "
... (it's " + random + " )");
System.out.print("Your guess? > ");
int guess = console.nextInt();
int count = 0;
do {
if ((random - guess) > 0) {
System.out.println("It's higher.");
System.out.print("Your guess? > ");
guess = console.nextInt();
count++;
}
else if ((random - guess) < 0) {
System.out.println("It's lower.");
System.out.print("Your guess? > ");
guess = console.nextInt();
count++;
}
else if (random == guess) {
count++;
}
} while (random != guess);
if (count == 1) {
System.out.println("You got it right on the first guess!!");
}
else {
System.out.println("You got it right in " + count + " guesses.");
}
return count;
}
// This method prints out the statistics.
public static void statistics(int x, int y) {
System.out.println("total games = " + x);
System.out.println("total guesses = " + (y));
System.out.println("guesses/game = ");
System.out.println("best game = ");
}
}
Have a look when totalGuess is assigned:
public static void main(String[] args) {
Random rand = new Random();
Scanner console = new Scanner(System.in);
intro();
String s = "";
int totalGames = 0;
int totalGuess = 0;
// ^ Initialized to zero
do {
totalGuess = game(console);
// ^ Assigned (not added) to the return value from game.
// Did you mean: totalGuess += game(console); ?
System.out.print("Do you want to play again? ");
s = console.next();
totalGames++;
} while (s.equals("y") || s.equals("Y") || s.equals("Yes") ||
s.equals("yes") || s.equals("Yes"));
totalGuess = totalGuess;
// ^ Assigned to itself. No action.
statistics(totalGames, totalGuess);
}

I am having the compile error "cannot find symbol - method getPlayer()"

I am supposed to be writing the game of nim. I am having trouble finding out how to fix the compile error
cannot find symbol - method getPlayer()
Also, is this the only problem you see? Or are there other issues that would cause this program to fail to compile or work properly.
import java.util.Scanner;
import java.util.Random;
public class Nim {
private int n;
private int compMode;
private int numberLeft;
private int numberTaken;
private boolean whoseTurn;
private String inputName;
private String name;
private String play;
private boolean yes;
Scanner in = new Scanner(System.in);
Random num = new Random();
public void setState() {
numberLeft = 100;
numberTaken = numberLeft;
}
public String getPlayer() {
inputName = in.next("");
inputName = name;
return name;
}
public void getCompPlay() {
compMode = num.nextInt(2);
if (compMode == 0) System.out.println("The computer is in smart mode");
if (compMode == 1) System.out.println("The computer is in random mode");
}
public void playGame() {
if (whoseTurn == true) {
System.out.println(name + "It is your turn...");
System.out.printf("Please enter the number you wish to take from the pile (Must be less than " + (numberLeft / 2) + "): ");
numberTaken = in.nextInt();
numberLeft -= numberTaken;
System.out.println("The number left is " + numberLeft);
whoseTurn = false;
}
if (whoseTurn == false) {
System.out.println("It is the computer's turn...");
if (compMode == 0) {
numberLeft = smartComputer(numberLeft);
System.out.println("The number left is " + numberLeft);
}
if (compMode == 1) {
numberLeft -= num.nextInt(numberLeft / 2);
System.out.println("The number left is " + numberLeft);
}
whoseTurn = true;
return;
}
if (yes == true) {
}
if (numberLeft <= 1) {
if (whoseTurn = false) {
System.out.println("You Win!");
} else {
System.out.println("You're horrible...you lost to a computer.");
}
}
if (numberLeft <= 1) {
if (whoseTurn = false) {
System.out.println("You Win!");
} else {
System.out.println("you lost to a computer.");
}
}
}
public static int smartComputer(int num) {
int power = 2;
while (power < num) {
power *= 2;
}
power /= 2;
num = power - 1;
return num;
}
public boolean playAnother() {
System.out.println("/nPlay Again? (y/n)");
play = in.next("");
if (play.equalsIgnoreCase("y")) return true;
else return false;
}
public void displayTotals() {}
}
And here is my Tester
public class NimTester {
public static void header() {
System.out.println("Eric Magnusson");
System.out.println("AP Comp Sci");
System.out.println("Game of Nim (P6.16)");
}
public static void main() {
Nim nim = new Nim(getPlayer(), getCompPlay());
do {
nim.setState();
nim.playGame();
nim.printWinner();
} while (playAnother());
nim.displayTotals();
}
}
Your class either has no specific Constructor, or it is not shwon in your code. To learn more about Construcotrs, please read the documentation here. To put it simple, a Constructor is a method that allows the JVM to organize the memory and initialize the object.
The default object constructor is a non-argument constructor that sets each child variable the default value.
Example:
Class Car{
public Car(){
System.out.print("car is built.");
}
}
Car aCar = new Car();
What you have is Nim nim = new Nim(getPlayer(), getCompPlay()); therefore your Nimclass has to have a public Nim(String x, String y) method.
Another problem with the code shown is, you are using an object method to create the object...
Either the method has to be static (read about it here), or it has to not depend on the object you are creating. To simplify your program, just save that value before using it in a variable, then use that variable in the object construction. Or even create a staic method like: public static String getJohn(){return "John"}, and then your code will work past that point.
Other issues with the code are related to readability, please read on Java Naming conventions, on how to name your variables, they will help maintenance, as well as allow other programmers to assist you.

Categories

Resources