Compare commits
3 Commits
22797be579
...
b442d9227d
Author | SHA1 | Date |
---|---|---|
Brett | b442d9227d | |
Brett | a06d971fcc | |
MikeBlu | b5669b1c27 |
|
@ -40,7 +40,6 @@ public class Board {
|
|||
}
|
||||
|
||||
public boolean movePiece(int x, int y, int newX, int newY){
|
||||
// System.out.println(x + " " + y + " || " + newX + " " + newY);
|
||||
ChessPiece selectedPiece;
|
||||
// make sure the place we are moving from has a piece
|
||||
if ((selectedPiece = get(x, y)) == null)
|
||||
|
@ -83,11 +82,12 @@ public class Board {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moveStates;
|
||||
}
|
||||
|
||||
public int evaluate () { // !!!!!!!! Fix this, add actual heuristic evaluation
|
||||
return (int)Math.random()*900;
|
||||
return (int)(Math.random()*10);
|
||||
}
|
||||
|
||||
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) {
|
||||
Board mainBoard = new Board();
|
||||
|
||||
int maxDepth = 2;
|
||||
int maxDepth = 5;
|
||||
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");
|
||||
|
||||
int counter = 0;
|
||||
|
||||
|
||||
// mainBoard.getMoves();
|
||||
|
||||
// display stuff //
|
||||
Display display = new Display(mainBoard);
|
||||
int counter = 0;
|
||||
long frames = 0;
|
||||
long lastTime = System.currentTimeMillis();
|
||||
long frameTime = System.currentTimeMillis();
|
||||
while(display.update(mainBoard)){
|
||||
while(display.update(bestStates[counter%(maxDepth+1)])){
|
||||
display.repaint();
|
||||
// limit usage of system resources by slowing the speed down to 60 fps.
|
||||
while ((System.currentTimeMillis() - frameTime) < 64f){
|
||||
|
@ -47,25 +48,49 @@ public class Main {
|
|||
TimeUnit.SECONDS.sleep(1);
|
||||
} catch (Exception ignored) {}
|
||||
|
||||
mainBoard = bestStates[counter%maxDepth];
|
||||
|
||||
counter++;
|
||||
|
||||
}
|
||||
|
||||
System.out.println("Hello world!");
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
public int alphaBeta (Board position, int depth, int alpha, int beta, boolean maximizing) {
|
||||
if (depth == 0 || position.kingInDanger) return Board.evaluate(position);
|
||||
public static int alphaBeta (Board position, Board[] bestStates, int depth, int alpha, int beta, boolean maximizing) {
|
||||
if (depth == 0) {
|
||||
bestStates[depth] = position;
|
||||
return position.evaluate();
|
||||
}
|
||||
|
||||
System.out.println(depth);
|
||||
|
||||
if (maximizing) {
|
||||
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) {
|
||||
if (depth == 0) {
|
||||
|
|
|
@ -54,11 +54,17 @@ public class King extends ChessPiece {
|
|||
|
||||
private void castleRight(){
|
||||
// casting has to move the rook on the right size of the king from white's perspective
|
||||
if (this.isWhite)
|
||||
b.movePiece(b.size() - 1, 0, b.size() - 3, 0);
|
||||
else
|
||||
b.movePiece(b.size()-1, b.size()-1, b.size()-3, b.size()-1)
|
||||
b.set(b.size()-3, b.size()-1, b.get(b.size()-1, b.size()-1));
|
||||
if (this.isWhite) {
|
||||
var p = b.get(b.size() - 1, 0);
|
||||
p.move(new Move(b.size() - 3, 0));
|
||||
b.set(b.size() - 3, 0, p);
|
||||
b.set(b.size() - 1, 0, null);
|
||||
} 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){
|
||||
|
@ -67,9 +73,16 @@ public class King extends ChessPiece {
|
|||
|
||||
private void castleLeft(){
|
||||
// casting has to move the rook on the left size of the king from white's perspective
|
||||
if (this.isWhite)
|
||||
b.set(3, 0, b.get(0, 0));
|
||||
else
|
||||
b.set(3, b.size()-1, b.get(0, b.size()-1));
|
||||
if (this.isWhite) {
|
||||
var p = b.get(0, 0);
|
||||
p.move(new Move(3, 0));
|
||||
b.set(3, 0, p);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue