Commit a59b1dc2 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

moved clicks to GUI and allow repeating clicks

parent 70c6702b
......@@ -13,22 +13,20 @@ import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
public class GUIBoard extends JPanel{
private Grid<GUIPiece> clickablePanels;
private TwoPlayerGame<MNKGame> game;
private MNKGameGUI gui;
private static final long serialVersionUID = 1L;
public GUIBoard(MNKGameGUI gui, TwoPlayerGame<MNKGame> game) {
this.game = game;
this.gui = gui;
setLayout(new GridLayout(game.height(), game.width()));
setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
setRequestFocusEnabled(true);
requestFocus();
fillWithClickable(game.height(), game.width());
fillWithClickable(game.height(), game.width(), game);
}
public void updateToGame() {
public void updateToGame(TwoPlayerGame<MNKGame> game) {
for(int y = 0; y<clickablePanels.getHeight(); y++) {
for (int x=0; x<clickablePanels.getWidth(); x++) {
clickablePanels.get(x, y).updateToGame(game.pieceAt(x, y));
......@@ -36,7 +34,7 @@ public class GUIBoard extends JPanel{
}
}
private void fillWithClickable(int height, int width) {
private void fillWithClickable(int height, int width, TwoPlayerGame<MNKGame> game) {
this.clickablePanels = new Grid<GUIPiece>(width, height, null);
for(int y = 0; y<height; y++) {
for (int x=0; x<width; x++) {
......@@ -64,9 +62,7 @@ public class GUIBoard extends JPanel{
class GameClickListener extends MouseAdapter{
public void mousePressed(MouseEvent me){
GUIPiece clickedPanel =(GUIPiece)me.getSource(); // get the reference to the box that was clicked
game.takeTurnforHumanPlayer(clickedPanel.X, clickedPanel.Y);
updateToGame();
gui.endOfTurn();
gui.registerClick(clickedPanel.X, clickedPanel.Y);
}
}
}
......
......@@ -38,9 +38,9 @@ public class GUIPiece extends JPanel{
}
void updateToGame(Piece p) {
if(p != Piece.NONE) {
makeUnClickable();
}
// if(p != Piece.NONE) {
// makeUnClickable();
// }
c = pieceToColor(p);
}
......
......@@ -75,10 +75,14 @@ s * Initializes a JFrame in which we place the game
this.updateUI();
}
/**
* Should be called after a click to update the UI to reflect the current game state
*/
public void endOfTurn() {
public void registerClick(int x, int y) {
try {
game.takeTurnforHumanPlayer(x, y);
}
catch(Exception e){
//GUI ERROR HANDLING
}
board.updateToGame(game);
updateMessage();
MNKGameGUI.super.updateUI();
if(!game.hasPossibleMoves()) {
......
......@@ -86,7 +86,7 @@ public abstract class MNKGame {
if( x >= width || x < 0 || y >= height || y < 0)
throw new IllegalArgumentException("Cannot add a piece to this position");
if(grid.get(x, y) != Piece.NONE)
throw new IllegalArgumentException("Position is already filled");
return;//throw new IllegalArgumentException("Position is already filled");
grid.set(x, y, currentPlayer);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment