Battleships basic stuff:

user interface
message negotiation

to come:
game fuctionality

git-svn-id: http://svn.igniterealtime.org/svn/repos/spark/trunk@12514 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Wolf Posdorfer
2011-06-20 16:15:43 +00:00
committed by wolf.posdorfer
parent f6d3adc091
commit ff64e48907
28 changed files with 1273 additions and 0 deletions

View File

@ -0,0 +1,98 @@
<project name="battleship" default="jar" basedir="..">
<property name="plug.dir" value="${basedir}"/>
<property name="plug.lib.dir" value="${plug.dir}/build/lib"/>
<property name="plug.lib.dist.dir" value="${plug.lib.dir}/dist"/>
<property name="spark.home" value="${basedir}/../../.."/>
<property name="spark.target" value="${basedir}/../../../target"/>
<property name="spark.build" value="${basedir}/../../../target/build"/>
<property name="classes.dir" value="${spark.target}/plugins-dev/battleship/classes"/>
<property name="src.dir" value="${plug.dir}/src/java"/>
<property name="resources.dir" location="${plug.dir}/src/resources"/>
<property name="target.dir" value="${spark.target}/plugins/battleship"/>
<property name="target.lib.dir" value="${target.dir}/lib"/>
<property name="jar.file" value="${target.dir}/lib/plugin-classes.jar"/>
<path id="lib.classpath">
<fileset dir="${plug.lib.dir}" includes="**/*.jar, **/*.zip"/>
<fileset dir="${spark.home}/target/build/lib" includes="**/*.jar, **/*.zip"/>
<fileset dir="${spark.home}/target/build/lib/windows" includes="**/*.jar"/>
</path>
<target name="clean" description="Cleans all build related output">
<delete file="${jar.file}"/>
<delete dir="${classes.dir}"/>
<delete dir="${target.dir}"/>
</target>
<target name="compile" description="Compiles plugin source">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}"
destdir="${classes.dir}"
classpathref="lib.classpath"
source="1.6"
debug="true"
target="1.6"/>
<copy todir="${classes.dir}">
<fileset dir="${src.dir}" includes="**/*.png"/>
<fileset dir="${src.dir}" includes="**/*.gif"/>
<fileset dir="${src.dir}" includes="**/*.jpg"/>
<fileset dir="${src.dir}" includes="**/*.jpeg"/>
<fileset dir="${src.dir}" includes="**/*.wav"/>
<fileset dir="${src.dir}" includes="**/*.properties"/>
</copy>
</target>
<target name="jar" depends="clean,compile,resources" description="Makes a plugin jar">
<mkdir dir="${target.dir}"/>
<mkdir dir="${target.lib.dir}"/>
<copy todir="${target.lib.dir}">
<fileset file="${plug.lib.dir}/lib" includes="**/*"/>
</copy>
<copy todir="${target.dir}">
<fileset file="${plug.dir}/plugin.xml"/>
</copy>
<jar basedir="${classes.dir}" file="${jar.file}" update="false"/>
<zip zipfile="${spark.build}/plugins/battleship.jar" basedir="${target.dir}"/>
<copy todir="${spark.home}/src/commercial">
<fileset file="${spark.build}/plugins/battleship.jar"/>
</copy>
</target>
<!-- resources =================================================================================== -->
<target name="resources">
<copy todir="${classes.dir}">
<fileset dir="${resources.dir}">
<include name="**/*"/>
</fileset>
<fileset dir="${src.dir}">
<include name="**/*.properties"/>
</fileset>
</copy>
</target>
<property name="pluginsstuff" value="${basedir}\..\..\..\target\build\plugins"/>
<target name="copy" depends="jar">
<echo message="${pluginsstuff}"/>
<copy todir="${pluginsstuff}">
<fileset file="${spark.build}/plugins/battleship.jar"/>
</copy>
</target>
</project>

View File

@ -0,0 +1,12 @@
<plugin>
<name>Battleships</name>
<class>battleship.BattleshipPlugin</class>
<author>Wolf Posdorfer</author>
<version>0.1</version>
<homePage>http://www.jivesoftware.com</homePage>
<description>Battleship plugin for Spark</description>
<email>9posdorf@informatik.uni-hamburg.de</email>
<minSparkVersion>2.6.1</minSparkVersion>
<os>Windows,Linux,Mac</os>
<java>1.6.0_00</java>
</plugin>

