Compare commits

..

No commits in common. "b442d9227d0716a823cede746fa7158f333ec50c" and "22797be579e6a260f2be52bcf0ba3c55dca818a4" have entirely different histories.

4 changed files with 22 additions and 190 deletions

View File

@ -40,6 +40,7 @@ public class Board {
} }
public boolean movePiece(int x, int y, int newX, int newY){ public boolean movePiece(int x, int y, int newX, int newY){
// System.out.println(x + " " + y + " || " + newX + " " + newY);
ChessPiece selectedPiece; ChessPiece selectedPiece;
// make sure the place we are moving from has a piece // make sure the place we are moving from has a piece
if ((selectedPiece = get(x, y)) == null) if ((selectedPiece = get(x, y)) == null)
@ -82,12 +83,11 @@ public class Board {
} }
} }
} }
return moveStates; return moveStates;
} }
public int evaluate () { // !!!!!!!! Fix this, add actual heuristic evaluation public int evaluate () { // !!!!!!!! Fix this, add actual heuristic evaluation
return (int)(Math.random()*10); return (int)Math.random()*900;
} }
public Board deepCopy () { public Board deepCopy () {

View File

@ -1,130 +0,0 @@
package project.ui;
import project.chess.Board;
import project.chess.ChessPiece;
import project.chess.Move;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.text.AttributedString;
import java.util.ArrayList;
public class Display extends JFrame implements MouseListener {
private static int xOffset = 128, yOffset = 128;
private static int width = 64, height = 64;
private int movingPointX = -1, movingPointY = -1;
private int selectionPointX = -1, selectionPointY = -1;
private Board b;
public Display(Board b){
this.b = b;
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(800, 800);
this.setVisible(true);
this.setEnabled(true);
this.setTitle("Ratatta");
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < b.size(); i++){
for (int j = 0; j < b.size(); j++){
ChessPiece p;
p = b.get(i,j);
if (p != null)
g.drawImage(p.getImage(), xOffset + (i) * width, yOffset + (j) * height, null);
}
}
if (movingPointX != -1 && movingPointY != -1) {
ChessPiece piece;
piece = b.get(movingPointX, movingPointY);
if (piece != null) {
ArrayList<Move> moves;
moves = piece.getMoves();
for (Move m : moves) {
drawSelectionRect(g, m);
}
if (selectionPointX != -1 && selectionPointY != -1) {
int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size() - 1) - ((selectionPointY - yOffset) / height);
for (Move m : moves){
if (m.getX() == localPointX && m.getY() == localPointY){
b.movePiece(piece.getPosition(), new Move(localPointX, localPointY));
selectionPointX = -1;
selectionPointY = -1;
movingPointX = -1;
movingPointY = -1;
return;
}
}
}
}
}
// handle user input (mouse clicking)
if (selectionPointX != -1 && selectionPointY != -1){
int localPointX = (selectionPointX - xOffset) / width;
int localPointY = (b.size()-1) - ((selectionPointY - yOffset) / height);
ChessPiece piece;
piece = b.get(localPointX, localPointY);
if (piece != null) {
movingPointX = localPointX;
movingPointY = localPointY;
}
selectionPointY = -1;
selectionPointX = -1;
}
}
private void drawSelectionRect(Graphics g, Move m){
g.drawRect(xOffset + (m.getX() * width), yOffset + ((b.size()-1 - m.getY()) * height), width, height);
}
public boolean update(Board newerBoard){
this.b = newerBoard;
return true;
}
public static Image loadImage(String path){
try {
return new ImageIcon(ImageIO.read(new File(path))).getImage();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
this.selectionPointX = mouseEvent.getX();
this.selectionPointY = mouseEvent.getY();
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
}
}

View File

@ -11,24 +11,23 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Board mainBoard = new Board(); Board mainBoard = new Board();
int maxDepth = 5; int maxDepth = 2;
Board[] bestStates = new Board[maxDepth]; Board[] bestStates = new Board[maxDepth];
alphaBeta(mainBoard,bestStates,maxDepth,Integer.MIN_VALUE,Integer.MAX_VALUE,true); minMax(mainBoard,bestStates,maxDepth,true);
System.out.println("Ai Done Computing"); System.out.println("Ai Done Computing");
int counter = 0;
// mainBoard.getMoves(); // mainBoard.getMoves();
// display stuff // // display stuff //
Display display = new Display(mainBoard); Display display = new Display(mainBoard);
int counter = 0;
long frames = 0; long frames = 0;
long lastTime = System.currentTimeMillis(); long lastTime = System.currentTimeMillis();
long frameTime = System.currentTimeMillis(); long frameTime = System.currentTimeMillis();
while(display.update(bestStates[counter%(maxDepth+1)])){ while(display.update(mainBoard)){
display.repaint(); display.repaint();
// limit usage of system resources by slowing the speed down to 60 fps. // limit usage of system resources by slowing the speed down to 60 fps.
while ((System.currentTimeMillis() - frameTime) < 64f){ while ((System.currentTimeMillis() - frameTime) < 64f){
@ -48,49 +47,25 @@ public class Main {
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
} catch (Exception ignored) {} } catch (Exception ignored) {}
mainBoard = bestStates[counter%maxDepth];
counter++; counter++;
} }
System.out.println("Hello world!"); System.out.println("Hello world!");
} }
public static int alphaBeta (Board position, Board[] bestStates, int depth, int alpha, int beta, boolean maximizing) { /*
if (depth == 0) { public int alphaBeta (Board position, int depth, int alpha, int beta, boolean maximizing) {
bestStates[depth] = position; if (depth == 0 || position.kingInDanger) return Board.evaluate(position);
return position.evaluate();
}
System.out.println(depth);
if (maximizing) { if (maximizing) {
int maxEval = Integer.MIN_VALUE; int maxEval = Integer.MIN_VALUE;
for (Board state: position.getMoves(maximizing) ) { position.
int eval = alphaBeta(state,bestStates,depth-1, alpha,beta,false);
maxEval = Math.max(maxEval,eval);
alpha = Math.max(alpha,eval);
if (beta <= alpha) {
break;
}
}
bestStates[depth-1] = position;
return maxEval;
} else {
int minEval = Integer.MAX_VALUE;
for (Board state: position.getMoves(maximizing) ) {
int eval = alphaBeta(state,bestStates,depth-1,alpha,beta,true);
minEval = Math.min(minEval,eval);
beta = Math.min(beta,eval);
if (beta <= alpha) {
break;
}
}
bestStates[depth-1] = position;
return minEval;
}
} }
} */
public static int minMax (Board position, Board[] bestStates, int depth, boolean maximizing) { public static int minMax (Board position, Board[] bestStates, int depth, boolean maximizing) {
if (depth == 0) { if (depth == 0) {

View File

@ -54,17 +54,11 @@ public class King extends ChessPiece {
private void castleRight(){ private void castleRight(){
// casting has to move the rook on the right size of the king from white's perspective // casting has to move the rook on the right size of the king from white's perspective
if (this.isWhite) { if (this.isWhite)
var p = b.get(b.size() - 1, 0); b.movePiece(b.size() - 1, 0, b.size() - 3, 0);
p.move(new Move(b.size() - 3, 0)); else
b.set(b.size() - 3, 0, p); b.movePiece(b.size()-1, b.size()-1, b.size()-3, b.size()-1)
b.set(b.size() - 1, 0, null); b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1));
} else {
var p = b.get(b.size() - 1, b.size() - 1);
p.move(new Move(b.size() - 3, b.size() - 1));
b.set(b.size() - 3, b.size() - 1, p);
b.set(b.size() - 1, b.size() - 1, null);
}
} }
private boolean checkIfRookValid(ChessPiece piece){ private boolean checkIfRookValid(ChessPiece piece){
@ -73,16 +67,9 @@ public class King extends ChessPiece {
private void castleLeft(){ private void castleLeft(){
// casting has to move the rook on the left size of the king from white's perspective // casting has to move the rook on the left size of the king from white's perspective
if (this.isWhite) { if (this.isWhite)
var p = b.get(0, 0); b.set(3, 0, b.get(0, 0));
p.move(new Move(3, 0)); else
b.set(3, 0, p); b.set(3, b.size()-1, b.get(0, b.size()-1));
b.set(0, 0, null);
} else {
var p = b.get(0, b.size() - 1);
p.move(new Move(3, b.size() - 1));
b.set(3, b.size() - 1, p);
b.set(0, b.size() - 1, null);
}
} }
} }