这是当玩家必须从游戏中移除时调用的方法。这两种方法都属于不同的类。
游戏板GUI类
Vector<Player> players = new Vector<Player>();
public void removePlayerFromGame(Player playerToRemove)
{
//go through the playerToRemove's properties and reset all their variables
for(int i = 0; i < playerToRemove.getPropertiesOwned().size(); i++)
{
//code to reset the player's properties to an unowned/unmodified state
}
//same with transports
for(int i = 0; i < playerToRemove.getTransportsOwned().size(); i++)
{
//code to reset the player's transports to an unowned/unmodified state
}
//just updating the vector based on the playerToRemove's position
if(players.get(0) == playerToRemove)
{
players.remove(playerToRemove);
updatePlayerInformation();
}
else
{
players.remove(playerToRemove);
updatePlayerVector(players);
updatePlayerInformation();
}
}
这就是该方法的调用方式:如果当前玩家 (fromMe) 登陆一个房产并且无法支付租金(即他们的余额由于 而达到 0 takefrombalance(1200);
,目前硬编码为 1200 以使测试更容易),他们将被移除if 语句if(fromMe.isBankrupt())
属性类
GameboardGUI gui = new GameboardGUI();
public void payRent(Player fromMe, Player toYou, int rent)
{
//remove rent from the current player
fromMe.takeFromBalance(1200);
//add it to the owner
toYou.addToBalance(rent);
GameboardGUI.addGameFeedMessage(fromMe.getName() + " has paid " + rent + " in rent to " + toYou.getName());
GameboardGUI.addGameFeedMessage(toYou.getName() + "'s balance is now " + toYou.getBalance());
if(fromMe.isBankrupt())
{
//THIS IS THE CALL THAT THROWS THE NullPointerException
gui.removePlayerFromGame(fromMe);
}
else
{
GameboardGUI.addGameFeedMessage(fromMe.getName() + "'s balance is now " + fromMe.getBalance());
}
}
这是到达该行时的堆栈跟踪:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PropertyTile.payRent(PropertyTile.java:254)
at PropertyTile.landedOnProperty(PropertyTile.java:239)
at GameboardGUI.playerHasLanded(GameboardGUI.java:1905)
at Player.setPosition(Player.java:82)
at Player.movePlayer(Player.java:101)
at GameboardGUI$11.actionPerformed(GameboardGUI.java:1536)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)