View File

@ -0,0 +1,129 @@
package battleship;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.plugin.Plugin;
import org.jivesoftware.spark.ui.ChatRoom;
import battleship.listener.ChatRoomOpeningListener;
import battleship.packets.GameOfferPacket;
import battleship.packets.MoveAnswerPacket;
import battleship.packets.MovePacket;
public class BattleshipPlugin implements Plugin{
private PacketListener _gameofferListener;
private ChatRoomOpeningListener _chatRoomListener;
@Override
public void initialize() {
ProviderManager.getInstance().addIQProvider(GameOfferPacket.ELEMENT_NAME, GameOfferPacket.NAMESPACE, GameOfferPacket.class);
ProviderManager.getInstance().addExtensionProvider(MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE, MovePacket.class);
ProviderManager.getInstance().addExtensionProvider(MoveAnswerPacket.ELEMENT_NAME, MoveAnswerPacket.NAMESPACE, MoveAnswerPacket.class);
_gameofferListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
GameOfferPacket invitation = (GameOfferPacket) packet;
if (invitation.getType() == IQ.Type.GET) {
showInvitationInChat(invitation);
}
}
};
SparkManager.getConnection().addPacketListener(_gameofferListener,
new PacketTypeFilter(GameOfferPacket.class));
_chatRoomListener = new ChatRoomOpeningListener();
SparkManager.getChatManager().addChatRoomListener(_chatRoomListener);
}
private void showInvitationInChat(final GameOfferPacket invitation) {
invitation.setType(IQ.Type.RESULT);
invitation.setTo(invitation.getFrom());
final ChatRoom room = SparkManager.getChatManager().getChatRoom(StringUtils.parseBareAddress(invitation.getFrom()));
String name = StringUtils.parseName(invitation.getFrom());
final JPanel panel = new JPanel();
JLabel text = new JLabel("Game request from" + name);
JLabel game = new JLabel("Battleships");
game.setFont(new Font("Dialog", Font.BOLD, 24));
game.setForeground(Color.RED);
JButton accept = new JButton(Res.getString("button.accept").replace("&", ""));
JButton decline = new JButton(Res.getString("button.decline").replace("&", ""));
panel.add(text);
panel.add(game);
panel.add(accept);
panel.add(decline);
room.getTranscriptWindow().addComponent(panel);
accept.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SparkManager.getConnection().sendPacket(invitation);
invitation.setStartingPlayer(!invitation.isStartingPlayer());
ChatRoomOpeningListener.createWindow(invitation, invitation.getFrom());
panel.remove(3);
panel.remove(2);
panel.repaint();
panel.revalidate();
}
});
decline.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
invitation.setType(IQ.Type.ERROR);
SparkManager.getConnection().sendPacket(invitation);
panel.remove(3);
panel.remove(2);
panel.repaint();
panel.revalidate();
}
});
}
@Override
public void shutdown() {
}
@Override
public boolean canShutDown() {
return false;
}
@Override
public void uninstall() {
}
}

View File

@ -0,0 +1,37 @@
package battleship;
import java.text.MessageFormat;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.jivesoftware.spark.util.log.Log;
public class BsRes {
private static PropertyResourceBundle prb;
static ClassLoader cl = BsRes.class.getClassLoader();
static {
prb = (PropertyResourceBundle) ResourceBundle
.getBundle("i18n/battleships_i18n");
}
public static final String getString(String propertyName) {
try {
return prb.getString(propertyName);
} catch (Exception e) {
Log.error(e);
return propertyName;
}
}
public static String getString(String propertyName, Object... obj) {
String str = prb.getString(propertyName);
if (str == null) {
return propertyName;
}
return MessageFormat.format(str, obj);
}
}

View File

@ -0,0 +1,52 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.gui;
import java.awt.GridLayout;
import java.awt.Image;
import javax.swing.JLabel;
import javax.swing.JPanel;
import battleship.types.Ship;
public class Display extends JPanel {
private static final long serialVersionUID = 2343499579008942774L;
private JLabel[] _labels;
public Display() {
setLayout(new GridLayout(5, 1));
_labels = new JLabel[5];
_labels[0] = new JLabel(Ship.TWO.getScaledInstance(100, 50, Image.SCALE_SMOOTH));
_labels[1] = new JLabel(Ship.THREE.getScaledInstance(100, 50, Image.SCALE_SMOOTH));
_labels[2] = new JLabel(Ship.THREE.getScaledInstance(100, 50, Image.SCALE_SMOOTH));
_labels[3] = new JLabel(Ship.FOUR.getScaledInstance(100, 50, Image.SCALE_SMOOTH));
_labels[4] = new JLabel(Ship.FIVE.getScaledInstance(100, 50, Image.SCALE_SMOOTH));
for(JLabel l : _labels)
{
add(l);
}
}
}

View File

@ -0,0 +1,122 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ChatRoom;
import battleship.BsRes;
import battleship.logic.GameBoard;
import battleship.packets.MoveAnswerPacket;
import battleship.packets.MovePacket;
public class GUI extends JPanel {
private static final long serialVersionUID = -7538765009749015196L;
private Display _display;
private GameboardGUI _myfield;
private GameboardGUI _theirfield;
private JFrame _owner;
private XMPPConnection _connection;
private int _gameID;
private GameBoard _gameboard;
public GUI(boolean imStarting, JFrame owner, XMPPConnection connection, int gameID) {
setLayout(new BorderLayout());
_owner = owner;
_connection = connection;
_gameID = gameID;
_gameboard = new GameBoard(imStarting);
_display = new Display();
_myfield = new GameboardGUI();
_theirfield = new GameboardGUI();
JPanel West = new JPanel(new BorderLayout(0, 5));
JPanel wnorth = new JPanel(new BorderLayout());
wnorth.add(new JLabel("Their Board"), BorderLayout.NORTH);
wnorth.add(_theirfield, BorderLayout.SOUTH);
JPanel wsouth = new JPanel(new BorderLayout());
wsouth.add(new JLabel("My Board"), BorderLayout.NORTH);
wsouth.add(_myfield);
JPanel East = new JPanel();
West.add(wsouth, BorderLayout.SOUTH);
West.add(wnorth, BorderLayout.NORTH);
East.add(_display);
add(East, BorderLayout.EAST);
add(West, BorderLayout.WEST);
_connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
MovePacket move = (MovePacket) packet.getExtension(
MovePacket.ELEMENT_NAME, MovePacket.NAMESPACE);
if (move.getGameID() == _gameID) {
boolean opponentMadeHit = _gameboard.placeBomb(move.getPositionX(), move.getPositionY());
if(opponentMadeHit)
{
Message m = createAnswer(move, packet.getFrom());
_connection.sendPacket(m);
}
}
}
}, new PacketExtensionFilter(MovePacket.ELEMENT_NAME,
MovePacket.NAMESPACE));
}
private Message createAnswer(MovePacket incoming, String from)
{
Message answer = new Message();
answer.setTo(from);
MoveAnswerPacket map = new MoveAnswerPacket();
map.setGameID(incoming.getGameID());
map.setPositionX(incoming.getPositionX());
map.setPositionY(incoming.getPositionY());
return answer;
}
}

View File

@ -0,0 +1,91 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.gui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import battleship.types.Types;
public class GameboardGUI extends JPanel{
private static final long serialVersionUID = -6429298293488133059L;
private Image _bg;
private JLabel[][] _labels;
public GameboardGUI()
{
ClassLoader cl = getClass().getClassLoader();
_bg = new ImageIcon(cl.getResource("water.png")).getImage();
setLayout(new GridLayout(10,10));
_labels = new JLabel[10][10];
for(JLabel[] ll : _labels)
{
for(JLabel l : ll)
{
l = new JLabel();
l.setBorder(BorderFactory.createLineBorder(Color.lightGray));
add(l);
}
}
this.setPreferredSize(new Dimension(400,400));
}
public void setField(int x, int y, Types t)
{
_labels[x][y].setIcon(t.getImage());
repaint();
revalidate();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
final Image backgroundImage = _bg;
double scaleX = getWidth() / (double) backgroundImage.getWidth(null);
double scaleY = getHeight() / (double) backgroundImage.getHeight(null);
AffineTransform xform = AffineTransform
.getScaleInstance(scaleX, scaleY);
((Graphics2D) g).drawImage(backgroundImage, xform, this);
}
}

