试图编写一个Rock Paper Scissors游戏

时间:2015-11-13 18:23:37

标签: java loops for-loop

好吧所以我需要创建一个创建Rock Paper Scissors类型游戏所需的代码,其示例输出看起来像这样:

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R
player had rock
computer had paper
!Computer wins <<Paper Covers Rock>>!
Do you want to play again? y

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R
player had rock
computer had scissors
!Player wins <<Rock Breaks Scissors>>!
Do you want to play again? y

Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: R
player had rock
computer had rock
!Draw Game!
Do you want to play again? n

所以我基本上完全难以理解,而且我不确定该怎么做,代码需要能够让用户选择R,P或S并让计算机确定这样的随机实例。我不是要求你们为我做整件事(但是请随意),但是我会非常感谢一些方向/帮助,所以这就是我到目前为止所做的:

RockPaperScissors类:

import java.util.Scanner;
import static java.lang.System.*;
import java.util.Random;

public class RockPaperScissors
{
    private String playChoice;
    private String compChoice;

    public RockPaperScissors
    {
        playChoice=null; //same as playChoice="";
        compChoice=null;
    }
    //loaded constructor 
    public RockPaperScissors(String player)
    {
        //add code here to call the setPlayers method
    }
    //set playChoice based on player choice and compChoice based on random number
    public void setPlayers (String player)
    {
        //switch case
        //R-player="rock"
        //P-player="paper"
        //S-player="scissors"

        //int num=random number 0 1 or 2
        //switch case
        //0-computer="rock"
        //1-computer="paper"
        //2-computer="scissors"
    }
    //using logic and possible player and computer choices, determine a winner and return the string that informs the player who won
    public String determineWinner()
    {
        String winner="";
        //if playChoice is the same as compChoice no winner (draw)
        //else-if-else if statements to compare playChoice and compChoice to find the winner
        return winner;
    }
    public String toString()
    {
        String output="";
        output=output + "player had " + playChoice+"\n;
        output+="computer had "+ compChoice;
        return output;
    }
}    

跑步者班:

import java.util.Scanner;
import static java.lang.System.*;

public class RPSRunner
{
    public static void main(String args[])
    {
        weapon[R,P,S]:: ");
        RockPaperScissors(keyboard.next());
        Scanner keyboard = new Scanner(System.in);
        char response;
        do{
            System.out.print("Rock­Paper­Scissors ­ pick your
            RockPaperScissors test = new
            System.out.println(test); //uses toString method
            System.out.println(test.determineWinner()+"\n");
            System.out.print("Do you want to play again? ");
            response = keyboard.next().charAt(0);
        }while(response=='Y'||response=='y');
    }
}

感谢您的帮助。

这里要求的是整个作业:

实验室描述:编写一个简单的Rock,Paper,Scissors游戏。生成随机选择

适用于电脑播放器。接下来,玩家将做出选择。最后,你选择了两个选择

并查看哪个玩家是赢家。您需要使用随机数。

可以在此处找到随机数的帮助(https://goo.gl/YgH1ld

1 个答案:

答案 0 :(得分:0)

你需要能够:

  1. 打印要求选择武器的提示(DONE)
  2. 获取武器的输入
  3. 为计算机播放器选择随机武器
  4. 确定谁获胜
  5. 打印要求再次播放的提示(完成)
  6. 再次播放输入(完成)
  7. 循环回到步骤1(完成)
  8. 所以你需要实现2-4。

    主要问题是跑步者的破裂。我发布了一个更正版本,其下方实现了2(但没有验证它的R,P或S):

    import java.util.Scanner;
    import static java.lang.System.*;
    
    public class RPSRunner
    {
        public static void main(String args[])
        {
            RockPaperScissors(keyboard.next());
            Scanner keyboard = new Scanner(System.in);
            char response;
            do{
                System.out.print("Rock­Paper­Scissors ­ pick your weapon[R,P,S]:: ");
                String player = keyboard.next();
                RockPaperScissors test = new RockPaperScissors(player);
                System.out.println(test); //uses toString method
                System.out.println(test.determineWinner()+"\n");
                System.out.print("Do you want to play again? ");
                response = keyboard.next().charAt(0);
            }while(response=='Y'||response=='y');
        }
    }
    

    从实验室作品开始。您认为这需要什么?

        public RockPaperScissors(String player)
        {
            //add code here to call the setPlayers method
        }