Merge branch 'main' of https://github.com/Tri11Paragon/COSC-3P71-Final-Project
commit
a06d971fcc
|
@ -40,7 +40,6 @@ 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)
|
||||||
|
@ -83,11 +82,12 @@ 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()*900;
|
return (int)(Math.random()*10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board deepCopy () {
|
public Board deepCopy () {
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,23 +11,24 @@ 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 = 2;
|
int maxDepth = 5;
|
||||||
Board[] bestStates = new Board[maxDepth];
|
Board[] bestStates = new Board[maxDepth];
|
||||||
|
|
||||||
minMax(mainBoard,bestStates,maxDepth,true);
|
alphaBeta(mainBoard,bestStates,maxDepth,Integer.MIN_VALUE,Integer.MAX_VALUE,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(mainBoard)){
|
while(display.update(bestStates[counter%(maxDepth+1)])){
|
||||||
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){
|
||||||
|
@ -47,25 +48,49 @@ 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) {
|
||||||
public int alphaBeta (Board position, int depth, int alpha, int beta, boolean maximizing) {
|
if (depth == 0) {
|
||||||
if (depth == 0 || position.kingInDanger) return Board.evaluate(position);
|
bestStates[depth] = position;
|
||||||
|
return position.evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(depth);
|
||||||
|
|
||||||
if (maximizing) {
|
if (maximizing) {
|
||||||
int maxEval = Integer.MIN_VALUE;
|
int maxEval = Integer.MIN_VALUE;
|
||||||
position.
|
for (Board state: position.getMoves(maximizing) ) {
|
||||||
|
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) {
|
||||||
|
|
Loading…
Reference in New Issue