Commit 6d4a2e78 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

cleaned up

parent bb062a60
......@@ -2,7 +2,7 @@ package inf101.v20.lab4.mnkgames;
public class ConnectFour extends MNKGame{
ConnectFour() {
public ConnectFour() {
super(7, 6, 4);
}
......
......@@ -10,7 +10,7 @@ public abstract class MNKGame {
private Piece currentPlayer;
private Piece winnerPiece;
enum Piece { NONE, BLACK, WHITE;
public enum Piece { NONE, BLACK, WHITE;
public Piece next() {
return this == Piece.BLACK ? Piece.WHITE : Piece.BLACK;
......
package inf101.v20.lab4.mnkgames;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import inf101.v20.lab4.mnkgames.MNKGame.Piece;
public class MNKGameComponent extends Component {
private static final long serialVersionUID = 4548104480314044678L;
private MNKGame game;
/**
* The height of each cell in pixels.
*/
private final int cellHeight = 20;
/**
* The width of each cell in pixels.
*/
private final int cellWidth = 20;
/**
* The size of the space between each cell and between the cell and the edge
* of the window.
*/
private final int padding = 1;
public MNKGameComponent(MNKGame game){
this.game = game;
}
/*
* (non-Javadoc)
*
* @see java.awt.Component#paint(java.awt.Graphics) Paints the labyrinth.
*/
@Override
public void paint(Graphics g) {
for (int x = 0; x < game.getWidth(); x++) {
for (int y = 0; y < game.getHeight(); y++) {
g.setColor(getColor(game.getPieceAt(x, y)));
g.fillRect(x * (cellHeight + padding) + padding, //
getHeight() - (y * (cellHeight + padding) + padding) - cellHeight, //
cellHeight, cellWidth);
}
}
}
/**
* TODO duplicated in MNKGameGUI
*
* Maps from Piece values to colors
*
* NB: White maps to red RN
*
* @param pieceAt
* @return
*/
protected Color getColor(Piece pieceAt) {
switch(pieceAt) {
case BLACK : return Color.BLACK;
case WHITE : return Color.RED;
default: return Color.WHITE;
}
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ package inf101.v20.lab4.mnkgames;
public class TicTacToe extends MNKGame{
TicTacToe() {
public TicTacToe() {
super(3, 3, 3);
}
}
package inf101.v20.lab4.mnkgames;
package inf101.v20.sem2.mnkgames.GUI;
import java.util.function.Supplier;
import inf101.v20.lab4.mnkgames.ConnectFour;
import inf101.v20.lab4.mnkgames.MNKGame;
import inf101.v20.lab4.mnkgames.TicTacToe;
public class GUIMain {
public static void main(String[] args) {
......
package inf101.v20.lab4.mnkgames;
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.BorderLayout;
import java.awt.Color;
......@@ -18,6 +18,7 @@ import javax.swing.JLabel;
import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.lab4.mnkgames.MNKGame;
import inf101.v20.lab4.mnkgames.MNKGame.Piece;
/**
......@@ -29,8 +30,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
private static final long serialVersionUID = 8755882090377973497L;
/**
*
* Initializes a JFrame in which we place the game
s * Initializes a JFrame in which we place the game
* @param gameSupplier A lambda function that provides new games
*/
public static void run(GameSupplier gameSupplier) {
......@@ -43,7 +43,8 @@ public class MNKGameGUI extends JPanel implements ActionListener{
f.setVisible(true);
}
MNKGame game;
// Fields
private MNKGame game;
private JButton resetBoardButton;
private JButton playConnectFourButton;
private JButton playTicTacToeButton;
......@@ -54,7 +55,6 @@ public class MNKGameGUI extends JPanel implements ActionListener{
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
this.game = gameSupplier.getTicTacToeGame();
}
/*
......@@ -62,15 +62,10 @@ public class MNKGameGUI extends JPanel implements ActionListener{
*
* @see
* java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*
* Called whenever a button is pressed or the timer tells us its time to
* make a step.
* Called whenever a button is pressed
*/
@Override
public void actionPerformed(ActionEvent e) {
// if (e.getSource() == resetBoardButton) {
// reset();
// }
if(e.getSource() == playConnectFourButton) {
reset(gameSupplier.getConnectFourGame());
}
......@@ -113,10 +108,8 @@ public class MNKGameGUI extends JPanel implements ActionListener{
/**
* Maps from Piece values to colors
*
* NB: White maps to red RN
*
* @param pieceAt
* @return
* @param pieceAt The piece to be drawn
* @return The color that this GUI implementation associates with the provided piece
*/
protected Color getColor(Piece pieceAt) {
switch(pieceAt) {
......@@ -126,10 +119,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100*game.getHeight(), 100*game.getWidth());
}
/**
* A class for clickable panels with x,y-coordinates
*
* @author anna
*
*/
private class GamePanel extends JPanel{
private int x, y;
......@@ -140,7 +140,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
this.y = y;
}
public void removeMouseListeners() {
private void removeMouseListeners() {
MouseListener[] mouseListeners = getMouseListeners();
for (int i = 0; i < mouseListeners.length; i++) {
this.removeMouseListener(mouseListeners[i]);
......@@ -167,6 +167,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
updateBoardUI();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(3, 3);
}
......@@ -194,7 +195,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{
}
p.setRequestFocusEnabled(true);
p.requestFocus();
add(p);
JPanel buttons = createButtonPanel();
add(buttons, BorderLayout.SOUTH);
add(statusMessage, BorderLayout.NORTH);
validate();
}
public JPanel createButtonPanel() {
resetBoardButton = new JButton();
resetBoardButton.addActionListener(this);
resetBoardButton.setText("Reset Board");
......@@ -215,16 +226,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
buttons.add(resetBoardButton, BorderLayout.CENTER);
buttons.add(playConnectFourButton, BorderLayout.WEST);
buttons.add(playTicTacToeButton, BorderLayout.EAST);
// p.add(leftButton);
// p.add(rightButton);
// p.add(upButton);
// p.add(downButton);
add(p);
// add(gameComponent, BorderLayout.CENTER);
add(buttons, BorderLayout.SOUTH);
add(statusMessage, BorderLayout.NORTH);
validate();
return buttons;
}
private void updateMessage() {
......
Supports Markdown
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