AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Write a program for stone scissors and paper game

Write a program that finds the winner of "stone, scissors and paper" game which is played by three players(Player1, Player2, Player3). The winner is based on the number of turns he wins them all. The output should be "Draw" if all three players have equal number of points.

Input Specification:

The first line of input consists of a single integer which is the number of rounds played. The following set of inputs are the game objects stone, scissors and paper shown by each player.

Sample Input:

3

stone scissors scissors

scissors stone paper

scissors paper scissors

Sample Output:

Player1 won the game

Explanation:

In the first round of the game, Player1 has stone where Player2 and Player3 have scissors which can be destroyed by stone. So Player1 scores 2 points in the first round. In the second round of the game, the three players have scissors, stone and paper. So all the three players get 1 points each since all three have equal chances of winning. In the third round, Player1 and Player3 have scissors where Player2 have paper, which can be destroyed by scissors. So Player1 and Player3 gets 1 point each. So finally, Player1 has scored the maximum points which determines him as the winner.

corejava x 333
programming x 150
Posted On : 2017-12-07 17:37:29.0
profile Divesh - anyforum.in Divesh
8800
up-rate
6
down-rate

Answers


import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Winner {
//put the objects in sorted power order
private static String[] gameObjects={"paper","scissors","stone"};
private static int totalPlayer=3;

public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n= Integer.parseInt(input.nextLine().trim());
int[] scores=new int[totalPlayer];
int max=0;
int winner=0;
List<String> gameObjectsList=Arrays.asList(gameObjects);
String[] temp;
for(int i=0;i<n;i++){
temp=input.nextLine().trim().split(" ");
//condition added to ignore the cases when equal chance to win for each player
if(!((gameObjectsList.indexOf(temp[0])!=gameObjectsList.indexOf(temp[1])
&& gameObjectsList.indexOf(temp[1])!=gameObjectsList.indexOf(temp[2])
&& gameObjectsList.indexOf(temp[0])!=gameObjectsList.indexOf(temp[2]))
||
(gameObjectsList.indexOf(temp[0])==gameObjectsList.indexOf(temp[1])
&& gameObjectsList.indexOf(temp[0])==gameObjectsList.indexOf(temp[2])))){
for(int j=0;j<totalPlayer;j++){
for(int k=0;k<totalPlayer;k++){
if(k!=j){
if(gameObjectsList.indexOf(temp[j])>gameObjectsList.indexOf(temp[k])){
scores[j]=scores[j]+1;
if(scores[j]>max){
max=scores[j];
winner=j+1;
}
}
}
}
}

}
}
if(scores[0]==scores[1] && scores[1]==scores[2])
System.out.println("Draw");
else
System.out.println("Player"+winner+" won the game");
}
}

Note:
-------------------------
Either enter all the inputs one by one or if you paste all the inputs altogether, remove blank new line between the input lines and press enter at last.

Posted On : 2017-12-09 00:50:49
Satisfied : 1 Yes  4 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185418912
Reply This Thread
up-rate
1
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in