示例#1
0
 public Connect4Board(Connect4Board oldBoard)
 {
     cols = oldBoard.GetNrColumns();
     rows = oldBoard.GetNrRows();
     board = new int[cols, rows];
     for (int i = 0; i < cols; i++)
     {
         for (int j = 0; j < rows; j++)
         {
             board[i, j] = oldBoard.GetDisc(i, j);
         }
     }
 }
示例#2
0
        public int CheckForWin(Connect4Board b)
        {
            int piece;
            //Checking vertically
            for (int i = 0; i < b.GetNrColumns(); i++)
            {
                for (int j = 0; j < b.GetNrRows()-3; j++)
                {
                    if ((piece = b.GetDisc(i, j)) != 0)
                    {
                        if (piece == b.GetDisc(i, j+1))
                        {
                            if (piece == b.GetDisc(i, j+2))
                            {
                                if (piece == b.GetDisc(i, j+3)) return piece;

                                else j += 2;
                            }
                            else j += 1;
                        }
                    }
                }
            }
            //Checking horizontally
            for (int j = 0; j < b.GetNrRows(); j++)
            {
                for (int i = 0; i < b.GetNrColumns()-3; i++)
                {
                    if ((piece = b.GetDisc(i, j)) != 0)
                    {
                        if (piece == b.GetDisc(i+1, j))
                        {
                            if (piece == b.GetDisc(i+2, j))
                            {
                                if (piece == b.GetDisc(i+3, j)) return piece;

                                else i += 2;
                            }
                            else i += 1;
                        }
                    }
                }
            }

            //Checking diagonally
            for (int i = 0; i < b.GetNrColumns()-3; i++)
            {
                for (int j = 0; j < b.GetNrRows()-3; j++)
                {
                    if ((piece = b.GetDisc(i, j)) != 0)
                    {
                        if (piece == b.GetDisc(i + 1, j + 1) && piece == b.GetDisc(i + 2, j + 2) && piece == b.GetDisc(i + 3, j + 3)) return piece;
                    }
                    if ((piece = b.GetDisc(i+3, j)) != 0)
                    {
                        if (piece == b.GetDisc(i + 2, j + 1) && piece == b.GetDisc(i + 1, j + 2) && piece == b.GetDisc(i, j + 3)) return piece;
                    }
                }
            }
            return 0;
        }
示例#3
0
        public double EvalSingle(Connect4Board b, int i, int j, int iplus, int jplus, int player)
        {
            int count = 0;
            bool opp = false;
            for (int x = 0; x < 4; x++)
            {
                int piece = b.GetDisc(i + iplus * x, j + jplus * x);
                if (piece == 3 - player) return 0.0;
                else if (piece == player) count++;
            }

            if (count == 2) return 5.0;
            else if (count == 3) return 25.0 + IsGoodTriple(b, i, j, iplus, jplus, player);
            else if (count == 4) return 2500.0;
            else return 0.0;
        }
示例#4
0
        public double IsGoodTriple(Connect4Board b, int i, int j, int iplus, int jplus, int player)
        {
            if (iplus == 0) return 0.0; // if vertical, can't be good triple
            int maxj = b.GetNrRows() - 1;
            if (j == maxj) return 0.0; // if bottom line can't be good triple

            int gap = -1;
            for (int x = 0; x < 4; x++)
            {
                int piece = b.GetDisc(i + iplus * x, j + jplus * x);
                if (piece == 3 - player) return 0.0; // make sure there's a gap and not another player
                if (piece == 0) gap = x; // get position of gap piece
            }

            if (gap == -1) return 0.0; // making sure nothing went wrong

            if (j + jplus * gap + 1 <= maxj && b.GetDisc(i + iplus * gap, j + jplus * gap + 1) == 0) // if piece below is within boundaries and is missing 
            {
                if (j + jplus * gap + 2 <= maxj && b.GetDisc(i + iplus * gap, j + jplus * gap + 2) == 0) return 100.0; // if piece below that is also within boundaries and is missing, return good score
                else return 200.0; // else return great score
            }
            return 0.0; // return nothing otherwise
        }
示例#5
0
 public Connect4Board(Connect4Board oldBoard)
 {
     for (int i = 0; i < cols; i++)
     {
         for (int j = 0; j < rows; j++)
         {
             board[i, j] = oldBoard.GetDisc(i, j);
         }
     }
 }