Example #1
0
        internal override int Play(object theMove)
        {
            Game.Move move = (Game.Move)theMove;
            _points = 0;

            if (Game.GetInstance().BoardFull(Game.GetInstance().Board))
            {
                Game.GetInstance().TheEnd = true;
            }
            else
            {
                bool canPlay = Game.GetInstance().CanPlay(Game.GetInstance().Board, ColorPlayer);

                if (!canPlay)
                {
                    if (Game.GetInstance().Block)
                    {
                        Game.GetInstance().TheEnd = true;
                    }
                    else
                    {
                        Game.GetInstance().Block = true;
                        MessageBox.Show(string.Format("Player {0} cannot play!", ColorPlayer));
                    }
                }
                else
                {
                    Game.GetInstance().Block = false;
                    //points = Game.GetInstance().Play(ref Game.GetInstance().Board, ColorPlayer, move);
                }
            }

            return(_points);
        }
Example #2
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
            {
                bool canPlay = Game.GetInstance().CanPlay(Game.GetInstance().Board, ColorPlayer);

                if (!canPlay)
                {
                    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;
                    //points = Game.GetInstance().Play(ref Game.GetInstance().Board, ColorPlayer, move);
                }
            }
            return(points);
        }
Example #3
0
        internal Game.Move BestSecondMove(int[,] board, int color)
        {
            int min = 60;
            int mobility;

            Game.Move bestMove = new Game.Move(-1, -1);
            Game.Move eMove    = new Game.Move(-1, -1);

            for (int i = 1; i < 7; i++)
            {
                for (int j = 1; j < 7; j++)
                {
                    eMove._column = i;
                    eMove._row    = j;
                    if (Game.GetInstance().IsSquarePlayable(board, color, eMove))
                    {
                        int[,] boardTemp2 = CopyArray(board);
                        //board.CopyTo(boardTemp2, 0);

                        Game.GetInstance().Play(ref boardTemp2, color, eMove);
                        mobility = Game.GetInstance().NbPossibleMoves(boardTemp2, color);
                        if (mobility < min)
                        {
                            min = mobility;
                            bestMove._column = eMove._column;
                            bestMove._row    = eMove._row;
                        }
                    }
                }
            }

            return(bestMove);
        }
Example #4
0
        private int EvalFin(int[,] board, int playerColor, Game.Move movePlayed)
        {
            int res = 0;

            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    if (board[i, j] != Game.EmptyCell)
                    {
                        if (board[i, j] == playerColor)
                        {
                            res++;
                        }
                        else
                        {
                            res--;
                        }
                    }
                }
            }

            if (res > 0)
            {
                return(victoire);
            }
            if (res == 0)
            {
                return(matchnul);
            }
            return(defaite);
        }
Example #5
0
        public TestBoardForm(int[,] boardTemp, int val, Game.Move eMove)
        {
            InitializeComponent();
            _board = boardTemp;

            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;
                }
            }

            lblCol.Text = eMove._column.ToString();
            lblRow.Text = eMove._row.ToString();
            lblVal.Text = val.ToString();

            DrawBoard();
        }
Example #6
0
        private int EvalMax(int[,] board, int player, int opponent, Game.Move move, int depth, int alpha,
                            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, opponent, move);
            }

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

                    int i   = 0;
                    int val = mini;
                    while (i < 8 && val < alpha)
                    {
                        eMove._column = i;
                        int j = 0;

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

                    if (val == mini)
                    {
                        val = EvalMin(modifiedBoard, player, opponent, _coupNul, depth - 1, val, profMobilite, block, difficult);
                    }

                    return(val);
                }
            }
        }
Example #7
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();
                    }
                }
            }
        }
Example #8
0
        public void UpdateHistory(Game.Move moveToConvert)
        {
            const string COLS = "ABCDEFGH";

            var currentPlayer = _players[_game.PlayerTurn].ColorPlayer == 1 ? "Black" : "White";

            if (moveToConvert._column == -1)
            {
                lbPastHistory.Items.Add(string.Format("{0} passed", currentPlayer));
            }
            else
            {
                char col = COLS[moveToConvert._row];

                lbPastHistory.Items.Add(string.Format("{0} {1}-{2}", currentPlayer, col, moveToConvert._column + 1));
            }

            int visibleItems = lbPastHistory.ClientSize.Height / lbPastHistory.ItemHeight;

            lbPastHistory.TopIndex = Math.Max(lbPastHistory.Items.Count - visibleItems + 1, 0);
        }
Example #9
0
        void PictureBox_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pictureBox = sender as PictureBox;

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

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

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

                statusLabel.Text = string.Format("{0}-{1}", move._row, move._column);

                if (cbDisplayAvailableMoves.Checked && _game.IsSquarePlayable(_game.Board, _players[_game.PlayerTurn].ColorPlayer, move))
                {
                    pictureBox.Image = Resources.AvailableMove;
                }
            }
        }
Example #10
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);
        }
Example #11
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]);
        }
Example #12
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);
        }
Example #13
0
 internal abstract int Play(Game.Move move, out Game.Move returnedMove);
Example #14
0
 private bool LastMove(int[,] board, int opponent, bool block, Game.Move movePlayed)
 {
     return(((block) && movePlayed._column == -1) || (Game.GetInstance().BoardFull(board)));
 }