Skip to content
Snippets Groups Projects
Commit bdac9f40 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

logic

parent 9fd0fdce
No related branches found
No related tags found
No related merge requests found
Showing
with 342 additions and 0 deletions
File added
File added
File added
File added
File added
File added
package de.deadlocker8.roadgame.logic;
import java.util.ArrayList;
import javafx.geometry.Point2D;
public class Board
{
private ArrayList<Tile> tiles;
private PossibleTiles possibleTiles;
public Board()
{
this.tiles = new ArrayList<>();
this.possibleTiles = new PossibleTiles();
initBoard();
}
private void initBoard()
{
Tile startTile = possibleTiles.getRandomTile();
startTile.setPosition(new Point2D(0, 0));
tiles.add(startTile);
}
public ArrayList<Tile> getTiles()
{
return tiles;
}
public boolean containsTileAtPosition(int x, int y)
{
for(Tile currentTile : tiles)
{
if((int)currentTile.getPosition().getX() == x && (int)currentTile.getPosition().getY() == y)
{
return true;
}
}
return false;
}
public ArrayList<Point2D> getFreeEdges(Tile tile, Tile playerTile)
{
ArrayList<Point2D> freeEdges = new ArrayList<>();
int x = (int)tile.getPosition().getX();
int y = (int)tile.getPosition().getY();
//North
if(!containsTileAtPosition(x, y-1))
{
if(tile.getN().equals(playerTile.getS()))
{
freeEdges.add(new Point2D(x, y-1));
}
}
//East
if(!containsTileAtPosition(x+1, y))
{
if(tile.getE().equals(playerTile.getW()))
{
}freeEdges.add(new Point2D(x+1, y));
}
//South
if(!containsTileAtPosition(x, y+1))
{
if(tile.getS().equals(playerTile.getN()))
{
freeEdges.add(new Point2D(x, y+1));
}
}
//West
if(!containsTileAtPosition(x-1, y))
{
if(tile.getW().equals(playerTile.getE()))
{
freeEdges.add(new Point2D(x-1, y));
}
}
return freeEdges;
}
public ArrayList<Point2D> getPossibleLocations(Tile tile)
{
ArrayList<Point2D> possibleLocations = new ArrayList<>();
for(Tile currentTile : tiles)
{
possibleLocations.addAll(getFreeEdges(currentTile, tile));
}
return possibleLocations;
}
public int getWidth()
{
int minX = 0;
int maxX = 0;
for(Tile currentTile : tiles)
{
if((int)currentTile.getPosition().getX() < minX)
{
minX = (int)currentTile.getPosition().getX();
}
if((int)currentTile.getPosition().getX() > maxX)
{
maxX = (int)currentTile.getPosition().getX();
}
}
return Math.abs(minX) + maxX + 1;
}
public int getHeight()
{
int minY = 0;
int maxY = 0;
for(Tile currentTile : tiles)
{
if((int)currentTile.getPosition().getY() < minY)
{
minY = (int)currentTile.getPosition().getY();
}
if((int)currentTile.getPosition().getX() > maxY)
{
maxY = (int)currentTile.getPosition().getY();
}
}
return Math.abs(minY) + maxY + 1;
}
public void addTile(Tile tile)
{
tiles.add(tile);
}
@Override
public String toString()
{
return "Board [tiles=" + tiles + "]";
}
}
\ No newline at end of file
package de.deadlocker8.roadgame.logic;
public enum EdgeLocation
{
NORTH, EAST, SOUTH, WEST
}
\ No newline at end of file
package de.deadlocker8.roadgame.logic;
public enum EdgeType
{
GRASS, ROAD
}
\ No newline at end of file
package de.deadlocker8.roadgame.logic;
import java.util.ArrayList;
import javafx.geometry.Point2D;
public class Game
{
private Board board;
private PossibleTiles possibleTiles;
public Game()
{
board = new Board();
possibleTiles = new PossibleTiles();
System.out.println(board);
}
public Tile getNextTile()
{
return possibleTiles.getRandomTile();
}
public ArrayList<Point2D> getPossibleLocations(Tile tile)
{
return board.getPossibleLocations(tile);
}
public void placeTile(Tile tile, Point2D position)
{
tile.setPosition(position);
board.addTile(tile);
}
public static void main(String[] args)
{
Game g = new Game();
Tile t = g.getNextTile();
System.out.println(t);
System.out.println(g.getPossibleLocations(t));
System.out.println(g.board.getWidth());
System.out.println(g.board.getHeight());
g.placeTile(t, g.getPossibleLocations(t).get(0));
System.out.println(g.board);
System.out.println(g.board.getWidth());
System.out.println(g.board.getHeight());
}
}
\ No newline at end of file
package de.deadlocker8.roadgame.logic;
import java.util.ArrayList;
import java.util.Random;
public class PossibleTiles
{
private ArrayList<Tile> possibleTiles;
public PossibleTiles()
{
possibleTiles = new ArrayList<>();
//curve
possibleTiles.add(new Tile(EdgeType.ROAD, EdgeType.ROAD, EdgeType.GRASS, EdgeType.GRASS));
//straight
possibleTiles.add(new Tile(EdgeType.ROAD, EdgeType.GRASS, EdgeType.ROAD, EdgeType.GRASS));
//T-tile
possibleTiles.add(new Tile(EdgeType.ROAD, EdgeType.ROAD, EdgeType.ROAD, EdgeType.GRASS));
}
public ArrayList<Tile> getPossibleTiles()
{
return possibleTiles;
}
public Tile getRandomTile()
{
Random random = new Random();
int index = random.nextInt(possibleTiles.size());
Tile tile = possibleTiles.get(index);
//random rotation
int rotate = random.nextInt(3);
for(int i = 0; i < rotate; i++)
{
tile.rotateRight();
}
return tile;
}
}
\ No newline at end of file
package de.deadlocker8.roadgame.logic;
import javafx.geometry.Point2D;
public class Tile
{
private EdgeType N;
private EdgeType E;
private EdgeType S;
private EdgeType W;
private Point2D position;
public Tile(EdgeType N, EdgeType E, EdgeType S, EdgeType W, int x, int y)
{
this.N = N;
this.E = E;
this.S = S;
this.W = W;
this.position = new Point2D(x, y);
}
public Tile(EdgeType N, EdgeType E, EdgeType S, EdgeType W)
{
this.N = N;
this.E = E;
this.S = S;
this.W = W;
this.position = null;
}
public EdgeType getN()
{
return N;
}
public EdgeType getE()
{
return E;
}
public EdgeType getS()
{
return S;
}
public EdgeType getW()
{
return W;
}
public Point2D getPosition()
{
return position;
}
public void setPosition(Point2D position)
{
this.position = position;
}
public void rotateRight()
{
EdgeType temp = N;
N = W;
W = S;
S = E;
E = temp;
}
public void rotateLeft()
{
EdgeType temp = N;
N = E;
E = S;
S = W;
W = temp;
}
@Override
public String toString()
{
return "Tile [N=" + N + ", E=" + E + ", S=" + S + ", W=" + W + ", position=" + position + "]";
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment