我正在尝试用C#制作Mastermind。除了将播放器的代码与计算机的代码进行比较之外,其他一切都还不错。我用数字代替颜色,然后将它们放在数组中。将比较这些阵列。但是问题是,当播放器的一个(代码)数字与计算机的多个数字相对应时,它将返回过多的引脚。我尝试使用布尔值来防止在有相应数字时再次进入循环,但是不幸的是这无法正常工作,而且我似乎无法弄清楚为什么。
// I am trying to use this boolean to prevent getting more than one
// white pin for a single number, however this does not work yet
bool alreadyOcurredNumber = false;
for(int i = 0; i < codeOfPlayer.Length; i++)
{
for (int j = 0; j < codeOfOpponent.Length; j++)
{
if (alreadyOcurredNumber == false)
Console.WriteLine("boven " + alreadyOcurredNumber);
{
//this if looks for corresponding 'colors' that are also at the same index
if ((codeOfOpponent[i] == codeOfPlayer[j]) && (i == j))
{
rightGuessRightIndex++;
alreadyOcurredNumber = true;
}
//this if-statement looks if 'colors' correspond, regardless of the index
else if ((codeOfOpponent[i] == codeOfPlayer[j]))
{
rightGuessWrongIndex++;
alreadyOcurredNumber = true;
}
}
}
alreadyOcurredNumber = false;
}
这是整个程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mastermind
{
class Program
{
public int[] createCodetoBreak(int[] inputArray) //Creating the code for the player to guess
{
int[] codeToGuess = new int[4];
Random rnd = new Random();
for (int i = 0; i < codeToGuess.Length; i++)
{
codeToGuess[i] = rnd.Next(1, 7);
}
return codeToGuess;
}
public int[] createCodeOfPlayer(int[] codeOfPlayer) //creating the player's code with validation
{
int i = 0;
while (i < codeOfPlayer.Length)
{
Console.WriteLine("Choose 4 of the next numbers! [1/2/3/4/5/6]");
int playerInput = Convert.ToInt32(Console.ReadLine());
switch (playerInput)
{
case 1:
codeOfPlayer[i] = playerInput;
i++;
break;
case 2:
codeOfPlayer[i] = playerInput;
i++;
break;
case 3:
codeOfPlayer[i] = playerInput;
i++;
break;
case 4:
codeOfPlayer[i] = playerInput;
i++;
break;
case 5:
codeOfPlayer[i] = playerInput;
i++;
break;
case 6:
codeOfPlayer[i] = playerInput;
i++;
break;
default:
Console.WriteLine("Voer ff wat normaals in man");
break;
}
}
return codeOfPlayer;
}
static void Main(string[] args)
{
int[] codeOfPlayer = new int[4];
int[] codeOfOpponent = new int[4];
int amountOfTurns = 0;
int rightGuessWrongIndex = 0;
int rightGuessRightIndex = 0;
//int wrongGuessWrongIndex = 0;
Program mastermind = new Program();
codeOfOpponent = mastermind.createCodetoBreak(codeOfOpponent);
codeOfPlayer = mastermind.createCodeOfPlayer(codeOfPlayer);
Console.WriteLine("The player's code is");
for (int i = 0; i < codeOfPlayer.Length; i++)
Console.Write(codeOfPlayer[i]);
Console.WriteLine("\nThe opponent's code is");
for (int i = 0; i < codeOfOpponent.Length; i++)
Console.Write(codeOfOpponent[i]);
Console.WriteLine('\n');
bool alreadyOcurredNumber = false;
//bepalen van de witte en zwarte/rode pinnetjes
for(int i = 0; i < codeOfPlayer.Length; i++)
{
for (int j = 0; j < codeOfOpponent.Length; j++)
{
if (alreadyOcurredNumber == false)
Console.WriteLine("boven " + alreadyOcurredNumber);
{
//Console.WriteLine(alreadyOcurredNumber);
if ((codeOfOpponent[i] == codeOfPlayer[j]) && (i == j))
{
rightGuessRightIndex++;
alreadyOcurredNumber = true;
}
else if ((codeOfOpponent[i] == codeOfPlayer[j]))
{
rightGuessWrongIndex++;
alreadyOcurredNumber = true;
Console.WriteLine(alreadyOcurredNumber);
}
}
}
alreadyOcurredNumber = false;
}
Console.WriteLine("Right number and right place: {0}", rightGuessRightIndex);
Console.WriteLine("Right number and wrong place: {0}", rightGuessWrongIndex);
//Console.WriteLine(wrongGuessWrongIndex);
if (codeOfPlayer.SequenceEqual(codeOfOpponent) && amountOfTurns < 20)
Console.WriteLine("You're right!");
else
{
Console.WriteLine("\n Try again! ");
amountOfTurns++;
}
Console.ReadKey();
}
}
}
代码中有一些草稿,请不要介意。只是为了提供上下文。