所以,一切正常。游戏会循环播放,但是当玩家不再想再次玩游戏时,只会显示该回合的分数,而不显示其他回合的分数。它应该显示如下内容:玩家 1 赢得 3 轮,计算机赢得 2 轮,平局 4 轮。但我错过了什么?
//RPSGame
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class RPSGame
{
private string playerMove;
private string computerMove;
private int draws;
private int playerWins;
private int computerWins;
// Constructor to Create a New Rock, Paper, Scissors Game Object
public RPSGame()
{
//Setting up game by asking user for their response
Console.WriteLine("Hey do you want to play a game? y or n: ");
playerMove = Console.ReadLine();
if (playerMove == "N" || playerMove == "n")
{
Console.WriteLine("Awe ok, No game for you then! Here is your score: ");
}
else if (playerMove == "Y" || playerMove == "y")
{
Console.WriteLine("Pick R for Rock, S for Scissors or P for Paper");
}
else
Console.WriteLine("Invalid Response, y or n only!");
}
public int Draws
{
get { return draws; }
}
public int PlayerWins
{
get { return playerWins; }
}
public int ComputerWins
{
get { return computerWins; }
}
public string PlayerMove // Property to set or return player's move
{
get
{
return playerMove;
}
set
{
playerMove = value.ToUpper();
}
}
public string ComputerMove // Property to return computer's move
{
get
{
return computerMove;
}
}
public void GetComputerMove()
{
Random rand = new Random();
int move = rand.Next(3) + 1; // Random number from 1 to 3
if (move == 1)
computerMove = "R";
else if (move == 2)
computerMove = "P";
else
computerMove = "S";
}
public string DisplayMoves()
{
string moves = "";
string cMove = "", pMove = "";
bool invalidMove = false;
if (playerMove == "R")
pMove = "Rock";
else if (playerMove == "P")
pMove = "Paper";
else if (playerMove == "S")
pMove = "Scissors";
else
{
pMove = "\nInvalid entry...Must enter an R, P, or S.";
invalidMove = true;
}
if (computerMove == "R")
cMove = "Rock";
else if (computerMove == "P")
cMove = "Paper";
else
cMove = "Scissors";
if (invalidMove)
moves = pMove;
else
moves = ("\nPlayer's Move: " + pMove +
"\nComputer's Move: " + cMove + "\n");
return moves;
}
public string DetermineWinner()
{
// R = Rock P = Paper S = Scissors
string message = "";
if (playerMove == computerMove)
{
message = ("It's a draw!");
draws++;
}
else if (playerMove == "R" && computerMove == "P")
{
message = ("Computer Wins! Paper Covers Rock!");
computerWins++;
}
else if (playerMove == "R" && computerMove == "S")
{
message = "Player Wins! Rock Smashes Scissors!";
playerWins++;
}
else if (playerMove == "P" && computerMove == "R")
{
message = "Player Wins! Paper Covers Rock!";
playerWins++;
}
else if (playerMove == "P" && computerMove == "S")
{
message = "Computer Wins! Scissors Cut Paper!";
computerWins++;
}
else if (playerMove == "S" && computerMove == "R")
{
message = "Computer Wins! Rock Smashes Scissors!";
computerWins++;
}
else if (playerMove == "S" && computerMove == "P")
{
message = "Player Wins! Sciccors Cut Paper!";
playerWins++;
}
return message;
}
public string ShowStats()
{
string stats = ("Draws: " + draws + "\n" +
"Player Wins: " + playerWins + "\n" +
"Computer Wins: " + computerWins + "\n");
return stats;
}
}
//Program
internal class Program
{
static void Main(string[] args)
{
bool gameLoop = true;
string answer;
while (gameLoop)
{
RPSGame game = new RPSGame();
game.PlayerMove = Console.ReadLine();
game.GetComputerMove();
Console.WriteLine("------------------------");
Console.WriteLine("Player Move: " + game.PlayerMove);
Console.WriteLine("Computer Move: " + game.ComputerMove);
Console.WriteLine(game.DisplayMoves);
Console.WriteLine(game.DetermineWinner());
Console.WriteLine("------------------------");
Console.WriteLine(" ");
Console.WriteLine("Do you want to play again y or n: ");
Console.WriteLine(" ");
answer = Console.ReadLine();
answer = answer.ToUpper();
if (answer == "Y")
{
gameLoop = true;
// Console.Clear();
}
else
{
gameLoop = false;
Console.WriteLine();
Console.WriteLine("------------------------");
Console.Write(game.ShowStats());
Console.WriteLine("------------------------");
}
}
}
}
问题在于
RPSGame game = new RPSGame();
已经有一段时间了。每次迭代您都会创建 RPSGame 的新实例并重写旧实例。每次创建新实例时都使用默认值(分数为零)..
您需要在循环之前创建一次 RPSGame 的实例。那么它就不会在每一轮中重写它,并且会保存所有轮次的分数数据。