首页 > 解决方案 > c# 如何使符号保持打开控制台(记忆游戏)

问题描述

我被困在我希望我的符号匹配时保持翻转的部分。这是一个用于项目的简单 4x4 记忆游戏。我应该做一个新的方法吗?如果有人能告诉我是否可以通过按键以某种方式重置电路板,那就太好了。

我是一个完全的初学者,所以如果有人能以一种非常简单的方式向我解释这一点,那将对我有很大帮助。目前我坚持这一点。

using System;

namespace Memory
{
    class Program
    {

        static Coordinate[,] board = new Coordinate[4, 4];

        static int PosX = 0;
        static int PosY = 0;

        static void Main(string[] args)
        {
            //randomize

            char[] symbols = new char[] { '#', '#', '1', '1', '2', '2', '3', '3', '4', '4', 'A', 'A', 'B', 'B', 'C', 'C' };
            Random rnd = new Random();

            for (int y = 0; y < board.GetLength(0); y++)
            {
                for (int x = 0; x < board.GetLength(1); x++)
                {
                    Coordinate c = new Coordinate();

                    while (true)
                    {
                        int index = rnd.Next(symbols.Length);
                        if (symbols[index] != ' ')
                        {
                            c.Symbol = symbols[index];
                            symbols[index] = ' ';
                            break;
                        }
                    }
                    board[y, x] = c;
                }
            }

            while (true)
            {
                DrawBoard();

                var key = Console.ReadKey();
                if(key.Key == ConsoleKey.LeftArrow)
                {
                    PosX--;
                }
                if (key.Key == ConsoleKey.RightArrow)
                {
                    PosX++;
                }
                if (key.Key == ConsoleKey.UpArrow)
                {
                    PosY--;
                }
                if (key.Key == ConsoleKey.DownArrow)
                {
                    PosY++;
                }
                if (key.Key == ConsoleKey.Spacebar)
                {
                    board[PosY, PosX].IsOpen = true;
                }

                Console.Clear();
            }
            Console.ReadLine();
        }

        static void DrawBoard()
        {
            for (int y = 0; y < board.GetLength(0); y++)
            {
                for (int x = 0; x < board.GetLength(1); x++)
                {
                    if(x == PosX && y == PosY)
                    {
                        Console.BackgroundColor = ConsoleColor.Blue;
                    }
                    if (!board[y, x].IsOpen)
                    {
                        Console.Write(". ");
                    }
                    else
                    {
                        Console.Write(board[y, x].Symbol + " ");
                    }
                    Console.BackgroundColor = ConsoleColor.Black;
                }
                Console.WriteLine();
            }
        }
    }

    class Coordinate
    {
        public char Symbol;
        public bool IsOpen;
        public bool isDone;
    }

}

标签: c#

解决方案


如果换行:

board[PosY, PosX].IsOpen = true;

board[PosY, PosX].IsOpen = !board[PosY, PosX].IsOpen;

再次选择时,所选字段将翻转。

或者用字母“R”将整个电路板翻转回来:

if (key.Key == ConsoleKey.R)
{
   for (int y = 0; y < board.GetLength(0); y++)
   {
      for (int x = 0; x < board.GetLength(1); x++)
      {
         if (board[y, x].IsOpen) board[y, x].IsOpen = false;
      }
   }
}

如果已实施,您应该扩展它isDone,因为如果一个字段isDone可能不应该恢复回来?


推荐阅读