Skip to content

Commit

Permalink
Implemented jumping and moving and dumb thunk standard AI
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Apr 28, 2016
1 parent b330443 commit a78217c
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 26 deletions.
95 changes: 89 additions & 6 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,115 @@
import java.io.Console;
import java.util.ArrayList;

import view.GamePanel;
import model.Board;
import model.Color;
import model.Location;
import model.Move;
import model.Piece;

public class Game {
private Board board;
private Color current_turn;
private GamePanel panel;


public Game(Board board) {
this.board = board;
current_turn = Color.BLACK;
}

public void movePiece(Move move) {
board.move(move);
if (move.isJump()) {
board.jump(move);
panel.removePiece(new Location((move.destination.row
+ move.source.row)/2,
(move.source.column
+ move.destination.column) / 2));
} else {
board.move(move);
}
}

public void switchTurn() {
this.current_turn = this.current_turn == Color.BLACK ?
Color.WHITE : Color.BLACK;
}

public ArrayList<Move> getAvailableMoves(Location source) {
Piece moved = board.getLastPieceMoved();
if (moved != null && moved.color == current_turn) {
// player just jumped
ArrayList<Move> jumpset = board.generateJumpMoves(moved);
if (jumpset.isEmpty()) {
switchTurn();
}
return jumpset;
}
ArrayList<Move> moves = board.generateMoves(board.getPiece(source));
ArrayList<Move> jumps = board.generateJumpMoves(board.getPiece(source));
ArrayList<Move> allMoves = new ArrayList<Move>(moves);
if (allMoves.isEmpty())
System.out.println("No available moves");
allMoves.addAll(jumps);
return allMoves;
}

public void playVsThunk() {
Color USER_COLOR = Color.BLACK;
Color THUNK_COLOR = Color.WHITE;
while (true) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (this.board.getMovesSinceCapture() > 50) break;
if (this.current_turn == THUNK_COLOR) {
Piece moved = this.board.getLastPieceMoved();
if (moved.color == THUNK_COLOR) {
// thunk just jumped
ArrayList<Move> jumpset = board.generateJumpMoves(moved);
if (jumpset.isEmpty()) {
switchTurn();
continue;
}
Move jump = jumpset.get(0);
movePiece(jump);
panel.moveArbitraryPiece(jump);
}
else {
ArrayList<Move> moveset = board.generateAllPossibleMoves(THUNK_COLOR);
if (moveset.isEmpty()) {
System.out.println("Thunk is out of moves.");
break;
}
Move theMove = moveset.get(0);
movePiece(theMove);
panel.moveArbitraryPiece(theMove);

if(!theMove.isJump()) {
switchTurn();
}
}
}
if (this.current_turn == USER_COLOR) {
//ArrayList<Move> moveset = board.generateAllPossibleMoves(USER_COLOR);
//if (moveset.isEmpty()) break;
}

//board.print();
}
Color winner = current_turn == Color.BLACK ? Color.WHITE : Color.BLACK;
System.out.println("THE WINNER IS " + winner.toString());
}


public void setGamePanel(GamePanel panel) {
this.panel = panel;
}

