Merge branch 'networking' into networking-start-online-game
This commit is contained in:
commit
998ff3d663
|
@ -1,5 +1,4 @@
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.awt.Point;
|
|
||||||
|
|
||||||
public abstract class AiPlayer extends Player {
|
public abstract class AiPlayer extends Player {
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import java.awt.*;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
@ -13,7 +13,7 @@ public class Board {
|
||||||
this.createShip(size - 13);
|
this.createShip(size - 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HitResponse hit (Point point){
|
public synchronized HitResponse hit (Point point){
|
||||||
HitResponse response = new HitResponse(HitResponseType.MISS,point);
|
HitResponse response = new HitResponse(HitResponseType.MISS,point);
|
||||||
for (int i = 0; i < this.ships.size(); i++) {
|
for (int i = 0; i < this.ships.size(); i++) {
|
||||||
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
||||||
|
@ -50,7 +50,7 @@ public class Board {
|
||||||
return ships;
|
return ships;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addHits(HitResponse hitResponse) {
|
public synchronized boolean addHits(HitResponse hitResponse) {
|
||||||
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
|
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
|
||||||
this.hits.add(hitResponse);
|
this.hits.add(hitResponse);
|
||||||
return true;
|
return true;
|
||||||
|
@ -58,7 +58,7 @@ public class Board {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HitResponse getHitResponsOnPoint(Point point) {
|
public synchronized HitResponse getHitResponsOnPoint(Point point) {
|
||||||
for (int i = 0; i < this.hits.size(); i++){
|
for (int i = 0; i < this.hits.size(); i++){
|
||||||
if (this.hits.get(i).getPoint().equals(point)){
|
if (this.hits.get(i).getPoint().equals(point)){
|
||||||
return this.hits.get(i);
|
return this.hits.get(i);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public class HitResponse {
|
public class HitResponse {
|
||||||
private HitResponseType type;
|
private HitResponseType type;
|
||||||
|
@ -9,6 +9,15 @@ public class HitResponse {
|
||||||
this.point = point;
|
this.point = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HitResponse (int typeIndex, Point point) {
|
||||||
|
if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) {
|
||||||
|
this.type = HitResponseType.values()[typeIndex];
|
||||||
|
this.point = point;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public HitResponseType getHitResponse() {
|
public HitResponseType getHitResponse() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
@ -20,4 +29,9 @@ public class HitResponse {
|
||||||
public void setType(HitResponseType type) {
|
public void setType(HitResponseType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.getPoint().toString() + " " + this.type.ordinal();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,19 @@ public class LocalPlayer extends Player {
|
||||||
super(size);
|
super(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveShoot(Point point) {
|
||||||
|
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
||||||
|
if (!(hitResponse == null)){
|
||||||
|
enemy.receiveHit(hitResponse);
|
||||||
|
} else {
|
||||||
|
enemy.receiveHit(this.board.hit(point));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
enemy.board.addHits(hitResponse);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,11 +1,18 @@
|
||||||
public abstract class OnlinePlayer extends Player implements AsyncSocketListener{
|
public abstract class OnlinePlayer extends Player implements AsyncSocketListener{
|
||||||
private AsyncSocket socket;
|
protected AsyncSocket socket;
|
||||||
|
|
||||||
public OnlinePlayer(int size, AsyncSocket socket) {
|
public OnlinePlayer(int size, AsyncSocket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
socket.setHandler(this);
|
||||||
//TODO Auto-generated constructor stub
|
//TODO Auto-generated constructor stub
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void receive(String message);
|
public abstract void receive(String message);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract void receiveShoot(Point point);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract void receiveHit(HitResponse hitResponse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,53 @@
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
public OnlinePlayer_1_1_0(int size, AsyncSocket socket) {
|
public OnlinePlayer_1_1_0(int size, AsyncSocket socket) {
|
||||||
super(size, socket);
|
super(size, socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receive(String message) {
|
public void receive(String message) {
|
||||||
SocketPackage p = new SocketPackage(message);
|
SocketPackage p = new SocketPackage(message);
|
||||||
|
|
||||||
// TODO: parse package
|
// TODO: parse package
|
||||||
|
switch (p.getName()) {
|
||||||
|
case "SHOOT":
|
||||||
|
if (Point.isValidSyntax(p.getData())){
|
||||||
|
Point point = new Point(p.getData());
|
||||||
|
this.enemy.receiveShoot(point);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "HIT":
|
||||||
|
List<String> data = p.splitData();
|
||||||
|
if (data.size()==2){
|
||||||
|
Point point = new Point(data.get(0));
|
||||||
|
int typeIndex = Integer.parseInt(data.get(1));
|
||||||
|
if (Point.isValidSyntax(data.get(0)) && typeIndex >= 0 && typeIndex < HitResponseType.values().length){
|
||||||
|
this.enemy.receiveHit(new HitResponse(typeIndex, point));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "CHAT":
|
||||||
|
//TODO CHAT
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
//nichts passier da Paket ungültig
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveShoot(Point point){
|
||||||
|
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,3 @@
|
||||||
import java.awt.*;
|
|
||||||
|
|
||||||
public abstract class Player {
|
public abstract class Player {
|
||||||
protected boolean myTurn;
|
protected boolean myTurn;
|
||||||
|
@ -14,18 +13,9 @@ public abstract class Player {
|
||||||
this.board = new Board(size);
|
this.board = new Board(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receiveShoot(Point point) {
|
public abstract void receiveShoot(Point point);
|
||||||
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
|
||||||
if (!(hitResponse == null)){
|
|
||||||
enemy.receiveHit(hitResponse);
|
|
||||||
} else {
|
|
||||||
enemy.receiveHit(this.board.hit(point));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void receiveHit(HitResponse hitResponse) {
|
public abstract void receiveHit(HitResponse hitResponse);
|
||||||
enemy.board.addHits(hitResponse);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void click(Point point) {
|
public void click(Point point) {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
public class Point {
|
||||||
|
private int x;
|
||||||
|
private int y;
|
||||||
|
|
||||||
|
public Point (int x, int y) {
|
||||||
|
this.setX(x);
|
||||||
|
this.setY(y);
|
||||||
|
}
|
||||||
|
public Point (String str) {
|
||||||
|
if (Point.isValidSyntax(str)) {
|
||||||
|
this.setX(str.charAt(0));
|
||||||
|
this.setY(Integer.parseInt(str.substring(1)));
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("String ist keine gültige Koordinate");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return (char) ('A' + this.x) + String.valueOf(this.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setX(int x) {
|
||||||
|
this.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setY(int y) {
|
||||||
|
this.y = y;
|
||||||
|
}
|
||||||
|
public void setX(char c) {
|
||||||
|
this.x = c - 'A';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isValidSyntax(String str) {
|
||||||
|
return str.matches("^[A-Z]\\d+$");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,4 @@
|
||||||
|
|
||||||
|
|
||||||
import java.awt.Point;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -65,21 +63,21 @@ public class Ship {
|
||||||
|
|
||||||
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
||||||
// ueberpruefe boundaries
|
// ueberpruefe boundaries
|
||||||
if (pos.x < 0 || pos.y < 0 || pos.x >= boardSize || pos.y >= boardSize) {
|
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bestimme die Endposition anhand der Ausrichtung
|
// bestimme die Endposition anhand der Ausrichtung
|
||||||
int endX = pos.x;
|
int endX = pos.getX();
|
||||||
int endY = pos.y;
|
int endY = pos.getY();
|
||||||
|
|
||||||
if (this.horizontal) { // rechts links
|
if (this.horizontal) { // rechts links
|
||||||
endX = pos.x + this.size - 1;
|
endX = pos.getX() + this.size - 1;
|
||||||
if (endX >= boardSize) {
|
if (endX >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else { // oben unten
|
} else { // oben unten
|
||||||
endY = pos.y + this.size - 1;
|
endY = pos.getY() + this.size - 1;
|
||||||
if (endY >= boardSize) {
|
if (endY >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -88,8 +86,8 @@ public class Ship {
|
||||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
// Liste an Punkten die das Schiff einnehmen wuerde
|
||||||
List<Point> shipPoints = new ArrayList<>();
|
List<Point> shipPoints = new ArrayList<>();
|
||||||
for (int i = 0; i < this.size; i++) {
|
for (int i = 0; i < this.size; i++) {
|
||||||
int x = this.horizontal ? pos.x + i : pos.x; //falls horizontal dann pos.x + i ansonsten pos.x
|
int x = this.horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x
|
||||||
int y = this.horizontal ? pos.y : pos.y + i;
|
int y = this.horizontal ? pos.getY() : pos.getY() + i;
|
||||||
shipPoints.add(new Point(x, y));
|
shipPoints.add(new Point(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,8 +123,8 @@ public class Ship {
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < this.size; i++) {
|
for (int i = 0; i < this.size; i++) {
|
||||||
int x = this.horizontal ? this.position.x + i : this.position.x;
|
int x = this.horizontal ? this.position.getX() + i : this.position.getX();
|
||||||
int y = this.horizontal ? this.position.y : this.position.y + i;
|
int y = this.horizontal ? this.position.getY() : this.position.getY() + i;
|
||||||
points.add(new Point(x, y));
|
points.add(new Point(x, y));
|
||||||
}
|
}
|
||||||
return points;
|
return points;
|
||||||
|
@ -137,8 +135,8 @@ public class Ship {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShipOnPos(Point pos){
|
public boolean isShipOnPos(Point pos){
|
||||||
if ((this.horizontal && pos.y == this.position.y && pos.x >= this.position.x && pos.x < this.position.x + size) ||
|
if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) ||
|
||||||
(!(this.horizontal) && pos.x == this.position.x && pos.y >= this.position.y && pos.y < this.position.y + size)) {
|
(!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -3,7 +3,6 @@ import javazoom.jl.player.Player;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.awt.Point;
|
|
||||||
|
|
||||||
public class SpecificAiPlayerMedium extends AiPlayer{
|
public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
|
|
||||||
|
@ -43,8 +42,8 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addAdjacentPoints(Point point) {
|
private void addAdjacentPoints(Point point) {
|
||||||
int x = point.x;
|
int x = point.getX();
|
||||||
int y = point.y;
|
int y = point.getY();
|
||||||
|
|
||||||
// Possible adjacent positions (up, down, left, right)
|
// Possible adjacent positions (up, down, left, right)
|
||||||
Point[] adjacentPoints = {
|
Point[] adjacentPoints = {
|
||||||
|
@ -67,7 +66,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
return point.x >= 0 && point.x < board.getSize() &&
|
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
||||||
point.y >= 0 && point.y < board.getSize();
|
point.getY() >= 0 && point.getY() < board.getSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
|
||||||
public class startMultiplayerGame extends JPanel {
|
public class startMultiplayerGame extends JPanel {
|
||||||
|
|
Loading…
Reference in New Issue