-2

我在策划游戏的一部分方面需要帮助。我有一个随机生成的 4 个 int 代码,用户有 10 次尝试正确猜测(每个猜测都有自己的数组)。用户可以获得完美匹配(正确的数组值中的正确数字),或数字匹配(正确的数字错误的数组值)。我怎样才能制作这样的代码?读取用户的答案并显示他当前拥有的匹配数量。

每个猜测都有自己的数组:

int [] guess1 = new int []{0,0,0,0,0,0};
int [] guess2 = new int []{0,0,0,0,0,0};

//依此类推,直到猜到 10。

然后我用游戏板上的 arr 替换每个猜测:

if (newGuess == 1)
{
arr[0] = guess[0];
arr[1] = guess[1];
arr[2] = guess[2];
arr[3] = guess[3];
}
else if (newGuess == 2)
{
arr1[0] = guess[0];
arr1[1] = guess[1];
arr1[2] = guess[2];
arr1[3] = guess[3];
} 

//依此类推,直到猜到 10

我只是不知道如何编写代码来读取完美和数字匹配并在每次猜测后显示 TOTAL 完美和数字匹配。我知道这可能含糊不清,但我现在很困惑,如果需要,我可以提供进一步的解释。谢谢 :)

class MainClass 
{
  static Random rng = new Random();
  const int LETTER_SUBTRACTION = 48;
  public static void Main (string[] args) 
  {
    bool gameDone = false;

主循环

while(gameDone == false)
    {
      for(int newGuess = 0; newGuess <= 10; newGuess++)
      {
        Console.WriteLine(newGuess);
        Console.Clear();
        DrawBoard();
        if (newGuess == 1)
        {
          GetGuess(guess1);
          ReplaceBoard(guess1, newGuess, code);
        }
        else if (newGuess == 2)
        {
          GetGuess(guess2);
          ReplaceBoard(guess2, newGuess, code);
        }
        else if (newGuess == 3)
        {
          GetGuess(guess3);
          ReplaceBoard(guess3, newGuess, code);
        }
        else if (newGuess == 4)
        {
          GetGuess(guess4);
          ReplaceBoard(guess4, newGuess, code);
        }
        else if (newGuess == 5)
        {
          GetGuess(guess5);
          ReplaceBoard(guess5, newGuess, code);
        }
        else if (newGuess == 6)
        {
          GetGuess(guess6);
          ReplaceBoard(guess6, newGuess, code);
        }
        else if (newGuess == 7)
        {
          GetGuess(guess7);
          ReplaceBoard(guess7, newGuess, code);
        }
        else if (newGuess == 8)
        {
          GetGuess(guess8);
          ReplaceBoard(guess8, newGuess, code);
        }
        else if (newGuess == 9)
        {
          GetGuess(guess9);
          ReplaceBoard(guess9, newGuess, code);
        }
        else if (newGuess == 10)
        {
          GetGuess(guess10);
          ReplaceBoard(guess10, newGuess, code);
        }
      }
      CheckMatch();
    }

我的随机码

private static int[] GetRandomCode()
  {
    //Store the numbers that represent the 8 possible numbers
    int [] numbers = new int[]{1,2,3,4,5,6,7,8};

    //Store the randomly generated code
    int [] code = new int[4];

    //Store the data needed to swap two elements of the numbers array
    int swapIndex1;
    int swapIndex2;
    int tempNumber;

    //Swap two random numbers in the numbers array 1000 times!
    for (int i = 0; i < 1000; i++)
    {
      //Genereate 2 random index values in the numbers array range
      swapIndex1 = rng.Next(0,numbers.Length);
      swapIndex2 = rng.Next(0,numbers.Length);

      //Swap the numbers at those index locations
      //(like we did back in the variables lesson a long time ago)
      tempNumber = numbers[swapIndex1];
      numbers[swapIndex1] = numbers[swapIndex2];
      numbers[swapIndex2] = tempNumber;
    }

    //Now just grab the first four elements of the numbers array as
    //our random code
    code[0] = numbers[0];
    code[1] = numbers[1];
    code[2] = numbers[2];
    code[3] = numbers[3];

    //Return the code for game play to begin
    return code;
  }

数组

static int [] arr = {0,0,0,0,0,0};
  static int [] arr1 = {0,0,0,0,0,0};
  static int [] arr2 = {0,0,0,0,0,0};
  static int [] arr3 = {0,0,0,0,0,0};
  static int [] arr4 = {0,0,0,0,0,0};
  static int [] arr5 = {0,0,0,0,0,0};
  static int [] arr6 = {0,0,0,0,0,0};
  static int [] arr7 = {0,0,0,0,0,0};
  static int [] arr8 = {0,0,0,0,0,0};
  static int [] arr9 = {0,0,0,0,0,0};

  static int [] guess1 = new int []{0,0,0,0,0,0};
  static int [] guess2 = new int []{0,0,0,0,0,0};
  static int [] guess3 = new int []{0,0,0,0,0,0};
  static int [] guess4 = new int []{0,0,0,0,0,0};
  static int [] guess5 = new int []{0,0,0,0,0,0};
  static int [] guess6 = new int []{0,0,0,0,0,0};
  static int [] guess7 = new int []{0,0,0,0,0,0};
  static int [] guess8 = new int []{0,0,0,0,0,0};
  static int [] guess9 = new int []{0,0,0,0,0,0};
  static int [] guess10 = new int []{0,0,0,0,0,0};

我的董事会

private static void DrawBoard()
  {
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("   ---Mastermind--");
    Console.ResetColor();
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("                          PERFECTS     NUMBERS");
    Console.ResetColor();
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("   {?}    {?}    {?}    {?}");
    Console.ResetColor();
    Console.ForegroundColor = ConsoleColor.DarkYellow;
    Console.WriteLine("______________________________________________");
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr9[0], arr9[1], arr9[2], arr9[3], arr9[4], arr9[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr8[0], arr8[1], arr8[2], arr8[3], arr8[4], arr8[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr7[0], arr7[1], arr7[2], arr7[3], arr7[4], arr7[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr6[0], arr6[1], arr6[2], arr6[3], arr6[4], arr6[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr5[0], arr5[1], arr5[2], arr5[3], arr5[4], arr5[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr4[0], arr4[1], arr4[2], arr4[3], arr4[4], arr4[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr3[0], arr3[1], arr3[2], arr3[3], arr3[4], arr3[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr2[0], arr2[1], arr2[2], arr2[3], arr2[4], arr2[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr1[0], arr1[1], arr1[2], arr1[3], arr1[4], arr1[5]);
    Console.WriteLine("----------------------+-----------------------");
    Console.WriteLine("   {0}    {1}    {2}    {3}   |      {4}            {5} ", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
    Console.ResetColor();
  }

让玩家猜测

private static void GetGuess(int [] guess)
  {
    string userGuess;

    Console.Write("Enter your guess: ");
    userGuess = Console.ReadLine();


    guess[0] = userGuess [0] - LETTER_SUBTRACTION;
    guess[1] = userGuess [1] - LETTER_SUBTRACTION;
    guess[2] = userGuess [2] - LETTER_SUBTRACTION;
    guess[3] = userGuess [3] - LETTER_SUBTRACTION;
  }

用玩家猜测替换棋盘

private static void ReplaceBoard(int [] guess, int newGuess, int [] code)
  {
    bool checkMatches = false;

    if (newGuess == 1)
    {
      arr[0] = guess[0];
      arr[1] = guess[1];
      arr[2] = guess[2];
      arr[3] = guess[3];
    } 
    else if (newGuess == 2)
    {
      arr1[0] = guess[0];
      arr1[1] = guess[1];
      arr1[2] = guess[2];
      arr1[3] = guess[3];
    }
    else if (newGuess == 3)
    {
      arr2[0] = guess[0];
      arr2[1] = guess[1];
      arr2[2] = guess[2];
      arr2[3] = guess[3];
    }
    else if (newGuess == 4)
    {
      arr3[0] = guess[0];
      arr3[1] = guess[1];
      arr3[2] = guess[2];
      arr3[3] = guess[3];
    }
    else if (newGuess == 5)
    {
      arr4[0] = guess[0];
      arr4[1] = guess[1];
      arr4[2] = guess[2];
      arr4[3] = guess[3];
    }
    else if (newGuess == 6)
    {
      arr5[0] = guess[0];
      arr5[1] = guess[1];
      arr5[2] = guess[2];
      arr5[3] = guess[3];
    }
    else if (newGuess == 7)
    {
      arr6[0] = guess[0];
      arr6[1] = guess[1];
      arr6[2] = guess[2];
      arr6[3] = guess[3];
    }
    else if (newGuess == 8)
    {
      arr7[0] = guess[0];
      arr7[1] = guess[1];
      arr7[2] = guess[2];
      arr7[3] = guess[3];
    }
    else if (newGuess == 9)
    {
      arr8[0] = guess[0];
      arr8[1] = guess[1];
      arr8[2] = guess[2];
      arr8[3] = guess[3];
    }
    else if (newGuess == 10)
    {
      arr9[0] = guess[0];
      arr9[1] = guess[1];
      arr9[2] = guess[2];
      arr9[3] = guess[3];
    }
  }

检查比赛(你告诉我的)

private static void CheckMatch()
  {
    int perfectGuesses = 0;
    int numberGuesses = 0;
    for (int i = 0; i < guess1.Length - 2; i++)
{
    if (arr[i] == guess1[i])
    {
        guess1[guess1.Length - 2]++;
    }
    else if (arr.Contains(guess1[i]))
    {
        guess1[guess1.Length - 1]++;
    }
}
    guess1[4] = perfectGuesses;
    guess1[5] = numberGuesses;
  }
4

1 回答 1

0

我不完全理解你的目标是什么,但这里是你想要的猜测:

using System.Linq;

int [] guess1 = new int []{0,0,0,0,0,0};
for (int i = 0; i < guess1.Length - 2; i++)
{
    if (arr[i] == guess1[i])
    {
        guess1[guess1.Length - 2]++;
    }
    else if (arr.Contains(guess1[i]))
    {
        guess1[guess1.Length - 1]++;
    }
}
于 2020-11-21T20:00:20.483 回答