public void RandomMove() { string configuration = TranslateBoard(); Movement choseMove; ArrayList botPieces = base.board.GetEnemyPieces(); ArrayList possibleMoves = ga.GenerateMutations(this, botPieces); int randomIndex = UnityEngine.Random.Range(0, possibleMoves.Count); choseMove = (Movement)possibleMoves[randomIndex]; BoardConfiguration bConfig = new BoardConfiguration(configuration); bConfig.AddMovement(choseMove, ga.AdaptationScore(choseMove)); configList.Add(bConfig); // Make the movement. Debug.Log("Chosed Movement: " + choseMove.ToString()); lastMovement = choseMove; base.board.MovePiece(choseMove); }
/// <summary> /// Random function to write some persitance tests in it. /// </summary> /// <remarks> /// Only used for Debug Purpose. /// </remarks> public void Test() { if (CheckersMultiplayer.Instance.IsTableTen) { BoardConfiguration foo = new BoardConfiguration("b#b###################w######w####B###b#########W########w###w######################################"); Movement movement = new Movement(new IntVector2(7, 1), new IntVector2(3, 5), new IntVector2(5, 3)); foo.AddMovement(movement, 5f); BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/gameStorage.dat", FileMode.Open); historic = (List <BoardConfiguration>)bf.Deserialize(file); file.Close(); bool result = false; bool result2 = false; foreach (BoardConfiguration bc in historic) { if (bc.Equals(foo)) { result = true; Debug.Log("find: " + bc.ToString()); Debug.Log("movement contains: " + bc.HasMovementConfiguration(foo.GetMovementsConfigurations()[0])); } } result2 = historic.Contains(foo); Debug.Log("result for: " + result + "\nresult contain: " + result2); } else { BoardConfiguration foo = new BoardConfiguration("b#b###################w######w####B###b#########W########w###w##"); Movement movement = new Movement(new IntVector2(7, 1), new IntVector2(3, 5), new IntVector2(5, 3)); foo.AddMovement(movement, 5f); BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/gameStorage.dat", FileMode.Open); historic = (List <BoardConfiguration>)bf.Deserialize(file); file.Close(); bool result = false; bool result2 = false; foreach (BoardConfiguration bc in historic) { if (bc.Equals(foo)) { result = true; Debug.Log("find: " + bc.ToString()); Debug.Log("movement contains: " + bc.HasMovementConfiguration(foo.GetMovementsConfigurations()[0])); } } result2 = historic.Contains(foo); Debug.Log("result for: " + result + "\nresult contain: " + result2); } }
/// <summary> /// Sign the start of that player's turn. /// </summary> public override void Play() { if (PlayerPrefs.GetString("difficult") == "easy") { addap = -100; } else if (PlayerPrefs.GetString("difficult") == "medium") { addap = -1000; } else if (PlayerPrefs.GetString("difficult") == "hard") { addap = -10000; } biggestAdaptation = addap; string configuration = TranslateBoard(); if (configList.Count >= 1 && !useHistoric) { configList[configList.Count - 1].GetMovementConfigurationWithMove(lastMovement) .AddResults(configuration); } Movement choseMove; // Select the same piece if it's playing again. if (base.currentPiece != null) { choseMove = (Movement)base.currentPiece.GetBestSucessiveCapture()[0]; } // Select a new piece if it's the first play of this turn. else { choseMove = ChooseOneMovement(); // Debug.LogError("choseMove " + choseMove); base.currentPiece = base.board.GetTile(choseMove.getOriginalPosition()) .GetChild().GetComponent <Piece>(); } // Make a function call to find the best adaptation in the historic. Movement moveByHistoric = ChoseMovementFromHistoric(configuration); // Choose between use the historic choice or not. if (ga.AdaptationScore(choseMove) / 2 < biggestAdaptation) { Debug.Log("Select historic movement " + biggestAdaptation); choseMove = moveByHistoric; useHistoric = true; } else { if (moveByHistoric != null) { Debug.Log("Historic is a bad choice\n historic:" + biggestAdaptation + " newMove " + ga.AdaptationScore(choseMove)); // Get possible moves for Bot's pieces. ArrayList botPieces = base.board.GetEnemyPieces(); ArrayList possibleMoves = ga.GenerateMutations(this, botPieces); // Get moves made that is saved in the historic BoardConfiguration bc = FindBoardConfiguration(configuration); ArrayList movesSaved = new ArrayList(); foreach (MovementConfiguration moveConf in bc.GetMovementsConfigurations()) { movesSaved.Add(moveConf.GetMove()); } // Remove from possible moves that which are in saved in historic ArrayList movesToBeRemoved = new ArrayList(); foreach (Movement moveSaved in movesSaved) { foreach (Movement move in possibleMoves) { if (move.Equals(moveSaved)) { movesToBeRemoved.Add(move); } } } foreach (Movement moveToRemove in movesToBeRemoved) { possibleMoves.Remove(moveToRemove); } // If doesn't left any movement, restore the movement and get one randomly. if (possibleMoves.Count == 0) { possibleMoves = ga.GenerateMutations(this, botPieces); int randomIndex = UnityEngine.Random.Range(0, possibleMoves.Count); choseMove = (Movement)possibleMoves[randomIndex]; } else { Debug.Log("RANDOM MOVE"); // Choose the best one with the adaptation function. choseMove = SelectBestMovement(possibleMoves); } } else { // Debug.Log("New configuration"); useHistoric = false; } } // Add the current board configuration with the chose movement to the config list. BoardConfiguration bConfig = new BoardConfiguration(configuration); bConfig.AddMovement(choseMove, ga.AdaptationScore(choseMove)); configList.Add(bConfig); // Make the movement. // Debug.Log("Chosed Movement: " + choseMove.ToString()); lastMovement = choseMove; base.board.MovePiece(choseMove); }