View File

@ -0,0 +1,98 @@
package battleship.listener;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomButton;
import org.jivesoftware.spark.ui.ChatRoomListenerAdapter;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import battleship.BsRes;
import battleship.gui.GUI;
import battleship.packets.GameOfferPacket;
public class ChatRoomOpeningListener extends ChatRoomListenerAdapter {
@Override
public void chatRoomOpened(final ChatRoom room) {
if (!(room instanceof ChatRoomImpl)) // Check for 1on1 Chat
{
return;
}
final ChatRoomButton sendGameButton = new ChatRoomButton("BS");
room.getToolBar().addChatRoomButton(sendGameButton);
final String opponentJID = ((ChatRoomImpl) room).getJID();
sendGameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final GameOfferPacket offer = new GameOfferPacket();
offer.setTo(opponentJID);
offer.setType(IQ.Type.GET);
room.getTranscriptWindow().insertCustomText(
BsRes.getString("request"), false, false,
Color.BLUE);
SparkManager.getConnection().sendPacket(offer);
SparkManager.getConnection().addPacketListener(
new PacketListener() {
@Override
public void processPacket(Packet packet) {
GameOfferPacket answer = (GameOfferPacket) packet;
answer.setStartingPlayer(offer
.isStartingPlayer());
answer.setGameID(offer.getGameID());
String name = StringUtils.parseName(opponentJID);
if (answer.getType() == IQ.Type.RESULT) {
// ACCEPT
room.getTranscriptWindow()
.insertCustomText(BsRes.getString("accepted", name), false,
false, Color.BLUE);
createWindow(answer, opponentJID);
} else {
// DECLINE
room.getTranscriptWindow()
.insertCustomText(BsRes.getString("declined", name), false,
false, Color.RED);
}
}
}, new PacketIDFilter(offer.getPacketID()));
}
});
}
public static void createWindow(GameOfferPacket answer, String opponentJID) {
JFrame frame = new JFrame(BsRes.getString("versus", StringUtils.parseName(opponentJID)));
frame.add(new GUI(answer.isStartingPlayer(),frame,SparkManager.getConnection(),answer.getGameID()));
frame.pack();
frame.setLocationRelativeTo(SparkManager.getChatManager().getChatContainer());
frame.setVisible(true);
}
}

View File

@ -0,0 +1,108 @@
package battleship.logic;
import battleship.types.Direction;
import battleship.types.Ship;
public class GameBoard {
private Ship[][] _fields;
private Shipmodel[] _myships;
private boolean myTurn;
public GameBoard(boolean myTurn) {
this.myTurn = myTurn;
_fields = new Ship[11][11];
_myships = new Shipmodel[5];
_myships[0] = new Shipmodel(2);
_myships[1] = new Shipmodel(3);
_myships[2] = new Shipmodel(3);
_myships[3] = new Shipmodel(4);
_myships[4] = new Shipmodel(5);
}
/**
* Places a Bomb of the Opponent on you field
* @param x
* @param y
* @return True if the Opponent hit a Ship, else false
*/
public boolean placeBomb(int x, int y)
{
if(_fields[x][y] != Ship.EMPTY)
{
Shipmodel smo =_myships[_fields[x][y].inArrayPosition()];
smo.setBomb();
return true;
}
else
{
myTurn=!myTurn; // Now its my turn
return false;
}
}
public boolean placeShip(int x, int y, Ship s, Direction dir) {
boolean placementOK = true;
if (dir == Direction.HORIZONTAL) // Left to right
{
for (int i = x; i < s.getFields(); i++) {
if (!checkSurrounding(i, y))
placementOK = false;
}
if (placementOK) {
for (int i = x; i < s.getFields(); i++) {
_fields[i][y] = s;
}
}
} else // top to bottom
{
for (int i = y; i < s.getFields(); i++) {
if (!checkSurrounding(x, i))
placementOK = false;
}
if (placementOK) {
for (int i = y; i < s.getFields(); i++) {
_fields[x][i] = s;
}
}
}
return placementOK;
}
private boolean checkSurrounding(int x, int y) {
if (_fields[x - 1][y - 1] != Ship.EMPTY || _fields[x][y - 1] != Ship.EMPTY
|| _fields[x + 1][y - 1] != Ship.EMPTY)
{
return false;
}
else if (_fields[x - 1][y] != Ship.EMPTY || _fields[x][y] != Ship.EMPTY
|| _fields[x + 1][y] != Ship.EMPTY)
{
return false;
}
else if (_fields[x - 1][y + 1] != Ship.EMPTY || _fields[x][y + 1] != Ship.EMPTY
|| _fields[x + 1][y + 1] != Ship.EMPTY)
{
return false;
}
else
return true;
}
private int getField(int x, int y)
{
Ship s = _fields[x][y];
return 0;
}
}

