Commit 357cb687 authored by mva021's avatar mva021
Browse files

Replaced grid with new version

parent 94aa0a35
package inf101.v20.lab4.grid;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* A Grid contains a set of elements
*
*/
public class Grid<T> implements IGrid<T>, Iterable<T>{
private List<T> cells;
private int height;
private int width;
/**
*
* Construct a grid with the given dimensions.
*
* @param width
* @param height
* @param initElement
* What the cells should initially hold (possibly null)
*/
public Grid(int width, int height, T initElement) {
if(width <= 0 || height <= 0)
throw new IllegalArgumentException();
this.height = height;
this.width = width;
cells = new ArrayList<>(height * width);
for (int i = 0; i < height * width; ++i) {
cells.add(initElement);
}
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
@Override
public void set(int x, int y, T elem) {
if(x < 0 || x >= width)
throw new IndexOutOfBoundsException();
if(y < 0 || y >= height)
throw new IndexOutOfBoundsException();
cells.set(coordinateToIndex(x, y), elem);
}
private int coordinateToIndex(int x, int y) {
return x + y*width;
}
@Override
public T get(int x, int y) {
if(x < 0 || x >= width)
throw new IndexOutOfBoundsException();
if(y < 0 || y >= height)
throw new IndexOutOfBoundsException();
return cells.get(coordinateToIndex(x, y));
}
@Override
public IGrid<T> copy() {
Grid<T> newGrid = new Grid<>(getWidth(), getHeight(), null);
for (int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
newGrid.set(x, y, get(x, y));
return newGrid;
}
@Override
public Iterator<T> iterator() {
return cells.iterator();
}
}
package inf101.v20.sem2.grid;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* A Grid contains a set of elements
*
*/
public class Grid<T> implements IGrid<T> {
private List<T> cells;
private int rows;
private int cols;
/**
*
* Construct a grid with the given dimensions.
*
* @param cols
* @param rows
* @param initElement
* What the cells should initially hold (possibly null)
*/
public Grid(int rows, int cols, T initElement) {
if(cols <= 0 || rows <= 0)
throw new IllegalArgumentException("number of rows and columns must be positive");
this.rows = rows;
this.cols = cols;
cells = new ArrayList<>(rows * cols);
for(int i = 0; i<rows*cols; i++) {
cells.add(initElement);
}
}
@Override
public int numRows() {
return rows;
}
@Override
public int numCols() {
return cols;
}
@Override
public void set(Location loc, T elem) {
check(loc);
cells.set(indexOf(loc), elem);
}
/**
* Computes the right index in the list from a location in the grid
* @param loc - the location in the grid
* @return the index in the list cells
*/
private int indexOf(Location loc) {
return loc.getCol() + loc.getRow()*cols;
}
/**
* Computes the location corresponding to an index in the list
* @param index
* @return
*/
private Location locationFromIndex(int index) {
if(index<0 || index > cells.size())
throw new IndexOutOfBoundsException("index is not a valid index of the board");
return new Location(index/cols,index%cols);
}
@Override
public T get(Location loc) {
check(loc);
return cells.get(indexOf(loc));
}
@Override
public IGrid<T> copy() {
Grid<T> newGrid = new Grid<>(numRows(), numCols(), null);
for(Location loc : locations())
newGrid.set(loc, get(loc));
return newGrid;
}
/**
* Checks that a given xy coordinate is within the bounds of the Grid
* @param x
* @param y
* @return
*/
public boolean validCoordinate(int x, int y) {
if(x < 0 || x >= cols || y < 0 || y >= rows)
return false;
return true;
}
public boolean validLocation(Location loc) {
return validCoordinate(loc.getCol(), loc.getRow());
}
/**
* Checks if a given xy coordinate is within the bounds of the Grid.
* If not an exception is thrown
* @param row
* @param col
*/
public void check(Location loc) {
if(!validLocation(loc))
throw new IndexOutOfBoundsException();
}
public boolean contains(Object obj) {
return this.cells.contains(obj);
}
public Location locationOf(Object target) {
return locationFromIndex(this.cells.indexOf(target));
}
@Override
public Iterator<T> iterator() {
return cells.iterator();
}
public GridLocationIterator locations() {
return new GridLocationIterator(this);
}
}
package inf101.v20.sem2.grid;
import java.util.Arrays;
import java.util.List;
public enum GridDirection {
EAST(1, 0),
NORTH(0, -1),
WEST(-1, 0),
SOUTH(0, 1), //
NORTHEAST(1, -1),
NORTHWEST(-1, -1),
SOUTHWEST(-1, 1),
SOUTHEAST(1, 1), //
CENTER(0, 0);
/**
* The four cardinal directions: {@link #NORTH}, {@link #SOUTH}, {@link #EAST},
* {@link #WEST}.
*/
public static final List<GridDirection> FOUR_DIRECTIONS = Arrays.asList(EAST, NORTH, WEST, SOUTH);
/**
* The eight cardinal and intercardinal directions: {@link #NORTH},
* {@link #SOUTH}, {@link #EAST}, {@link #WEST}, {@link #NORTHWEST},
* {@link #NORTHEAST}, {@link #SOUTHWEST}, {@link #SOUTHEAST}.
*/
public static final List<GridDirection> EIGHT_DIRECTIONS = Arrays.asList(EAST, NORTHEAST, NORTH, NORTHWEST, WEST,
SOUTHWEST, SOUTH, SOUTHEAST);
/**
* The eight cardinal and intercardinal directions ({@link #EIGHT_DIRECTIONS}),
* plus {@link #CENTER}.
*/
public static final List<GridDirection> NINE_DIRECTIONS = Arrays.asList(EAST, NORTHEAST, NORTH, NORTHWEST, WEST,
SOUTHWEST, SOUTH, SOUTHEAST, CENTER);
private final int dx;
private final int dy;
private GridDirection(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
/**
* @return The change to your X-coordinate if you were to move one step in this
* direction
*/
public int getDx() {
return dx;
}
/**
* @return The change to your Y-coordinate if you were to move one step in this
* direction
*/
public int getDy() {
return dy;
}
}
package inf101.v20.sem2.grid;
import java.util.Iterator;
public class GridLocationIterator implements Iterator<Location>, Iterable<Location> {
Grid<?> grid;
int row;
int col;
public GridLocationIterator(Grid<?> grid) {
this.grid = grid;
}
@Override
public boolean hasNext() {
return row<grid.numRows() && col<grid.numCols();
}
@Override
public Location next() {
Location elem = new Location(row, col);
col++;
if(col>=grid.numCols()) {
col=0;
row++;
}
return elem;
}
@Override
public Iterator<Location> iterator() {
return new GridLocationIterator(grid);
}
}
package inf101.v20.lab4.grid;
package inf101.v20.sem2.grid;
/**
* IGrid is a generic grid
......@@ -6,17 +6,17 @@ package inf101.v20.lab4.grid;
* @author Anna Eilertsen - anna.eilertsen@uib.no
*
*/
public interface IGrid<T> {
public interface IGrid<T> extends Iterable<T> {
/**
* @return The height of the grid.
*/
int getHeight();
int numRows();
/**
* @return The width of the grid.
*/
int getWidth();
int numCols();
/**
*
......@@ -29,7 +29,7 @@ public interface IGrid<T> {
* @param y The row of the cell to change the contents of.
* @param element The contents the cell is to have.
*/
void set(int x, int y, T element);
void set(Location loc, T element);
/**
*
......@@ -41,7 +41,7 @@ public interface IGrid<T> {
* @param x The column of the cell to get the contents of.
* @param y The row of the cell to get contents of.
*/
T get(int x, int y);
T get(Location loc);
/**
* Make a copy
......@@ -49,5 +49,11 @@ public interface IGrid<T> {
* @return A shallow copy of the grid, with the same elements
*/
IGrid<T> copy();
Location locationOf(T target);
public Iterable<Location> locations();
public boolean contains(Object obj);
}
\ No newline at end of file
package inf101.v20.sem2.grid;
/**
* represents an row, column index of a grid.
* @author mva021
*
*/
public class Location {
private int row;
private int col;
/**
* Constructor for location, note that a Location is independent of the Grid implementation,
* hence can have values not corresponding to a cell in the grid.
*
* @param row
* @param col
*/
public Location(int row, int col){
this.row = row;
this.col = col;
}
/**
* Makes a new Location representing the location adjacent in the given direction
* @param dir
* @return
*/
public Location move(GridDirection dir) {
return new Location(row+dir.getDy(), col+dir.getDx());
}
/**
* @return the index of the row containing this Location.
*/
public int getRow() {
return row;
}
/**
* @return the index of the column containing this Location.
*/
public int getCol() {
return col;
}
@Override
public boolean equals(Object obj) {
if(obj ==null || !(obj instanceof Location)) {
return false;
}
Location loc = (Location) obj;
return this.getRow()==loc.getRow() && this.getCol()==loc.getCol();
}
@Override
public String toString() {
return "Location: "+getRow()+","+getCol();
}
}
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import inf101.v20.sem2.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
public class GUIBoard extends JPanel{
private Grid<GUIPiece> clickablePanels;
private MNKGameGUI gui;
private static final long serialVersionUID = 1L;
public GUIBoard(MNKGameGUI gui, TwoPlayerGame<MNKGame> 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(), game);
}
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));
}
}
}
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++) {
GUIPiece pan = new GUIPiece(x, y);
pan.addMouseListener(new GameClickListener());
add(pan);
pan.updateToGame(game.pieceAt(x, y));
clickablePanels.set(x, y, pan);
}
}
}
/**
* Makes all panels unclickable
*/
public void makeAllUnclickable() {
clickablePanels.forEach(GUIPiece::makeUnClickable);
}
/**
* Class that describes what happens on click
* connects the click with the game
* then callback to GUI
*/
class GameClickListener extends MouseAdapter{
public void mousePressed(MouseEvent me){
GUIPiece clickedPanel =(GUIPiece)me.getSource(); // get the reference to the box that was clicked
gui.registerClick(clickedPanel.X, clickedPanel.Y);
}
}
}
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
public class GUIPiece extends JPanel{
private static final long serialVersionUID = 1L;
public final int X, Y;
private Color c;
public GUIPiece(int x, int y) {
this.X = x;
this.Y = y;
setEnabled(true);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
private void removeMouseListeners() {
MouseListener[] mouseListeners = this.getMouseListeners();
for (int i = 0; i < mouseListeners.length; i++) {
this.removeMouseListener(mouseListeners[i]);
}
}
public Dimension getPreferredSize() {
return new Dimension(3, 3);
}
@Override
public Color getBackground() {
return c;
}
void updateToGame(Piece p) {
// if(p != Piece.NONE) {
// makeUnClickable();
// }
c = pieceToColor(p);
}
public void makeUnClickable() {
removeMouseListeners();
}
/**
* Maps from Piece values to colors
*
* @param pieceAt The piece to be drawn
* @return The color that this GUI implementation associates with the provided piece
*/
private static Color pieceToColor(Piece pieceAt) {
switch(pieceAt) {
case BLACK : return Color.getHSBColor(90, 65, 20.3f);
case WHITE : return Color.getHSBColor(308, 26, 34);
default: return Color.WHITE;
}
}
}
package inf101.v20.sem2.mnkgames.GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;