|
shenyoujia
- 管理员
- 14848
- 1423
-
2009-11-07
|
shenyoujia
2010-12-07 09:04
|只看楼主
2#
t
T
- namespace ConsoleApplication1
{ public class Program { static bool[, ,] cells;
static Program() { PrepareData(); }
public static void Main() { JudgeWin();
}
static private void JudgeWin() { bool result = true;
for (int i = 0; i < cells.GetLength(0); i++) { //判断横竖 for (int times = 0; times < 2; times++) { Context context; if (times == 0) context = new HengContext(); else context = new ShuContext();
for (int j = 0; j < cells.GetLength(1); j++) { for (int k = 0; k < cells.GetLength(2); k++) { result &= context.GetTrueOrFalse(cells, i, j, k); } if(result) PrintWin( i); result =true; } }
//判断斜 for (int j = 0, k = 2; j <= k; j++, k--) result &= cells[i, j, k]; if (result) PrintWin(i); result = true; } }
private static void PrintWin(int i) { Console.WriteLine("第{0}位选手获胜!", i + 1); }
private static void PrepareData() { cells = new bool[2, 3, 3] { // Player 1 moves { {true,false,false}, {true,false,false}, {true,false,false} },
// Player 2 moves { {false,false,true}, {false,true,false}, {true,true,false} } }; }
}
abstract class Context {
internal protected abstract bool GetTrueOrFalse(bool[, ,] cells, int i, int j, int k);
}
class HengContext : Context {
internal protected override bool GetTrueOrFalse(bool[, ,] cells, int i, int j, int k) { return cells[i, j, k]; } }
class ShuContext : Context { internal protected override bool GetTrueOrFalse(bool[, ,] cells, int i, int j, int k) { return cells[i, k, j]; } } }
复制代码
|