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

not able to display binary tree
Java get X, Y, Width, Height cords from an Image in Slick2D
Creating a Class Mathematical Function
How do I undeploy all artifacts from WildFly?
JavaFX: How to know which part of an Observable has been modified?
Calling super.paintcomponent(g) in class doesn't work
Optimisation of searching HashMap with list of values
Passing a parameter into Duckling Clojure function from within Java application
Appium: Why is repeating touchAction: press&release frequently skip the input fields in a form?
How to hash SubjectPublicKeyInfo for certificate pinning using okhttp 3.x in Android
404 page when run as web application project
How to detect if two Strings in an array equal
LibGDX Table showing up too small on high DPI screens?
How to get an Array into Jlist
Output Arraylist String without brackets and commas
How to read in list objects to be serialized and deserialized using a different constructor?

Categories

HOME
proxy
pycharm
relayjs
vmware
alfresco
xmpp
bookshelf.js
cmd
cross-browser
mvvmcross
amazon-cloudformation
alignment
task
vaadin7
sms-gateway
jqwidget
xlsxwriter
riot.js
mmap
rundeck
shopware
numerical-methods
internet-explorer-9
web-api-testing
social-media
maquette
gsmcomm
google-rich-snippets
nameservers
yadcf
fog
stringtemplate
textmate
xmlreader
accelerate-framework
ghost4j
multilingual
hockeyapp
hilbert-curve
reportbuilder
google-closure
fakeiteasy
outlook-api
wptoolkit
fault
errordocument
ionicons
magma
typed-lambda-calculus
sqldf
dwscript
background-service
messenger
autorest
contact-list
jwplayer7
pickadate
design-by-contract
revolution-r
system.management
tcpserver
libressl
processmodel
mmc
c++03
npapi
dd
jscript.net
codeigniter-url
transcoding
oracle-warehouse-builder
cloud-connect
intentservice
apc
ruby-datamapper
gridcontrol
first-responder
pysimplesoap
multipage
objective-c-2.0
nsmanagedobject
dotnethighcharts
punbb
nsdatecomponents
gnu-prolog
f#-powerpack
krl
substrings
zend-tool
simpletest
sustainable-pace
gacutil

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