所以我正在制作蛇和梯子游戏。如果player1和player2落在同一网格上,他们必须进行决斗。在我的游戏中,两个玩家必须有一个石头剪刀游戏。我无法让我的方法将值int int返回到我的子程序Begin()。如果玩家1或2赢得了石头剪刀战,胜利价值将告诉begin方法。
我的代码:
public static String Begin // Recieves data from the main method
{
// start Begin method
int win=0;
if(P1Position==P2Position||P2Position==P1Position){
System.out.println("==========================================================================");
System.out.println (player1+" is currently on square " + P1Position);
System.out.println (player2+" is currently on square " + P2Position);
System.out.println("==========================================================================");
firstplay(choice1,choice2);
determineOutcome(choice1,choice2,win);
if(win==1){
determineOutcome(choice1,choice2,win);
P2Position=P1Position-P1Roll;
}
else{
determineOutcome(choice1,choice2,win);
P1Position=P2Position-P2Roll;
}
}
}
public static void firstplay(int choice1,int choice2)throws IOException{//USER
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("DUEL!!!\nThe Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot");
System.out.println("1=Rock\n2=Paper\n3=Scissors\n===========");
System.out.println(player1+" choose:");
choice1=Integer.parseInt(br.readLine());
System.out.println(player2+" choose:");
choice2=Integer.parseInt(br.readLine());
if(choice1==1){
System.out.println(player1+" chose Rock");//If user picked ROck
}
else if(choice1==2){
System.out.println(player1+" chose Paper");//If user picked Paper
}
else if(choice1==3){
System.out.println(player1+" chose Scissors");//If user picked Scissors
}
if(choice2==1){
System.out.println(player2+" chose Rock");
}
else if(choice2==2){
System.out.println(player2+" chose Paper");
}
else if(choice2==3){
System.out.println(player2+" chose Scissors");
}
}
public static int determineOutcome(int choice1,int choice2,int win)throws IOException{
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int die=0;
//Rock vs Papaer
if(choice2==1&&choice1==2){
System.out.println(player1+" WON PAPER BEATS ROCK");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==2&&choice1==1){
System.out.println(player2+" WON PAPAER BEATS ROCK ");
System.out.println(player1+" keeps their spot");
win=0;
}
//Scissors vs Paper
else if(choice2==2&&choice1==3){
System.out.println(player1+" WON SCISSORS BEAT PAPER ");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==3&&choice1==2){
System.out.println(player2+" WON SCISSORS BEAT PAPER ");
System.out.println(player2+" keeps their spot");
win=0;
}
//Rock vs Scissors
else if(choice2==3&&choice1==1){
System.out.println(player1+" WON ROCK BEATS SCISSORS ");
System.out.println(player1+" keeps their spot");
win=1;
}
else if(choice2==1&&choice1==3){
System.out.println(player2+" WON ROCK BEATS SCISSORS ");
System.out.println(player2+" keeps their spot");
win=0;
}
//Ties
else if(choice2==1&&choice1==1){
System.out.println("YOU'VE TIED. Play once again");
}
else if(choice2==2&&choice1==2){
System.out.println("YOU'VE TIED. Play once again");
}
else if(choice2==3&&choice1==3){
System.out.println("YOU'VE TIED. Play once again");
}
return win;
}
}//end class
输出:
yo Rolled a 2
po Rolled a 2
------------------------------------------------------------------------
==========================================================================
yo is currently on square 2
po is currently on square 2
==========================================================================
DUEL!!!
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot
1=Rock
2=Paper
3=Scissors
===========
yo choose:
1
po choose:
3
yo chose Rock
po chose Scissors
==========================================================================
yo is currently on square 0
po is currently on square 2
==========================================================================
yo press r to roll
预期: 输出:
yo Rolled a 2
po Rolled a 2
------------------------------------------------------------------------
==========================================================================
yo is currently on square 2
po is currently on square 2
==========================================================================
DUEL!!!
The Player who wins this round of rock paper scissors will keep their spot,and the loser ther that player's old spot
1=Rock
2=Paper
3=Scissors
===========
yo choose:
1
po choose:
3
yo chose Rock
po chose Scissors
yo won rock beats scissors.
yo keeps their spot
==========================================================================
yo is currently on square 2
po is currently on square 0
==========================================================================
yo press r to roll
答案 0 :(得分:1)
您必须存储方法int
返回的determineOutcome
值。
win = determineOutcome(choice1,choice2,win);
if(win==1){
...
}
请注意,没有必要将win
作为参数传递给determineOutcome()
,因为您没有使用其实际值。你可以试试这个:
public static int determineOutcome(int choice1, int choice2) throws IOException
{
// Initializes the BufferReader for user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int die = 0;
int win = 2; // 2 == TIE
//Rock vs Papaer
if (choice2 == 1 && choice1 == 2) {
System.out.println(player1 + " WON PAPER BEATS ROCK");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 2 && choice1 == 1) {
System.out.println(player2 + " WON PAPAER BEATS ROCK ");
System.out.println(player1 + " keeps their spot");
win = 0;
}
//Scissors vs Paper
else if (choice2 == 2 && choice1 == 3) {
System.out.println(player1 + " WON SCISSORS BEAT PAPER ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 3 && choice1 == 2) {
System.out.println(player2 + " WON SCISSORS BEAT PAPER ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Rock vs Scissors
else if (choice2 == 3 && choice1 == 1) {
System.out.println(player1 + " WON ROCK BEATS SCISSORS ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 1 && choice1 == 3) {
System.out.println(player2 + " WON ROCK BEATS SCISSORS ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Ties
else if (choice2 == 1 && choice1 == 1) {
System.out.println("YOU'VE TIED. Play once again");
} else if (choice2 == 2 && choice1 == 2) {
System.out.println("YOU'VE TIED. Play once again");
} else if (choice2 == 3 && choice1 == 3) {
System.out.println("YOU'VE TIED. Play once again");
}
return win;
}
在此声明局部变量 win
,在相应的条件中设置它(0, 1 or 2
)并在结尾处返回。
注意:如果您的条件确定玩家是否赢得了所有案例,您可以将{em>“Ties个案”替换为else
:< / p>
//Rock vs Scissors
else if (choice2 == 3 && choice1 == 1) {
System.out.println(player1 + " WON ROCK BEATS SCISSORS ");
System.out.println(player1 + " keeps their spot");
win = 1;
} else if (choice2 == 1 && choice1 == 3) {
System.out.println(player2 + " WON ROCK BEATS SCISSORS ");
System.out.println(player2 + " keeps their spot");
win = 0;
}
//Ties
else {
System.out.println("YOU'VE TIED. Play once again");
}