石头,布,剪刀游戏-如何结束游戏,当一个人赢了三次
本文关键字:游戏 一个人 三次 结束 何结束 石头 | 更新日期: 2023-09-27 17:50:14
我正在写一个石头、布、剪刀的游戏,和电脑玩。这很有效,但我想打破比赛,当一个人赢了三次。但它一直在循环……我对编程真的很陌生,所以如果代码很乱请原谅……(
很抱歉我发了两篇文章,但是我的第一篇文章在我把它改成英文后被推到了很远的地方以获得一些帮助…
This is the Main:
static void Main(string[] args)
{
Game ssp = new Game();
Interaction.MsgBox("Welcome!");
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
这个类包含了处理游戏的方法:
字符串CompusterChoice;
//Starts the game
public void Start()
{
//Computers hand
Random rnd = new Random();
int x = rnd.Next(0, 3);
if (x == 0)
{ DatornsVal = "Rock"; }
else if (x == 1)
{ DatornsVal = "Paper"; }
else if (x == 2)
{ DatornsVal = "Scissor"; }
}
//Look who will win
public void Seewicharethewinner(string _Choice)
{
string PlayerChoice = _Choice;
string _PlayerChoice = _Choice.ToUpper();
string _ComputerChoice = ComputerChoice.ToUpper();
if (_PlayerChoice == _ComputerChoice)
{
Interaction.MsgBox("Tie!'nYour choice was: " + _Choice + "'n" + "Computer choice was: " + _ComputerChoice);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
else if (_ComputerChoice == "ROCK" && _PlayerChoice == "SCISSOR" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "PAPER" || _ComputerChoice == "PAPER"
&& _PlayerChoice == "ROCK")
{
Interaction.MsgBox("You Lose!'nYour choice was: " + _Choice + "'n" + "Computer choice was: " + _ComputerChoice);
int player = 0;
int computer = 1;
Points(computer, player);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
else if (_ComputerChoice == "ROCK" && _PlayerChoice == "PAPER" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "ROCK" || _ComputerChoice == "PAPER"
&& _PlayerChoice == "SICSSOR")
{
Interaction.MsgBox("You won!'nYour choice was: " + _Choice + "'n" + "Computer choice was: " + _ComputerChoice);
int player = 1;
int computer = 0;
Points(computer, player);
string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
ssp.Start();
ssp.Seewicharethevinner(Choice);
}
}
public void Points(int _computer, int _player)
{
int computerpoints = 0;
int playerpoints = 0;
if (_computer > _player)
{
computerpoints++;
}
else
{
playerpoints++;
}
if (computerpoints == 3)
{
Interaction.MsgBox("Computer won three times!");
}
if (playerpoints == 3)
{
Interaction.MsgBox("You won three times!");
}
}
Points方法内部:
int computerpoints = 0;
int playerpoints = 0;
如果我没看错的话,你一直在调用这个Points函数,并重置这两个局部声明的int。
这些变量将在每次调用函数时初始化为0。
你应该试着用这个代替:
static int computerpoints = 0;
static int playerpoints = 0;
这样你的整型就不会再被初始化为0了。确保将这些变量声明为全局变量,这样它们就不会超出作用域(不要在方法内部声明它们)。