Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 13, 2016
2 parents 2e193ed + e74ba68 commit d1ddbdf
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 110 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# CSE-4705-Checkers
A very good checkers player.

So far the view works.
88 changes: 19 additions & 69 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JPanel;

import model.Board;
import model.Location;
import model.Piece;

public class CheckersCanvas extends JPanel implements MouseListener {
public class CheckersCanvas extends JPanel {

public static final int BOARD_DIM = 8;
public static final int CANVAS_SIZE = 800;

private Square[][] board;

private Square moveDestination;
private Square moveSource;


GridBagConstraints layoutConstraints;

public CheckersCanvas() {
public CheckersCanvas(GamePanel gamePanel) {
super(new GridLayout(BOARD_DIM, BOARD_DIM));
this.board = new Square[BOARD_DIM][BOARD_DIM];
initSquares();

this.setPreferredSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
this.setMaximumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
this.setMinimumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));

initCanvas(gamePanel);
}

private void initCanvas(GamePanel gamePanel) {
initSquares(gamePanel);
initCheckers();
}

public void initSquares() {
private void initSquares(GamePanel gamePanel) {
for (int i = 0; i < BOARD_DIM; ++i) {
if (i % 2 == 0) {
for (int j = 0; j < BOARD_DIM/2; ++j) {
/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2));
board[i][j*2] = blackSquare;
blackSquare.addMouseListener(this);
blackSquare.addMouseListener(gamePanel);
this.add(blackSquare);

/* Create a red square */
Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2 + 1));
board[i][j*2 + 1] = redSquare;
redSquare.addMouseListener(this);
redSquare.addMouseListener(gamePanel);
this.add(redSquare);
}
} else {
for (int j = 0; j < BOARD_DIM/2; ++j) {
/* Create a red square */
Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2));
board[i][j*2] = redSquare;
redSquare.addMouseListener(this);
redSquare.addMouseListener(gamePanel);
this.add(redSquare);

/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2 + 1));
board[i][j*2 + 1] = blackSquare;
blackSquare.addMouseListener(this);
blackSquare.addMouseListener(gamePanel);
this.add(blackSquare);
}
}
Expand All @@ -77,59 +82,4 @@ private void initCheckers() {
}
}

@Override
public void mouseClicked(MouseEvent arg0) {


}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
Square square = (Square) e.getComponent();
if(square.hasPiece()) {
if (!square.isSelected()) {
square.setSelected(true);
if (moveSource != null)
moveSource.setSelected(false);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = null;
moveSource = square;
} else {
if (square == moveSource)
moveSource = null;
square.setSelected(false);
}
} else {

if (!square.isSelected()) {
square.setSelected(true);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = square;
} else {
if (square == moveDestination)
moveDestination = null;
square.setSelected(false);
}
}

}

@Override
public void mouseReleased(MouseEvent e) {

}
}
72 changes: 63 additions & 9 deletions src/view/CheckersWindow.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,87 @@
package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;


public class CheckersWindow extends JFrame {

public static final int HEIGHT = 800;
public static final int HEIGHT = 825;
public static final int WIDTH = 800;
private CheckersCanvas canvas;
private GamePanel gamePanel;


public CheckersWindow() {
super("Checkers");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER);
initGamePanel();
this.createMenuBar();
this.setVisible(true);
this.setResizable(false);

//pack();
}

public CheckersCanvas getCanvas() {
return canvas;

private void createMenuBar() {
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");
//New Game
JMenuItem newGame = new JMenuItem("New game");
newGame.setMnemonic(KeyEvent.VK_N);
newGame.setToolTipText("Start a new game");
newGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER);
}
});
//Quit
JMenuItem quit = new JMenuItem("Quit");
quit.setMnemonic(KeyEvent.VK_Q);
quit.setToolTipText("Exit application");
quit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Rules
JMenuItem instructions = new JMenuItem("Instructions");
instructions.setMnemonic(KeyEvent.VK_I);
instructions.setToolTipText("How to play");
instructions.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "<html><ol><li>instr 1</li></ol></html>", "title", JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null, "<html>instr 1<br></html>", "title", JOptionPane.INFORMATION_MESSAGE);
}
});

file.add(quit);
file.add(newGame);
file.add(instructions);
menubar.add(file);
menubar.setVisible(true);
setJMenuBar(menubar);
}

public void setCanvas(CheckersCanvas canvas) {
this.canvas = canvas;


private void initGamePanel() {
this.gamePanel = new GamePanel();
this.getContentPane().add(this.gamePanel, BorderLayout.CENTER);
}

}
91 changes: 91 additions & 0 deletions src/view/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package view;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements MouseListener {
private JLabel messageBar;
private CheckersCanvas canvas;

private Square moveDestination;
private Square moveSource;

public GamePanel() {
this.messageBar = new JLabel("Select a piece to move.");
this.canvas = new CheckersCanvas(this);

this.add(this.messageBar);
this.add(this.canvas);
}

public void displayMessage(String message) {
this.messageBar.setText(message);
}

@Override
public void mouseClicked(MouseEvent arg0) {


}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
Square square = (Square) e.getComponent();
if(square.hasPiece()) {
if (!square.isSelected()) {
square.setSelected(true);
if (moveSource != null)
moveSource.setSelected(false);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = null;
moveSource = square;
displayMessage("Select a destination.");
} else {
if (square == moveSource)
moveSource = null;
square.setSelected(false);
displayMessage("Select a piece to move.");
}
} else {

if (!square.isSelected()) {
square.setSelected(true);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = square;
if (moveSource != null)
displayMessage("Select \"Move\" to move the piece.");
else
displayMessage("Select a piece to move.");
} else {
if (square == moveDestination)
moveDestination = null;
square.setSelected(false);
if (moveSource != null)
displayMessage("Select a destination.");
}
}

}

@Override
public void mouseReleased(MouseEvent e) {

}
}
32 changes: 0 additions & 32 deletions src/view/RoundedBorder.java

This file was deleted.

0 comments on commit d1ddbdf

Please sign in to comment.