Merge branch 'master' of https://github.com/SemvdH/netwerk-programming-eindopdracht
This commit is contained in:
Binary file not shown.
@@ -162,7 +162,7 @@ public class MainGame extends Game implements ClientCallback {
|
||||
|
||||
private void playSong() {
|
||||
// play music
|
||||
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/beat.mp3", Files.FileType.Internal));
|
||||
Music music = Gdx.audio.newMusic(Gdx.files.getFileHandle("core/assets/sound/beat.mp3", Files.FileType.Internal));
|
||||
music.setVolume(.1f);
|
||||
music.play();
|
||||
music.setLooping(true);
|
||||
@@ -205,7 +205,8 @@ public class MainGame extends Game implements ClientCallback {
|
||||
renderTurnText();
|
||||
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
|
||||
clearRender(67, 168, 186, 1);
|
||||
renderString("FACTION SELECT\nYou are: " + username + "\nPress 1 for mega corporation, press 2 for hackers", Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
|
||||
String text = username == null ? "Connecting to server..." : "FACTION SELECT\nYou are: " + username + "\nPress 1 for mega corporation, press 2 for hackers";
|
||||
renderString(text, Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
|
||||
if (this.ready && this.enemyReady) {
|
||||
if (this.chosenFaction == Faction.HACKER) {
|
||||
chooseHacker();
|
||||
@@ -392,7 +393,7 @@ public class MainGame extends Game implements ClientCallback {
|
||||
} else if (data instanceof TeamData) {
|
||||
// check if it is not our own message
|
||||
if (!((TeamData) data).getUsername().equals(this.username)) {
|
||||
// if we have already chosen a faction, so we were first
|
||||
// if we have not yet chosen a faction, select the opposing faction
|
||||
TeamData teamData = (TeamData) data;
|
||||
Faction enemyFaction = teamData.getFaction();
|
||||
if (this.chosenFaction == null) {
|
||||
@@ -410,7 +411,7 @@ public class MainGame extends Game implements ClientCallback {
|
||||
if (!moveData.getUsername().equals(this.username)) {
|
||||
GameTile tile = mapRenderer.getGameTile(moveData.getPos());
|
||||
GameCharacter character = enemyTeam.get(moveData.getCharacterName());
|
||||
gameInputProcessor.removeCharacterFromTile(character);
|
||||
mapRenderer.removeCharacterFromTile(character);
|
||||
tile.visit(character);
|
||||
}
|
||||
} else if (data instanceof DamageData) {
|
||||
|
||||
@@ -94,7 +94,6 @@ public class GameInputProcessor implements InputProcessor {
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
// System.out.println(camera.position.x + " , " + camera.position.y);
|
||||
if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
|
||||
|
||||
if (keysList.contains(keycode)) {
|
||||
@@ -156,14 +155,14 @@ public class GameInputProcessor implements InputProcessor {
|
||||
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
|
||||
|
||||
if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
|
||||
removeCharacterFromTile(mainGame.getSelectedCharacter());
|
||||
mainGame.mapRenderer.removeCharacterFromTile(mainGame.getSelectedCharacter());
|
||||
gameTile.visit(mainGame.getSelectedCharacter());
|
||||
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
|
||||
mainGame.increaseTurn();
|
||||
mainGame.send(new MoveData(mainGame.getUsername(), mainGame.getSelectedCharacter().getName(), mainGame.mapRenderer.getPos(gameTile)));
|
||||
}
|
||||
}
|
||||
// clicking on enemy
|
||||
// clicking on enemy
|
||||
if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) {
|
||||
if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
|
||||
if (!gameTile.getCharacter().isDead()) {
|
||||
@@ -202,18 +201,7 @@ public class GameInputProcessor implements InputProcessor {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeCharacterFromTile(GameCharacter character) {
|
||||
rowLoop:
|
||||
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
|
||||
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
|
||||
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
|
||||
gameTile.removeCharacter();
|
||||
break rowLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
|
||||
@@ -240,6 +240,23 @@ public class MapRenderer implements Renderable {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* remove character from tile
|
||||
* @param character the character to remove
|
||||
*/
|
||||
public void removeCharacterFromTile(GameCharacter character) {
|
||||
rowLoop:
|
||||
for (int row = 0; row < getGameTiles().length; row++) {
|
||||
for (int col = 0; col < getGameTiles()[0].length; col++) {
|
||||
GameTile gameTile = getGameTiles()[row][col];
|
||||
if (gameTile.containsCharacter() && gameTile.getCharacter().equals(character)) {
|
||||
gameTile.removeCharacter();
|
||||
break rowLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* resize the screen
|
||||
* @param screenWidth the width of the screen
|
||||
|
||||
@@ -1,107 +1,112 @@
|
||||
package netwerkprog.game.util.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
protected TreeNode<E> root;
|
||||
protected int size = 0;
|
||||
|
||||
public int sum () {
|
||||
|
||||
public int sum() {
|
||||
return this.sum(this.getRoot());
|
||||
}
|
||||
|
||||
public int sum( TreeNode<E> node ) {
|
||||
// Schrijf hier jouw code...
|
||||
public int sum(TreeNode<E> node) {
|
||||
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nodeValue = (Integer) node.element; // Tip, omdat E nog onbekend is doen we het zo (niet helemaal netjes)
|
||||
int nodeValue = (Integer) node.element;
|
||||
return sum(node.left) + sum(node.right);
|
||||
}
|
||||
|
||||
public int totalLeaves () {
|
||||
|
||||
public int totalLeaves() {
|
||||
return this.totalLeaves(this.getRoot());
|
||||
}
|
||||
|
||||
public int totalLeaves ( TreeNode<E> node ) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
if (node.left == null && node.right == null) {
|
||||
return 1;
|
||||
}
|
||||
return totalLeaves(node.left) + totalLeaves(node.right);
|
||||
public int totalLeaves(TreeNode<E> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
if (node.left == null && node.right == null) {
|
||||
return 1;
|
||||
}
|
||||
return totalLeaves(node.left) + totalLeaves(node.right);
|
||||
}
|
||||
|
||||
/** Create a default binary tree */
|
||||
|
||||
/**
|
||||
* Create a default binary tree
|
||||
*/
|
||||
public BST() {
|
||||
}
|
||||
|
||||
/** Create a binary tree from an array of objects */
|
||||
/**
|
||||
* Create a binary tree from an array of objects
|
||||
*/
|
||||
public BST(E[] objects) {
|
||||
for (int i = 0; i < objects.length; i++)
|
||||
insert(objects[i]);
|
||||
for (E object : objects) insert(object);
|
||||
}
|
||||
|
||||
@Override /** Returns true if the element is in the tree */
|
||||
/**
|
||||
* Returns true if the element is in the tree
|
||||
*/
|
||||
@Override
|
||||
public boolean search(E e) {
|
||||
return search(e, root);
|
||||
}
|
||||
|
||||
private boolean search(E e, TreeNode<E> tree)
|
||||
{
|
||||
if (tree == null)
|
||||
{
|
||||
private boolean search(E e, TreeNode<E> tree) {
|
||||
// nog niet correct
|
||||
if (tree == null) {
|
||||
return false;
|
||||
}
|
||||
if (e.compareTo(tree.element) == 0)
|
||||
{
|
||||
if (e.compareTo(tree.element) == 0) {
|
||||
return true;
|
||||
}
|
||||
if (e.compareTo(tree.element) < 0)
|
||||
{
|
||||
if (e.compareTo(tree.element) < 0) {
|
||||
return search(e, tree.left);
|
||||
}
|
||||
|
||||
else // (e.compareTo(tree.element) > 0)
|
||||
} else // (e.compareTo(tree.element) > 0)
|
||||
{
|
||||
return search(e, tree.right);
|
||||
}
|
||||
}
|
||||
|
||||
@Override /** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully */
|
||||
public boolean insert(E e) {
|
||||
/**
|
||||
* Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully
|
||||
*/
|
||||
@Override
|
||||
public void insert(E e) {
|
||||
if (root == null) {
|
||||
root = createNewNode(e); // Create a new root
|
||||
size++;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return insert(e, root);
|
||||
} else {
|
||||
insert(e, root);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Insert element o into the binary tree
|
||||
/**
|
||||
* Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully
|
||||
pre: root != null
|
||||
* pre: root != null
|
||||
*/
|
||||
public boolean insert(E e, TreeNode<E> tree) {
|
||||
if (e.compareTo(tree.element) == 0) {
|
||||
return false; // Duplicate node not inserted
|
||||
}
|
||||
else if (e.compareTo(tree.element) < 0 && tree.left != null)
|
||||
return insert(e, tree.left);
|
||||
} else if (e.compareTo(tree.element) < 0 && tree.left != null)
|
||||
return insert(e, tree.left);
|
||||
|
||||
else if (e.compareTo(tree.element) > 0 && tree.right != null)
|
||||
return insert(e, tree.right);
|
||||
|
||||
// Create the new node and attach it to the parent node
|
||||
// Create the new node and attach it to the parent node
|
||||
else {
|
||||
if (e.compareTo(tree.element) < 0) {
|
||||
tree.left = createNewNode(e);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
tree.right = createNewNode(e);
|
||||
}
|
||||
size++;
|
||||
@@ -111,15 +116,20 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
|
||||
|
||||
protected TreeNode<E> createNewNode(E e) {
|
||||
return new TreeNode<E>(e);
|
||||
return new TreeNode<>(e);
|
||||
}
|
||||
|
||||
@Override /** Inorder traversal from the root*/
|
||||
/**
|
||||
* Inorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
public void inorder() {
|
||||
inorder(root);
|
||||
}
|
||||
|
||||
/** Inorder traversal from a subtree */
|
||||
/**
|
||||
* Inorder traversal from a subtree
|
||||
*/
|
||||
protected void inorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
inorder(root.left);
|
||||
@@ -127,12 +137,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
inorder(root.right);
|
||||
}
|
||||
|
||||
@Override /** Postorder traversal from the root */
|
||||
/**
|
||||
* Post order traversal from the root
|
||||
*/
|
||||
@Override
|
||||
public void postorder() {
|
||||
postorder(root);
|
||||
}
|
||||
|
||||
/** Postorder traversal from a subtree */
|
||||
/**
|
||||
* Post order traversal from a subtree
|
||||
*/
|
||||
protected void postorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
postorder(root.left);
|
||||
@@ -140,12 +155,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
System.out.print(root.element + " ");
|
||||
}
|
||||
|
||||
@Override /** Preorder traversal from the root */
|
||||
/**
|
||||
* Preorder traversal from the root
|
||||
*/
|
||||
@Override
|
||||
public void preorder() {
|
||||
preorder(root);
|
||||
}
|
||||
|
||||
/** Preorder traversal from a subtree */
|
||||
/**
|
||||
* Preorder traversal from a subtree
|
||||
*/
|
||||
protected void preorder(TreeNode<E> root) {
|
||||
if (root == null) return;
|
||||
System.out.print(root.element + " ");
|
||||
@@ -153,8 +173,10 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
preorder(root.right);
|
||||
}
|
||||
|
||||
/** This inner class is static, because it does not access
|
||||
any instance members defined in its outer class */
|
||||
/**
|
||||
* This inner class is static, because it does not access
|
||||
* any instance members defined in its outer class
|
||||
*/
|
||||
public static class TreeNode<E extends Comparable<E>> {
|
||||
protected E element;
|
||||
protected TreeNode<E> left;
|
||||
@@ -165,41 +187,49 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override /** Get the number of nodes in the tree */
|
||||
/**
|
||||
* Get the number of nodes in the tree
|
||||
*/
|
||||
@Override
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/** Returns the root of the tree */
|
||||
/**
|
||||
* Returns the root of the tree
|
||||
*/
|
||||
public TreeNode<E> getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/** Returns a path from the root leading to the specified element */
|
||||
public java.util.ArrayList<TreeNode<E>> path(E e) {
|
||||
java.util.ArrayList<TreeNode<E>> list =
|
||||
new java.util.ArrayList<TreeNode<E>>();
|
||||
/**
|
||||
* Returns a path from the root leading to the specified element
|
||||
*/
|
||||
public ArrayList<TreeNode<E>> path(E e) {
|
||||
ArrayList<TreeNode<E>> list =
|
||||
new ArrayList<>();
|
||||
TreeNode<E> current = root; // Start from the root
|
||||
|
||||
while (current != null) {
|
||||
list.add(current); // Add the node to the list
|
||||
if (e.compareTo(current.element) < 0) {
|
||||
current = current.left;
|
||||
}
|
||||
else if (e.compareTo(current.element) > 0) {
|
||||
} else if (e.compareTo(current.element) > 0) {
|
||||
current = current.right;
|
||||
}
|
||||
else
|
||||
} else
|
||||
break;
|
||||
}
|
||||
|
||||
return list; // Return an array list of nodes
|
||||
}
|
||||
|
||||
@Override /** Delete an element from the binary tree.
|
||||
/**
|
||||
* Delete an element from the binary tree.
|
||||
* Return true if the element is deleted successfully
|
||||
* Return false if the element is not in the tree */
|
||||
public boolean delete(E e) {
|
||||
* Return false if the element is not in the tree
|
||||
*/
|
||||
@Override
|
||||
public void delete(E e) {
|
||||
// Locate the node to be deleted and also locate its parent node
|
||||
TreeNode<E> parent = null;
|
||||
TreeNode<E> current = root;
|
||||
@@ -207,32 +237,28 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
if (e.compareTo(current.element) < 0) {
|
||||
parent = current;
|
||||
current = current.left;
|
||||
}
|
||||
else if (e.compareTo(current.element) > 0) {
|
||||
} else if (e.compareTo(current.element) > 0) {
|
||||
parent = current;
|
||||
current = current.right;
|
||||
}
|
||||
else
|
||||
} else
|
||||
break; // Element is in the tree pointed at by current
|
||||
}
|
||||
|
||||
if (current == null)
|
||||
return false; // Element is not in the tree
|
||||
return; // Element is not in the tree
|
||||
|
||||
// Case 1: current has no left child
|
||||
if (current.left == null) {
|
||||
// Connect the parent with the right child of the current node
|
||||
if (parent == null) {
|
||||
root = current.right;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (e.compareTo(parent.element) < 0)
|
||||
parent.left = current.right;
|
||||
else
|
||||
parent.right = current.right;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Case 2: The current node has a left child
|
||||
// Locate the rightmost node in the left subtree of
|
||||
// the current node and also its parent
|
||||
@@ -251,15 +277,17 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
if (parentOfRightMost.right == rightMost)
|
||||
parentOfRightMost.right = rightMost.left;
|
||||
else
|
||||
// Special case: parentOfRightMost == current
|
||||
parentOfRightMost.left = rightMost.left;
|
||||
// Special case: parentOfRightMost == current
|
||||
parentOfRightMost.left = rightMost.left;
|
||||
}
|
||||
|
||||
size--;
|
||||
return true; // Element deleted successfully
|
||||
}
|
||||
|
||||
@Override /** Obtain an iterator. Use inorder. */
|
||||
/**
|
||||
* Obtain an iterator. Use inorder.
|
||||
*/
|
||||
@Override
|
||||
public java.util.Iterator<E> iterator() {
|
||||
return new InorderIterator();
|
||||
}
|
||||
@@ -267,41 +295,51 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
// Inner class InorderIterator
|
||||
private class InorderIterator implements java.util.Iterator<E> {
|
||||
// Store the elements in a list
|
||||
private java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<E>();
|
||||
private final java.util.ArrayList<E> list =
|
||||
new java.util.ArrayList<>();
|
||||
private int current = 0; // Point to the current element in list
|
||||
|
||||
public InorderIterator() {
|
||||
inorder(); // Traverse binary tree and store elements in list
|
||||
}
|
||||
|
||||
/** Inorder traversal from the root*/
|
||||
/**
|
||||
* Inorder traversal from the root
|
||||
*/
|
||||
private void inorder() {
|
||||
inorder(root);
|
||||
}
|
||||
|
||||
/** Inorder traversal from a subtree */
|
||||
/**
|
||||
* Inorder traversal from a subtree
|
||||
*/
|
||||
private void inorder(TreeNode<E> root) {
|
||||
if (root == null)return;
|
||||
if (root == null) return;
|
||||
inorder(root.left);
|
||||
list.add(root.element);
|
||||
inorder(root.right);
|
||||
}
|
||||
|
||||
@Override /** More elements for traversing? */
|
||||
/**
|
||||
* More elements for traversing?
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (current < list.size())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return current < list.size();
|
||||
}
|
||||
|
||||
@Override /** Get the current element and move to the next */
|
||||
/**
|
||||
* Get the current element and move to the next
|
||||
*/
|
||||
@Override
|
||||
public E next() {
|
||||
return list.get(current++);
|
||||
}
|
||||
|
||||
@Override /** Remove the current element */
|
||||
/**
|
||||
* Remove the current element
|
||||
*/
|
||||
@Override
|
||||
public void remove() {
|
||||
delete(list.get(current)); // Delete the current element
|
||||
list.clear(); // Clear the list
|
||||
@@ -309,7 +347,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Remove all elements from the tree */
|
||||
/**
|
||||
* Remove all elements from the tree
|
||||
*/
|
||||
public void clear() {
|
||||
root = null;
|
||||
size = 0;
|
||||
@@ -317,9 +357,9 @@ public class BST<E extends Comparable<E>> extends AbstractTree<E> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String res = "";
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (E e : this) {
|
||||
res += e.toString();
|
||||
res.append(e.toString());
|
||||
}
|
||||
return "BST{" +
|
||||
"root=" + root +
|
||||
|
||||
@@ -2,28 +2,28 @@ package netwerkprog.game.util.tree;
|
||||
|
||||
public interface Tree<E> extends Iterable<E> {
|
||||
/** Return true if the element is in the tree */
|
||||
boolean search(E e);
|
||||
public boolean search(E e);
|
||||
|
||||
/** Insert element o into the binary tree
|
||||
* Return true if the element is inserted successfully */
|
||||
boolean insert(E e);
|
||||
public void insert(E e);
|
||||
|
||||
/** Delete the specified element from the tree
|
||||
* Return true if the element is deleted successfully */
|
||||
boolean delete(E e);
|
||||
public void delete(E e);
|
||||
|
||||
/** Inorder traversal from the root*/
|
||||
void inorder();
|
||||
public void inorder();
|
||||
|
||||
/** Postorder traversal from the root */
|
||||
void postorder();
|
||||
public void postorder();
|
||||
|
||||
/** Preorder traversal from the root */
|
||||
void preorder();
|
||||
public void preorder();
|
||||
|
||||
/** Get the number of nodes in the tree */
|
||||
int getSize();
|
||||
public int getSize();
|
||||
|
||||
/** Return true if the tree is empty */
|
||||
boolean isEmpty();
|
||||
public boolean isEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user