Block.cs
namespace SimpleFactory
{
interface Block
{
int[][] GetData();
void Change();
}
public class Square : Block
{
int[][] data;
public Square()
{
data = new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 0, 0 }
};
}
public int[][] GetData()
{
return data;
}
public void Change() { }
}
public class Bar : Block
{
int index;
int[][][] data;
public Bar()
{
index = 0;
data = new int[][][]
{
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 0, 0 },
new int[] { 1, 1, 1, 1 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 0, 0 },
}
};
}
public int[][] GetData()
{
return data[index];
}
public void Change()
{
index = (index + 1) % 2;
}
}
public class LBlock : Block
{
int index;
int[][][] data;
public LBlock()
{
index = 0;
data = new int[][][]
{
new int[][]
{
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 1, 0 },
new int[] { 1, 1, 1, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 1, 0 },
new int[] { 0, 0, 1, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 1, 1, 1, 0 },
new int[] { 1, 0, 0, 0 },
new int[] { 0, 0, 0, 0 },
}
};
}
public int[][] GetData()
{
return data[index];
}
public void Change()
{
index = (index + 1) % 4;
}
}
public class LRBlock : Block
{
int index;
int[][][] data;
public LRBlock()
{
index = 0;
data = new int[][][]
{
new int[][]
{
new int[] { 0, 0, 1, 0 },
new int[] { 0, 0, 1, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 1, 1, 1, 0 },
new int[] { 0, 0, 1, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 1, 0, 0, 0 },
new int[] { 1, 1, 1, 0 },
new int[] { 0, 0, 0, 0 },
}
};
}
public int[][] GetData()
{
return data[index];
}
public void Change()
{
index = (index + 1) % 4;
}
}
public class ZBlock : Block
{
int index;
int[][][] data;
public ZBlock()
{
index = 0;
data = new int[][][]
{
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 1, 1, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 0, 1, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 1, 0, 0 },
}
};
}
public int[][] GetData()
{
return data[index];
}
public void Change()
{
index = (index + 1) % 2;
}
}
public class ZRBlock : Block
{
int index;
int[][][] data;
public ZRBlock()
{
index = 0;
data = new int[][][]
{
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 1, 1, 0, 0 },
new int[] { 0, 0, 0, 0 },
},
new int[][]
{
new int[] { 0, 0, 0, 0 },
new int[] { 0, 1, 0, 0 },
new int[] { 0, 1, 1, 0 },
new int[] { 0, 0, 1, 0 },
}
};
}
public int[][] GetData()
{
return data[index];
}
public void Change()
{
index = (index + 1) % 2;
}
}
}
BlockFactory.cs
namespace SimpleFactory
{
class BlockFactory
{
public static Block CreateBlock(int type)
{
Block block = null;
switch (type)
{
case 0:
block = new Square();
break;
case 1:
block = new Bar();
break;
case 2:
block = new LBlock();
break;
case 3:
block = new LRBlock();
break;
case 4:
block = new ZBlock();
break;
default:
block = new ZRBlock();
break;
}
return block;
}
}
}
Program.cs
namespace SimpleFactory
{
class Program
{
static void Main(string[] args)
{
Block block = BlockFactory.CreateBlock(0);
ConsoleKeyInfo keyInfo;
while (true)
{
keyInfo = Console.ReadKey(true);
if (keyInfo.KeyChar == '0')
{
block = BlockFactory.CreateBlock(0);
printBlock(block);
}
else if (keyInfo.KeyChar == '1')
{
block = BlockFactory.CreateBlock(1);
printBlock(block);
}
else if (keyInfo.KeyChar == '2')
{
block = BlockFactory.CreateBlock(2);
printBlock(block);
}
else if (keyInfo.KeyChar == '3')
{
block = BlockFactory.CreateBlock(3);
printBlock(block);
}
else if (keyInfo.KeyChar == '4')
{
block = BlockFactory.CreateBlock(4);
printBlock(block);
}
else if (keyInfo.KeyChar == '5')
{
block = BlockFactory.CreateBlock(5);
printBlock(block);
}
else if (keyInfo.KeyChar == '.')
{
block.Change();
printBlock(block);
}
else if (keyInfo.KeyChar == '6')
{
Console.Clear();
}
else
{
break;
}
}
}
static void printBlock(Block block)
{
int[][] data = block.GetData();
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if(data[i][j] == 0)
Console.Write(" ");
else
Console.Write("■");
}
Console.WriteLine();
}
}
}
}
类结构图
运行结果