Commit 1abc43b1 authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

Default board supports 2, 4 and 6 players

parent 9aebe1bd
......@@ -10,29 +10,16 @@ import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.controllers.GameController;
import com.mygdx.game.controllers.NetworkController;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.model.DefaultBoard;
import com.mygdx.game.model.Game;
import com.mygdx.game.model.GameMode;
import com.mygdx.game.model.GameState;
import com.mygdx.game.model.gamemodes.rules.DefaultRules;
import com.mygdx.game.views.CinematicView;
import com.mygdx.game.views.GameViewManager;
import com.mygdx.game.views.LoadingView;
import com.mygdx.game.views.PlayView;
import com.mygdx.game.views.enums.CharacterAssets;
import com.mygdx.game.views.enums.CinematicAssets;
import com.mygdx.game.views.enums.LobbyAssets;
import com.mygdx.game.views.enums.MenuAssets;
import java.util.ArrayList;
public class UniCheckersClient extends ApplicationAdapter {
PlayerController playerController;
GameController gameController;
private GameViewManager gvm;
private SpriteBatch sb;
......@@ -53,7 +40,7 @@ public class UniCheckersClient extends ApplicationAdapter {
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);
// Initialize Models..
// Initialize Models.. // TODO: remove?
// Initialize controllers..
NetworkController networkController = new NetworkController();
......@@ -67,7 +54,6 @@ public class UniCheckersClient extends ApplicationAdapter {
gvm = new GameViewManager();
gvm.push(new LoadingView(gvm, playerController, assetManager, stage, skin));
//gvm.push(new CinematicView(gvm, playerController, assetManager, stage, skin));
}
@Override
......
......@@ -18,6 +18,7 @@ public class GameController {
public GameController(Game model, PlayView view, PlayerController playerController) {
this.model = model;
this.view = view;
this.playerController = playerController;
this.activePiece = null;
this.previousCoordinateClicked = null;
......@@ -36,15 +37,17 @@ public class GameController {
if (activePiece != null) {
activePiece.setRotateHead(false);
}
// TODO: Check if the piece is owned by this player
activePiece = this.view.getPiece(cubeCoordinates);
activePiece.setRotateHead(true);
previousCoordinateClicked = cubeCoordinates;
// If clicked piece is owned by player, activate piece
if(this.model.getPieceOwnerId(cubeCoordinates) == playerController.getPlayer().getID()) {
activePiece = this.view.getPiece(cubeCoordinates);
activePiece.setRotateHead(true);
previousCoordinateClicked = cubeCoordinates;
}
} else { // Field does NOT have piece
if (activePiece != null) {
// TODO: Check if it is the players turn (could be done in model)
// Try to move piece in model, if successful, move piece in view
if (this.model.movePiece(previousCoordinateClicked, cubeCoordinates)) {
// Try to move piece in model. If successful, move piece in view
if (this.model.movePiece(previousCoordinateClicked, cubeCoordinates, playerController.getPlayer().getID())) {
this.view.movePiece(activePiece, cubeCoordinates);
activePiece.setRotateHead(false);
activePiece = null;
......
......@@ -21,7 +21,7 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.0.122"; //.136 //"192.168.87.20";
String IP4_LAN_ADDRESS = "192.168.0.122"; //136 //"192.168.87.20";
client.connect(10000, IP4_LAN_ADDRESS, 54555, 54777);
} catch (IOException e) {
e.printStackTrace();
......
......@@ -17,8 +17,8 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
this.playerTurnId = playerIds.elementAt(playerTurnIndex);
}
fun getStartFieldCoordinates(): List<List<Vector3>>? {
return this.gameState.getRules().generateStartFields()
fun getStartFieldCoordinates(): List<List<Vector3>> {
return this.gameState.getRules().generateStartFields(playerIds.size)
}
fun fieldExists(cubeCoordinates: Vector3): Boolean {
......@@ -29,30 +29,44 @@ class Game(gameState: GameState, playerIds: HashSet<Int>) {
return this.gameState.getBoardState().fields[cubeCoordinates]?.hasPiece() == true
}
fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3): Boolean {
// TODO: Check if valid move
val pieceMoved: Boolean = this.gameState.getBoardState().movePiece(fromCoordinates, toCoordinates)
fun movePiece(fromCoordinates: Vector3, toCoordinates: Vector3, playerId: Int): Boolean {
// Check if players turn
if (playerId == playerTurnId) {
// TODO: Check if valid move
val pieceMoved: Boolean = this.gameState.getBoardState().movePiece(fromCoordinates, toCoordinates)
if (pieceMoved) {
nextPlayer()
if (pieceMoved) {
nextPlayer()
}
return pieceMoved
} else {
return false
}
}
return pieceMoved
fun getPlayerTurnId(): Int {
return playerTurnId
}
fun nextPlayer() {
playerTurnIndex++
if (playerTurnIndex > playerIds.size) {
if (playerTurnIndex == playerIds.size) {
playerTurnIndex = 0
}
playerTurnId = playerIds.elementAt(playerTurnIndex)
}
fun isPlayerFinished(player: Player): Boolean {
val targetFields = this.gameState.getRules().getPlayerTargetFields(player.id)
fun getPieceOwnerId(coordinates: Vector3): Int {
return gameState.getBoardState().fields[coordinates]?.getPiece()?.getOwnerId() ?: -1
}
fun isPlayerFinished(playerId: Int): Boolean {
val targetFields = this.gameState.getRules().getPlayerTargetFields(playerId)
for (targetField: Vector3 in targetFields) {
if (this.gameState.getBoardState().fields[targetField]?.getPiece()?.getOwnerId() != player.id) {
if (this.gameState.getBoardState().fields[targetField]?.getPiece()?.getOwnerId() != playerId) {
return false
}
}
......
......@@ -28,16 +28,15 @@ class DefaultBoard: AbstractBoard() {
}
override fun placePiecesAtStart(rules: AbstractRules, playerIds: HashSet<Int>) {
var playerIndex = 0
for (coordinateSet: List<Vector3> in rules.generateStartFields()) {
val startFields: List<List<Vector3>> = rules.generateStartFields(playerIds.size)
for ((playerIndex, coordinateSet: List<Vector3>) in startFields.withIndex()) {
for (coordinate: Vector3 in coordinateSet) {
//val piece = Piece(playerIds.elementAt(playerIndex)) // TODO: use this
val piece = Piece(playerIndex)
val piece = Piece(playerIds.elementAt(playerIndex))
fields[coordinate]?.setPiece(piece)
}
playerIndex++
}
}
......
......@@ -8,7 +8,7 @@ abstract class AbstractRules {
abstract var moveRange: Int // How far a piece can be moved
abstract var jumpRange: Int // How far a piece can jump (over other pieces)
abstract fun getPlayerStartfields(playerId: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerId: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun generateStartFields(): List<List<Vector3>> // Returns a list with lists of startfields
abstract fun getPlayerStartfields(playerIndex: Int) : List<Vector3> // Returns a list of startfields for a given player
abstract fun getPlayerTargetFields(playerIndex: Int) : List<Vector3> // Returns a list of targetfields for a given player
abstract fun generateStartFields(playerCount: Int): List<List<Vector3>> // Returns a list with lists of startfields
}
\ No newline at end of file
......@@ -6,16 +6,16 @@ import com.mygdx.game.model.Field
class DefaultRules: AbstractRules() {
// Sequence: top, upperright, lowerright, bottom, lowerleft, upperleft
override var startFields = generateStartFields()
override var startFields = generateStartFields(6) // TODO: is this used/necessary?
override var moveRange = 1
override var jumpRange = 2
override fun getPlayerStartfields(playerId: Int): List<Vector3> {
return startFields[playerId]
override fun getPlayerStartfields(playerIndex: Int): List<Vector3> {
return startFields[playerIndex]
}
override fun getPlayerTargetFields(playerId: Int): List<Vector3> {
var index = playerId
override fun getPlayerTargetFields(playerIndex: Int): List<Vector3> {
var index = playerIndex
for (i in 0..2) {
index++
......@@ -27,34 +27,53 @@ class DefaultRules: AbstractRules() {
return startFields[index]
}
override fun generateStartFields(): List<List<Vector3>> {
return(listOf(
listOf( Vector3(1F,4F,-5F), Vector3(2F,3F,-5F), Vector3(3F,2F,-5F), Vector3(4F,1F,-5F),
Vector3(2F,4F,-6F), Vector3(3F,3F,-6F), Vector3(4F,2F,-6F), Vector3(3F,4F,-7F),
Vector3(4F,3F,-7F), Vector3(4F,4F,-8F)
),
listOf( Vector3(5F,-1F,-4F), Vector3(5F,-2F,-3F), Vector3(5F,-3F,-2F), Vector3(5F,-4F,-1F),
Vector3(6F,-2F,-4F), Vector3(6F,-3F,-3F), Vector3(6F,-4F,-2F), Vector3(7F,-3F,-4F),
Vector3(7F,-4F,-3F), Vector3(8F,-4F,-4F)
),
listOf( Vector3(4F,-5F,1F), Vector3(3F,-5F,2F), Vector3(2F,-5F,3F), Vector3(1F,-5F,4F),
Vector3(4F,6F,2F), Vector3(3F,-6F,3F), Vector3(2F,-6F,4F), Vector3(4F,-7F,3F),
Vector3(3F,-7F,4F), Vector3(4F,-8F,4F)
),
listOf( Vector3(-4F,-1F,5F), Vector3(-3F,-2F,5F), Vector3(-2F,-3F,5F), Vector3(-1F,-4F,5F),
Vector3(-4F,-2F,6F), Vector3(-3F,-3F,6F), Vector3(-2F,-4F,6F), Vector3(-4F,-3F,7F),
Vector3(-3F,-4F,7F), Vector3(-4F,-4F,8F)
),
listOf( Vector3(-5F,4F,1F), Vector3(-5F,3F,2F), Vector3(-5F,2F,3F), Vector3(-5F,1F,4F),
Vector3(-6F,4F,2F), Vector3(-6F,3F,3F), Vector3(-6F,2F,4F), Vector3(-7F,4F,3F),
Vector3(-7F, 3F, 4F), Vector3(-8F,4F,4F)
),
listOf( Vector3(-1F,5F,-4F), Vector3(-2F,5F,-3F), Vector3(-3F,5F,-2F), Vector3(-4F,5F,-1F),
Vector3(-2F,6F,-4F), Vector3(-3F,6F,-3F), Vector3(-4F,6F,-2F), Vector3(-3F,7F,-4F),
Vector3(-4F,7F,-3F), Vector3(-4F,8F,-4F)
)
)
override fun generateStartFields(playerCount: Int): List<List<Vector3>> {
val startFields: MutableList<List<Vector3>> = mutableListOf()
val allStartFields: List<List<Vector3>> = listOf(
listOf( Vector3(1F,4F,-5F), Vector3(2F,3F,-5F), Vector3(3F,2F,-5F), Vector3(4F,1F,-5F),
Vector3(2F,4F,-6F), Vector3(3F,3F,-6F), Vector3(4F,2F,-6F), Vector3(3F,4F,-7F),
Vector3(4F,3F,-7F), Vector3(4F,4F,-8F)
),
listOf( Vector3(5F,-1F,-4F), Vector3(5F,-2F,-3F), Vector3(5F,-3F,-2F), Vector3(5F,-4F,-1F),
Vector3(6F,-2F,-4F), Vector3(6F,-3F,-3F), Vector3(6F,-4F,-2F), Vector3(7F,-3F,-4F),
Vector3(7F,-4F,-3F), Vector3(8F,-4F,-4F)
),
listOf( Vector3(4F,-5F,1F), Vector3(3F,-5F,2F), Vector3(2F,-5F,3F), Vector3(1F,-5F,4F),
Vector3(4F,6F,2F), Vector3(3F,-6F,3F), Vector3(2F,-6F,4F), Vector3(4F,-7F,3F),
Vector3(3F,-7F,4F), Vector3(4F,-8F,4F)
),
listOf( Vector3(-4F,-1F,5F), Vector3(-3F,-2F,5F), Vector3(-2F,-3F,5F), Vector3(-1F,-4F,5F),
Vector3(-4F,-2F,6F), Vector3(-3F,-3F,6F), Vector3(-2F,-4F,6F), Vector3(-4F,-3F,7F),
Vector3(-3F,-4F,7F), Vector3(-4F,-4F,8F)
),
listOf( Vector3(-5F,4F,1F), Vector3(-5F,3F,2F), Vector3(-5F,2F,3F), Vector3(-5F,1F,4F),
Vector3(-6F,4F,2F), Vector3(-6F,3F,3F), Vector3(-6F,2F,4F), Vector3(-7F,4F,3F),
Vector3(-7F, 3F, 4F), Vector3(-8F,4F,4F)
),
listOf( Vector3(-1F,5F,-4F), Vector3(-2F,5F,-3F), Vector3(-3F,5F,-2F), Vector3(-4F,5F,-1F),
Vector3(-2F,6F,-4F), Vector3(-3F,6F,-3F), Vector3(-4F,6F,-2F), Vector3(-3F,7F,-4F),
Vector3(-4F,7F,-3F), Vector3(-4F,8F,-4F)
)
)
when (playerCount) {
2 -> {
startFields.add(allStartFields[0])
startFields.add(allStartFields[3])
}
4 -> {
startFields.add(allStartFields[1])
startFields.add(allStartFields[2])
startFields.add(allStartFields[4])
startFields.add(allStartFields[5])
}
6 -> startFields.addAll(allStartFields)
else -> {
print("ERROR: DefaultBoard needs 2, 4 or 6 players.")
}
}
return startFields
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment