Ejemplo n.º 1
0
        private void PlayAi()
        {
            if (!Game.GetInstance().TheEnd)
            {
//                Cursor = Cursors.WaitCursor;
//                loadingBox.Visible = true;
//                loadingBox.Refresh();

                int points = _players[_game.PlayerTurn].Play(new Game.Move(-1, -1));

                if (points == 0)
                {
                    // ai player cannot play, passes his turn

                    Game.GetInstance().InvertTurn();
                    Game.GetInstance().Turn++;
                }
                else
                {
                    _players[_game.PlayerTurn].Score += points + 1;

                    Game.GetInstance().InvertTurn();
                    Game.GetInstance().Turn++;

                    _players[_game.PlayerTurn].Score -= points;

//                    DrawBoard();
                }

                if (_game.NbPossibleMoves(_game.Board, _players[_game.PlayerTurn].ColorPlayer) == 0)
                {
                    if (_game.Block || _game.BoardFull(_game.Board))
                    {
                        _game.Winner(_players[0], _players[1]);
                    }
                    else
                    {
                        _game.Block = true;
                        MessageBox.Show(string.Format("Player {0} cannot play!", _players[_game.PlayerTurn].ColorPlayer));

                        Game.GetInstance().InvertTurn();
                        Game.GetInstance().Turn++;

                        PlayAi();
                    }
                }

//                loadingBox.Visible = false;
//                loadingBox.Refresh();
//                Cursor = Cursors.Default;
            }
            else
            {
                Game.GetInstance().Winner(_players[0], _players[1]);
            }
        }
Ejemplo n.º 2
0
        private void Square_Click(object sender, EventArgs e)
        {
            PictureBox pictureBox = sender as PictureBox;

            if (!Game.GetInstance().TheEnd&& pictureBox != null)
            {
                Game.Move move = new Game.Move(-1, -1);

                string[] coords = pictureBox.Tag.ToString().Split(';');

                move._column = Convert.ToInt32(coords[0]);
                move._row    = Convert.ToInt32(coords[1]);

                if (move._column >= 0 && move._column < 8 && move._row >= 0 && move._row < 8)
                {
                    if (_game.IsSquarePlayable(_game.Board, _players[_game.PlayerTurn].ColorPlayer, move))
                    {
                        int points = Game.GetInstance()
                                     .Play(ref Game.GetInstance().Board, _players[_game.PlayerTurn].ColorPlayer, move);

                        if (points > 0)
                        {
                            UpdateHistory(move);

                            _players[_game.PlayerTurn].Score += points + 1;

                            Game.GetInstance().InvertTurn();
                            Game.GetInstance().Turn++;

                            _players[_game.PlayerTurn].Score -= points;

                            DrawBoard();
                        }
                        else
                        {
                            Game.GetInstance().InvertTurn();
                            Game.GetInstance().Turn++;
                        }

                        if (_game.BoardFull(_game.Board))
                        {
                            _game.TheEnd = true;
                        }

                        PlayAi();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public Form1()
        {
            InitializeComponent();
            AppCenter.Start("c19c0fce-cc65-4b15-880e-456344e83cb0", typeof(Analytics));

            lbPastHistory = new ListBox {
                Parent = this, Width = 100, Height = 244, Top = 135, Left = 393
            };
            Controls.Add(lbPastHistory);

            loadingBox.Image   = Resources.loading;
            loadingBox.Visible = false;

            tbPlayer1.Text = "Black";
            tbPlayer2.Text = "White";

            _game = Game.GetInstance();

            tabPictureBox = new PictureBox[8, 8];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    tabPictureBox[i, j] = new PictureBox
                    {
                        Parent = panelBoard,
                        Width  = 45,
                        Height = 45,
                        Left   = j * 45,
                        Top    = i * 45,
                        Name   = i + ";" + j,
                        Tag    = i + ";" + j
                    };

                    tabPictureBox[i, j].Click      += Square_Click;
                    tabPictureBox[i, j].MouseEnter += PictureBox_MouseEnter;
                    tabPictureBox[i, j].MouseLeave += PictureBox_MouseLeave;
                }
            }

            _players = new Player[2];

            NewGame(new HumanPlayer(Black), new AIPlayer(White, 4), 1);

            cbDisplayAvailableMoves.Checked = true;
        }
Ejemplo n.º 4
0
        public Form1()
        {
            InitializeComponent();

            loadingBox.Image   = Resources.loading;
            loadingBox.Visible = false;

            tbPlayer1.Text = "Black";
            tbPlayer2.Text = "White";

            _game = Game.GetInstance();

            tabPictureBox = new PictureBox[8, 8];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    tabPictureBox[i, j] = new PictureBox
                    {
                        Parent = panelBoard,
                        Width  = 45,
                        Height = 45,
                        Left   = j * 45,
                        Top    = i * 45,
                        Name   = i + ";" + j,
                        Tag    = i + ";" + j
                    };

                    tabPictureBox[i, j].Click      += Square_Click;
                    tabPictureBox[i, j].MouseEnter += PictureBox_MouseEnter;
                    tabPictureBox[i, j].MouseLeave += PictureBox_MouseLeave;
                }
            }

            _players = new Player[2];

            NewGame(new HumanPlayer(Black), new AIPlayer(White, 4), 1);

            cbDisplayAvailableMoves.Checked = true;
        }
