VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C# L型棋牌覆盖实现代码与效果

//Main

 

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 

namespace ChessBoard
{
    class Program
    {
        //谁能教教我英语啊,英语语法什么的错误之处还望海涵,
        static void Main(string[] args)
        {
            Function obj = new Function();

            Console.WriteLine("Please intput CheseBoard Size(2^size):");
            int size = (int)Math.Pow(2, Convert.ToInt32(Console.ReadLine()));
            if (size != 1)
            {
                ConsoleColor FC = Console.ForegroundColor;
                //string[] Color = { "Black" , "DarkBlue" , "DarkGreen" , "DarkCyan" , "Gray",
                //                   "DarkRed" , "DarkMagenta" , "DarkYellow" , "Red",
                //                   "DarkGray" , "Blue" , "Green" , "Cyan", "Magenta",
                //                   "Yellow" , "White"};
                string[,] Board = new string[size, size];

                //Do you know ?
                String[] Colors = ConsoleColor.GetNames(typeof(ConsoleColor));

                Console.WriteLine("please input special grid position (row and col):");
                int rows = Convert.ToInt32(Console.ReadLine());
                int cols = Convert.ToInt32(Console.ReadLine());

                obj.CheseBoard(Board, size, rows, cols);

                for (int r = 0; r < Board.GetLength(0); r++)
                {
                    for (int c = 0; c < Board.GetLength(1); c++)
                    {
                        int Value = Convert.ToInt32(Board[r, c].ToString());
                        if (Value > 0)
                        {
                            if (Value > 15)
                            {
                                Value %= 15;
                            }
                            if ((Value %= 15) == 0)
                            {
                                Value += 1;
                            }
                            Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), Colors[Value]);
                        }
                        Console.Write(Board[r, c] + "  ");
                        // Console.ForegroundColor = FC;
                        Console.ResetColor();
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("Bugs Bug ! ! !");
            }
            Console.ReadKey();
        }
    }
}


//Class

 

 

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

 

namespace ChessBoard
{
    class Function
    {
        /// <summary>
        /// 初始化L型骨牌
        /// </summary>
        private int LDominoNumberInitial = 0;
       

        /// <summary>
        ///  L型骨牌棋盘覆盖
        /// </summary>
        /// <param name="Board"></param>
        /// <param name="size"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        public void CheseBoard(string[,] Board, int size, int row, int col)
        {
            int InitialRow = 0;
            int InitialCol = 0;
            //不合法的输入
            if (row > size - 1 || col > size - 1)
            {
                Console.WriteLine("Error !!!!!!!!!!");
            }
                //棋盘只有一个格子
            else if (size == 1)
            {
                Console.WriteLine(Board[row, col] = "-1");
            }
            else
            {
                Board[row, col] = "-1";
                DivisionBoard(Board, InitialRow, InitialCol, row, col, size);
            }
        }

        /// <summary>
        /// 大棋盘4分为小的棋盘,在没有特殊位置的小棋盘中放L骨牌一角(作为特殊位置)
        /// 然后再次对每个小的4划分...至只有一个格子.
        /// </summary>
        /// <param name="Board"></param>
        /// <param name="InitialRow"></param>
        /// <param name="InitialCol"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="size"></param>
        public void DivisionBoard(string[,] Board, int InitialRow, int InitialCol, int row, int col, int size)
        {
            if (size == 1)
            {
                return;
            }
            //It's Important....全局的骨牌数的副本
            int LDominoNumber = LDominoNumberInitial++;
            //判断特殊位置的界限值
            size /= 2;
            //left up
            if (row < InitialRow + size && col < InitialCol + size)
            {
                //特殊位置在里面
                DivisionBoard(Board, InitialRow, InitialCol, row, col, size);
            }
            else
            {
                //不在里面,在这里面放L骨牌的一角,为下次递归做准备..
                if (LDominoNumber < 10)
                {
                    Board[InitialRow + size - 1, InitialCol + size - 1] = "0" + LDominoNumber.ToString();
                }
                else
                {
                    Board[InitialRow + size - 1, InitialCol + size - 1] = LDominoNumber.ToString();
                }
                //Console.ForegroundColor = FC;
                //最左上角
                DivisionBoard(Board, InitialRow, InitialCol, InitialRow + size - 1, InitialCol + size - 1, size);
            }
            //right up
            if (row < InitialRow + size && col >= InitialCol + size)
            {
                DivisionBoard(Board, InitialRow, InitialCol + size, row, col, size);
            }
            else
            {
                if (LDominoNumber < 10)
                {
                    Board[InitialRow + size - 1, InitialCol + size] = "0" + LDominoNumber.ToString();
                }
                else
                {
                    Board[InitialRow + size - 1, InitialCol + size] = LDominoNumber.ToString();
                }
                DivisionBoard(Board, InitialRow, InitialCol + size, InitialRow + size - 1, InitialCol + size, size);
            }
            //left down
            if (row >= InitialRow + size && col < InitialCol + size)
            {
                DivisionBoard(Board, InitialRow + size, InitialCol, row, col, size);
            }
            else
            {
                if (LDominoNumber < 10)
                {
                    Board[InitialRow + size, InitialCol + size - 1] = "0" + LDominoNumber.ToString();
                }
                else
                {
                    Board[InitialRow + size, InitialCol + size - 1] = LDominoNumber.ToString();
                }
                DivisionBoard(Board, InitialRow + size, InitialCol, InitialRow + size, InitialCol + size - 1, size);
            }
            //right down
            if (row >= InitialRow + size && col >= InitialCol + size)
            {
                DivisionBoard(Board, InitialRow + size, InitialCol + size, row, col, size);
            }
            else
            {
                if (LDominoNumber < 10)
                {
                    Board[InitialRow + size, InitialCol + size] = "0" + LDominoNumber.ToString();
                }
                else
                {
                    Board[InitialRow + size, InitialCol + size] = LDominoNumber.ToString();
                }
                DivisionBoard(Board, InitialRow + size, InitialCol + size, InitialRow + size, InitialCol + size, size);
            }
        }
    }
}


//程序运行结果截图

 

 


相关教程