Skip to content

Introduce delegation based cell implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package uk.ac.kcl.inf.szschaler.minesweeper;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based.DelegationFactory;
import uk.ac.kcl.inf.szschaler.minesweeper.view.MineFieldFrame;

/**
* Main class to run mine sweeper.
*
* @author Steffen Zschaler
*/
public class DelegatingMain {

public static void main(String[] args) {
new MineFieldFrame(new DelegationFactory()).setVisible(true);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package uk.ac.kcl.inf.szschaler.minesweeper;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.InheritanceCellFactory;
import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based.InheritanceCellFactory;
import uk.ac.kcl.inf.szschaler.minesweeper.view.MineFieldFrame;

/**
* Main class to run mine sweeper.
*
* @author Steffen Zschaler
*/
public class Main {
public class InheritanceMain {

public static void main(String[] args) {
new MineFieldFrame(new InheritanceCellFactory()).setVisible(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based;

/**
* Contents of a cell.
*
* @author Steffen Zschaler
*/
public enum CellContents {

ZERO(0), ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7),
EIGHT(8), MINE;

private int numMines;

private CellContents() {
numMines = -1;
}

private CellContents(int numMines) {
this.numMines = numMines;
}

public String getLabelText() {
switch (numMines) {
case -1:
return "M";
case 0:
return "";
default:
return "" + numMines;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.Cell;
import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.CellFactory;

/**
* A factory producing inheritance-based cells.
*
* @author Steffen Zschaler
*/
public class DelegationFactory implements CellFactory {

@Override
public Cell createMine() {
return new UndiscoveredCell(CellContents.MINE);
}

@Override
public Cell createEmptyField(int numMines) {
return new UndiscoveredCell(CellContents.values()[numMines]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JLabel;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.Cell;

/**
* A cell that has already been discovered.
*
* @author Steffen Zschaler
*/
public class DiscoveredCell implements Cell {

private CellContents contents;

public DiscoveredCell(CellContents contents) {
this.contents = contents;
}

@Override
public Component formatUIRepresentation(JButton jbUnclicked,
JLabel jlClicked) {
jlClicked.setText(contents.getLabelText());
jlClicked.setForeground(Color.orange);
return jlClicked;
}

@Override
public boolean isDiscovered() {
return true;
}

@Override
public boolean isFlagged() {
return false;
}

@Override
public boolean isEmpty() {
return contents == CellContents.ZERO;
}

@Override
public boolean isMine() {
return contents == CellContents.MINE;
}

@Override
public Cell getDiscovered() {
return this;
}

@Override
public Cell getFlagToggled() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JLabel;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.Cell;

/**
* A cell that has not yet been discovered.
*
* @author Steffen Zschaler
*/
public class UndiscoveredCell implements Cell {

/**
* The cell's contents.
*/
private CellContents contents;

private boolean flagged = false;

public UndiscoveredCell(CellContents contents) {
this.contents = contents;
}

@Override
public Component formatUIRepresentation(JButton jbUnclicked,
JLabel jlClicked) {
jbUnclicked.setBackground(Color.orange);
jbUnclicked.setText(((isFlagged()) ? ("F") : ("")));
return jbUnclicked;
}

@Override
public boolean isDiscovered() {
return false;
}

@Override
public boolean isFlagged() {
return flagged;
}

@Override
public boolean isEmpty() {
return contents == CellContents.ZERO;
}

@Override
public boolean isMine() {
return contents == CellContents.MINE;
}

/**
* Create a new discovered cell with the same contents.
*/
@Override
public Cell getDiscovered() {
return new DiscoveredCell(contents);
}

/**
* Return this cell, but with the flag toggled.
*/
@Override
public Cell getFlagToggled() {
flagged = !flagged;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This package contains an implementation of cells based on delegation rather than inheritance.
* Note that we have split the management of cell state into two parts: sub-classes of Cell
* manage the discovery and flag state, while the different instance of the CellContents enum
* manage what the cell contains.
*
* @author Steffen Zschaler
*/
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.delegation_based;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells;
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based;

import java.awt.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells;
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based;

import java.awt.Component;

import javax.swing.JButton;
import javax.swing.JLabel;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.Cell;

/**
* Inheritance-based implementation of the Cell interface.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells;
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based;

import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.Cell;
import uk.ac.kcl.inf.szschaler.minesweeper.model.cells.CellFactory;

/**
* CellFactory implementation producing inheritance-based cells.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells;
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based;

import java.awt.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* This package contains a cell implementation that is inheritance-based.
*
* @author Steffen Zschaler
*/
package uk.ac.kcl.inf.szschaler.minesweeper.model.cells.inheritance_based;