Last active
May 8, 2025 21:56
-
-
Save nikhil-RGB/a564b34e54f6c06d013cf32e3cc947ef to your computer and use it in GitHub Desktop.
Simple recursive function, contd captures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Minimax related functions start from here: | |
//1- This function clones the current board and returns a copy of it | |
Checkers cloneGame() { | |
Checkers cloneBoard = Checkers(); | |
cloneBoard.board = | |
this.board.map((innerList) => List<Token>.from(innerList)).toList(); | |
return cloneBoard; | |
} | |
//2- This function returns a List of all the children from a current board | |
//i.e: All possible boards which can emerge from one reference state(this) | |
List<Checkers> stepInto() { | |
Checkers refBoard = this; | |
List<Checkers> childBoards = []; | |
LinkedHashMap<Point, List<List<Point>>> availableMoves = | |
refBoard.movesMap(); | |
availableMoves.forEach((ogLocation, moves) { | |
//Iterate through the map to check every move at every point | |
//Here moves is a List<List<Point>> moveset for Point ogLocation | |
bool isCapture = moves[0][0] != const Point(-11, -11); | |
if (isCapture) { | |
//also handle chained captures | |
//handle all following moves here as well | |
List<Checkers> captureChildren = | |
refBoard.continuedCapture(ogLocation, moves); | |
childBoards.addAll(captureChildren); | |
} else { | |
//handle standard move play for minimax | |
List<Point> possibleLocations = moves[1]; | |
List<Checkers> children = []; | |
for (Point newLoc in possibleLocations) { | |
Checkers childBoard = refBoard.cloneGame(); | |
childBoard.executeStandardMove(ogLocation, newLoc); | |
childBoard.kingAfterMove(newLoc); | |
children.add(childBoard); | |
} | |
childBoards.addAll(children); | |
} | |
}); | |
return childBoards; | |
} | |
//3- recursive call for all boards after capture, including contd captures | |
List<Checkers> continuedCapture(Point source, List<List<Point>> moves) { | |
Checkers refBoard = this; | |
List<Checkers> boards = []; | |
for (List<Point> capDiagonal in moves) { | |
Checkers childBoardCap = refBoard.cloneGame(); | |
List<List<Point>> newCaps = | |
childBoardCap.executeCapture(source, capDiagonal); | |
newCaps = childBoardCap.kingAfterMove(capDiagonal[1]) | |
? childBoardCap.captureSequences(capDiagonal[1]) | |
: newCaps; | |
if (newCaps.isEmpty) { | |
boards.add(childBoardCap); | |
} else { | |
Point newSource = capDiagonal[1]; | |
boards.addAll(childBoardCap.continuedCapture(newSource, newCaps)); | |
} | |
} | |
return boards; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment