From ecf88068ac387a686c6428ca6379fa6fb8682da1 Mon Sep 17 00:00:00 2001 From: MikeBlu <92647684+MikeBlu@users.noreply.github.com> Date: Sun, 18 Dec 2022 02:03:41 -0500 Subject: [PATCH] Added initial test harness for minMax algorithm --- Michael _Edit's_(non_destructive)/Board.java | 87 ++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Michael _Edit's_(non_destructive)/Board.java diff --git a/Michael _Edit's_(non_destructive)/Board.java b/Michael _Edit's_(non_destructive)/Board.java new file mode 100644 index 0000000..cef2a1f --- /dev/null +++ b/Michael _Edit's_(non_destructive)/Board.java @@ -0,0 +1,87 @@ +package project; + +import project.chess.Board; +import project.chess.Move; +import project.ui.Display; + +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + +public class Main { + public static void main(String[] args) { + Board mainBoard = new Board(); + + int maxDepth = 2; + Board[] bestStates = new Board[maxDepth]; + + minMax(mainBoard,bestStates,maxDepth,true); + + System.out.println("Ai Done Computing"); + + int counter = 0; + + // mainBoard.getMoves(); + + // display stuff // + Display display = new Display(mainBoard); + long frames = 0; + long lastTime = System.currentTimeMillis(); + long frameTime = System.currentTimeMillis(); + while(display.update(mainBoard)){ + display.repaint(); + // limit usage of system resources by slowing the speed down to 60 fps. + while ((System.currentTimeMillis() - frameTime) < 64f){ + Thread.yield(); + } + frameTime = System.currentTimeMillis(); + + // print out the FPS of the display. + frames++; + if (System.currentTimeMillis() - lastTime > 1000){ + System.out.println("FPS: " + frames); + frames = 0; + lastTime = System.currentTimeMillis(); + } + + try { + TimeUnit.SECONDS.sleep(1); + } catch (Exception ignored) {} + + mainBoard = bestStates[counter%maxDepth]; + + counter++; + + } + System.out.println("Hello world!"); + + + } + + public static int minMax (Board position, Board[] bestStates, int depth, boolean maximizing) { + if (depth == 0) { + bestStates[depth] = position; + return position.evaluate(); + } + + System.out.println(depth); + + if (maximizing) { + int maxEval = Integer.MIN_VALUE; + for (Board state: position.getMoves(maximizing) ) { + int eval = minMax(state,bestStates,depth-1, false); + maxEval = Math.max(maxEval,eval); + } + bestStates[depth-1] = position; + return maxEval; + } else { + int minEval = Integer.MAX_VALUE; + for (Board state: position.getMoves(maximizing) ) { + int eval = minMax(state,bestStates,depth-1, true); + minEval = Math.min(minEval,eval); + } + bestStates[depth-1] = position; + return minEval; + } + } + +}