Пример #1
0
 static void saveGameResult(ChessBoard board)
 {
     int[] resultsArr = board.TraversedPositions.ToArray();
     results.addGameResult(resultsArr);
 }
Пример #2
0
        static int[] determineNextMove(ChessBoard board, Knight knight, int runMethod)
        {
            // Create a list to store all possible next moves
            List <int[]> possibleMoves = new List <int[]>();

            // Loop through Knight's all possible moves
            for (int i = 0; i < knight.Moves.Length / 2; i++)
            {
                // Determine next possible move based on current position
                int   nextPossibleX    = knight.X + knight.Moves[i, 0];
                int   nextPossibleY    = knight.Y + knight.Moves[i, 1];
                int[] nextPossibleMove = { nextPossibleX, nextPossibleY };

                // Add next possible move to List of possible moves if it's available on the board
                if (board.Positions.Any(a => a.SequenceEqual(nextPossibleMove)))
                {
                    possibleMoves.Add(nextPossibleMove);
                }
            }

            // Return next move based on game approach (non-intelligent/intelligent)
            switch (runMethod)
            {
            case 1:
                // Generate random number based on number of possible moves
                int randomIndex = random.Next(0, possibleMoves.Count);

                // If any moves are possible, return a random one from the list of possible moces
                if (possibleMoves.Count > 0)
                {
                    return(possibleMoves[randomIndex]);
                }
                break;

            case 2:
                // If any moves possible
                if (possibleMoves.Count > 0)
                {
                    // GET ACCESSIBILTY VALUES OF ALL BOARD POSITIONS REACHABLE BY THE KNIGHT
                    List <int> accessValsBasedOnIndexes = new List <int>();
                    int        smallestIndex;

                    // Loop through possible moves
                    for (int i = 0; i < possibleMoves.Count; i++)
                    {
                        // Get relative board index of the possible move
                        int indexOfPossibleMove = board.Positions.FindIndex(possibleMoves[i].SequenceEqual);
                        // Add accessibilty matrix value of that board position into the list
                        accessValsBasedOnIndexes.Add(board.PositionsAccessibilty[indexOfPossibleMove]);
                    }

                    // DETERMINE ACCESSIBLE BOARD POSITION BASED ON SMALLEST ACCESSIBILTY VALUE
                    smallestIndex = accessValsBasedOnIndexes[0];

                    // Loop through all acquired accessibilty values to find the smallest value
                    for (int i = 0; i < accessValsBasedOnIndexes.Count; i++)
                    {
                        // If accessibilty value is smaller than currently assigned one,
                        // change it to current accessibilty value
                        if (accessValsBasedOnIndexes[i] < smallestIndex)
                        {
                            smallestIndex = accessValsBasedOnIndexes[i];
                        }
                    }

                    // Loop through all acquired accessibilty values to find all values equal to the
                    // smallest accessibilty value and add their List indexes to the list
                    List <int> indexesOfSmallestValues = new List <int>();
                    for (int i = 0; i < accessValsBasedOnIndexes.Count; i++)
                    {
                        if (accessValsBasedOnIndexes[i] == smallestIndex)
                        {
                            indexesOfSmallestValues.Add(i);
                        }
                    }

                    // Get randomly selected index of one of the smallest accessibility values
                    int randomVal     = random.Next(0, indexesOfSmallestValues.Count);
                    int selectedIndex = indexesOfSmallestValues[randomVal];

                    // Return the move with the randomly selected smallest accessibilty value
                    return(possibleMoves[selectedIndex]);
                }
                break;

            default:
                break;
            }

            // If no moves were possible, return and out of bounds move
            return(new int[] { 9, 9 });
        }