View File

@ -0,0 +1,41 @@
package battleship.logic;
public class Shipmodel {
private int[] _ship;
public Shipmodel(int size)
{
_ship = new int[size];
}
public void setBomb()
{
for(int i=0; i<_ship.length;i++)
{
if(_ship[i]==0)
{
_ship[i]=1;
break;
}
}
}
public boolean isDestroyed()
{
boolean dest = true;
for(int i=0; i<_ship.length && dest; i++)
{
dest= _ship[i]!=0;
}
return dest;
}
public int getSize()
{
return _ship.length;
}
}

View File

@ -0,0 +1,99 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.packets;
import java.util.Random;
import org.jivesoftware.smack.packet.IQ;
/**
* The Game Offer Packet
*
* @author wolf.posdorfer
* @version 20.06.2011
*/
public class GameOfferPacket extends IQ {
public static final String ELEMENT_NAME = "battleship";
public static final String NAMESPACE = "battleship";
private int gameID;
private boolean imTheStartingPlayer;
public GameOfferPacket() {
super();
imTheStartingPlayer = new Random().nextBoolean();
gameID = Math.abs(new Random().nextInt());
}
/**
* Returns the game ID.
*
* @return the game ID.
*/
public int getGameID() {
return gameID;
}
/**
* Sets the game ID.
*
* @param gameID
* the game ID.
*/
public void setGameID(int gameID) {
this.gameID = gameID;
}
/**
* Returns true if the user making the game invitation is the starting
* player.
*
* @return true if the user making the game invite is the starting player.
*/
public boolean isStartingPlayer() {
return imTheStartingPlayer;
}
/**
* Sets whether the user making the game invitation is the starting player.
*
* @param startingPlayer
* true if the user making the game invite is the starting
* player.
*/
public void setStartingPlayer(boolean startingPlayer) {
this.imTheStartingPlayer = startingPlayer;
}
public String getChildElementXML() {
StringBuffer buf = new StringBuffer();
buf.append("<" + ELEMENT_NAME + " xmlns=\"" + NAMESPACE + "\">");
if (getType() == IQ.Type.GET) {
buf.append("<gameID>").append(gameID).append("</gameID>");
buf.append("<startingPlayer>").append(imTheStartingPlayer)
.append("</startingPlayer>");
buf.append(getExtensionsXML());
}
buf.append("</" + ELEMENT_NAME + ">");
return buf.toString();
}
}

View File

@ -0,0 +1,117 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.packets;
import org.jivesoftware.smack.packet.PacketExtension;
/**
* The MoveAnswer Packet Extension
*
* @author wolf.posdorfer
* @version 20.06.2011
*/
public class MoveAnswerPacket implements PacketExtension {
public static final String ELEMENT_NAME = "bs-move";
public static final String NAMESPACE = "battleship";
private int posx;
private int posy;
private int gameID;
private int hit;
private int shiptype;
public int getGameID() {
return gameID;
}
public void setGameID(int gameID) {
this.gameID = gameID;
}
@Override
public String getElementName() {
return ELEMENT_NAME;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
public int getPositionX() {
return posx;
}
public void setPositionX(int posx) {
this.posx = posx;
}
public int getPositionY() {
return posy;
}
public void setPositionY(int posy) {
this.posy = posy;
}
public int getHit()
{
return hit;
}
public void setHit(int hit)
{
this.hit = hit;
}
public void setShiptype(int x)
{
this.shiptype = x;
}
public int getShiptype()
{
return shiptype;
}
@Override
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<" + ELEMENT_NAME + " xmlns=\"" + NAMESPACE + "\">");
buf.append("<gameID>").append(gameID).append("</gameID>");
buf.append("<positionX>").append(posx).append("</positionX>");
buf.append("<positionY>").append(posy).append("</positionY>");
buf.append("<hit>").append(hit).append("</hit>");
buf.append("<shiptype>").append(shiptype).append("</shiptype>");
buf.append("</" + ELEMENT_NAME + ">");
return buf.toString();
}
}

