Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Michael Gold on 6/15/2017.
- */
- public class GameLogic {
- private Board board;
- private JBoard jboard;
- private Token.Color color;
- public GameLogic(Board board, JBoard jboard) {
- this.board = board;
- this.jboard = jboard;
- this.color = Token.Color.Red;
- }
- public void giveTurn(Position justPlayed) {
- if(this.didTheyWin(justPlayed)) {
- }
- if(this.color == Token.Color.Red) this.color = Token.Color.Blue;
- else this.color = Token.Color.Red;
- //give a color:::
- //wait for player to play:::
- if(this.didTheyWin(justPlayed)) {
- //game is over, there is a winner.
- }
- //else if(/* no more tiles to play*/) {
- //game is over, it's a draw.
- //}
- this.jboard.update();
- //set whose turn it is
- //enable for that player
- //disable for other player
- //wait for them to play
- //check if game is over, if so then declare this player winner and other player loser
- }
- private boolean didTheyWin(Position p) {
- //get a player to check for, just after their turn.
- //for each direction from the new token:
- //check columns, rows, diagonal, diagonal for four in a row.
- //Note: this method calculates the total number of connections. There only need to be at least 3 connections to call it "four in a row."
- //search NE and SW
- Position ne = new Position(1, -1);
- Position sw = new Position(-1, 1);
- if(this.board.sameColorPerDirection(p, ne) + this.board.sameColorPerDirection(p, sw) > 2) {return true;}
- //search E and W
- Position e = new Position(1, 0);
- Position w = new Position(-1, 0);
- if(this.board.sameColorPerDirection(p, e) + this.board.sameColorPerDirection(p, w) > 2) {return true;}
- //search SE and NW
- Position se = new Position(1, 1);
- Position nw = new Position(-1, -1);
- if(this.board.sameColorPerDirection(p, se) + this.board.sameColorPerDirection(p, nw) > 2) {return true;}
- //search S (no need to check north, impossible for anything to be above the just played tile)
- Position s = new Position (0, 1);
- if(this.board.sameColorPerDirection(p, s) + 1 > 3) {return true;}
- return false;
- }
- }
- public class Board {
- private int width;
- private int height;
- private Tile[][] tiles;
- public Board(int width, int height) {
- this.width = width;
- this.height = height;
- this.tiles = new Tile[this.width][this.height];
- for(int x=0; x<this.width; x++) {
- for(int y=0; y<this.height; y++) {
- this.tiles[x][y] = new Tile();
- }
- }
- this.reset();
- }
- public void reset() {
- for(int x=0; x<this.width; x++) {
- for(int y=0; y<this.height; y++) {
- Tile t = this.tiles[x][y];
- if(y == this.height-1) {t.setIsPlayable(true);}
- else t.setIsPlayable(false);
- }
- }
- }
- public int sameColorPerDirection(Position start, Position direction) {
- int counter = 0;
- Position nbr = Position.add(start, direction);
- if(this.getTile(start).getToken().getColor() == this.getTile(nbr).getToken().getColor()) {
- counter++;
- counter += sameColorPerDirection(nbr, direction);
- }
- return counter;
- //0
- //add
- //if equals...
- //1
- //0
- //add
- //if equals...
- //1
- //0
- //add
- //if equals... nope
- //returns 0;
- //1+0;
- //returns 1;
- //1+1;
- //returns 2;
- //this is correct
- }
- public void updatePlayableTiles(Position curPos) {
- //input position is the most recently played tile.
- this.tiles[curPos.getX()][curPos.getY()].setIsPlayable(false);
- if(curPos.getY() > 0) { this.tiles[curPos.getX()][curPos.getY() - 1].setIsPlayable(true); }
- }
- public Tile getTile(Position p) {
- if (p.getX() < 0 || p.getX() > this.width) return null;
- if (p.getY() < 0 || p.getY() > this.height) return null;
- return this.tiles[p.getX()][p.getY()];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement