Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I don't know what I did wrong, please help me fix the error, THANKS!!! import java.util.Scanner; import java.io.*; public class RockPaperScissorsModel { public class player

I don't know what I did wrong, please help me fix the error, THANKS!!!

import java.util.Scanner;

import java.io.*;

public class RockPaperScissorsModel {

public class player {

public String playerName;

public int playerNumber;

public int gamesWon;

public int totalGames;

public int matchesWon;

public int matchesPlayed;

public int forfeits;

public player(String name) {

playerName=name;

}

public void setPlayerNumber (int number){

playerNumber=number;

}

public int gamesWon(){

return gamesWon;

}

public int totalGames(){

return totalGames;

}

public int matchesWon(){

return matchesWon;

}

public int matchesPlayed(){

return matchesPlayed;

}

public int forfeits(){

return forfeits;

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//declare the scanners which are to be used

Scanner scanner1 = new Scanner(System.in);

Scanner scanner2 = new Scanner(System.in);

Scanner scanner3 = new Scanner(System.in);

Scanner scanner4 = new Scanner(System.in);

Scanner scanner5 = new Scanner(System.in);

Scanner scanner6 = new Scanner(System.in);

//get the names of the player

System.out.println("Player 1 enter name");

player player1 = new player(scanner1.nextLine());

player1.setPlayerNumber(1);

System.out.println("Player 2 enter name");

player player2 = new player(scanner2.nextLine());

player2.setPlayerNumber(2);

//arrays for user with selection options and for player 1 and player 2 errors for forfeit method

String[] item = { "Rock", "Paper", "Scissors"};

String[] player1errors = {"q","w","e","r","t","a","g","z","x","c","v"};

String[] player2errors ={"y","u","i","o","p","h","b","n","m"};

int games = 0;

//get user input for how mnay games are to be played

System.out.println("How many games do you want to play?");

int numGames = scanner6.nextInt();

//continue the program until the user input number is met

while(games

int matches = 0;

//each game will have 3 matches

while(matches<3){

//randomly "swap" rock, paper, and scissors at the start player1's turn with the new randomly generated number assigned

/*what swap does is, swap 2 elements with each other in an array in this case the ITEM array which holds the

possible options for a player to choose from*/

for (int i = 0; i < item.length; i++) {

int r = (int) (Math.random() * (i+1));

String swap = item[r];

item[r] = item[i];

item[i] = swap;

}

System.out.println(player1.playerName+" choose one: S= "+item[0]+", D= "+ item[1]+ ", and F= "+ item[2]);

//get user input on their choice

;

String player1choice=scanner3.nextLine();

while (player1choice.length()>1);

/*for(int i=0;i<1;i++){

System.out.println("***********************************************************");

}*/

String current1=null;

//Get the choice they picked and save as a string

if(player1choice.equalsIgnoreCase("s")){

current1 = item[0];

}else if(player1choice.equalsIgnoreCase("d")){

current1 = item[1];

}else if(player1choice.equalsIgnoreCase("f")){

current1 = item[2];

}

//randomly "swap" rock, paper, and scissors at the start of player2's turn with the new randomly generated number assigned

//once again swapping 2 elements in an array

for (int i = 0; i < item.length; i++) {

int r = (int) (Math.random() * (i+1));

String swap = item[r];

item[r] = item[i];

item[i] = swap;

}

System.out.println(player2.playerName+" choose one: J= "+item[0]+", K= "+ item[1]+ ", and L= "+ item[2]);

String player2choice=scanner4.nextLine();

String current2=null;

if(player2choice.equalsIgnoreCase("j")){

current2 = item[0];

}else if(player2choice.equalsIgnoreCase("k")){

current2 = item[1];

}else if(player2choice.equalsIgnoreCase("l")){

current2 = item[2];

}

boolean player1forfeits = false;

boolean player2forfeits = false;

//checking if any players entered an invalid character from their side of the keyboard

for(int i=0;i

if(player1choice.equalsIgnoreCase(player1errors[i])){

player1forfeits=true;

}

}

for(int i=0;i

if(player2choice.equalsIgnoreCase(player2errors[i])){

player2forfeits=true;

}

}

int player1wins = 0;

int player2wins = 0;

//based on the winner of each match this increments the variables which will then be written into a file...a whole bunch of if/elseif

if(player1forfeits && player2forfeits){

System.out.println("Both players have entered invalid choices, try again");

}else if(player1forfeits){

System.out.println("Player 1 has entered an invalid choice and will forfeit this match");

player1.forfeits=player1.forfeits+1;

player1.matchesPlayed=player1.matchesPlayed+1;

player2.matchesPlayed=player2.matchesPlayed+1;

player2.matchesWon=player2.matchesWon+1;

matches++;

player2wins++;

}else if(player2forfeits){

System.out.println("Player 2 has entered an invalid choice and will forfeit this match");

player2.forfeits=player2.forfeits+1;

player2.matchesPlayed=player2.matchesPlayed+1;

player1.matchesPlayed=player1.matchesPlayed+1;

player1.matchesWon=player1.matchesWon+1;

matches++;

player1wins++;

}else if(current1==current2){

System.out.println("Match ends in tie, try again");

}else if(current1.equals("Rock") && current2.equals("Paper")){

System.out.println(player2.playerName + " wins match number "+(matches+1));

player2.matchesWon=player2.matchesWon+1;

player2.matchesPlayed=player2.matchesPlayed+1;

player1.matchesPlayed=player1.matchesPlayed+1;

matches++;

player2wins++;

}else if(current2.equals("Rock") && current1.equals("Paper")){

System.out.println(player1.playerName + " wins match number "+(matches+1));

player1.matchesWon=player1.matchesWon+1;

player1.matchesPlayed=player1.matchesPlayed+1;

player2.matchesPlayed=player2.matchesPlayed+1;

matches++;

player1wins++;

}else if(current1.equals("Scissors") && current2.equals("Paper")){

System.out.println(player1.playerName + " wins match number "+(matches+1));

player1.matchesWon=player1.matchesWon+1;

player1.matchesPlayed=player1.matchesPlayed+1;

player2.matchesPlayed=player2.matchesPlayed+1;

matches++;

player1wins++;

}else if(current2.equals("Scissors") && current1.equals("Paper")){

System.out.println(player2.playerName + " wins match number "+(matches+1));

player2.matchesWon=player2.matchesWon+1;

player2.matchesPlayed=player2.matchesPlayed+1;

player1.matchesPlayed=player1.matchesPlayed+1;

matches++;

player2wins++;

}else if(current1.equals("Rock") && current2.equals("Scissors")){

System.out.println(player1.playerName + " wins match number "+(matches+1));

player1.matchesWon=player1.matchesWon+1;

player1.matchesPlayed=player1.matchesPlayed+1;

player2.matchesPlayed=player2.matchesPlayed+1;

matches++;

player1wins++;

}else if(current2.equals("Rock") && current1.equals("Scissors")){

System.out.println(player2.playerName + " wins match number "+(matches+1));

player2.matchesWon=player2.matchesWon+1;

player2.matchesPlayed=player2.matchesPlayed+1;

player1.matchesPlayed=player1.matchesPlayed+1;

matches++;

player2wins++;

}

//display who wins

if(matches==3){

player1.totalGames=player1.totalGames+1;

player2.totalGames=player2.totalGames+1;

games++;

if(player1wins>player2wins){

player1.gamesWon=player1.gamesWon+1;

System.out.println(player1.playerName+" won the game.");

}else{

player2.gamesWon=player2.gamesWon+1;

System.out.println(player2.playerName+" has won the game.");

}

}

}

}

//write the results of the game into a file called resultsOfRSP

try {

FileWriter filewriter = new FileWriter("C:\\Users\\Kahini\\Desktop\ esultsofRPS.txt", true);

PrintWriter printwriter = new PrintWriter(filewriter);

printwriter.println("********************************************************************");

printwriter.println("Results for Player 1: "+player1.playerName + " againest Player 2:" + player2.playerName);

printwriter.println(player1.playerName + ":");

printwriter.println("Matches Played: "+ player1.matchesPlayed());

printwriter.println("Matches Won: "+ player1.matchesWon());

printwriter.println("Games Played: "+ player1.totalGames());

printwriter.println("Games Won: "+ player1.gamesWon());

printwriter.println("Forfeits: " + player1.forfeits());

printwriter.println(player2.playerName + ":");

printwriter.println("Matches Played: "+ player2.matchesPlayed());

printwriter.println("Matches Won: "+ player2.matchesWon());

printwriter.println("Games Played: "+ player2.totalGames());

printwriter.println("Games Won: "+ player2.gamesWon());

printwriter.println("Forfeits: " + player2.forfeits());

printwriter.println("*********************************************************************");

printwriter.close();

System.out.println("Results have been updated in a text file");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions