栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C# L型棋牌覆盖实现代码与效果

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
    {
        ///


        /// 初始化L型骨牌
        ///

        private int LDominonumberInitial = 0;
       

        ///


        ///  L型骨牌棋盘覆盖
        ///

        ///
        ///
        ///
        ///
        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);
            }
        }

        ///


        /// 大棋盘4分为小的棋盘,在没有特殊位置的小棋盘中放L骨牌一角(作为特殊位置)
        /// 然后再次对每个小的4划分...至只有一个格子.
        ///

        ///
        ///
        ///
        ///
        ///
        ///
        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);
            }
        }
    }
}

//程序运行结果截图

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/127468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号