コード例 #1
0
        public static int[] NextMove(GameMessage gameMessage)
        {
            int[] nextMove = new int[] { 0, 0 };


            int nextMoveScore = -1000;
            int curScore      = 0;



            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (gameMessage.board[i][j] == 0)
                    {
                        if (IsValidMove(gameMessage, i, j, gameMessage.player))
                        {
                            curScore = PlayerScore(gameMessage, i, j);
                            GameMessage oppEval = gameMessage;
                            oppEval.board[i][j] = gameMessage.player;
                            curScore            = curScore + GetBestOppScore(oppEval, i, j, GetOppPlayer(gameMessage));
                            if (curScore > nextMoveScore)
                            {
                                nextMoveScore = curScore;
                                nextMove      = new[] { i, j };
                            }
                        }
                    }
                }
            }



            return(nextMove);
        }
コード例 #2
0
ファイル: AI.cs プロジェクト: JesseKuntz/atomic-games-2018
        public static List <KeyValuePair <int, int> > listOfMoves(GameMessage gameMessage)
        {
            int otherPlayer;

            if (gameMessage.player == 1)
            {
                otherPlayer = 2;
            }
            else
            {
                otherPlayer = 1;
            }

            List <KeyValuePair <int, int> > moves = new List <KeyValuePair <int, int> >();

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (gameMessage.board[i][j] == gameMessage.player)
                    {
                        // UP
                        if (i - 1 >= 0 && gameMessage.board[i - 1][j] == otherPlayer)
                        {
                            int k = i - 2;
                            while (k >= 0 && gameMessage.board[k][j] == otherPlayer)
                            {
                                k--;
                            }
                            if (k >= 0 && gameMessage.board[k][j] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, j)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, j));
                            }
                        }

                        // UP RIGHT
                        if (i - 1 >= 0 && j + 1 <= 7 && gameMessage.board[i - 1][j + 1] == otherPlayer)
                        {
                            int k = i - 2;
                            int l = j + 2;
                            while (k >= 0 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                            {
                                k--;
                                l++;
                            }
                            if (k >= 0 && l <= 7 && gameMessage.board[k][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, l));
                            }
                        }

                        // RIGHT
                        if (j + 1 <= 7 && gameMessage.board[i][j + 1] == otherPlayer)
                        {
                            int l = j + 2;
                            while (l <= 7 && gameMessage.board[i][l] == otherPlayer)
                            {
                                l++;
                            }
                            if (l <= 7 && gameMessage.board[i][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(i, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(i, l));
                            }
                        }

                        // DOWN RIGHT
                        if (i + 1 <= 7 && j + 1 <= 7 && gameMessage.board[i + 1][j + 1] == otherPlayer)
                        {
                            int k = i + 2;
                            int l = j + 2;
                            while (k <= 7 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                            {
                                k++;
                                l++;
                            }
                            if (k <= 7 && l <= 7 && gameMessage.board[k][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, l));
                            }
                        }

                        // DOWN
                        if (i + 1 <= 7 && gameMessage.board[i + 1][j] == otherPlayer)
                        {
                            int k = i + 2;
                            while (k <= 7 && gameMessage.board[k][j] == otherPlayer)
                            {
                                k++;
                            }
                            if (k <= 7 && gameMessage.board[k][j] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, j)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, j));
                            }
                        }

                        // DOWN LEFT
                        if (i + 1 <= 7 && j - 1 >= 0 && gameMessage.board[i + 1][j - 1] == otherPlayer)
                        {
                            int k = i + 2;
                            int l = j - 2;
                            while (k <= 7 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                            {
                                k++;
                                l--;
                            }
                            if (k <= 7 && l >= 0 && gameMessage.board[k][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, l));
                            }
                        }

                        // LEFT
                        if (j - 1 >= 0 && gameMessage.board[i][j - 1] == otherPlayer)
                        {
                            int l = j - 2;
                            while (l >= 0 && gameMessage.board[i][l] == otherPlayer)
                            {
                                l--;
                            }
                            if (l >= 0 && gameMessage.board[i][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(i, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(i, l));
                            }
                        }

                        // UP LEFT
                        if (i - 1 >= 0 && j - 1 >= 0 && gameMessage.board[i - 1][j - 1] == otherPlayer)
                        {
                            int k = i - 2;
                            int l = j - 2;
                            while (k >= 0 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                            {
                                k--;
                                l--;
                            }
                            if (k >= 0 && l >= 0 && gameMessage.board[k][l] == 0 && !moves.Contains(new KeyValuePair <int, int>(k, l)))
                            {
                                moves.Add(new KeyValuePair <int, int>(k, l));
                            }
                        }
                    }
                }
            }
            return(moves);
        }
