Beispiel #1
0
        public void RandomizeValue_HasRandomizedCorrectly_Equal()
        {
            int    modifyProcentage = 20;
            int    seed             = 1337;
            Random randomNumber     = new Random(seed);

            double expectedNumber = ((double)(randomNumber.Next(-modifyProcentage, modifyProcentage)) / 100);

            double resultNumber = Bayes.RandomizeValue(modifyProcentage, seed);

            Assert.Equal(expectedNumber, resultNumber);
        }
Beispiel #2
0
        public void ReadLinesFromTxtFile_HasReadFromFileCorreclty_Equal()
        {
            string fileName = "TxtFile.txt";

            using (File.Create(fileName)) { }
            string path = Path.GetFullPath(fileName);

            string[] expected = { "1 2 3 4", "5 6 7 8", "9 10 11 12", "13 14 15 16" };
            File.WriteAllLines(path, expected);

            string[] result = Bayes.ReadLinesFromTxtFile(path);
            File.Delete(fileName);

            Assert.Equal(expected, result);
        }
Beispiel #3
0
        public void HandleEvalConfig_HasHandledEvalConfigCorrect_Equal()
        {
            string fileName = "EvalConfigTest.txt";

            using (File.Create(fileName)) { }
            string path = Path.GetFullPath(fileName);

            string[] textLines = { "false", "20", "1337", "10", "15" };
            File.WriteAllLines(fileName, textLines);
            int Depth;

            Bayes.HandleEvalConfig(out Depth, path);

            File.Delete(path);

            Assert.Equal(Convert.ToBoolean(textLines[0]), Bayes.TuneEvalValues);
            Assert.Equal(textLines[1], Bayes.TuningBounderies.ToString());
            Assert.Equal(textLines[2], Bayes.Seed.ToString());
            Assert.Equal(textLines[3], Bayes.RepetetionPerEvalConfiguration.ToString());
            Assert.Equal(textLines[4], Depth.ToString());
        }
Beispiel #4
0
        public void RandomizeEvalConfiguration_HasRandomizedCorrectly_Equal()
        {
            string fileName = "EvalConfigTest.txt";

            using (File.Create(fileName)) { }
            string path = Path.GetFullPath(fileName);

            string[] textLines = { "true", "20", "1337", "10", "15" };
            File.WriteAllLines(fileName, textLines);
            int Depth;

            Bayes.HandleEvalConfig(out Depth, path);

            Assert.True(Bayes.TuneEvalValues);
            Assert.Equal(20, Bayes.TuningBounderies);
            Assert.Equal(1337, Bayes.Seed);
            Assert.Equal(10, Bayes.RepetetionPerEvalConfiguration);
            Assert.Equal(15, Depth);

            Random randomGenerator = new Random(Bayes.Seed);

            #region resultString
            ChessBoard board = new ChessBoard();
            board.InitializeGame();
            string result;
            result = Eval.BasePawnValue.ToString() + " " + Eval.BaseBishopValue.ToString() + " " + Eval.BaseKnightValue.ToString() + " " + Eval.BaseRookValue + " " +
                     Eval.BaseQueenValue.ToString() + " " + Eval.BaseKingValue.ToString() + " " + Eval.CheckMateConstant.ToString() + " " + Eval.LateGameBonusRooks.ToString() + " " +
                     Eval.LateGameBonusKnights.ToString() + " " + Eval.LateGameBonusBishop.ToString() + " " + Eval.FactorRook.ToString() + " " + Eval.FactorBishop.ToString() + " " +
                     Eval.FactorKnight.ToString() + " ";


            foreach (int i in board.WhitePawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKing.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackPawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKing.PositionValues)
            {
                result += i + " ";
            }
            #endregion

            string[] ToBeModifiedConfigurationString = result.Split(' ');
            string   modifiedString = null;

            foreach (string valueString in ToBeModifiedConfigurationString)
            {
                if (valueString != string.Empty)
                {
                    double value = Convert.ToDouble(valueString);
                    value           = value * (1 - ((double)(randomGenerator.Next(-Bayes.TuningBounderies, Bayes.TuningBounderies)) / 100));
                    modifiedString += value + " ";
                }
            }

            Assert.Equal(modifiedString, Bayes.RandomizeEvalConfiguration(result));
        }