View File

@ -0,0 +1,90 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.packets;
import org.jivesoftware.smack.packet.PacketExtension;
/**
* The Move Packet extension
*
* @author wolf.posdorfer
* @version 20.06.2011
*/
public class MovePacket implements PacketExtension {
public static final String ELEMENT_NAME = "bs-move";
public static final String NAMESPACE = "battleship";
private int posx;
private int posy;
private int gameID;
public int getGameID() {
return gameID;
}
public void setGameID(int gameID) {
this.gameID = gameID;
}
@Override
public String getElementName() {
return ELEMENT_NAME;
}
@Override
public String getNamespace() {
return NAMESPACE;
}
public int getPositionX() {
return posx;
}
public void setPositionX(int posx) {
this.posx = posx;
}
public int getPositionY() {
return posy;
}
public void setPositionY(int posy) {
this.posy = posy;
}
@Override
public String toXML() {
StringBuffer buf = new StringBuffer();
buf.append("<" + ELEMENT_NAME + " xmlns=\"" + NAMESPACE + "\">");
buf.append("<gameID>").append(gameID).append("</gameID>");
buf.append("<positionX>").append(posx).append("</positionX>");
buf.append("<positionY>").append(posy).append("</positionY>");
buf.append("</" + ELEMENT_NAME + ">");
return buf.toString();
}
}

View File

@ -0,0 +1,7 @@
package battleship.types;
public enum Direction {
HORIZONTAL, VERTICAL;
}

View File

@ -0,0 +1,105 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.types;
import java.awt.Image;
import javax.swing.ImageIcon;
public enum Ship {
EMPTY("empty.png"), TWO("2.png"), THREE("3.png"), THREE2("3.png"), FOUR("4.png"), FIVE("5.png");
private String image;
private Ship(String s) {
image = s;
}
public ImageIcon getImage() {
ClassLoader cl = getClass().getClassLoader();
return new ImageIcon(cl.getResource(image));
}
public static Ship valueOf(int x) {
switch (x) {
case 2:
return TWO;
case 3:
return THREE;
case 4:
return THREE2;
case 5:
return FOUR;
case 6:
return FIVE;
default:
return EMPTY;
}
}
/**
* Returns the Position inside the Array
* @return
*/
public int inArrayPosition() {
switch (this) {
case TWO:
return 0;
case THREE:
return 1;
case THREE2:
return 2;
case FOUR:
return 3;
case FIVE:
return 4;
default:
return 0;
}
}
/**
* Returns the Occupying Fields
* @return
*/
public int getFields() {
switch (this) {
case TWO:
return 2;
case THREE:
return 3;
case THREE2:
return 3;
case FOUR:
return 4;
case FIVE:
return 5;
}
return 0;
}
public ImageIcon getScaledInstance(int w, int h, int hints) {
Image img = getImage().getImage();
return new ImageIcon(img.getScaledInstance(w, h, hints));
}
}

View File

@ -0,0 +1,56 @@
/**
* $RCSfile: ,v $
* $Revision: $
* $Date: $
*
* Copyright (C) 2004-2011 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package battleship.types;
import javax.swing.ImageIcon;
public enum Types {
EMPTY("empty.png"),
MISS("miss.png"),
SHIP("ship.png"),
SHIPHIT("shiphit.png");
public Types getValue(int x) {
switch (x) {
case 1:
return MISS;
case 2:
return SHIP;
case 3:
return SHIPHIT;
default:
return EMPTY;
}
}
private String _iconme;
private Types(String iconme) {
_iconme=iconme;
}
public ImageIcon getImage()
{
ClassLoader cl = getClass().getClassLoader();
return new ImageIcon(cl.getResource(_iconme));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -0,0 +1,11 @@
###########################
# Battleships #
# by wolf.posdorfer #
###########################
title = Battleships
versus = Battleships versus {0}
request = You requested a game of Battleships
has.request = {0} has requested a game of Battleships
accepted = {0} has accepted the invitation
declined = {0} has declined the invitation

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 794 KiB