コード例 #3
0
ファイル: AI.cs プロジェクト: JesseKuntz/atomic-games-2018
        public static int[][] makeMove(GameMessage gameMessage, KeyValuePair <int, int> move)
        {
            int otherPlayer;

            if (gameMessage.player == 1)
            {
                otherPlayer = 2;
            }
            else
            {
                otherPlayer = 1;
            }

            int i = move.Key;
            int j = move.Value;

            // UP
            if (i - 1 >= 0 && gameMessage.board[i - 1][j] == otherPlayer)
            {
                int k = i - 2;
                while (k >= 0 && gameMessage.board[k][j] == otherPlayer)
                {
                    k--;
                }
                if (k >= 0 && gameMessage.board[k][j] == gameMessage.player)
                {
                    k = i - 1;
                    while (k >= 0 && gameMessage.board[k][j] == otherPlayer)
                    {
                        gameMessage.board[k][j] = gameMessage.player;
                        k--;
                    }
                }
                ;
            }

            // RIGHT UP
            if (i - 1 >= 0 && j + 1 <= 7 && gameMessage.board[i - 1][j + 1] == otherPlayer)
            {
                int k = i - 2;
                int l = j + 2;
                while (k >= 0 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                {
                    k--;
                    l++;
                }
                if (k >= 0 && l <= 7 && gameMessage.board[k][l] == gameMessage.player)
                {
                    k = i - 1;
                    l = j + 1;
                    while (k >= 0 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                    {
                        gameMessage.board[k][l] = gameMessage.player;
                        k--;
                        l++;
                    }
                }
                ;
            }

            // Right
            if (j + 1 <= 7 && gameMessage.board[i][j + 1] == otherPlayer)
            {
                int l = j + 2;
                while (l <= 7 && gameMessage.board[i][l] == otherPlayer)
                {
                    l++;
                }
                if (l <= 7 && gameMessage.board[i][l] == gameMessage.player)
                {
                    l = j + 1;
                    while (l <= 7 && gameMessage.board[i][l] == otherPlayer)
                    {
                        gameMessage.board[i][l] = gameMessage.player;
                        l++;
                    }
                }
                ;
            }

            // RIGHT DOWN
            if (i + 1 <= 7 && j + 1 <= 7 && gameMessage.board[i + 1][j + 1] == otherPlayer)
            {
                int k = i + 2;
                int l = j + 2;
                while (k <= 7 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                {
                    k++;
                    l++;
                }
                if (k <= 7 && l <= 7 && gameMessage.board[k][l] == gameMessage.player)
                {
                    k = i + 1;
                    l = j + 1;
                    while (k <= 7 && l <= 7 && gameMessage.board[k][l] == otherPlayer)
                    {
                        gameMessage.board[k][l] = gameMessage.player;
                        k++;
                        l++;
                    }
                }
                ;
            }

            // DOWN
            if (i + 1 <= 7 && gameMessage.board[i + 1][j] == otherPlayer)
            {
                int k = i + 2;
                while (k <= 7 && gameMessage.board[k][j] == otherPlayer)
                {
                    k++;
                }
                if (k <= 7 && gameMessage.board[k][j] == gameMessage.player)
                {
                    k = i + 1;
                    while (k <= 7 && gameMessage.board[k][j] == otherPlayer)
                    {
                        gameMessage.board[k][j] = gameMessage.player;
                        k++;
                    }
                }
                ;
            }

            // LEFT DOWN
            if (i + 1 <= 7 && j - 1 >= 0 && gameMessage.board[i + 1][j - 1] == otherPlayer)
            {
                int k = i + 2;
                int l = j - 2;
                while (k <= 7 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                {
                    k++;
                    l--;
                }
                if (k <= 7 && l >= 0 && gameMessage.board[k][l] == gameMessage.player)
                {
                    k = i + 1;
                    l = j - 1;
                    while (k <= 7 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                    {
                        gameMessage.board[k][l] = gameMessage.player;
                        k++;
                        l--;
                    }
                }
                ;
            }

            // LEFT
            if (j - 1 >= 0 && gameMessage.board[i][j - 1] == otherPlayer)
            {
                int l = j - 2;
                while (l >= 0 && gameMessage.board[i][l] == otherPlayer)
                {
                    l--;
                }
                if (l >= 0 && gameMessage.board[i][l] == gameMessage.player)
                {
                    l = j - 1;
                    while (l >= 0 && gameMessage.board[i][l] == otherPlayer)
                    {
                        gameMessage.board[i][l] = gameMessage.player;
                        l--;
                    }
                }
                ;
            }

            // LEFT UP
            if (i - 1 >= 0 && j - 1 >= 0 && gameMessage.board[i - 1][j - 1] == otherPlayer)
            {
                int k = i - 2;
                int l = j - 2;
                while (k >= 0 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                {
                    k--;
                    l--;
                }
                if (k >= 0 && l >= 0 && gameMessage.board[k][l] == gameMessage.player)
                {
                    k = i - 1;
                    l = j - 1;
                    while (k >= 0 && l >= 0 && gameMessage.board[k][l] == otherPlayer)
                    {
                        gameMessage.board[k][l] = gameMessage.player;
                        k--;
                        l--;
                    }
                }
                ;
            }

            gameMessage.board[i][j] = gameMessage.player;

            return(gameMessage.board);
        }
コード例 #4
0
        public static int PlayerScore(GameMessage gameMessage, int row, int column)
        {
            switch (row)
            {
            case 0:
                if (column == 0 || column == 7)
                {
                    return(100);
                }
                else
                {
                    return(90);
                }

            case 1:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else
                {
                    return(0);
                }

            case 2:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else if (column == 1 || column == 6)
                {
                    return(0);
                }
                else
                {
                    return(50);
                }

            case 3:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else if (column == 1 || column == 6)
                {
                    return(0);
                }
                else
                {
                    return(50);
                }

            case 4:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else if (column == 1 || column == 6)
                {
                    return(0);
                }
                else
                {
                    return(50);
                }

            case 5:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else if (column == 1 || column == 6)
                {
                    return(0);
                }
                else
                {
                    return(50);
                }

            case 6:
                if (column == 0 || column == 7)
                {
                    return(90);
                }
                else
                {
                    return(0);
                }

            case 7:
                if (column == 0 || column == 7)
                {
                    return(100);
                }
                else
                {
                    return(90);
                }

            default:
                return(0);
            }
        }
コード例 #5
0
        public static int GetBestOppScore(GameMessage gameMessage, int row, int column, int player)
        {
            int score = 100;

            if (row + 1 < 8)
            {
                if (IsValidMove(gameMessage, row + 1, column, player))
                {
                    if (score > OppScore(gameMessage, row + 1, column))
                    {
                        score = OppScore(gameMessage, row + 1, column);
                    }
                }
            }
            if (row - 1 >= 0)
            {
                if (IsValidMove(gameMessage, row - 1, column, player))
                {
                    if (score > OppScore(gameMessage, row - 1, column))
                    {
                        score = OppScore(gameMessage, row - 1, column);
                    }
                }
            }
            if (column - 1 >= 0)
            {
                if (IsValidMove(gameMessage, row, column - 1, player))
                {
                    if (score > OppScore(gameMessage, row, column - 1))
                    {
                        score = OppScore(gameMessage, row, column - 1);
                    }
                }
            }
            if (column + 1 < 8)
            {
                if (IsValidMove(gameMessage, row, column + 1, player))
                {
                    if (score > OppScore(gameMessage, row, column + 1))
                    {
                        score = OppScore(gameMessage, row, column + 1);
                    }
                }
            }
            if (row + 1 < 8 && column + 1 < 8)
            {
                if (IsValidMove(gameMessage, row + 1, column + 1, player))
                {
                    if (score > OppScore(gameMessage, row + 1, column + 1))
                    {
                        score = OppScore(gameMessage, row + 1, column + 1);
                    }
                }
            }
            if (row - 1 >= 0 && column - 1 >= 0)
            {
                if (IsValidMove(gameMessage, row - 1, column - 1, player))
                {
                    if (score > OppScore(gameMessage, row - 1, column - 1))
                    {
                        score = OppScore(gameMessage, row - 1, column - 1);
                    }
                }
            }
            if (row + 1 < 8 && column - 1 >= 0)
            {
                if (IsValidMove(gameMessage, row + 1, column - 1, player))
                {
                    if (score > OppScore(gameMessage, row + 1, column - 1))
                    {
                        score = OppScore(gameMessage, row + 1, column - 1);
                    }
                }
            }
            if (row - 1 >= 0 && column + 1 < 8)
            {
                if (IsValidMove(gameMessage, row - 1, column + 1, player))
                {
                    if (score > OppScore(gameMessage, row - 1, column + 1))
                    {
                        score = OppScore(gameMessage, row - 1, column + 1);
                    }
                }
            }
            return(score);
        }
コード例 #6
0
        // Rates a move. TODO: Change -100 to -30
        public static int OppScore(GameMessage gameMessage, int row, int column)
        {
            if (IsValidMove(gameMessage, row, column, gameMessage.player))
            {
                switch (row)
                {
                case 0:
                    if (column == 0 || column == 7)
                    {
                        return(-60);
                    }
                    else
                    {
                        return(-30);
                    }

                case 1:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else
                    {
                        return(30);
                    }

                case 2:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else if (column == 1 || column == 6)
                    {
                        return(+30);
                    }
                    else
                    {
                        return(0);
                    }

                case 3:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else if (column == 1 || column == 6)
                    {
                        return(30);
                    }
                    else
                    {
                        return(0);
                    }

                case 4:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else if (column == 1 || column == 6)
                    {
                        return(30);
                    }
                    else
                    {
                        return(0);
                    }

                case 5:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else if (column == 1 || column == 6)
                    {
                        return(30);
                    }
                    else
                    {
                        return(0);
                    }

                case 6:
                    if (column == 0 || column == 7)
                    {
                        return(-30);
                    }
                    else
                    {
                        return(30);
                    }

                case 7:
                    if (column == 0 || column == 7)
                    {
                        return(-60);
                    }
                    else
                    {
                        return(-30);
                    }

                default:
                    return(0);
                }
            }
            else
            {
                return(0);
            }
        }
コード例 #7
0
        // Checks whether a players move is valid given a row and column
        public static bool IsValidMoveOG(GameMessage gameMessage, int row, int column, int player)
        {
            int oppPlayer;

            if (player == 1)
            {
                oppPlayer = 2;
            }
            else
            {
                oppPlayer = 1;
            }



            if (gameMessage.board[row][column] == 0)
            {
                bool maybeValid = false;

                //Checks for vertical play
                if (row <= 5)
                {
                    for (int i = row + 1; i < 7; i++)
                    {
                        if (gameMessage.board[i][column] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[i][column] == player && maybeValid == true)
                        {
                            Console.WriteLine("93");
                            return(true);
                        }
                        else
                        {
                            maybeValid = false;
                            break;
                        }
                    }
                }
                maybeValid = false;
                //Checks for horizontal play

                if (row >= 2)
                {
                    for (int i = row - 1; i >= 1; i--)
                    {
                        if (gameMessage.board[i][column] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[i][column] == player && maybeValid == true)
                        {
                            Console.WriteLine("116");
                            return(true);
                        }
                        else
                        {
                            maybeValid = false;
                            break;
                        }
                    }
                }
                maybeValid = false;
                if (column <= 5)
                {
                    for (int i = column + 1; i < 7; i++)
                    {
                        if (gameMessage.board[row][i] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[row][i] == player && maybeValid == true)
                        {
                            Console.WriteLine("137");
                            return(true);
                        }
                        else
                        {
                            maybeValid = false;
                            break;
                        }
                    }
                }
                maybeValid = false;
                if (column >= 2)
                {
                    for (int i = column - 1; i >= 1; i--)
                    {
                        if (gameMessage.board[row][i] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[row][i] == player && maybeValid == true)
                        {
                            Console.WriteLine("158");
                            return(true);
                        }
                        else
                        {
                            maybeValid = false;
                            break;
                        }
                    }
                }
                maybeValid = false;
                int rowCheck    = row;
                int columnCheck = column;

                if (row <= 5 && column <= 5)
                {
                    while (rowCheck < 8 && rowCheck >= 0 && columnCheck < 8 && columnCheck >= 0)
                    {
                        rowCheck    = rowCheck++;
                        columnCheck = columnCheck++;
                        if (gameMessage.board[rowCheck][columnCheck] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[rowCheck][columnCheck] == player && maybeValid == true)
                        {
                            Console.WriteLine("180");
                            return(true);
                        }
                        else
                        {
                            maybeValid  = false;
                            rowCheck    = row;
                            columnCheck = column;
                            break;
                        }
                    }
                }
                maybeValid  = false;
                rowCheck    = row;
                columnCheck = column;
                if (row >= 2 && column <= 5)
                {
                    while (rowCheck < 8 && rowCheck >= 0 && columnCheck < 8 && columnCheck >= 0)
                    {
                        rowCheck    = rowCheck--;
                        columnCheck = columnCheck++;
                        if (gameMessage.board[rowCheck][columnCheck] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[rowCheck][columnCheck] == player && maybeValid == true)
                        {
                            Console.WriteLine("211");
                            return(true);
                        }
                        else
                        {
                            maybeValid  = false;
                            rowCheck    = row;
                            columnCheck = column;
                            break;
                        }
                    }
                }
                maybeValid  = false;
                rowCheck    = row;
                columnCheck = column;
                if (row >= 2 && column >= 2)
                {
                    while (rowCheck < 8 && rowCheck >= 0 && columnCheck < 8 && columnCheck >= 0)
                    {
                        rowCheck    = rowCheck--;
                        columnCheck = columnCheck--;
                        if (gameMessage.board[rowCheck][columnCheck] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[rowCheck][columnCheck] == player && maybeValid == true)
                        {
                            Console.WriteLine("238");
                            return(true);
                        }
                        else
                        {
                            maybeValid  = false;
                            rowCheck    = row;
                            columnCheck = column;
                            break;
                        }
                    }
                }
                maybeValid  = false;
                rowCheck    = row;
                columnCheck = column;
                if (row <= 5 && column >= 2)
                {
                    while (rowCheck < 8 && rowCheck >= 0 && columnCheck < 8 && columnCheck >= 0)
                    {
                        rowCheck    = rowCheck++;
                        columnCheck = columnCheck--;
                        if (gameMessage.board[rowCheck][columnCheck] == oppPlayer)
                        {
                            maybeValid = true;
                        }
                        else if (gameMessage.board[rowCheck][columnCheck] == player && maybeValid == true)
                        {
                            Console.WriteLine("265");
                            return(true);
                        }
                        else
                        {
                            maybeValid  = false;
                            rowCheck    = row;
                            columnCheck = column;
                            break;
                        }
                    }
                }
                return(false);
            }
            else
            {
                return(false);
            }
        }
コード例 #8
0
        public static bool IsValidMove(GameMessage gameMessage, int row, int column, int player)
        {
            // Initialize boolean legal as false
            bool legal = false;
            int  oppPlayer;

            if (player == 1)
            {
                oppPlayer = 2;
            }
            else
            {
                oppPlayer = 1;
            }

            // If the cell is empty, begin the search
            // If the cell is not empty there is no need to check anything
            // so the algorithm returns boolean legal as is
            if (gameMessage.board[row][column] == 0)
            {
                // Initialize variables
                int  posX;
                int  posY;
                bool found;
                int  current;

                // Searches in each direction
                // x and y describe a given direction in 9 directions
                // 0, 0 is redundant and will break in the first check
                for (int x = -1; x <= 1; x++)
                {
                    for (int y = -1; y <= 1; y++)
                    {
                        // Variables to keep track of where the algorithm is and
                        // whether it has found a valid move
                        posX = column + x;
                        posY = row + y;

                        found = false;

                        if (posY < 0 || posY > 7 || posX > 7 || posX < 0)
                        {
                            current = -1;
                        }
                        else if (x == 0 && y == 0)
                        {
                            current = -1;
                        }
                        else
                        {
                            current = gameMessage.board[posY][posX];
                        }

                        // Check the first cell in the direction specified by x and y
                        // If the cell is empty, out of bounds or contains the same color
                        // skip the rest of the algorithm to begin checking another direction
                        if (current == oppPlayer)
                        {
                            while (!found)
                            {
                                posX += x;
                                posY += y;
                                if (posY < 0 || posY > 7 || posX > 7 || posX < 0)
                                {
                                    found   = true;
                                    current = -1;
                                }
                                else
                                {
                                    current = gameMessage.board[posY][posX];
                                }

                                // If the algorithm finds another piece of the same color along a direction
                                // end the loop to check a new direction, and set legal to true
                                if (current == gameMessage.player)
                                {
                                    found = true;
                                    Console.WriteLine("I declare x=" + posX + " y=" + posY + " to be a player");
                                    //legal = true;

                                    posX   -= x;
                                    posY   -= y;
                                    current = gameMessage.board[posY][posX];
                                    bool flip = false;
                                    while (current != 0)
                                    {
                                        if (gameMessage.board[posY][posX] == oppPlayer)
                                        {
                                            flip = true;
                                        }
                                        posX -= x;
                                        posY -= y;

                                        current = gameMessage.board[posY][posX];
                                        if (flip == true && current == 0)
                                        {
                                            legal = true;
                                        }
                                    }
                                }
                                // If the algorithm reaches an out of bounds area or an empty space
                                // end the loop to check a new direction, but do not set legal to true yet
                                else if (current != gameMessage.player || current != oppPlayer)
                                {
                                    found = true;
                                    break;
                                }
                                if (current == 0)
                                {
                                    found = true;
                                    break;
                                }
                                if (current == -1)
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(legal);
        }
コード例 #9
0
        public static bool legalMove(int r, int c, int color, bool flip, GameMessage gameMessage)
        {
            // Initialize boolean legal as false
            bool legal = false;

            // If the cell is empty, begin the search
            // If the cell is not empty there is no need to check anything
            // so the algorithm returns boolean legal as is
            if (gameMessage.board[r][c] == 0)
            {
                // Initialize variables
                int  posX;
                int  posY;
                bool found;
                int  current;

                // Searches in each direction
                // x and y describe a given direction in 9 directions
                // 0, 0 is redundant and will break in the first check
                for (int x = -1; x <= 1; x++)
                {
                    for (int y = -1; y <= 1; y++)
                    {
                        // Variables to keep track of where the algorithm is and
                        // whether it has found a valid move
                        posX    = c + x;
                        posY    = r + y;
                        found   = false;
                        current = gameMessage.board[posY][posX];

                        // Check the first cell in the direction specified by x and y
                        // If the cell is empty, out of bounds or contains the same color
                        // skip the rest of the algorithm to begin checking another direction
                        if (current == -1 || current == 0 || current == color)
                        {
                            continue;
                        }

                        // Otherwise, check along that direction
                        while (!found)
                        {
                            posX   += x;
                            posY   += y;
                            current = gameMessage.board[posY][posX];

                            // If the algorithm finds another piece of the same color along a direction
                            // end the loop to check a new direction, and set legal to true
                            if (current == color)
                            {
                                found = true;
                                legal = true;

                                // If flip is true, reverse the directions and start flipping until
                                // the algorithm reaches the original location
                                if (flip)
                                {
                                    posX   -= x;
                                    posY   -= y;
                                    current = gameMessage.board[posY][posX];

                                    while (current != 0)
                                    {
                                        gameMessage.board[posY][posX] = color;
                                        posX   -= x;
                                        posY   -= y;
                                        current = gameMessage.board[posY][posX];
                                    }
                                }
                            }
                            // If the algorithm reaches an out of bounds area or an empty space
                            // end the loop to check a new direction, but do not set legal to true yet
                            else if (current == -1 || current == 0)
                            {
                                found = true;
                            }
                        }
                    }
                }
            }

            return(legal);
        }
コード例 #10
0
        public void testLegalMoves()
        {
            GameMessage gm = new GameMessage();

            gm.board = new int[8][];
            for (int i = 0; i < 8; i++)
            {
                gm.board[i] = new int[8];
            }
            gm.player = 2;

            Board b = new Board(gm);

            Debug.Assert(gm.board.Length == 8);

            b.myBoard[3][3] = 1;
            b.myBoard[3][4] = 2;
            b.myBoard[4][3] = 2;
            b.myBoard[4][4] = 1;
            Console.WriteLine(b);

            //Make sure legalMove works as it should.
            Console.Write("Testing Board.legalMove()... ");

            Console.Write(" 0 ");

            // Placing a piece that doesn't touch any other pieces

            /*
             * for (int i = 0; i < 8; i++)
             * {
             *  Debug.Assert(!b.legalMove(new int[] { 0, i }));
             *  Debug.Assert(!b.legalMove(new int[] { 1, i }));
             *  Debug.Assert(!b.legalMove(new int[] { 6, i }));
             *  Debug.Assert(!b.legalMove(new int[] { 7, i }));
             *
             *  Debug.Assert(!b.legalMove(new int[] { i, 0 }));
             *  Debug.Assert(!b.legalMove(new int[] { i, 1 }));
             *  Debug.Assert(!b.legalMove(new int[] { i, 6 }));
             *  Debug.Assert(!b.legalMove(new int[] { i, 7 }));
             * }
             *
             * Console.Write(" 1 ");
             *
             * // Legal moves
             * Debug.Assert(b.legalMove(new int[] {2,3}));
             * Debug.Assert(b.legalMove(new int[] {3,2}));
             * Debug.Assert(b.legalMove(new int[] {4,5}));
             * Debug.Assert(b.legalMove(new int[] {5,4}));
             *
             * Console.Write(" 2 ");
             *
             * // Remaining Illegal Moves
             * Debug.Assert(!b.legalMove(new int[] {2,2}));
             * Debug.Assert(!b.legalMove(new int[] {2,5}));
             * Debug.Assert(!b.legalMove(new int[] {5,2}));
             * Debug.Assert(!b.legalMove(new int[] {5,5}));
             *
             * Debug.Assert(!b.legalMove(new int[] {4,2}));
             * Debug.Assert(!b.legalMove(new int[] {5,3}));
             * Debug.Assert(!b.legalMove(new int[] {2,4}));
             * Debug.Assert(!b.legalMove(new int[] {3,5}));
             *
             * Console.Write(" 3 ");
             *
             */
            Console.WriteLine("Passed!");
        }
コード例 #11
0
        public void testMakeMove()
        {
            GameMessage gm = new GameMessage();

            gm.board = new int[8][];
            for (int i = 0; i < 8; i++)
            {
                gm.board[i] = new int[8];
            }
            gm.player = 2;

            Board b = new Board(gm);

            Debug.Assert(gm.board.Length == 8);

            b.myBoard[3][3] = 1;
            b.myBoard[3][4] = 2;
            b.myBoard[4][3] = 2;
            b.myBoard[4][4] = 1;
            Console.WriteLine(b);

            Console.Write("Testing Board.makeMove()... ");


            // Turn One
            b.myPlayersTurn = 1;
            b.makeMove(new int[] { 3, 5 });
            Debug.Assert(b.myBoard[3][3] == 1);
            Debug.Assert(b.myBoard[3][4] == 1);
            Debug.Assert(b.myBoard[3][5] == 1);
            Debug.Assert(b.myBoard[4][3] == 2);
            Debug.Assert(b.myBoard[4][4] == 1);

            Console.Write(" 1 ");

            // Turn Two
            b.makeMove(new int[] { 2, 5 });
            Debug.Assert(b.myBoard[3][3] == 1);
            Debug.Assert(b.myBoard[3][5] == 1);
            Debug.Assert(b.myBoard[3][4] == 2);
            Debug.Assert(b.myBoard[4][3] == 2);
            Debug.Assert(b.myBoard[2][5] == 2);
            Debug.Assert(b.myBoard[4][4] == 1);

            Console.Write(" 2 ");

            // Turn 3
            b.makeMove(new int[] { 2, 4 });
            Debug.Assert(b.myBoard[3][3] == 1);
            Debug.Assert(b.myBoard[3][4] == 1);
            Debug.Assert(b.myBoard[3][5] == 1);
            Debug.Assert(b.myBoard[2][4] == 1);
            Debug.Assert(b.myBoard[4][4] == 1);
            Debug.Assert(b.myBoard[4][3] == 2);
            Debug.Assert(b.myBoard[2][5] == 2);

            Console.Write(" 3 ");

            // Turn 4
            // From now on only test items that should have changed, no longer checking if others remained the same.
            b.makeMove(new int[] { 2, 3 });
            Debug.Assert(b.myBoard[2][3] == 2);
            Debug.Assert(b.myBoard[2][4] == 2);
            Debug.Assert(b.myBoard[3][3] == 2);

            Console.Write(" 4 ");

            //Turn 5
            b.makeMove(new int[] { 1, 2 });
            Debug.Assert(b.myBoard[1][2] == 1);
            Debug.Assert(b.myBoard[2][3] == 1);

            Console.Write(" 5 ");

            // Turn 6
            b.makeMove(new int[] { 4, 5 });
            Debug.Assert(b.myBoard[4][5] == 2);
            Debug.Assert(b.myBoard[3][5] == 2);
            Debug.Assert(b.myBoard[4][4] == 2);

            Console.Write(" 6 ");

            // Turn 7
            b.makeMove(new int[] { 5, 6 });
            Debug.Assert(b.myBoard[5][6] == 1);
            Debug.Assert(b.myBoard[4][5] == 1);

            Console.Write(" 7 ");

            // Turn 8 (End State)
            b.makeMove(new int[] { 5, 5 });
            Debug.Assert(b.myBoard[5][5] == 2);
            Debug.Assert(b.myBoard[4][5] == 2);

            Debug.Assert(b.myBoard[1][2] == 1);
            Debug.Assert(b.myBoard[2][3] == 1);
            Debug.Assert(b.myBoard[2][4] == 2);
            Debug.Assert(b.myBoard[2][5] == 2);
            Debug.Assert(b.myBoard[3][3] == 2);
            Debug.Assert(b.myBoard[3][4] == 1);
            Debug.Assert(b.myBoard[3][5] == 2);
            Debug.Assert(b.myBoard[4][3] == 2);
            Debug.Assert(b.myBoard[4][4] == 2);
            Debug.Assert(b.myBoard[5][6] == 1);

            Console.Write(" 8 ");

            Console.Write(" Passed! ");
        }