Beispiel #5
0
        public void FindBestEvalConfiguration_HasFoundTheBest_Equal()
        {
            string fileName = "TxtFile.txt";

            using (File.Create(fileName)) { }
            #region ResultString
            ChessBoard board = new ChessBoard();
            board.InitializeGame();
            string result;
            result = Eval.BasePawnValue.ToString() + " " + Eval.BaseBishopValue.ToString() + " " + Eval.BaseKnightValue.ToString() + " " + Eval.BaseRookValue + " " +
                     Eval.BaseQueenValue.ToString() + " " + Eval.BaseKingValue.ToString() + " " + Eval.CheckMateConstant.ToString() + " " + Eval.LateGameBonusRooks.ToString() + " " +
                     Eval.LateGameBonusKnights.ToString() + " " + Eval.LateGameBonusBishop.ToString() + " " + Eval.FactorRook.ToString() + " " + Eval.FactorBishop.ToString() + " " +
                     Eval.FactorKnight.ToString() + " ";


            foreach (int i in board.WhitePawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKing.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackPawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKing.PositionValues)
            {
                result += i + " ";
            }
            #endregion
            string   path      = Path.GetFullPath(fileName);
            string[] textLines = { "111 False " + result, "111 False " + result, "111 False " + result, "111 False " + result, "111 False " + result, "111 True " + result, "111 True " + result, "111 True " + result, "111 True " + result, "111 True " + result,
                                   "222 False " + result, "222 False " + result, "222 False " + result, "222 False " + result, "222 True " + result,  "222 True " + result, "222 True " + result, "222 True " + result, "222 True " + result, "222 True " + result };

            File.WriteAllLines(path, textLines);

            Assert.Equal(result, Bayes.FindBestEvalConfiguration(path));
            File.Delete(fileName);
        }
Beispiel #6
0
        public void CreateHashFromEvalConfigurationString_HasCreatedTheRightHash_Equal()
        {
            #region ResultString
            ChessBoard board = new ChessBoard();
            board.InitializeGame();
            string result;
            result = Eval.BasePawnValue.ToString() + " " + Eval.BaseBishopValue.ToString() + " " + Eval.BaseKnightValue.ToString() + " " + Eval.BaseRookValue + " " +
                     Eval.BaseQueenValue.ToString() + " " + Eval.BaseKingValue.ToString() + " " + Eval.CheckMateConstant.ToString() + " " + Eval.LateGameBonusRooks.ToString() + " " +
                     Eval.LateGameBonusKnights.ToString() + " " + Eval.LateGameBonusBishop.ToString() + " " + Eval.FactorRook.ToString() + " " + Eval.FactorBishop.ToString() + " " +
                     Eval.FactorKnight.ToString() + " ";


            foreach (int i in board.WhitePawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKing.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackPawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKing.PositionValues)
            {
                result += i + " ";
            }
            #endregion
            HashAlgorithm algorithm        = SHA256.Create();
            byte[]        resultHash       = algorithm.ComputeHash(Encoding.UTF8.GetBytes(result));
            string        resultHashString = BitConverter.ToString(resultHash).Replace("-", string.Empty);

            Assert.Equal(resultHashString, Bayes.CreateHashFromEvalConfigurationString(result));
        }
Beispiel #7
0
        public void ModifyValues_HasModifiedValuesCorrectly_Equal()
        {
            ChessBoard board = new ChessBoard();

            board.InitializeGame();
            string inputValues = null;
            string result;

            for (int i = 0; i < 781; i++)
            {
                inputValues += "1 ";
            }

            Bayes.ModifyValues(inputValues, board);


            result = Eval.BasePawnValue.ToString() + " " + Eval.BaseBishopValue.ToString() + " " + Eval.BaseKnightValue.ToString() + " " + Eval.BaseRookValue + " " +
                     Eval.BaseQueenValue.ToString() + " " + Eval.BaseKingValue.ToString() + " " + Eval.CheckMateConstant.ToString() + " " + Eval.LateGameBonusRooks.ToString() + " " +
                     Eval.LateGameBonusKnights.ToString() + " " + Eval.LateGameBonusBishop.ToString() + " " + Eval.FactorRook.ToString() + " " + Eval.FactorBishop.ToString() + " " +
                     Eval.FactorKnight.ToString() + " ";


            foreach (int i in board.WhitePawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKing.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackPawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKing.PositionValues)
            {
                result += i + " ";
            }

            Assert.Equal(inputValues, result);
        }
