@@ -1,6 +1,7 @@
package se.olander.android.copsandrobbers.models;

import android.graphics.Color;
import android.util.Log;

import java.util.Map;

@@ -15,14 +16,17 @@ public class GameEngine implements GraphView.OnNodeClickListener {
private GameState gameState;
private Graph graph;
private OnGameEventHandler onGameEventHandler;
private int numberOfTurns;
private long startTime;

private Cop focusedCop;

public GameEngine(Level level) {
graph = new Graph(level);
gameState = GameState.MOVE_COPS;
robberAI = new MiniMaxRobberAI(graph);
// robberAI.initialize();
numberOfTurns = 0;
startTime = System.currentTimeMillis();

for (Cop cop : graph.getCops()) {
cop.getCurrentNode().setHighlight(Color.GRAY);
@@ -35,36 +39,42 @@ public void setOnGameEventHandler(OnGameEventHandler onGameEventHandler) {

@Override
public void onNodeClick(Node node) {
if (focusedCop != null) {
Node focusedNode = focusedCop.getCurrentNode();
if (node.hasCop()) {
Cop cop = node.getAnyCop();
cop.getCurrentNode().setHighlight(Color.BLUE);
for (Node neighbour : graph.getNeighbours(cop.getCurrentNode())) {
neighbour.setHighlight(Color.GREEN);
}
focusedCop = cop;
}
else if (focusedCop != null) {
if (graph.areNeighbours(focusedCop.getCurrentNode(), node)) {
for (Node n : graph.getNodes()) {
n.setHighlight(null);
}
focusedCop.move(node);
focusedNode = node;
focusedNode.setHighlight(Color.BLUE);
for (Node neighbour : graph.getNeighbours(focusedNode)) {
node.setHighlight(Color.BLUE);
for (Node neighbour : graph.getNeighbours(node)) {
neighbour.setHighlight(Color.GREEN);
}
endOfTurn();
}
}
else {
if (node.getCops().size() == 1) {
Cop cop = node.getAnyCop();
cop.getCurrentNode().setHighlight(Color.BLUE);
for (Node neighbour : graph.getNeighbours(cop.getCurrentNode())) {
neighbour.setHighlight(Color.GREEN);
}
focusedCop = cop;
}
}
}

private void endOfTurn() {
numberOfTurns += 1;
killRobbers();
moveRobbers();
if (allRobbersAreDead()) {
victory();
}
else {
moveRobbers();
}
}

private void victory() {
onGameEventHandler.victory();
}

private void killRobbers() {
@@ -79,20 +89,37 @@ private void killRobbers() {
private void moveRobbers() {
Map<Robber, Node> moves = robberAI.calculateMoves();
for (Map.Entry<Robber, Node> move : moves.entrySet()) {
move.getKey().move(move.getValue());
Robber robber = move.getKey();
if (robber.isDead()) {
continue;
}

Node node = move.getValue();
robber.move(node);
}
}

private void onCopMove(Node from, Node to) {
// if (to.isRobber()) {
// onGameEventHandler.victory();
// }
private boolean allRobbersAreDead() {
for (Robber robber : graph.getRobbers()) {
if (!robber.isDead()) {
return false;
}
}
return true;
}

public Graph getGraph() {
return graph;
}

public int getNumberOfTurns() {
return numberOfTurns;
}

public long getTotalTime() {
return System.currentTimeMillis() - startTime;
}

public enum GameState {
DIALOG,
MOVE_COPS,
@@ -74,4 +74,19 @@ public void setStartNode(int startNode) {
this.startNode = startNode;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Level level = (Level) o;

return title != null ? title.equals(level.title) : level.title == null;
}

@Override
public int hashCode() {
return title != null ? title.hashCode() : 0;
}
}
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="You won!"
android:textSize="36sp" />

<Space
android:layout_width="wrap_content"
android:layout_height="20dp" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Turns"
android:textSize="18sp" />

<TextView
android:id="@+id/number_of_turns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textSize="18sp"
android:text="9999" />

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Time"
android:textSize="18sp" />

<TextView
android:id="@+id/total_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textSize="18sp"
android:text="9999" />

</RelativeLayout>

<Space
android:layout_width="wrap_content"
android:layout_height="20dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
android:id="@+id/nextLevel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next Level"/>

<Button
android:id="@+id/select_levels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to Levels"/>

<Button
android:id="@+id/replay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Replay"/>

</LinearLayout>

</LinearLayout>