Beispiel #1
0
        private int[] dbRetrieveMove(string boardState)
        {
            // String to select the entry in the data base with the boardstate if it exists.
            string selectSQL = "SELECT TOP 1 * FROM ChessTable WHERE BoardState='" + boardState + "' AND Depth ='" + BoardDepth + "'";

            // Get the DataTable resulting from the select query.
            DataTable tbl = ChessDB.GetDataTable(selectSQL);

            // Initialize bestMove.
            int[] bestMove = new int[2]; bestMove[0] = 0; bestMove[1] = 0;

            // Condtion to test if the query had a result.
            if (tbl.Rows.Count != 0)
            {
                // Get the Moves string.
                string   dbMoves   = tbl.Rows[0]["Moves"].ToString();
                string[] MoveSplit = dbMoves.Split("Black".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                string   moveS;

                // Condition to test which players turn it is.
                if (ChessBoard.BoardState.WhitesMove)
                {
                    // Declare and initialize moveS to be a possible white move.
                    moveS = MoveSplit[0].Trim("White ".ToCharArray());
                }
                else
                {
                    // Declare and initialize moveS to be a possible black move.
                    moveS = MoveSplit[1].Trim();
                }
                // Conditon to test if moveS has a move.
                if (moveS.Length > 0)
                {
                    // Set bestMove to the move found in the database.
                    int[] Moves = moveS.Split(' ').Select(m => int.Parse(m)).ToArray();

                    string moveStr = "Player: " + ChessBoard.BoardState.WhitesMove + " bestMoves ";

                    foreach (int i in Moves)
                    {
                        moveStr += i + " ";
                    }

                    Console.WriteLine(moveStr);

                    // Delare Random to find a random best move.
                    Random rng = new Random();

                    // Get a random index of a best move.
                    int index = rng.Next(Moves.Length / 2);

                    // Set the best move.
                    bestMove[0] = Moves[(index * 2)]; bestMove[1] = Moves[(index * 2) + 1];

                    // Return bestMove.
                    return(bestMove);
                }
            }
            return(bestMove);// Return bestMove.
        }
Beispiel #2
0
        // Method to update the Chess Database.
        private string dbUpdateAddRecord(string boardState, int Depth, string Moves)
        {
            // String to select the entry in the data base with the boardstate if it exists.
            string selectSQL = "SELECT TOP 1 * FROM ChessTable WHERE BoardState='" + boardState + "' AND Depth ='" + Depth + "'";

            // Get the DataTable resulting from the select query.
            DataTable tbl = ChessDB.GetDataTable(selectSQL);

            // Declare insertSQL string to hold the insert query.
            string insertSQL = "";

            // Condition to test if the data table has entries. The boardState has been entered before.
            if (tbl.Rows.Count == 0)
            {
                // Condition to test whos move is being entered.
                if (ChessBoard.BoardState.WhitesMove)
                {
                    insertSQL = "INSERT INTO ChessTable(BoardState, Depth, Moves) VALUES('" + boardState + "', '" + Depth + ("', 'White " + Moves + "Black") + "')";  // Insert White move.
                }
                else
                {
                    insertSQL = "INSERT INTO ChessTable(BoardState, Depth, Moves) VALUES('" + boardState + "', '" + Depth + ("', 'White " + "Black " + Moves) + "')";  // Insert Black move.
                }

                // Execute the INSERT.
                ChessDB.ExecuteSQL(insertSQL);

                tbl = ChessDB.GetDataTable(selectSQL);

                return(tbl.Rows[0]["Moves"].ToString());
            }
            else
            {
                // Get the Moves string to be updated.
                string dbMoves = tbl.Rows[0]["Moves"].ToString();
                // Declare MoveSplit string array to hold the split of the two players moves.
                string[] MoveSplit = dbMoves.Split("Black".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                // Declare string to hold the SQL UPDATE query
                string updateSQL = "";

                // Condtion to test whos move will be updated.
                if (ChessBoard.BoardState.WhitesMove)
                {
                    updateSQL = "UPDATE ChessTable SET Moves = '" + ("White " + Moves + " Black" + MoveSplit[1]) + "' WHERE BoardState = '" + boardState + "'"; // UPDATE White move.
                    Console.WriteLine($"White Move UPDATE: {("White " + Moves + " Black" + MoveSplit[1])}");
                }
                else
                {
                    updateSQL = "UPDATE ChessTable SET Moves = '" + (MoveSplit[1] + "Black " + Moves) + "' WHERE BoardState = '" + boardState + "'"; // UPDATE Black move.
                    Console.WriteLine($"Black Move UPDATE: {(MoveSplit[1] + "Black " + Moves)}");
                }
            }

            return(tbl.Rows[0]["Moves"].ToString());
        }