我目前正在制作C#2播放器Rock剪刀游戏,但我因该做什么而受困。 从本质上讲,用户输入他们想要的用于岩石,纸张和剪刀的键。 我将这些键存储在变量中,以便程序记住它们。问题是,我找不到或想不到程序在启动游戏后如何检测到这些键的按下方式。 我认为可能是枚举,还是可能将它们存储在数组中?
不确定,这是我到目前为止所拥有的。
static bool winner = false;
static string player1;
static string player2;
static ConsoleKeyInfo Rock1;
static ConsoleKeyInfo Rock2; //variables for objects and players names etc
static ConsoleKeyInfo Scissors1;
static ConsoleKeyInfo Scissors2;
static ConsoleKeyInfo Paper1;
static ConsoleKeyInfo Paper2;
static void Main(string[] args)
{
do
{
PlayerChoosesNameAndKeys();
game(); //main game loop
} while (winner == false);
}
static public void PlayerChoosesNameAndKeys()
{
for (int i = 1; i <= 2; i++)
{
Console.WriteLine("Player {0} chooses there name:", i);
if (i == 1)
{
player1 = Console.ReadLine(); //players choose names
Console.WriteLine("Player 1's name is: {0}", player1);
}
else
{
player2 = Console.ReadLine();
Console.WriteLine("Player 2's name is: {0}", player2);
}
}
Console.WriteLine("Players now choose the keys they wish to represent the different objects");
for (int i = 0; i <= 5; i++)
{
if (i == 0)
{
Console.WriteLine("{0} choose what key Rock is", player1);
Rock1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
else
{
if (i == 1)
{
Console.WriteLine("{0} choose what key Paper is", player1);
Paper1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 2) //selection to decide what keybinds players want
{
Console.WriteLine("{0} choose what Key Sisscors is", player1);
Scissors1 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 3)
{
Console.WriteLine("{0} choose what key Rock is", player2);
Rock2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 4)
{
Console.WriteLine("{0} choose what key Paper is", player2);
Paper2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
if (i == 5)
{
Console.WriteLine("{0} choose what Key Sisscors is", player2);
Scissors2 = Console.ReadKey();
Console.WriteLine();
Console.WriteLine("Press enter to confirm");
Console.ReadLine();
}
}
}
static public void game()
{
int score1 = 0;
int score2 = 0;
int count = 1;
//actual game
do
{
Console.WriteLine("Round {0}", count);
Console.WriteLine("Rock...");
Console.WriteLine("Paper...");
Console.WriteLine("Scissors...");
Console.WriteLine("{0} input Object!: ",player1);
ConsoleKeyInfo input = Console.ReadKey();
Console.WriteLine();
if (input.Key == ConsoleKey.Rock1)
{
}
} while (score1 != 10 || score2 != 10);
}
答案 0 :(得分:0)
您可以简单地将用户选择另存为字符(例如char Rock
);然后使用ConsoleKeyInfo.KeyChar
:
if (input.KeyChar == Rock)
{
}
例如:
char Rock = 'r'; // default value
Console.WriteLine("{0} choose what key Rock is", player1);
Rock = Console.ReadKey().KeyChar;
或者只需使用已保存的密钥检查已插入的密钥:
Console.WriteLine("{0} choose what key Rock is", player1);
ConsoleKeyInfo Rock = Console.ReadKey();
然后:
ConsoleKeyInfo input = Console.ReadKey();
if (input.Key == Rock.Key)
{
}