public void MakeNextMove() { movesTaken++; if (movesTaken > moveLimit) { genomes[currentGenome].fitness = getRowsCleared(); evaluateNextGenome(); } else { var possibleMoves = getAllPossibleMoves(); saveState(StateSynced); //Move move = getHighestRatedMove(possibleMoves); //The above could be replace with the following LINQ syntax: Move move = possibleMoves.OrderByDescending(mv => mv.rating).First(); for (var rotations = 0; rotations < move.rotation; rotations++) { StateSynced.PlayerInput(PlayerInput.RotateClockwise); } if (move.translation < 0) { for (var lefts = 0; lefts < Math.Abs(move.translation); lefts++) { StateSynced.PlayerInput(PlayerInput.Left); } } else if (move.translation > 0) { for (var rights = 0; rights < move.translation; rights++) { StateSynced.PlayerInput(PlayerInput.Right); } } if (inspectMoveSelection) { moveAlgorithim = move.algorithim; } //Would need to send details to the view with algorothim behavior } }
/// <summary> /// Run through all the possible moves that can be made in a given situation /// </summary> /// <returns> /// A list of Move objects which all have corresponding ratings /// </returns> public List <Move> getAllPossibleMoves() { this.isGameover = false; List <Move> possibleMoves = new List <Move>(); saveState(StateSynced); for (int rotations = 0; rotations < 4; rotations++) {//I get this first loop. 4 rotations, 4 possible ways you could rotate a piece. List <int> originalPos = new List <int>(); for (int translations = -5; translations <= 5; translations++) {//I don't get this loop. Why 10 translations? surely there are only 3 translations - //left, right and no move? Also, why is this a nested loop? If we are looking at all possible //moves, their options are a rotation and a translation? //There's some interesting and cool code here. If you want help with it, let me know. loadState(prevGameState); //rotate the shape for (int i = 0; i < rotations; i++) { gameState.CurrentPiece.RotateClockwise(); } //move the shape if (translations < 0) { for (int i = 0; i < Math.Abs(translations); i++) { gameState.PlayerInput(PlayerInput.Left); } } else if (translations > 0) { for (int i = 0; i < translations; i++) { gameState.PlayerInput(PlayerInput.Right); } } if (!originalPos.Contains(gameState.CurrentPiece.PositionX)) { Result MoveDownResult = moveToBottom(); Algorithim alg = new Algorithim { rowsCleared = MoveDownResult.rowsCleared * genomes[currentGenome].rowsCleared, weightedHeight = getWeightedHeight(gameState) * genomes[currentGenome].weightedHeight, cumulativeHeight = getCumulativeHeight(gameState) * genomes[currentGenome].cumulativeHeight, relativeHeight = getRealtiveHeight(gameState) * genomes[currentGenome].relativeHeight, holes = getHoles(gameState) * genomes[currentGenome].holes, roughness = getRoughness(gameState) * genomes[currentGenome].roughness }; int rating = 0; rating += alg.rowsCleared * genomes[currentGenome].rowsCleared; rating += alg.weightedHeight * genomes[currentGenome].weightedHeight; rating += alg.cumulativeHeight * genomes[currentGenome].cumulativeHeight; rating += alg.relativeHeight * genomes[currentGenome].relativeHeight; rating += alg.holes * genomes[currentGenome].holes; rating += alg.roughness * genomes[currentGenome].roughness; if (this.isGameover == true) { rating -= 500; } possibleMoves.Add(new Move() { rotation = rotations, translation = translations, rating = rating, algorithim = alg }); } } } loadState(StateSynced); return(possibleMoves); }