📄 risk.java
字号:
// CS 582 - Fall 1996 - OSU// Jean-Guy Spetonimport java.awt.*;import java.io.*;import java.util.*;public final class Risk extends Frame{ private Player players[]; private DrawPile drawPile; // GUI objects. private Button quit; private Label statusLine; private World world; private Canvas currentPlayerColor; private Label currentPlayerName; private Label currentPlayerStats; private Button endPhase; private Button endTurn; private Label attackDie[]; private PileCanvas pileCanvas; private Button tradeCards; // Index into players[]. private int currentTurn; // We need to record which phase we're in because of the event-based // nature of the GUI. Events such as clicking on a country will have // different meanings depending on the current phase. private int currentPhase; // The different phases. protected final static int PLACE_ARMIES = 0; protected final static int ATTACK_FROM = 1; protected final static int ATTACK_TO = 2; protected final static int ATTACK_MOVE = 3; protected final static int FORTIFY_FROM = 4; protected final static int FORTIFY_TO = 5; protected final static int FORCED_TRADE = 6; // For phases which require a country 'memory'. private int currentCountry, currentCountryTarget; // What the next card set trade-in is worth. private int cardBonus = 4; // Public constructor. public Risk() { super("Risk -- The Game of Global Conquest!"); drawPile = new DrawPile(); setLayout(new GridBagLayout()); quit = new Button("Quit"); constrain(this, quit, 0, 0, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); currentPlayerStats = new Label("", Label.CENTER); constrain(this, currentPlayerStats, 1, 0, 4, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1.0, 0.0, 2, 2, 2, 2); world = new World(this); constrain(this, world, 0, 1, 5, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 0, 0, 0, 0); currentPlayerColor = new Canvas(); constrain(this, currentPlayerColor, 0, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); currentPlayerName = new Label(); constrain(this, currentPlayerName, 1, 2, 1, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 0.5, 0.0, 2, 2, 2, 2); statusLine = new Label("", Label.LEFT); constrain(this, statusLine, 2, 2, 1, 1, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER, 1.0, 0.0, 2, 2, 2, 2); endPhase = new Button("End Phase"); constrain(this, endPhase, 3, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); endTurn = new Button("End Turn"); constrain(this, endTurn, 4, 2, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); constrain(this, new Label("Die:"), 0, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); constrain(this, new Label("Cards:"), 1, 3, 3, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); attackDie = new Label[5]; Panel p = new Panel(); p.setLayout(new GridLayout(3, 2, 0, 0)); for (int i = 0; i < 5; i++) { attackDie[i] = new Label("0"); p.add(attackDie[i]); } constrain(this, p, 0, 4, 1, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); pileCanvas = new PileCanvas(this); constrain(this, pileCanvas, 1, 4, 3, 1, GridBagConstraints.BOTH, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); tradeCards = new Button("Trade"); tradeCards.disable(); constrain(this, tradeCards, 4, 4, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.0, 0.0, 2, 2, 2, 2); pack(); } // Helper method to simplify using GridBagContraints objects. private void constrain(Container container, Component component, int grid_x, int grid_y, int grid_width, int grid_height, int fill, int anchor, double weight_x, double weight_y, int top, int left, int bottom, int right) { GridBagConstraints c = new GridBagConstraints(); c.gridx = grid_x; c.gridy = grid_y; c.gridwidth = grid_width; c.gridheight = grid_height; c.fill = fill; c.anchor = anchor; c.weightx = weight_x; c.weighty = weight_y; if (top+bottom+left+right > 0) c.insets = new Insets(top, left, bottom, right); ((GridBagLayout)container.getLayout()).setConstraints(component, c); container.add(component); } // AWT methods. public void addNotify() { super.addNotify(); statusLine.setFont(new Font(getFont().getName(), Font.BOLD, getFont().getSize())); } public void paint(Graphics g) { super.paint(g); Player pl = getCurrentPlayer(); currentPlayerStats.setText("Countries: " + pl.getTotalCountries() + " Armies: " + pl.getTotalArmies() + " Cards: " + pl.getPile().size() + " Card Bonus: " + cardBonus); } public boolean action(Event event, Object arg) { if (event.target == quit) { System.exit(0); } else if (event.target == endPhase) { switch (currentPhase) { case PLACE_ARMIES: setPhase(ATTACK_FROM); break; case ATTACK_FROM: case ATTACK_TO: case ATTACK_MOVE: setPhase(FORTIFY_FROM); break; case FORTIFY_FROM: case FORTIFY_TO: nextTurn(); } } else if (event.target == endTurn) { nextTurn(); } else if (event.target == tradeCards) { getCurrentPlayer().tradeCards(cardBonus); setArmiesToPlace(getCurrentPlayer().getArmies()); incrementCardBonus(); checkCardSet(); world.repaint(); pileCanvas.repaint(); if ((getCurrentPhase() == FORCED_TRADE) && (getCurrentPlayer().getPile().size() < 5)) { setPhase(PLACE_ARMIES); } } return true; } // Game methods. // Read player names from stdin. public void registerPlayers() { // Default player colors. Color playerColors[] = { Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.magenta }; System.out.println("Enter player names, one per line, empty line to finish."); System.out.println("Minimum 2 players, maximum 6."); DataInputStream dis = new DataInputStream(System.in); Vector v = new Vector(); try { while (true) { System.out.print("Player " + (v.size() + 1) + ": "); System.out.flush(); String line = dis.readLine(); if (line.length() == 0) { if (v.size() < 2) { System.out.println("Minimum of 2 players required."); System.exit(0); } break; } Player pl = new Player(this, line, playerColors[v.size()]); v.addElement(pl); // No more than 6 players. if (v.size() == 6) { break; } } } catch (IOException e) { System.err.println(e.toString()); System.exit(0); } // We'll convert the Vector of players into an array, to avoid // many future casts and generally to simplify matters (not to // mention efficiency considerations). players = new Player[v.size()]; for (int i = 0; i < players.length; i++) { players[i] = (Player)v.elementAt(i); } } // Initial army placement, turn setup, etc. public void setup() { int initialArmies = 20 + ((6 - players.length) * 5); // Initialize player army reserve count. for (int i = 0; i < players.length; i++) { players[i].setArmies(initialArmies); } // Randomly assign countries to players. Country randomCountries[] = world.randomCountryPermutation(); for (int i = 0; i < randomCountries.length; i += players.length) { for (int j = 0; j < players.length; j++) { if ((i+j) < randomCountries.length) { players[j].addCountry(randomCountries[i+j]); players[j].decrementArmies(1); randomCountries[i+j].incrementArmies(1); } } } // Evenly distribute remaining armies to those countries owned // by each player. for (int i = 0; i < players.length; i++) { players[i].distributeArmies(); } // players[0] has the first turn. setCurrentTurn(0); } protected Player getCurrentPlayer() { return players[currentTurn]; } protected int getCurrentPhase() { return currentPhase; } protected int getCurrentCountry() { return currentCountry; } protected int getCurrentCountryTarget() { return currentCountryTarget; } private void setCurrentTurn(int num) { currentTurn = num; currentPlayerColor.setBackground(players[num].getColor()); currentPlayerName.setText(players[num].getName()); players[currentTurn].gainBonusArmies(); if (getCurrentPlayer().getPile().size() > 4) { setPhase(FORCED_TRADE); } else { setPhase(PLACE_ARMIES); } checkCardSet(); repaint(); pileCanvas.repaint(); } private void nextTurn() { // Give the player a card if deserved. if (getCurrentPlayer().getCardFlag()) { Card c = drawPile.drawTopCard(); if (c != null) { // c is null if the pile is empty getCurrentPlayer().addCard(c); } getCurrentPlayer().clearCardFlag(); } // Find the next player still in the game. int nextTurn = currentTurn; while (true) { nextTurn = ++nextTurn % players.length; if (players[nextTurn].numCountries() > 0) { setCurrentTurn(nextTurn); break; } } } private void incrementCardBonus() { if (cardBonus >= 15) { cardBonus += 5; } else if (cardBonus == 12) { cardBonus += 3; } else { cardBonus += 2; } repaint(); } protected void checkCardSet() { if (getCurrentPlayer().canTradeCards() && !tradeCards.isEnabled()) { tradeCards.enable(); } else if (!getCurrentPlayer().canTradeCards() && tradeCards.isEnabled()) { tradeCards.disable(); } } protected void setArmiesToPlace(int n) { if (n > 0) { statusLine.setText("You may place " + n + " more armies."); } else if (n == -1) { statusLine.setText("You must trade in some cards!"); } else { // assume have placed all armies, advance phase setPhase(ATTACK_FROM); } } protected void setAttackRolls(int rolls[]) { for (int i = 0; i < rolls.length; i++) { attackDie[i].setText("" + rolls[i]); } } protected void setPhase(int phase) { setPhase(phase, -1); } protected void setPhase(int phase, int n) { currentPhase = phase; switch (phase) { case FORCED_TRADE: endPhase.disable(); endTurn.disable(); pileCanvas.repaint(); setArmiesToPlace(-1); break; case PLACE_ARMIES: if (!endPhase.isEnabled()) { endPhase.enable(); endTurn.enable(); } setArmiesToPlace(getCurrentPlayer().getArmies()); checkCardSet(); break; case ATTACK_FROM: if (tradeCards.isEnabled()) { // no trading after placement phase tradeCards.disable(); } statusLine.setText("Select country to attack from."); break; case ATTACK_TO: currentCountry = n; statusLine.setText("Attack which country from " + world.getCountry(n).getName() + "?"); break; case ATTACK_MOVE: statusLine.setText("Click on " + world.getCountry(n).getName() + " to move armies from " + world.getCountry(currentCountry).getName() + "."); currentCountryTarget = n; break; case FORTIFY_FROM: statusLine.setText("Select country to fortify from."); world.repaint(); break; case FORTIFY_TO: currentCountry = n; statusLine.setText("Fortify which country from " + world.getCountry(n).getName() + "?"); break; } } protected World getWorld() { return world; } public static void main(String args[]) { // RegisterFrame rf = new RegisterFrame(risk); // rf.show(); Risk risk = new Risk(); risk.registerPlayers(); risk.setup(); // Once the window is showing, actions will be driven by GUI // events, and so the static driver ends here. risk.show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -