Question
/** method 2.0 - illiustration of more methods To do: 1. complete the check method for three player with full document 2. create method to
/**
method 2.0 - illiustration of more methods
To do:
1. complete the "check" method for three player with full document
2. create method to generate a symbol for computer and use it in the
twoPlayers andthreePlayers
3. complete the fourthPlayers method. Make sure
the game keep going until a winner is found for all intermediate round
4. use the same logic we discussed, handle 5-players game
*/
import java.util.*;
public class Mar30Methods{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args){
//get number of player
int players = getInt("Enter a number between 2 and 7:");
System.out.println("You entered: " + players );
switch (players){
case 2:
twoPlayers();
break;
case 3:
threePlayers();
break;
case 4:
fourPlayers();
break;
}
}//main
/** get an positive int
@param prompt a string to send to the user
@return a positive int
*/
private static int getInt(String prompt){
int input;
System.out.print(prompt);
input = keyboard.nextInt();
keyboard.nextLine(); //clear input buffer
return input;
}
/**
prompt and get a valid character (R,P,S, Q) form user
@param none
@return a character in [R,P,S,Q]
*/
private static char chooseSymbol(){
char input;
while(true){
System.out.print("Choose a symbol:[R,P,S] or (Q to quit):");
input = keyboard.nextLine().trim().toUpperCase().charAt(0);
/* first logic
if(input != 'R' && input != 'P' && input != 'S' && input != 'Q'){
System.out.println("Invalid input, try again");
continue;
}
break; //get out of the loop
*/
if(input == 'R' || input == 'P' || input == 'S' || input == 'Q'){
break; //got a valid character, break the loop
}
System.out.println("INVALID input, try again!");
}
return input;
}
/** return a number representing which player won
@param c first player symbol
@param x second player symbol
@return 0 if tie, 1 if first won, 2 if second won
*/
private static int check(char c, char x){
int winner = 0; //assume there is tie to start with
if((c == 'R' && x == 'S') || (c == 'P' && x == 'R') ||
(c == 'S' && x == 'P') ){
winner = 1; // first player wins
}else if(( x == 'R' && c == 'S') || (x == 'P' && c == 'R') ||
(x == 'S' && c == 'P') ){
winner = 2;//second player wins
}
return winner;
}
/** ...*/
private static int check(char x, char y, char z){
int winner = 0; //tie
if((x == 'R' && y == 'S' && z == 'S') ||
(x == 'P' && y == 'R' && z == 'R') ||
(x == 'S' && y == 'P' && z == 'P')){
winner = 1;
}
// continue for other two players
return winner;
}
/**
handle 2-player game
*/
private static int twoPlayers(){
char player1 = chooseSymbol();
char player2 = chooseSymbol();//make it computer
System.out.println("Player 1: " + player1 + " vs Player 2: " + player2);
int result = check(player1, player2);
if(result == 0){
System.out.println("Tie!");
}else if(result == 1){
System.out.println("Player 1 wins");
}else{
System.out.println("Player 2 wins");
}
return result;
}
/**
handle 3 players
*/
private static int threePlayers(){
char player1 = chooseSymbol();
char player2 = chooseSymbol();//should generate the symbol
char player3 = chooseSymbol();//should generate the symbol
int result = check(player1, player2, player3);//check winner
if(result == 0){
System.out.println("Tie!");
}else if(result == 1){
System.out.println("Player 1 wins");
}else if(result == 2){
System.out.println("Player 2 wins");
}else{
System.out.println("Player 3 wins");
}
return result;
}
/**
handle 4 players
*/
private static int fourPlayers(){
int winner = 0;
int winnerR1 = 0; //assume there is a tie in round 1
//play until some one win
System.out.println("Elimination round 1");
do{
winnerR1 = twoPlayers(); //player 1 vs player 2
}while(winnerR1 == 0);
//play until some one win
System.out.println("Elimination round 2");
//put your logic here
return winner;
}
}//end of class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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