-The rest of the game engine. I screwed up the commit last time.
This commit is contained in:
163
src/model/Board.java
Normal file
163
src/model/Board.java
Normal file
@@ -0,0 +1,163 @@
|
||||
package model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Board {
|
||||
public enum TileColor {
|
||||
BLUE, GREEN, NONE, RED, YELLOW
|
||||
}
|
||||
|
||||
public static final int NUM_COLS = 6;
|
||||
public static final int NUM_ROWS = 6;
|
||||
public static final int ROW_REMOVAL_SIZE = 3;
|
||||
|
||||
private final TileColor[][] board;
|
||||
|
||||
public Board() {
|
||||
board = new TileColor[NUM_ROWS][NUM_COLS];
|
||||
for (int r = 0; r < NUM_ROWS; r++) {
|
||||
for (int c = 0; c < NUM_COLS; c++) {
|
||||
setTile(new CellPointer(r, c), TileColor.NONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board[r][c];
|
||||
}
|
||||
|
||||
public void setTile(CellPointer cp, TileColor tile) {
|
||||
board[cp.r][cp.c] = tile;
|
||||
|
||||
if (tile != TileColor.NONE) {
|
||||
ArrayList<CellPointer> remove = new ArrayList<CellPointer>();
|
||||
ArrayList<CellPointer> hold;
|
||||
int count;
|
||||
|
||||
// Check up-and-down.
|
||||
count = 0;
|
||||
hold = new ArrayList<CellPointer>();
|
||||
loop: for (int row = cp.r; row >= 0; row--) {
|
||||
if (board[row][cp.c] == tile) {
|
||||
hold.add(new CellPointer(row, cp.c));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
|
||||
loop: for (int row = cp.r + 1; row < NUM_ROWS; row++) {
|
||||
if (board[row][cp.c] == tile) {
|
||||
hold.add(new CellPointer(row, cp.c));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
|
||||
if (count >= ROW_REMOVAL_SIZE) {
|
||||
remove.addAll(hold);
|
||||
}
|
||||
|
||||
// Check left-and-right.
|
||||
count = 0;
|
||||
hold = new ArrayList<CellPointer>();
|
||||
loop: for (int col = cp.c; col >= 0; col--) {
|
||||
if (board[cp.r][col] == tile) {
|
||||
hold.add(new CellPointer(cp.r, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
|
||||
loop: for (int col = cp.c + 1; col < NUM_COLS; col++) {
|
||||
if (board[cp.r][col] == tile) {
|
||||
hold.add(new CellPointer(cp.r, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
}
|
||||
|
||||
if (count >= ROW_REMOVAL_SIZE) {
|
||||
remove.addAll(hold);
|
||||
}
|
||||
|
||||
// Check descending diagonal.
|
||||
count = 0;
|
||||
hold = new ArrayList<CellPointer>();
|
||||
int row = cp.r;
|
||||
int col = cp.c;
|
||||
loop: while (row >= 0 && col >= 0) {
|
||||
if (board[row][col] == tile) {
|
||||
hold.add(new CellPointer(row, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
|
||||
row--;
|
||||
col--;
|
||||
}
|
||||
|
||||
row = cp.r + 1;
|
||||
col = cp.c + 1;
|
||||
loop: while (row < NUM_ROWS && col < NUM_COLS) {
|
||||
if (board[row][col] == tile) {
|
||||
hold.add(new CellPointer(row, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
|
||||
row++;
|
||||
col++;
|
||||
}
|
||||
|
||||
if (count >= ROW_REMOVAL_SIZE) {
|
||||
remove.addAll(hold);
|
||||
}
|
||||
|
||||
// Check ascending diagonal.
|
||||
count = 0;
|
||||
hold = new ArrayList<CellPointer>();
|
||||
|
||||
row = cp.r;
|
||||
col = cp.c;
|
||||
loop: while (row < NUM_ROWS && col >= 0) {
|
||||
if (board[row][col] == tile) {
|
||||
hold.add(new CellPointer(row, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
|
||||
row++;
|
||||
col--;
|
||||
}
|
||||
|
||||
row = cp.r - 1;
|
||||
col = cp.c + 1;
|
||||
loop: while (row >= 0 && col < NUM_COLS) {
|
||||
if (board[row][col] == tile) {
|
||||
hold.add(new CellPointer(row, col));
|
||||
count++;
|
||||
} else {
|
||||
break loop;
|
||||
}
|
||||
|
||||
row--;
|
||||
col++;
|
||||
}
|
||||
|
||||
if (count >= ROW_REMOVAL_SIZE) {
|
||||
remove.addAll(hold);
|
||||
}
|
||||
|
||||
for (CellPointer cell : remove) {
|
||||
board[cell.r][cell.c] = TileColor.NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/model/CellPointer.java
Normal file
11
src/model/CellPointer.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package model;
|
||||
|
||||
public class CellPointer {
|
||||
public CellPointer(int row, int col) {
|
||||
r = row;
|
||||
c = col;
|
||||
}
|
||||
|
||||
public int r;
|
||||
public int c;
|
||||
}
|
||||
13
src/model/ComController.java
Normal file
13
src/model/ComController.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package model;
|
||||
|
||||
|
||||
public abstract class ComController {
|
||||
|
||||
protected Board board = null;
|
||||
|
||||
public ComController(Board brd) {
|
||||
board = brd;
|
||||
}
|
||||
|
||||
public abstract Move getMove();
|
||||
}
|
||||
21
src/model/Move.java
Normal file
21
src/model/Move.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package model;
|
||||
|
||||
import model.Board.TileColor;
|
||||
|
||||
public class Move {
|
||||
private final TileColor color;
|
||||
private final CellPointer cp;
|
||||
|
||||
public Move(CellPointer cllPntr, TileColor tlClr) {
|
||||
cp = cllPntr;
|
||||
color = tlClr;
|
||||
}
|
||||
|
||||
public CellPointer getCell() {
|
||||
return cp;
|
||||
}
|
||||
|
||||
public TileColor getColor() {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
154
src/model/Referee.java
Normal file
154
src/model/Referee.java
Normal file
@@ -0,0 +1,154 @@
|
||||
package model;
|
||||
|
||||
import javax.security.auth.Refreshable;
|
||||
|
||||
import model.Board.TileColor;
|
||||
import model.comController.RandomComController;
|
||||
import controller.PlayerController;
|
||||
|
||||
public class Referee implements Refreshable {
|
||||
|
||||
public enum Message {
|
||||
COM_TURN {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Waiting for the computer's move.";
|
||||
}
|
||||
},
|
||||
GAME_OVER {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Game over!";
|
||||
}
|
||||
},
|
||||
NONE {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "";
|
||||
}
|
||||
},
|
||||
PLAYER_TURN {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Waiting for the player's move.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final Board board;
|
||||
|
||||
private final ComController cc;
|
||||
|
||||
private final PlayerController pc = new PlayerController();
|
||||
|
||||
private boolean playerTurn;
|
||||
private boolean refresh = true;
|
||||
private int score = 0;
|
||||
|
||||
public Referee() {
|
||||
board = new Board();
|
||||
cc = new RandomComController(board);
|
||||
playerTurn = true;
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
Move mv;
|
||||
|
||||
if (playerTurn && pc.isReady()) {
|
||||
mv = pc.getMove();
|
||||
|
||||
if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) {
|
||||
playToken(pc.getMove());
|
||||
|
||||
refresh = true;
|
||||
}
|
||||
|
||||
else {
|
||||
pc.denyMove();
|
||||
}
|
||||
}
|
||||
|
||||
else if (!playerTurn) {
|
||||
mv = cc.getMove();
|
||||
|
||||
playToken(mv);
|
||||
|
||||
refresh = true;
|
||||
}
|
||||
}
|
||||
|
||||
public ComController getCom() {
|
||||
return cc;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
if (isOver()) {
|
||||
return Message.GAME_OVER.toString();
|
||||
}
|
||||
|
||||
else if (isPlayersTurn()) {
|
||||
return Message.PLAYER_TURN.toString();
|
||||
}
|
||||
|
||||
else {
|
||||
return Message.COM_TURN.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public PlayerController getPlayer() {
|
||||
return pc;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public TileColor getTile(int r, int c) {
|
||||
return board.getTile(r, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCurrent() {
|
||||
return !refresh;
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
if (board.getTile(r, c) == TileColor.NONE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void playToken(Move move) {
|
||||
|
||||
board.setTile(move.getCell(), move.getColor());
|
||||
|
||||
if (playerTurn) {
|
||||
incrementScore();
|
||||
}
|
||||
|
||||
incrementTurn();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
refresh = false;
|
||||
}
|
||||
|
||||
private void incrementScore() {
|
||||
score++;
|
||||
}
|
||||
|
||||
private void incrementTurn() {
|
||||
playerTurn = !playerTurn;
|
||||
}
|
||||
|
||||
private boolean isPlayersTurn() {
|
||||
return playerTurn;
|
||||
}
|
||||
}
|
||||
48
src/model/comController/RandomComController.java
Normal file
48
src/model/comController/RandomComController.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package model.comController;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import model.Board;
|
||||
import model.Board.TileColor;
|
||||
import model.CellPointer;
|
||||
import model.ComController;
|
||||
import model.Move;
|
||||
|
||||
public class RandomComController extends ComController {
|
||||
|
||||
private final Random rand = new Random();
|
||||
|
||||
public RandomComController(Board brd) {
|
||||
super(brd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Move getMove() {
|
||||
TileColor tile = TileColor.BLUE;
|
||||
int r = -1;
|
||||
int c = -1;
|
||||
|
||||
while (tile != TileColor.NONE) {
|
||||
r = rand.nextInt(Board.NUM_ROWS);
|
||||
c = rand.nextInt(Board.NUM_COLS);
|
||||
|
||||
tile = board.getTile(r, c);
|
||||
}
|
||||
|
||||
switch (rand.nextInt(4)) {
|
||||
case 0:
|
||||
tile = TileColor.BLUE;
|
||||
break;
|
||||
case 1:
|
||||
tile = TileColor.GREEN;
|
||||
break;
|
||||
case 2:
|
||||
tile = TileColor.RED;
|
||||
break;
|
||||
case 3:
|
||||
tile = TileColor.YELLOW;
|
||||
}
|
||||
|
||||
return new Move(new CellPointer(r, c), tile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user