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);
}
}
}
}
//程序运行结果截图