Ejemplo n.º 5
0
        internal Game.Move BestMove(int[,] board, int player, int depth, bool block, bool difficult)
        {
            Game.Move bestMove = new Game.Move(-1, -1);
            Game.Move eMove    = new Game.Move(-1, -1);

            bestMove._column = -1;
            bestMove._row    = -1;

            int mval = mini; // best move value

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    //int[,] boardTemp = CopyArray(board);

                    eMove._column = i;
                    eMove._row    = j;

                    int nbMoves = Game.GetInstance().NbPossibleMoves(board, player);

                    if (Game.GetInstance().IsSquarePlayable(board, player, eMove))
                    {
                        var val = EvalMin(board, player, GetOpponent(player), eMove, depth, mini, depth % 4, block, difficult); // value of the tested move

                        //TestBoardForm testForm = new TestBoardForm(board, val, eMove);
                        //testForm.Show();

                        if (val > mval)
                        {
                            bestMove._column = eMove._column;
                            bestMove._row    = eMove._row;
                            mval             = val;
                        }
                    }
                }
            }

            return(bestMove);
        }
Ejemplo n.º 6
0
        internal Game.Move ChooseFirstMove(int[,] board, int color)
        {
            Game.Move move = new Game.Move(-1, -1);

            List <Game.Move> listOfMoves = new List <Game.Move>();

            for (int i = 2; i < 5; i++)
            {
                for (int j = 2; j < 5; j++)
                {
                    move._column = i;
                    move._row    = j;
                    if (Game.GetInstance().IsSquarePlayable(board, color, move))
                    {
                        listOfMoves.Add(move);
                    }
                }
            }

            Random rnd = new Random();
            int    r   = rnd.Next(listOfMoves.Count);

            return(listOfMoves[r]);
        }
Ejemplo n.º 7
0
        internal override int Play(Game.Move move, out Game.Move returnedMove)
        {
            returnedMove = new Game.Move(move._column, move._row);
            int points = 0;

            if (Game.GetInstance().BoardFull(Game.GetInstance().Board))
            {
                Game.GetInstance().TheEnd = true;
            }
            else
            {
                if (Game.GetInstance().Turn == 1)
                {
                    // 1st turn
                    move = MinMax.GetInstance().ChooseFirstMove(Game.GetInstance().Board, ColorPlayer);
                }
                else if (Game.GetInstance().Turn == 2)
                {
                    // 2nd turn
                    move = MinMax.GetInstance().BestSecondMove(Game.GetInstance().Board, ColorPlayer);
                }
                else
                {
                    switch (Game.GetInstance().Difficulty)
                    {
                    case 1:
                        move = MinMax.GetInstance().BestMove(Game.GetInstance().Board, ColorPlayer, Depth, Game.GetInstance().Block, false);
                        break;

                    case 2:
                        move = MinMax.GetInstance().BestMove(Game.GetInstance().Board, ColorPlayer, Depth, Game.GetInstance().Block, false);
                        break;

                    case 3:
                        move = MinMax.GetInstance().BestMove(Game.GetInstance().Board, ColorPlayer, Depth, Game.GetInstance().Block, true);
                        break;
                    }
                }

                if (move._row == -1)
                {
                    if (Game.GetInstance().Block)
                    {
                        Game.GetInstance().TheEnd = true;
                    }
                    else
                    {
                        Game.GetInstance().Block = true;
                        MessageBox.Show(string.Format("{0} cannot play!", Name));
                    }
                }
                else
                {
                    Game.GetInstance().Block = false;

//                    MessageBox.Show(string.Format("Best move: col:{0} - row:{1}", move._column, move._row));

                    points = Game.GetInstance().Play(ref Game.GetInstance().Board, ColorPlayer, move);
                }
            }

            returnedMove = move;
            return(points);
        }
Ejemplo n.º 8
0
 public bool PlayPiece(int row, int column) => Game.GetInstance().Board.PlaceColor(row, column, Color);
Ejemplo n.º 9
0
 public int GetScore() => Game.GetInstance().Board.GetScoreForColor(Color);
Ejemplo n.º 10
0
 private bool LastMove(int[,] board, int opponent, bool block, Game.Move movePlayed)
 {
     return(((block) && movePlayed._column == -1) || (Game.GetInstance().BoardFull(board)));
 }
Ejemplo n.º 11
0
        private int EvalMin(int[,] board, int player, int opponent, Game.Move move, int depth, int beta,
                            int profMobilite, bool block, bool difficult)
        {
            int[,] modifiedBoard = CopyArray(board);
            Game.Move eMove = new Game.Move(-1, -1);

            if (move._column != -1)
            {
                Game.GetInstance().PlayNew(board, out modifiedBoard, player, move);
            }

            if (LastMove(modifiedBoard, player, block, move))
            {
                return(EvalFin(modifiedBoard, player, move));
            }
            else
            {
                if (depth == 0)
                {
                    return(Eval(modifiedBoard, player));
                }
                else
                {
                    if (move._row == -1)
                    {
                        block = true;
                    }

                    int i   = 0;
                    int val = maxi;
                    while (i < 8 && val > beta)
                    {
                        eMove._column = i;
                        int j = 0;

                        while (j < 8 && val > beta)
                        {
                            eMove._row = j;
                            if (Game.GetInstance().IsSquarePlayable(modifiedBoard, opponent, eMove))
                            {
                                val = Math.Min(val,
                                               EvalMax(modifiedBoard, player, opponent, eMove, depth - 1, val, profMobilite, block,
                                                       difficult));
                            }
                            j++;
                        }
                        i++;
                    }

                    if (val == maxi)
                    {
                        val = EvalMax(modifiedBoard, player, opponent, _coupNul, depth - 1, val, profMobilite, block, difficult);
                    }
                    if (depth == profMobilite)
                    {
                        val = val - Game.GetInstance().NbPossibleMoves(modifiedBoard, opponent);
                        if (difficult)
                        {
                            val = val + Game.GetInstance().NbPossibleMoves(modifiedBoard, player);
                        }
                    }

                    return(val);
                }
            }
        }