Unknown issue finding symbol in Java code - java

First off, I'm pretty green when it comes to java. So, I'm making a rock paper scissors game with 2 classes and an object connecting the two. For some unknown reason, when I use the object, it cannot find the method im pointing to. The error is "cannot find symbol" and is in the first class where it is game.RockPaperScissors();
public class RPSRunner {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
char response;
//add in a do while loop after you get the basics up and running
out.print("type in your prompt [R,P,S] :: ");
String player = keyboard.nextLine();
RockPaperScissors game = new RockPaperScissors();
do {
game. **RockPaperScissor **(player);
} while (player == r || p || s);
}
}
and
public class RockPaperScissors {
String playChoice;
String compChoice;
Random random = new Random();
int rand = 0;
public RockPaperScissors() {
playChoice = " ";
compChoice = " ";
}
public RockPaperScissors(String player) {
setPlayers(player);
}
public void setPlayers(String player) {
playChoice = player;
rand = random.nextInt(3);
if (rand == 0) {
compChoice = "r";
}
if (rand == 1) {
compChoice = "p";
}
if (rand == 2) {
compChoice = "s";
}
System.out.println("player had " + playChoice);
System.out.println("computer had " + compChoice);
}
public String determineWinner() {
String winner = "";
if ((compChoice == "r") && (playChoice == "p")) ;
{
winner = "!Player wins << Paper covers Rock>>!";
}
if ((compChoice == "r") && (playChoice == "s")) ;
{
winner = "! Computer wins << Rock breaks Scissors >>!";
}
if ((compChoice == "p") && (playChoice == "r")) ;
{
winner = "! Computer wins << Paper covers Rock>>!";
}
if ((compChoice == "p") && (playChoice == "s")) ;
{
winner = "!Player wins << Scissors cuts paper >>!";
}
if ((compChoice == "s") && (playChoice == "p")) ;
{
winner = "! Computer wins << Scissors cuts paper >>!";
}
if ((compChoice == "s") && (playChoice == "r")) ;
{
winner = "!Player wins << Rock breaks Scissors >>!";
}
if (compChoice == playChoice) ;
{
winner = " !Tie << Both the computer and player have selected " + compChoice + " >>!";
}
return winner;
}
public String toString() {
String output = "";
return output;
}
}

Change
public RockPaperScissors(String player)
To
public void RockPaperScissors(String player)
The former is a constructor while the latter is a method. Also,there seems to be numerous other problems in your code(like the condition of the do...while loop,using == to compare Strings etc)

Related

