Example #1
0
        public bool WinningMove(TicTacToe t)
        {
            MoveCount n = x;

            if (n.player != t.CurrentPlayer())
            {
                return(false);
            }
            for (int i = 0; i < win; i++)
            {
                if (n.columns[i] == (win - 1))
                {
                    for (int j = 0; j < win; j++)
                    {
                        if (t.ValInBoard(j, i) == '\0')
                        {
                            t.Insert(n.player, j, i);
                            return(true);
                        }
                    }
                }
                if (n.rows[i] == (win - 1))
                {
                    for (int j = 0; j < win; j++)
                    {
                        if (t.ValInBoard(i, j) == '\0')
                        {
                            t.Insert(n.player, i, j);
                            return(true);
                        }
                    }
                }
            }
            if (n.diagonals[0] == (win - 1))
            {
                for (int j = 0; j < win; j++)
                {
                    if (t.ValInBoard(j, j) == '\0')
                    {
                        t.Insert(n.player, j, j);
                        return(true);
                    }
                }
            }
            else if (n.diagonals[1] == (win - 1))
            {
                for (int j = 0; j < win; j++)
                {
                    if (t.ValInBoard(j, (size - 1 - j)) == '\0')
                    {
                        t.Insert(n.player, j, (size - 1 - j));
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #2
0
 private char VertWin(TicTacToe t)
 {
     for (int h = 0; h < t.BoardSize(); h++)
     {
         if (t.ValInBoard(0, h) == t.ValInBoard(1, h) && t.ValInBoard(1, h) == t.ValInBoard(2, h))
         {
             return(t.ValInBoard(0, h));
         }
     }
     return('\0');
 }
Example #3
0
 private char HoriWin(TicTacToe t)
 {
     for (int v = 0; v < t.BoardSize(); v++)
     {
         if (t.ValInBoard(v, 0) == t.ValInBoard(v, 1) && t.ValInBoard(v, 1) == t.ValInBoard(v, 2))
         {
             return(t.ValInBoard(v, 0));
         }
     }
     return('\0');
 }
Example #4
0
        private char DiagWin(TicTacToe t)
        {
            char c = t.ValInBoard(0, 0);

            if (c == t.ValInBoard(1, 1) && c == t.ValInBoard(2, 2))
            {
                return(c);
            }
            else
            {
                c = t.ValInBoard(0, 2);
                if (c == t.ValInBoard(1, 1) && c == t.ValInBoard(2, 0))
                {
                    return(c);
                }
            }

            return('\0');
        }
Example #5
0
        public char IsWinner(TicTacToe t)
        {
            char c = HoriWin(t);

            if (c != '\0')
            {
                return(c);
            }
            else if ((c = VertWin(t)) != '\0')
            {
                return(c);
            }
            else if ((c = DiagWin(t)) != '\0')
            {
                return(c);
            }
            else
            {
                return('\0');
            }
        }
Example #6
0
        static void Main(string[] args)
        {
            TicTacToe t = new TicTacToe();

#if AI
            TTT_AI ai = new TTT_AI();
#endif
            char victor = t.IsWinner(t);

            t.DisplayBoard();
            t.DisplayPosition();

            for (;;)
            {
                ConsoleKeyInfo k = Console.ReadKey();
                Console.Clear();
                if (k.Key == ConsoleKey.R)
                {
                    t.Reset();
#if AI
                    ai.ResetAI();
#endif
                }
                else if (k.Key == ConsoleKey.Q || k.Key == ConsoleKey.Escape)
                {
                    System.Environment.Exit(0);
                }
                else if (victor == '\0')
                {
                    switch (k.Key)
                    {
                    case ConsoleKey.W: t.MoveUp(); break;

                    case ConsoleKey.S: t.MoveDown(); break;

                    case ConsoleKey.A: t.MoveLeft(); break;

                    case ConsoleKey.D: t.MoveRight(); break;

                    case ConsoleKey.Enter: if (t.InsertPlayer())
                        {
                            t.IncCounter();
                        }
#if AI
                        ai.MoveInput(t);
                        ai.WinningMove(t);
#endif
                        break;
                    }
                }
                t.DisplayBoard();
                t.DisplayPosition();
                if ((victor = t.IsWinner(t)) != '\0')
                {
                    Console.WriteLine("Winner is {0}.", victor);
                }
                else if (t.IsFull(t))
                {
                    Console.WriteLine("Draw.");
                }
            }
        }
Example #7
0
 public bool IsFull(TicTacToe t)
 {
     return((total >= t.BoardSize() * t.BoardSize()) ? true : false);
 }