java


Minimax: save copy of the board for backtracking


I am trying to implement a Minimax (with alpha beta pruning. My Problem now is that if I evaluate a position and backtrack to the next move in the iteration (one level up) the "currentBoard" is not the initial board but the one from the evaluated leaf, even though makeMove and removeFigure both return a new board.
So how can I "save" the old board for correct backtracking?
P.s: I want to use copying instead of undoing a move because the board is a simple hashmap so i guess its easier this way.
Here is the code I have so far:
public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) {
int score;
if (depth == 0) {
return Evaluator.evaluateLeaf(whichColorAmI, currentBoard);
}
else if (maximisingPlayer) {
ArrayList<Move> possibleMoves= new ArrayList<Move>();
possibleMoves=getPossibleMoves(whichColorAmI, currentBoard);
for (Move iterMoveForMe : possibleMoves) {
if(currentBoard.figureAt(iterMoveForMe.to)!=null){
currentBoard = currentBoard.removeFigure(iterMoveForMe.to);
}
currentBoard= currentBoard.moveFigure(iterMoveForMe.from, iterMoveForMe.to);
score = alphaBeta(currentBoard, depth-1, alpha, beta, false);
if(score>=alpha){
alpha=score;
if(depth==initialDepth){
moveToMake=iterMoveForMe;
}
}
if (alpha>=beta) {
break;
}
}
return alpha;
}
else {[Minimizer...]
}
I guess I found a way to do this. At least it seems to work. They key is to make a copy right after the for loop and use this copy later on instead of the currentBoard so the currentBoard for the loop gets never modified.
public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) {
Display dis = new ConsoleDisplay();
int score;
if (depth == 0) {
int evaluatedScore = Evaluator.evaluateLeaf(whichColorAmI, currentBoard);
return evaluatedScore;
}
else if (maximisingPlayer) {
ArrayList<Move> possibleMoves= new ArrayList<Move>();
possibleMoves=getPossibleMoves(whichColorAmI, currentBoard);
for (Move iterMoveForMe : possibleMoves) {
Board copy = new Board(currentBoard.height, currentBoard.width,currentBoard.figures());
if(copy.figureAt(iterMoveForMe.to)!=null){
copy = currentBoard.removeFigure(iterMoveForMe.to);
}
copy= copy.moveFigure(iterMoveForMe.from, iterMoveForMe.to);
score = alphaBeta(copy, depth-1, alpha, beta, false);
if(score>=alpha){
alpha=score;
if(depth==maxDepth){
moveToMake=iterMoveForMe;
}
}
if (alpha>=beta) {
break;
}
}
return alpha;
}
else {

Related Links

antlr4 simplifying math expression
Is it possible to embed ActiveMQ like instance in Mulesoft
maven-shade-plugin : exclude a dependency and all its transitive dependencies
Java Code is not running due to version incompatibility of Java [duplicate]
url for audioInputStream seems empty
Java redefine a method dynamically
Gradle generated Jar does not include gwt.xml file?
Amazon s3 streaming in mule
how to get MySQL database URL? [duplicate]
Specific query error - SQLite Android
I am getting a Cannot find symbol error that I can't resolve
How to Create Eclipse plugin with external jar using Ant build
What causes “Illegal start of expression” errors?
How to handle ThreadPool/ExecutorService in Spring
how to create OSLC docs using Jena?
How to sync the Host name and Service Name on the tnsnames.ora file using Java and JComboBox and TextField boxes

Categories

HOME
yii2
variables
setup-deployment
razor
tizen-web-app
win32gui
onelogin
convolution
amazon-cloudformation
floating-action-button
size
libtiff
saxon
pc
fatal-error
reactcsstransitiongroup
pythonanywhere
contextmenu
dbext
mmap
claims-based-identity
java-7
uninstall
phpfox
maquette
ping
microsoft-chart-controls
google-rich-snippets
scorm2004
commit
mmenu
force-layout
document.write
firebase-admin
cubic-spline
strptime
sage-one
elgg
sqldf
smart-table
word-vba-mac
log4c
thrust
firebaseui
dlna
bluegiga
cartesian-product
marching-cubes
angular-leaflet-directive
myo
event-bubbling
composite
geonetwork
fscommand
ng-animate
android-imagebutton
terminfo
has-many-through
contenttype
bundles
jboss-weld
industrial
multiprocessor
gridfs
soundtouch
quartz-graphics
bulkloader
eclipse-memory-analyzer
orchardcms-1.7
mt
bluepill
viewswitcher
deobfuscation
mod-auth
sql-server-profiler
gin
geneva-server

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App