making java tic tac toe vs computer [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Thats my tictactoe game and i have this problem i cant find out...When i compile my programm
Welcome To Tic Tac Toe Professor Falken!
***Use Numbers 1-9 To Select A Square***
_1_|_2_|_3_|
_4_|_5_|_6_|
_7_|_8_|_9_|
You Go First!
___|___|___|
___|___|___|
___|___|___|
Player X, enter move (1 - 9):
10
INVALID MOVE: Enter number 1 - 9 only:
5
___|___|___|
___|_X_|___|
___|___|___|
Player O, enter move (1 - 9):
___|___|___|
___|_X_|_O_|
___|___|___|
Player X, enter move (1 - 9):
4
___|___|___|
_X_|_X_|_O_|
___|___|___|
Player O, enter move (1 - 9):
And stops there, i dont know why can anyone help me?
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
private Scanner in;
private boardPiece[][] board = {{new boardPiece(),new boardPiece(),new boardPiece()},
{new boardPiece(),new boardPiece(),new boardPiece()},
{new boardPiece(),new boardPiece(),new boardPiece()}};
private char turn = 'X';
private boolean win = false;
private int count = 0;
private Random random = new Random();
private int randomNumber = random.nextInt(9);
public static void main(String [] args)
{
int replay;
TicTacToe game = new TicTacToe();
game.in = new Scanner(System.in);
System.out.println("Welcome To Tic Tac Toe Professor Falken!");
System.out.println("***Use Numbers 1-9 To Select A Square***");
System.out.println("_1_|_2_|_3_|");
System.out.println("_4_|_5_|_6_|");
System.out.println("_7_|_8_|_9_|");
System.out.println(" n You Go First!");
game.play();
System.out.println("Would you like to play again?(1 = Yes & 2 = No): ");
replay = game.in.nextInt();
while(replay != 2){
game.init();
game.play();
System.out.println("Would you like to play again?(1 = Yes & 2 = No): ");
replay = game.in.nextInt();
}
game.in.close();
System.out.println("How about a nice game of chess :p");
}
public void play()
{
printBoard();
while(!win)
move();
}
public void printBoard()
{
for(int x=0; x<3; x++){
for(int y=0; y<3; y++){
System.out.print(board[x][y].piece);
}
System.out.println();
}
}
public void move()
{
int move = 0;
String valid = "";
System.out.println("Player " + turn + ", enter move (1 - 9): ");
if(turn == 'O') {
move = randomNumber; }
else {
move = in.nextInt();
}
valid = checkMove(move);
while(valid != "ok")
{
if(turn == 'X') {
System.out.println("INVALID MOVE: "+ valid);
move = in.nextInt();
}
else {
move = randomNumber;
}
valid = checkMove(move);
}
count++;
board[(move-1)/3][(move-1)%3].piece = "_"+turn+"_|";
board[(move-1)/3][(move-1)%3].player = turn;
board[(move-1)/3][(move-1)%3].used = true;
printBoard();
if(count >= 5)
checkWin(move);
if(turn == 'X')
turn = 'O';
else
turn = 'X';
}
public String checkMove(int move)
{
if(move < 1 || move > 9)
return "Enter number 1 - 9 only: ";
else
if(board[(move-1)/3][(move-1)%3].used)
return "That move has been used. Enter another move (1 - 9): ";
else
return "ok";
}
public void checkWin(int move)
{
for(int x = 0; x<3; x++){ //Horizontal
if((board[x][0].used && board[x][1].used && board[x][2].used) &&
(board[x][0].player == board[x][1].player && board[x][0].player == board[x][2].player)){
System.out.println("Congratulations Player " + turn + "!!! You win!");
win = true;
return;
}
}
for(int y = 0; y<3; y++)
{
if((board[0][y].used && board[1][y].used && board[2][y].used) &&
(board[0][y].player == board[1][y].player && board[0][y].player == board[2][y].player)){
System.out.println("Congratulations Player " + turn + "!!! You win!");
win = true;
return;
}
}
if((board[0][0].used && board[1][1].used && board[2][2].used) &&
(board[0][0].player == board[1][1].player && board[0][0].player == board[2][2].player)){
System.out.println("Congratulations Player " + turn + "!!! You win!");
win = true;
return;
}
if((board[2][0].used && board[1][1].used && board[0][2].used) &&
(board[2][0].player == board[1][1].player && board[2][0].player == board[0][2].player))
{
System.out.println("Congratulations Player " + turn + "!!! You win!");
win = true;
return;
}
if(count==9){
System.out.println("Draw! Nobody Wins (ยด???`)");
win = true;
return;
}
}
public void init()
{
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
board[x][y] = new boardPiece();
}
}
turn = 'X';
win = false;
count = 0;
}
class boardPiece{
public String piece;
public char player;
public boolean used;
boardPiece(){
piece = "___|";
used = false;
}
}
}
I made a some changes in move and checkMove
public void move() {
int move = 0;
Boolean valid = false;
System.out.println("Player " + turn + ", enter move (1 - 9): ");
if (turn == 'O') {
move = randomNumber;
} else {
move = in.nextInt();
}
valid = checkMove(move);
while (!valid) {
if (turn == 'X') {
move = in.nextInt();
} else {
move = random.nextInt(9);
}
valid = checkMove(move);
}
count++;
board[(move - 1) / 3][(move - 1) % 3].piece = "_" + turn + "_|";
board[(move - 1) / 3][(move - 1) % 3].player = turn;
board[(move - 1) / 3][(move - 1) % 3].used = true;
printBoard();
if (count >= 5) {
checkWin(move);
}
if (turn == 'X') {
turn = 'O';
}else {
turn = 'X';
}
}
public Boolean checkMove(int move) {
if (move < 1 || move > 9) {
System.out.println("INVALID MOVE: Enter number 1 - 9 only: ");
return false;
}else if (board[(move - 1) / 3][(move - 1) % 3].used) {
System.out.println("INVALID MOVE: That move has been used. Enter another move (1 - 9): ");
return false;
}else {
return true;
}
}

Java program terminated before running

import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
board b = new board();
while (!b.detectWin('x') && !b.detectWin('o') && !b.detectTie()) {
b.printBoard();
System.out.println("Player 1, where do you want to put an x?");
int xAnswer = input.nextInt();
b.setSpot('x', xAnswer);
b.printBoard();
System.out.println("Player 2, where do you want to put an o?");
int oAnswer = input.nextInt();
b.setSpot('o', oAnswer);
b.printBoard();
if(b.detectWin('x')) {
System.out.println("Player 1 won!");
}else if (b.detectWin('o')) {
System.out.println("Player 2 won!");
}else {
System.out.println("There was a tie.");
}
}
}
}
It's a tic tac toe program, here's the board:
public class board {
private char[] board;
public board() {
board = new char[]
{'0', '1', '2',
'3', '4', '5',
'6', '7', '8'};
}
public void setSpot(char player, int position) {
if (board[position] == position+48) {
board[position] = player;
}else {
System.out.println("Incorrect move, turn skipped");
}
}
public boolean detectWin(char player){
if((board[0] == player && board[1] == player && board[2] == player)
|| (board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player))
return true;
else
return false;
}
public boolean detectTie() {
for (int i = 0; i > board.length; i++) {
if (board[i] == i+48)
return false;
}
return true;
}
public void printBoard() {
System.out.println(board[0] + " | " +board[1] +" | " +board[2]);
System.out.println("---------");
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println("--------");
System.out.println(board[6] + " | " + board [7] + " | " + board[8]);
}
}
It keeps getting terminated, and I can't find out why. No error messages.
(placeholder for post is mostly code error placeholder for post is mostly code error placeholder for post is mostly code error placeholder for post is mostly code error placeholder for post is mostly code error
simple typo in your code, change detectTie as follows:
public boolean detectTie() {
for (int i = 0; i < board.length; i++) {
if (board[i] == i+48)
return false;
}
return true;
}
and you should be good. you were using int i = 0; i > board.length; i++, so i was starting with 0, and that was not greater than the board arrays length so it never executed the loop and just returned true, this in turn caused !b.detectTie() to return false, meaning the main while loop was never executed.

Rock, Paper, Scissors- BlueJ crazy loop

the following is my code for a rock, paper, scissors game in BlueJ. When I compile and the user enters an input, the computer immediately prints numerous outputs from playerWins(). The game ends when the user types "quit". Could someone help me so my screen won't be flooded? (and if there is any way to condense my code that would also be great).
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors
{
public static void main(String[] args)
{
int wins = 0, losses = 0, ties = 0;
boolean output;
Scanner scan = new Scanner(System.in);
System.out.print("(R)ock, (P)aper, (S)cissors, or quit: ");
String playerChoice = scan.nextLine();
while (playerChoice.equals("quit") == false)
{
playerChoice = playerChoice.toUpperCase();
String computerChoice = getComputerChoice();
if(playerChoice.equals(computerChoice) != true)
{
output = playerWins(playerChoice, computerChoice);
if (output == true)
{
wins++;
}
else if (output == false)
{
losses++;
}
}
else
{
ties++;
System.out.println("Tie!");
}
}
System.out.println("QUIT");
System.out.println("Wins: " + wins);
System.out.println("Losses: " + losses);
System.out.println("Ties: " + ties);
scan.close();
}
public static String getComputerChoice()
{
Random gen = new Random();
int num = gen.nextInt(30) + 1;
if (num % 3 == 2)
{
return "R";
}
else if (num % 3 == 1)
{
return "P";
}
else
{
return "S";
}
}
public static boolean playerWins(String playerChoice, String computerChoice)
{
if (playerChoice.equals("R") == true)
{
if (computerChoice.equals("P") == true)
{
System.out.println("My Point! \nP beats R"); // Rock is beaten by paper
return false;
}
else if (computerChoice.equals("S") == true)
{
System.out.println("Your Point! \nR beats S"); // Rock beats scissors
return true;
}
}
else if (playerChoice.equals("P") == true)
{
if (computerChoice.equals("R") == true)
{
System.out.println("Your Point! \nP beats R"); //Paper beats rock
return true;
}
else if (computerChoice.equals("S") == true)
{
System.out.println("My Point! \nS beats P"); //Paper is beaten by scissors
return false;
}
}
else if (playerChoice.equals("S") == true)
{
if (computerChoice.equals("P") == true)
{
System.out.println("Your Point! \nS beats P"); //Scissor beats paper
return true;
}
else if (computerChoice.equals("R") == true)
{
System.out.println("My Point! \nR beats S"); //Scissors is beaten by rock
return false;
}
}
return false;
}
}
These two lines are the problem:
String playerChoice = scan.nextLine();
while (playerChoice.equals("quit") == false) {
You are reading one line and then checking that line over and over again. You need to read a new line inside the loop. The way you have it now, it is just trying to make the same move over and over infinite times.
Try:
String playerChoice = scan.nextLine();
while (playerChoice.equals("quit") == false) {
//do all of the stuff that is already inside your loop
playerChoice = scan.nextLine();
}
This will get user input again every time after processing the last input.
You are checking the user-input only once, that is before the loop:
String playerChoice = scan.nextLine();
One very nice method to solve that is the following:
String playerChoice;
while((playerChoice = scan.nextLine()).equals("quit") == false)
{
//more code
}
What the above code does, is that playerChoice is set right before .equals("quit") is called. If you use this method you don't need one more line at the end of your loop to set playerChoice but just do that in the head of your loop.

Using a return value from a method that also has print statements

I am working on a ROCK PAPER SCISSORS project for class and am trying to use a method which prints who wins the match and also return a value I'm using to determine who has the most wins.
However in the first method when I assign the method to a value to use it it prints out the statements in the evaluate method, is there any way around this or will I have to create another method that does the same thing just with out the print statements?
(userWins and computerWins are declared and initialized in main() )
main( ) {
int userWins = 0;
int computerWins = 0;
.....
int value = winEvaluation(userChoice, computerChoice);
if (value == 1){
userWins++;
} else if (value == 2){
computerWins++;
} else{
}
System.out.prinln("user wins: " + userWins + " computer wins: " + computerWins);
}
private static int evaluate(int userChoice, int computerChoice) {
int winValue = 0;
String win = "You win.";
String lose = "I win.";
String draw = "We picked the same thing! This round is a draw.";
if( userChoice == 1) {
if(computerChoice == 1){
System.out.println(draw);
winValue = 0;
}else if(computerChoice == 2){
System.out.print("PAPER covers ROCK." );
System.out.println(lose);
winValue = 2;
}else {
System.out.print("ROCK breaks SCISSORS.");
System.out.println(win);
winValue = 1;
}
}else if (userChoice == 2 ) {
if (computerChoice == 1){
System.out.print("PAPER covers ROCK.");
System.out.println(win);
winValue = 1;
}else if (computerChoice == 2){
System.out.println(draw);
winValue = 0;
}else {
System.out.print("SCISSORS cuts PAPER.");
System.out.println(lose);
winValue = 2;
}
}else if (userChoice == 3){
if (computerChoice == 1){
System.out.print("ROCK breaks SCISSORS.");
System.out.println(lose);
winValue = 2;
}else if (computerChoice == 2){
System.out.print("SCISSORS cuts PAPER.");
System.out.println(win);
winValue = 1;
}else {
System.out.println(draw);
winValue = 0;
}
}
return winValue;
}

Java Else If Block Not Working Correctly [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
Hopefully this is a quick question. I'm making a simple rock paper scissors game. I make a random computer choice fine, and get the user's choice fine. But when I try to find who wins, it prints the last else in my else if block that is for invalid input.
It prints "Enter a valid choice" when a correct choice has been made.
import java.util.Random;
import javax.swing.JOptionPane;
public class JavaApplication4 {
public static void main(String[] args)
{
Random ranNums = new Random();
int comp = ranNums.nextInt(3);
String comp2;
String winner;
String user = JOptionPane.showInputDialog
(null, "Enter rock, paper, or scissors");
user.toLowerCase();
if(comp == 0)
comp2 = "rock";
else if(comp == 1)
comp2 = "paper";
else
comp2 = "scissors";
//Computer wins
if(comp2 == "rock" && user == "scissors")
winner = "The computer wins";
else if(comp2 == "paper" && user == "rock")
winner = "The computer wins";
else if(comp2 == "scissors" && user == "paper")
winner = "The computer wins";
//Tie game
else if(comp2 == "rock" && user == "rock")
winner = "It's a tie";
else if(comp2 == "paper" && user == "paper")
winner = "It's a tie";
else if(comp2 == "scissors" && user == "scissors")
winner = "It's a tie";
//User wins
else if(comp2 == "scissors" && user == "rock")
winner = "You win!";
else if(comp2 == "rock" && user == "paper")
winner = "You win!";
else if(comp2 == "paper" && user == "scissors")
winner = "You win!";
else
winner = "Enter a valid choice";
JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
"The computer picked " + comp2 + "\n" +
winner);
}
}
you can't use
comp == "paper"
to compare strings in java. You use
comp.equals("paper")
or if case doesn't matter
comp.equalsIgnoreCase("paper")
Don't compare Strings using ==. Use the equals or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if ("bar".equals(fu)) {
// do something
}
or,
if ("bar".equalsIgnoreCase(fu)) {
// do something
}
One way to simplify your code is to create a RockPaperScissors enum and give it its on compare method. Something like:
enum RockPaperScissors {
ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors");
private String text;
private static int[][] winMatrix = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}};
private RockPaperScissors(String text) {
this.text = text;
}
#Override
public String toString() {
return text;
}
// can't use compareTo since it is a final method for enums
public int compareVs(RockPaperScissors other) {
int thisOrdinal = ordinal();
int otherOrdinal = other.ordinal();
return winMatrix[thisOrdinal][otherOrdinal];
}
}
Then to compare one enum vs. another, simply call its compareVs(...) method passing in the other enum.
So your huge if/else block would reduce down to:
// assuming that user and comp are RockPaperScissors variables
int result = user.compareVs(comp);
if (result == 1) {
System.out.println("You've won!");
} else if (result == 0) {
System.out.println("It's a tie!");
} if (result == -1) {
System.out.println("You've lost!");
}
If you are like me and you enjoy raw code:
import java.util.Random;
import javax.swing.JOptionPane;
public class JavaApplication4 {
public static void main(String[] args)
{
Random ranNums = new Random();
int comp = ranNums.nextInt(3);
String comp2;
String winner;
String user = JOptionPane.showInputDialog
(null, "Enter rock, paper, or scissors");
user.toLowerCase();
if(comp == 0)
comp2 = "rock";
else if(comp == 1)
comp2 = "paper";
else
comp2 = "scissors";
//Computer wins
if(comp2.equals("rock") && user.equals( "scissors"))
winner = "The computer wins";
else if(comp2.equals("paper") && user.equals( "rock"))
winner = "The computer wins";
else if(comp2.equals("scissors") && user.equals( "paper"))
winner = "The computer wins";
//Tie game
else if(comp2.equals("rock") && user.equals( "rock"))
winner = "It's a tie";
else if(comp2.equals("paper") && user.equals( "paper"))
winner = "It's a tie";
else if(comp2.equals("scissors") && user.equals( "scissors"))
winner = "It's a tie";
//User wins
else if(comp2.equals("scissors") && user.equals( "rock"))
winner = "You win!";
else if(comp2.equals("rock") && user.equals( "paper"))
winner = "You win!";
else if(comp2.equals("paper") && user.equals( "scissors"))
winner = "You win!";
else
winner = "Enter a valid choice";
JOptionPane.showMessageDialog(null, "You picked " + user + "\n" +
"The computer picked " + comp2 + "\n" +
winner);
}
}
Hope that helped!
You cannot compare string like this: if (comp2 == "rock")
You need to write: if ("rock".equals(comp2))

Categories

Resources