possible to win now

git-svn-id: svn://anubis/gvsu@104 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-04-13 23:11:59 +00:00
parent e8b697c75f
commit eff1c65fd9
3 changed files with 41 additions and 0 deletions

View File

@ -14,6 +14,7 @@ public class BlobWars extends JFrame
private BlobWarsPanel m_panel; private BlobWarsPanel m_panel;
private JTextField m_nameField; private JTextField m_nameField;
private JTextField m_serverField; private JTextField m_serverField;
private JLabel m_statusLabel;
private JButton m_connectButton; private JButton m_connectButton;
private EventHandler m_handler; private EventHandler m_handler;
private javax.swing.Timer m_timer; private javax.swing.Timer m_timer;
@ -47,6 +48,10 @@ public class BlobWars extends JFrame
add(m_connectButton = new JButton("Connect")); add(m_connectButton = new JButton("Connect"));
m_connectButton.addActionListener(m_handler); m_connectButton.addActionListener(m_handler);
}}); }});
add(new JPanel() {{
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(m_statusLabel = new JLabel());
}});
// add(new Box.Filler(new Dimension(0, 0), // add(new Box.Filler(new Dimension(0, 0),
// new Dimension(0, 10000), // new Dimension(0, 10000),
// new Dimension(10000, 10000))); // new Dimension(10000, 10000)));
@ -234,6 +239,11 @@ public class BlobWars extends JFrame
Integer shotid = Integer.parseInt(s.substring(7)); Integer shotid = Integer.parseInt(s.substring(7));
m_world.removeShot(shotid); m_world.removeShot(shotid);
} }
else if (s.startsWith("WINNER:"))
{
String winner = s.substring(7);
m_statusLabel.setText(winner + " won the match!");
}
} }
} }

View File

@ -13,6 +13,7 @@ public class BlobWarsServer
private HashMap<Socket, String> m_socketToPlayerName; private HashMap<Socket, String> m_socketToPlayerName;
private HashMap<String, Player> m_lastPlayers; private HashMap<String, Player> m_lastPlayers;
private HashMap<Integer, Shot> m_lastShots; private HashMap<Integer, Shot> m_lastShots;
private int m_lastNumPlayersAlive;
public static void main(String[] args) public static void main(String[] args)
{ {
@ -22,6 +23,7 @@ public class BlobWarsServer
public BlobWarsServer() public BlobWarsServer()
{ {
m_lastNumPlayersAlive = 0;
m_world = new BlobWarsWorld(); m_world = new BlobWarsWorld();
m_clientUpdates = new Vector<ClientUpdate>(); m_clientUpdates = new Vector<ClientUpdate>();
m_socketToPlayerName = new HashMap<Socket, String>(); m_socketToPlayerName = new HashMap<Socket, String>();
@ -198,6 +200,25 @@ public class BlobWarsServer
} }
} }
/* check number of players alive, possibly declare winner */
Player alivePlayer = null;
int alivePlayers = 0;
for (Player p : players.values())
{
if (p.health > 0.0)
{
alivePlayers++;
alivePlayer = p;
}
}
if (alivePlayers == 1 && alivePlayers < m_lastNumPlayersAlive)
{
/* game is over, alivePlayer won */
sendLine += "WINNER:" + alivePlayer.name + "\n";
m_world.resetPlayers();
}
m_lastNumPlayersAlive = alivePlayers;
/* write updated info to each client */ /* write updated info to each client */
synchronized (m_socketToPlayerName) synchronized (m_socketToPlayerName)
{ {

View File

@ -24,6 +24,16 @@ public class BlobWarsWorld
m_shots.clear(); m_shots.clear();
} }
public void resetPlayers()
{
for (Player p : m_players.values())
{
p.health = 1.0;
p.dx = 0;
p.dy = 0;
}
}
public boolean playerExists(String name) public boolean playerExists(String name)
{ {
return m_players.containsKey(name); return m_players.containsKey(name);