public Game(Color start) {
this.board = new Board();
current_turn = start;
public Color getCurrentTurn() {
return current_turn;
}

}
52 changes: 51 additions & 1 deletion src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Board {
// Board properties and representation
private final int BOARD_SIZE = 8;
private Piece[][] representation;

private Piece lastPieceMoved;

// Move properties
private int movesSinceCapture;
Expand All @@ -24,7 +26,7 @@ public Board() {
representation = new Piece[BOARD_SIZE][BOARD_SIZE];
movesSinceCapture = 0;
init();

lastPieceMoved = null;
}

/**
Expand All @@ -40,6 +42,7 @@ public Board(Board other) {
}
}
movesSinceCapture = other.getMovesSinceCapture();
lastPieceMoved = other.getLastPieceMoved();
}

public boolean isValidSquare(Location location) {
Expand Down Expand Up @@ -146,6 +149,8 @@ public void move(Move move) {
representation[move.source.row][move.source.column] = null;
representation[move.destination.row][move.destination.column]
.setLocation(new Location(move.destination.row, move.destination.column));
Piece moved = representation[move.destination.row][move.destination.column];
this.lastPieceMoved = moved;
}

/**
Expand All @@ -159,6 +164,11 @@ public void jump(Move jump) {
representation[jump.destination.row][jump.destination.column] =
representation[jump.source.row][jump.source.column];
representation[jump.source.row][jump.source.column] = null;
representation[jump.destination.row][jump.destination.column]
.setLocation(new Location(jump.destination.row, jump.destination.column));

Piece moved = representation[jump.destination.row][jump.destination.column];
this.lastPieceMoved = moved;
}

/**
Expand Down Expand Up @@ -230,6 +240,42 @@ public ArrayList<Board> generateFrontier(Color color) {
return from_jumps;
}

public ArrayList<Move> generateJumpMoveFrontier(Color color) {
ArrayList<Move> frontier = new ArrayList<Move>();
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
Piece p = this.representation[i][j];
if (null != p && p.getColor() == color) {
ArrayList<Move> jump_moves = generateJumpMoves(this.representation[i][j]);
frontier.addAll(jump_moves);
}
}
}
return frontier;
}

public ArrayList<Move> generateMoveMoveFrontier(Color color) {
ArrayList<Move> frontier = new ArrayList<Move>();
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
Piece p = this.representation[i][j];
if(null != p && p.getColor() == color) {
ArrayList<Move> moves = generateMoves(this.representation[i][j]);
frontier.addAll(moves);
}
}
}
return frontier;
}

public ArrayList<Move> generateAllPossibleMoves(Color color) {
ArrayList<Move> from_jumps = generateJumpMoveFrontier(color);
if (from_jumps.isEmpty()) {
return generateMoveMoveFrontier(color);
}
return from_jumps;
}

/**
* Print the current board representation
*/
Expand Down Expand Up @@ -296,6 +342,10 @@ public Piece[][] getRepresentation() {

public int getMovesSinceCapture() {
return this.movesSinceCapture;
}

public Piece getLastPieceMoved() {
return this.lastPieceMoved;
}
}

5 changes: 5 additions & 0 deletions src/model/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public Location(Location other) {
public boolean equals(Location other) {
return this.row == other.row && this.column == other.column;
}

@Override
public String toString() {
return "(" + this.row + ", " + this.column + ")";
}
}
10 changes: 10 additions & 0 deletions src/model/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ public Move(Location source, Location destination) {
this.destination = destination;
}

public boolean isJump() {
return Math.abs(source.column - destination.column) == 2
&& Math.abs(source.row - destination.row) == 2;
}

@Override
public String toString() {
return "From (" + this.source.row + ", " + this.source.column + ")" +
" to (" + this.destination.row + ", " + this.destination.column + ")";
}

}
5 changes: 5 additions & 0 deletions src/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public boolean equals(Piece other) {
this.location.equals(other.getLocation()) &&
this.type == other.getType();
}

@Override
public String toString() {
return "Piece(" + this.color + ", " + this.location + ", " + this.type + ")";
}
}
27 changes: 24 additions & 3 deletions src/test/GUITest.java → src/test/GameTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package test;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import controller.Game;
import model.Board;
import model.Color;
import view.CheckersWindow;

public class GUITest {
public class GameTest {

public static void main(String[] args) {

Expand All @@ -29,7 +31,26 @@ public static void main(String[] args) {
catch (IllegalAccessException e) {
// handle exception
}
CheckersWindow window = new CheckersWindow(new Game(Color.WHITE));
window.open();
Board board = new Board();

final Game game = new Game(board);

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
CheckersWindow window = new CheckersWindow(game);
window.open();

new Thread(new Runnable() {

@Override
public void run() {
game.playVsThunk();
}

}).start();;
}
});

}
}
12 changes: 9 additions & 3 deletions src/view/Checker.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package view;

import java.awt.*;

import javax.swing.*;

/**
Expand All @@ -12,9 +13,9 @@
public class Checker extends JPanel {

/* The color of the checker */
private Color color;
public final model.Color color;

public Checker(Color color) {
public Checker(model.Color color) {
super();
this.color = color;
initChecker();
Expand Down Expand Up @@ -50,7 +51,12 @@ protected void paintComponent(Graphics g) {

/* Set the graphics object's color to the checker's color
* and paint an oval which represents the checker. */
g2.setColor(color);
if (this.color == model.Color.WHITE) {
g2.setColor(new Color(0xB1B2B3));
}
if (this.color == model.Color.BLACK) {
g2.setColor(new Color(89, 89, 89));
}
g2.fillOval(5, 5, getSize().width-10,getSize().height-10);

/* Make a call to the super classes painComponent method */
Expand Down
Loading

0 comments on commit a78217c

Please sign in to comment.