示例#1
0
        private void AIMove()//Will be called when it is time for the AI to perform a move
        {
            Invalidate();
            string status = Engine.ComputerMove(); //Calls the method for the AI to make its move, logic and board manipulation done inside GameEngine

            if (status == "w")                     //Below is how the form knows what the game engine means/if no moves should be allowed
            {
                MessageBox.Show("Congratulations, You Win!");
                gameover = true;
            }
            else if (status == "l")
            {
                MessageBox.Show("You Lose!");
                gameover = true;
            }
            else if (status == "t")
            {
                MessageBox.Show("You Tied!");
                gameover = true;
            }
            Invalidate();
        }
示例#2
0
 private void Form1_MouseClick(object sender, MouseEventArgs e)
 {
     Graphics g = CreateGraphics();
     ApplyTransform(g);
     PointF[] p = { new Point(e.X, e.Y) };
     g.TransformPoints(CoordinateSpace.World,
         CoordinateSpace.Device, p);
     if (p[0].X < 0 || p[0].Y < 0) return;
     int i = (int)(p[0].X / block);
     int j = (int)(p[0].Y / block);
     if (i > 2 || j > 2) return;
     else if (grid[i, j] == CellSelection.N) //only allow setting empty cells
     {
         if (!gameOver)
         {
             if (e.Button == MouseButtons.Left)
             {
                 grid[i, j] = CellSelection.X;
                 GameEngine engine = new GameEngine(board);
                 if (engine.checkWin() != 0)
                 {
                     if (engine.checkWin() == 1)
                     {
                         MessageBox.Show("You win!");
                         gameOver = true;
                         Invalidate();
                     }
                     else
                     {
                         MessageBox.Show("Computer wins");
                         gameOver = true;
                         Invalidate();
                     }
                 }
                 board[((3 * j) + i)] = 1; //1 means O
                 turncounter++;
             }
             if ((turncounter % 2 == 1) && !gameOver)//default turncounter = 0, player goes first
             {
                 GameEngine engine = new GameEngine(board);
                 if (engine.checkWin() != 0)
                 {
                     if (engine.checkWin() == 1)
                     {
                         Invalidate();
                         MessageBox.Show("You win!");
                         gameOver = true;
                         Invalidate();
                     }
                     else
                     {
                         Invalidate();
                         MessageBox.Show("Computer wins");
                         gameOver = true;
                         Invalidate();
                     }
                 }
                 if (!gameOver && turncounter < 9)
                 {
                     Point move = engine.ComputerMove();
                     grid[move.X, move.Y] = CellSelection.O;
                     board[((3 * move.Y) + move.X)] = 2;
                     if (engine.checkWin() != 0)
                     {
                         if (engine.checkWin() == 1)
                         {
                             Invalidate();
                             MessageBox.Show("You win!");
                             gameOver = true;
                             Invalidate();
                         }
                         else
                         {
                             Invalidate();
                             MessageBox.Show("Computer wins");
                             gameOver = true;
                             Invalidate();
                         }
                     }
                 }
                 turncounter++;
             }
         }
         else
         {
             turncounter = 0;
             for (int ii = 0; ii < 3; ii++)
             {
                 for (int jj = 0; jj < 3; jj++)
                 {
                     board[((3 * ii) + jj)] = 0;
                     grid[ii, jj] = CellSelection.N;
                 }
             }
             gameOver = false;
         }
     }
     else
     {
         MessageBox.Show("Invalid Move");
     }
     Invalidate();
 }
示例#3
0
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            Graphics g = CreateGraphics();

            ApplyTransform(g);
            PointF[] p = { new Point(e.X, e.Y) };
            g.TransformPoints(CoordinateSpace.World,
                              CoordinateSpace.Device, p);
            if (p[0].X < 0 || p[0].Y < 0)
            {
                return;
            }
            int i = (int)(p[0].X / block);
            int j = (int)(p[0].Y / block);

            if (i > 2 || j > 2)
            {
                return;
            }
            else if (grid[i, j] == CellSelection.N) //only allow setting empty cells
            {
                if (!gameOver)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        grid[i, j] = CellSelection.X;
                        GameEngine engine = new GameEngine(board);
                        if (engine.checkWin() != 0)
                        {
                            if (engine.checkWin() == 1)
                            {
                                MessageBox.Show("You win!");
                                gameOver = true;
                                Invalidate();
                            }
                            else
                            {
                                MessageBox.Show("Computer wins");
                                gameOver = true;
                                Invalidate();
                            }
                        }
                        board[((3 * j) + i)] = 1; //1 means O
                        turncounter++;
                    }
                    if ((turncounter % 2 == 1) && !gameOver)//default turncounter = 0, player goes first
                    {
                        GameEngine engine = new GameEngine(board);
                        if (engine.checkWin() != 0)
                        {
                            if (engine.checkWin() == 1)
                            {
                                Invalidate();
                                MessageBox.Show("You win!");
                                gameOver = true;
                                Invalidate();
                            }
                            else
                            {
                                Invalidate();
                                MessageBox.Show("Computer wins");
                                gameOver = true;
                                Invalidate();
                            }
                        }
                        if (!gameOver && turncounter < 9)
                        {
                            Point move = engine.ComputerMove();
                            grid[move.X, move.Y]           = CellSelection.O;
                            board[((3 * move.Y) + move.X)] = 2;
                            if (engine.checkWin() != 0)
                            {
                                if (engine.checkWin() == 1)
                                {
                                    Invalidate();
                                    MessageBox.Show("You win!");
                                    gameOver = true;
                                    Invalidate();
                                }
                                else
                                {
                                    Invalidate();
                                    MessageBox.Show("Computer wins");
                                    gameOver = true;
                                    Invalidate();
                                }
                            }
                        }
                        turncounter++;
                    }
                }
                else
                {
                    turncounter = 0;
                    for (int ii = 0; ii < 3; ii++)
                    {
                        for (int jj = 0; jj < 3; jj++)
                        {
                            board[((3 * ii) + jj)] = 0;
                            grid[ii, jj]           = CellSelection.N;
                        }
                    }
                    gameOver = false;
                }
            }
            else
            {
                MessageBox.Show("Invalid Move");
            }
            Invalidate();
        }