未解出的谜题,先留纪念,以备后查

[ 673 查看 / 1 回复 ]

  1.     public class Program
  2.     {
  3.         static bool[, ,] cells = new bool[2, 3, 3]
  4.             {
  5.                 // Player 1 moves
  6.                 {
  7.                     {true,false,false},
  8.                     {true,false,false},
  9.                     {false,false,true}
  10.                 },

  11.                 // Player 2 moves
  12.                 {
  13.                     {false,false,true},
  14.                     {false,true,false},
  15.                     {true,true,false}
  16.                 }
  17.             };
  18.         public static void Main()
  19.         {
  20.             JudgeSucceedFirst();

  21.             JudgeSucceedSecond();

  22.             JudgeSucceedThird();

  23.         }

  24.         private static void JudgeSucceedThird()
  25.         {
  26.             bool result = true;

  27.             for (int i = 0; i < cells.GetLength(0); i++)
  28.             {
  29.                 //result &= cells[i, 0, 0] && cells[i, 1, 1] && cells[i, 2, 2];
  30.                 for (int j = 0; j < cells.GetLength(1); j++) result &= cells[i, j, j];
  31.                 if (result) Console.WriteLine("第{0}位选手获胜!", i + 1);
  32.                 else result = true;
  33.             }
  34.         }

  35.         private static void JudgeSucceedSecond()
  36.         {
  37.             bool result = true;

  38.             for (int i = 0; i < cells.GetLength(0); i++)
  39.             {
  40.                 for (int j = 0; j < cells.GetLength(2); j++)
  41.                 {
  42.                     for (int k = 0; k < cells.GetLength(1); k++) result &= cells[i, k, j];
  43.                     if (result) Console.WriteLine("第{0}位选手获胜!", i + 1);
  44.                     else result = true;
  45.                 }
  46.             }
  47.         }

  48.         private static void JudgeSucceedFirst()
  49.         {
  50.             bool result = true;

  51.             for (int i = 0; i < cells.GetLength(0); i++)
  52.             {
  53.                 for (int j = 0; j < cells.GetLength(1); j++)
  54.                 {
  55.                     for (int k = 0; k < cells.GetLength(2); k++) result &= cells[i, j, k];
  56.                     if (result) Console.WriteLine("第{0}位选手获胜!", i + 1);
  57.                     else result = true;
  58.                 }
  59.             }
  60.         }


  61.     }
复制代码
TOP

  1. 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];
            }
        }
    }
复制代码
TOP