您好我已经完成了两个班级制作Rock Paper Scissors游戏的任务,而且除了我在玩游戏时没有得到正确的赢家之外,我已经得到了大部分工作。帮我理解为什么?该程序在RPS类
中的playRound()方法中分析计算机输入和用户输入之间的关系主类:
using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class VrEyeRaycaster : MonoBehaviour {
public string objectCollided;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//create the ray to cast forward
RaycastHit hit;
Vector3 origin = transform.position;
Vector3 direction = transform.TransformDirection(Vector3.forward)*20;
Ray ray = new Ray(origin, direction);
Debug.DrawRay(origin, direction, Color.green);
if (Physics.Raycast(ray, out hit, 100f))
{
objectCollided = hit.collider.gameObject.name;
print(objectCollided);
}
else
{
string s = "Nothing hit by RayCaster";
print(s);
}
}
RPS课程:
import java.util.Scanner;
import java.util.Random;
public class GameMain {
public static void main(String[] args) {
System.out.println("Welcome to Rock, Paper, Scissors.\nYou will play against the computer ;-)\n");
Scanner scan = new Scanner(System.in);
boolean keepPlaying = true;
String playerStr = "", computerStr = "";
// using a seed means the same sequence of numbers will be generated.
int seed = 123456;
Random rnd = new Random(seed);
RPSGame game = new RPSGame(rnd);
// The looping section
while (keepPlaying) {
System.out.println("Enter R for rock, P for paper, or S for scissors. Enter X to quit.");
playerStr = scan.nextLine();
System.out.println("You entered: " + playerStr);
if (playerStr.equalsIgnoreCase("X"))
keepPlaying = false;
else if (game.isValidInput(playerStr)) {
game.playRound(playerStr);
computerStr = game.getComputerChoice();
System.out.println("The computer chose: " + computerStr);
if (game.playerWins())
System.out.println("You win!");
else if (game.computerWins())
System.out.println("Computer wins!");
else
System.out.println("It's a tie!");
System.out.println(game.getScoreReportStr());
} else // invalid input
System.out.println("Your input should be R, P, or S. Please enter again.");
}
System.out.println("Bye for now.");
}
}
答案 0 :(得分:1)
看起来你问题的一部分可能在于你所展示的内容,而不是你用来玩游戏的东西。当你调用game.playround()时,你正在调用getRandomChoice(),它随机选择计算机用来玩游戏的“对象”。但是,在主类的下一行中,您调用game.getComputerChoice(),它再次调用getRandomChoice()。你需要意识到的是,每当你调用getRandomChoice()方法时,你(可能)会得到不同的答案。答案是将计算机“选择”存储在变量中,只需在getComputerChoice()方法中返回该变量,而不是再次调用getRandomChoice()。
答案 1 :(得分:0)
问题看起来像是在你的" getComputerChoice()"方法
而不是返回" getRandomChoice()"的新实例,它应该返回" computerChoice",因为计算机选择已经被" getRandomChoice()填充",并再次调用它只会导致做出新的选择。
public String getComputerChoice() {
return computerChoice;
}
答案 2 :(得分:0)
你没有存放东西。随机做一次,每轮存放一次。然后在这个方法中,你没有重置东西,我们重复了很多代码。所以有一个谁赢了,0 ==领带,1 ==玩家和2 ==计算机。
将计算机存放在computerChoice中,并且每回合只设置一次。
public void playRound(String playerChoice) {
resetRound();
getRandomChoice();
playerWins = false;//better to use an int for who won /round status
computerWins = false;
rounds++;//do not need to repeat this, its common so put it here
if ((playerChoice.equals("R") && computerChoice.equals("S"))
|| (playerChoice.equals("P") && computerChoice.equals("R"))
|| (playerChoice.equals("S") && computerChoice.equals("P"))) {
playerWins = true;//this needs to be reset
playerScore++;
} else if ((playerChoice.equals("R") && computerChoice.equals("P"))
|| (playerChoice.equals("P") && computerChoice.equals("S"))
|| (playerChoice.equals("S") && computerChoice.equals("R"))) {
computerWins = true;
computerScore++;
} else {
isTie = true;
}