Beispiel #8
0
        public void WriteLineToTxtFile_HasWrittenToFileCorrectly_Equal()
        {
            #region ResultString
            ChessBoard board = new ChessBoard();
            board.InitializeGame();
            string result;
            result = Eval.BasePawnValue.ToString() + " " + Eval.BaseBishopValue.ToString() + " " + Eval.BaseKnightValue.ToString() + " " + Eval.BaseRookValue + " " +
                     Eval.BaseQueenValue.ToString() + " " + Eval.BaseKingValue.ToString() + " " + Eval.CheckMateConstant.ToString() + " " + Eval.LateGameBonusRooks.ToString() + " " +
                     Eval.LateGameBonusKnights.ToString() + " " + Eval.LateGameBonusBishop.ToString() + " " + Eval.FactorRook.ToString() + " " + Eval.FactorBishop.ToString() + " " +
                     Eval.FactorKnight.ToString() + " ";


            foreach (int i in board.WhitePawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.WhiteKing.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackPawn.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKnight.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackBishop.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackRook.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackQueen.PositionValues)
            {
                result += i + " ";
            }

            foreach (int i in board.BlackKing.PositionValues)
            {
                result += i + " ";
            }
            #endregion
            string fileName = "TxtFile.txt";
            string path     = Path.GetFullPath(fileName);
            using (File.Create(fileName)){}
            Bayes.WriteLineToTxtFile(path, result);

            string[] readText = File.ReadAllLines(path);
            File.Delete(fileName);

            Assert.Equal(result, readText.Last <string>());
        }
Beispiel #9
0
        public void Handler(string inputString)
        {
            Trace.WriteLine("Handler() input: " + inputString);
            string[] input = inputString.Split(' ');
            switch (input[0])
            {
            case "protover":
                protover = Convert.ToInt32(input[1]);
                if (protover == 2)
                {
                    Console.WriteLine("feature colors=0 done=1");
                }
                break;

            case "new":
                chessBoard.InitializeGame();
                Bayes.HandleEvalConfig(out depth, Path.GetFullPath("config.txt"));
                if (Bayes.TuneEvalValues)
                {
                    Bayes.ModifyValues(Bayes.RandomizeEvalConfiguration(Bayes.CreateOutputString(chessBoard)), chessBoard);
                }
                else
                {
                    Bayes.ModifyValues(Bayes.FindBestEvalConfiguration(Path.GetFullPath("log.txt")), chessBoard);
                }
                isFirstGo = true;
                break;

            case "level":
                movesPerMinutes = Convert.ToInt32(input[1]);
                minutes         = Convert.ToInt32(input[2]);
                break;

            case "accepted":
                switch (input[1])
                {
                case "colors":
                    isAcceptedColors = true;
                    break;

                case "time":
                    isAcceptedTime = true;
                    break;
                }
                break;

            case "quit":
                System.Environment.Exit(0);
                break;

            case "force":
                force = true;
                break;

            case "rejected":
                throw new Exception("Feature '" + input[1] + "' was not accepted.");

            case "result":

                if (Bayes.TuneEvalValues)
                {
                    Bayes.RepetetionPerEvalConfiguration--;
                    if (Bayes.RepetetionPerEvalConfiguration == 0)
                    {
                        Bayes.Seed++;
                        Bayes.RepetetionPerEvalConfiguration = 10;
                    }
                }

                Bayes.WriteToEvalConfig(Path.GetFullPath("config.txt"), depth);

                switch (input[1])
                {
                case "1-0":
                    chessBoard.State = ChessBoardGameState.WhiteMate;
                    Bayes.WriteLineToTxtFile(Path.GetFullPath("log.txt"), Bayes.CreateEvalConfigurationStringLogFormat(true, Bayes.CreateOutputString(chessBoard)));
                    break;

                case "0-1":
                    chessBoard.State = ChessBoardGameState.BlackMate;
                    Bayes.WriteLineToTxtFile(Path.GetFullPath("log.txt"), Bayes.CreateEvalConfigurationStringLogFormat(false, Bayes.CreateOutputString(chessBoard)));
                    break;

                case "1/2-1/2":
                    chessBoard.State = ChessBoardGameState.Draw;
                    Bayes.WriteLineToTxtFile(Path.GetFullPath("log.txt"), Bayes.CreateEvalConfigurationStringLogFormat(false, Bayes.CreateOutputString(chessBoard)));
                    break;

                case "*":
                    Console.WriteLine("Error (unknown command): " + input[0]);
                    break;
                }
                break;

            case "go":
                if (isFirstGo == true)
                {
                    force = false;
                    DoMove();
                    isFirstGo = false;
                }
                else
                {
                    Console.WriteLine("Error (Not handled): go");
                }
                break;

            default:
                if (STRINGMOVE_FORMAT.IsMatch(input[0]))
                {
                    Logger.OutputMove(input[0]);
                    ColoredBitBoard bitBoardMoveRecived = _winboardConverter.ConvertStringMoveToBitBoard(input[0]);
                    chessBoard.Update(bitBoardMoveRecived);
                    whiteTurn = !whiteTurn;
                    if (force == false)
                    {
                        DoMove();
                    }
                }
                else
                {
                    Console.WriteLine("Error (unknown command): " + input[0]);
                }
                break;
            }
        }