Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bc0ec26
Showing
11 changed files
with
502 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
idea-files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.antwerkz.wsdemos</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<groupId>com.antwerkz.wsdemos</groupId> | ||
<artifactId>core</artifactId> | ||
<version>1.0</version> | ||
<name>WebSocket Demo core libraries</name> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.antwerkz.wsdemos</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0</version> | ||
<packaging>pom</packaging> | ||
|
||
<name>Websocket Demos Parent</name> | ||
|
||
<modules> | ||
<module>core</module> | ||
<module>war</module> | ||
</modules> | ||
|
||
<build> | ||
<defaultGoal>package</defaultGoal> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.6</source> | ||
<target>1.6</target> | ||
<!--<compilerArgument>-proc:none</compilerArgument>--> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-release-plugin</artifactId> | ||
<version>2.1</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>glassfish</id> | ||
<name>glassfish</name> | ||
<url>http://download.java.net/maven/glassfish</url> | ||
</repository> | ||
<repository> | ||
<id>repository.jboss.org</id> | ||
<name>JBoss Maven Repository</name> | ||
<url>http://repository.jboss.org/maven2</url> | ||
</repository> | ||
<repository> | ||
<id>antwerkz</id> | ||
<url>http://antwerkz.com/</url> | ||
</repository> | ||
<repository> | ||
<id>maven.annotation.rg</id> | ||
<url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url> | ||
</repository> | ||
<repository> | ||
<id>jfrog</id> | ||
<url>http://www.jfrog.org/artifactory/plugins-releases</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.sun.grizzly</groupId> | ||
<artifactId>grizzly-websockets</artifactId> | ||
<version>${grizzly.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.grizzly</groupId> | ||
<artifactId>grizzly-http-servlet</artifactId> | ||
<version>${grizzly.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<version>5.14.2</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<properties> | ||
<grizzly.version>1.9.32</grizzly.version> | ||
</properties> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.antwerkz.wsdemos</groupId> | ||
<artifactId>parent</artifactId> | ||
<version>1.0</version> | ||
</parent> | ||
|
||
<groupId>com.antwerkz.wsdemos</groupId> | ||
<artifactId>war</artifactId> | ||
<version>1.0</version> | ||
|
||
<name>War!</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.sun.grizzly</groupId> | ||
<artifactId>grizzly-websockets</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.grizzly</groupId> | ||
<artifactId>grizzly-http-servlet</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
7 changes: 7 additions & 0 deletions
7
war/src/main/java/com/antwerkz/wsdemos/war/ActionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.antwerkz.wsdemos.war; | ||
|
||
public class ActionHandler { | ||
public Result strike() { | ||
return Result.MISS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.antwerkz.wsdemos.war; | ||
|
||
public class Piece { | ||
private Type type; | ||
private boolean[][] hits; | ||
|
||
public Piece(final Type type) { | ||
this.type = type; | ||
type.createHits(this); | ||
} | ||
|
||
public void setHits(final boolean[][] values) { | ||
hits = values; | ||
} | ||
|
||
public boolean isDestroyed() { | ||
boolean destroyed = true; | ||
for (boolean[] hit : hits) { | ||
for (boolean b : hit) { | ||
destroyed &= b; | ||
} | ||
} | ||
|
||
return destroyed; | ||
} | ||
|
||
public Result strike(final int x, final int y) { | ||
hits[x][y] = true; | ||
return Result.HIT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.antwerkz.wsdemos.war; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.sun.grizzly.websockets.WebSocket; | ||
|
||
public class Player { | ||
private ActionHandler[][] handlers; | ||
private String name; | ||
private boolean ready = false; | ||
private List<Piece> pieces = new ArrayList<Piece>(); | ||
private WebSocket socket; | ||
|
||
public Player(final WebSocket socket) { | ||
this.socket = socket; | ||
handlers = new ActionHandler[WarGame.DIMENSION][]; | ||
for (int i = 0; i < handlers.length; i++) { | ||
handlers[i] = new ActionHandler[WarGame.DIMENSION]; | ||
for (int j = 0; j < handlers.length; j++) { | ||
handlers[i][j] = new ActionHandler(); | ||
} | ||
} | ||
} | ||
|
||
public WebSocket getSocket() { | ||
return socket; | ||
} | ||
|
||
public boolean isReady() { | ||
return ready; | ||
} | ||
|
||
public void setReady(final boolean ready) { | ||
this.ready = ready; | ||
} | ||
|
||
public boolean isDefeated() { | ||
boolean defeated = true; | ||
for (Piece piece : pieces) { | ||
defeated &= piece.isDestroyed(); | ||
} | ||
return defeated; | ||
} | ||
|
||
public Result place(Type type, final int mapX, final int mapY) { | ||
final int[] dimensions = type.dimensions(); | ||
final Piece piece = new Piece(type); | ||
pieces.add(piece); | ||
for (int xIndex = 0; xIndex < dimensions[0]; xIndex++) { | ||
for (int yIndex = 0; yIndex < dimensions[1]; yIndex++) { | ||
final int x = mapX + xIndex; | ||
final int y = mapY + yIndex; | ||
handlers[xIndex][yIndex] = new ActionHandler() { | ||
@Override | ||
public Result strike() { | ||
piece.strike(x, y); | ||
return isDefeated() ? Result.VICTORY : Result.HIT; | ||
} | ||
}; | ||
} | ||
} | ||
ready = pieces.size() == Type.values().length; | ||
return ready ? Result.READY : Result.PLACED; | ||
} | ||
|
||
public Result strike(int x, int y) { | ||
if (x < 0 || y < 0 || x >= WarGame.DIMENSION || y >= WarGame.DIMENSION) { | ||
return Result.OFFMAP; | ||
} | ||
return handlers[x][y].strike(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.antwerkz.wsdemos.war; | ||
|
||
public enum Result { | ||
HIT, | ||
MISS, | ||
OCCUPIED, | ||
PLACED, | ||
READY, | ||
VICTORY, | ||
OFFMAP,; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.antwerkz.wsdemos.war; | ||
|
||
public enum Type { | ||
AA { | ||
@Override | ||
public int[] dimensions() { | ||
return new int[] {1, 2}; | ||
} | ||
}, | ||
TANK { | ||
@Override | ||
public int[] dimensions() { | ||
return new int[] {2, 2}; | ||
} | ||
}, | ||
PLATOON{ | ||
@Override | ||
public int[] dimensions() { | ||
return new int[] {2, 4}; | ||
} | ||
|
||
}, | ||
HQ{ | ||
@Override | ||
public int[] dimensions() { | ||
return new int[] {3, 3}; | ||
} | ||
|
||
}; | ||
|
||
boolean[][] create(int width, int height) { | ||
boolean spaces[][] = new boolean[width][]; | ||
for (int i = 0; i < spaces.length; i++) { | ||
spaces[i] = new boolean[height]; | ||
} | ||
|
||
return spaces; | ||
} | ||
|
||
public void createHits(final Piece piece) { | ||
final int[] dimensions = dimensions(); | ||
piece.setHits(create(dimensions[0], dimensions[1])); | ||
} | ||
|
||
public abstract int[] dimensions(); | ||
|
||
@Override | ||
public String toString() { | ||
return name(); | ||
} | ||
} |
Oops, something went wrong.