⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 septon

📁 一些JAVA的小程序
💻
📖 第 1 页 / 共 4 页
字号:
#!/bin/sh# This is a shell archive (produced by GNU sharutils 4.2).# To extract the files from this archive, save it to some FILE, remove# everything before the `!/bin/sh' line above, then type `sh FILE'.## Made on 1996-12-10 14:17 PST by <speton@eel>.# Source directory was `/nfs/ca/u2/s/speton/cs582/risk'.## Existing files will *not* be overwritten unless `-c' is specified.## This shar contains:# length mode       name# ------ ---------- ------------------------------------------#  12482 -rw-r--r-- Risk.java#   7256 -rw-r--r-- World.java#  14555 -rw-r--r-- Country.java#   4110 -rw-r--r-- Player.java#    686 -rw-r--r-- Pile.java#    670 -rw-r--r-- DrawPile.java#   3951 -rw-r--r-- Card.java#   1424 -rw-r--r-- PileCanvas.java#    340 -rw-r--r-- Makefile#save_IFS="${IFS}"IFS="${IFS}:"gettext_dir=FAILEDlocale_dir=FAILEDfirst_param="$1"for dir in $PATHdo  if test "$gettext_dir" = FAILED && test -f $dir/gettext \     && ($dir/gettext --version >/dev/null 2>&1)  then    set `$dir/gettext --version 2>&1`    if test "$3" = GNU    then      gettext_dir=$dir    fi  fi  if test "$locale_dir" = FAILED && test -f $dir/shar \     && ($dir/shar --print-text-domain-dir >/dev/null 2>&1)  then    locale_dir=`$dir/shar --print-text-domain-dir`  fidoneIFS="$save_IFS"if test "$locale_dir" = FAILED || test "$gettext_dir" = FAILEDthen  echo=echoelse  TEXTDOMAINDIR=$locale_dir  export TEXTDOMAINDIR  TEXTDOMAIN=sharutils  export TEXTDOMAIN  echo="$gettext_dir/gettext -s"fitouch -am 1231235999 $$.touch >/dev/null 2>&1if test ! -f 1231235999 && test -f $$.touch; then  shar_touch=touchelse  shar_touch=:  echo  $echo 'WARNING: not restoring timestamps.  Consider getting and'  $echo "installing GNU \`touch', distributed in GNU File Utilities..."  echofirm -f 1231235999 $$.touch#if mkdir _sh13934; then  $echo 'x -' 'creating lock directory'else  $echo 'failed to create lock directory'  exit 1fi# ============= Risk.java ==============if test -f 'Risk.java' && test "$first_param" != -c; then  $echo 'x -' SKIPPING 'Risk.java' '(file already exists)'else  $echo 'x -' extracting 'Risk.java' '(text)'  sed 's/^X//' << 'SHAR_EOF' > 'Risk.java' &&// CS 582 - Fall 1996 - OSU// Jean-Guy SpetonXimport java.awt.*;import java.io.*;import java.util.*;Xpublic final class Risk extends Frame{X  private Player players[];X  private DrawPile drawPile;XX  // GUI objects.X  private Button     quit;X  private Label      statusLine;X  private World      world;X  private Canvas     currentPlayerColor;X  private Label      currentPlayerName;X  private Label      currentPlayerStats;X  private Button     endPhase;X  private Button     endTurn;X  private Label      attackDie[];X  private PileCanvas pileCanvas;X  private Button     tradeCards;XX  // Index into players[].X  private int currentTurn;XX  // We need to record which phase we're in because of the event-basedX  // nature of the GUI.  Events such as clicking on a country will haveX  // different meanings depending on the current phase.X  private int currentPhase;XX  // The different phases.X  protected final static int PLACE_ARMIES = 0;X  protected final static int ATTACK_FROM  = 1;X  protected final static int ATTACK_TO    = 2;X  protected final static int ATTACK_MOVE  = 3;X  protected final static int FORTIFY_FROM = 4;X  protected final static int FORTIFY_TO   = 5;X  protected final static int FORCED_TRADE = 6;XX  // For phases which require a country 'memory'.X  private int currentCountry, currentCountryTarget;XX  // What the next card set trade-in is worth.X  private int cardBonus = 4;XX  // Public constructor.X  public Risk()X  {X    super("Risk -- The Game of Global Conquest!");XX    drawPile = new DrawPile();X    X    setLayout(new GridBagLayout());XX    quit = new Button("Quit");X    constrain(this, quit, 0, 0, 1, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);X    X    currentPlayerStats = new Label("", Label.CENTER);X    constrain(this, currentPlayerStats, 1, 0, 4, 1,X	      GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,X	      1.0, 0.0, 2, 2, 2, 2);X    X    world = new World(this);X    constrain(this, world, 0, 1, 5, 1,X	      GridBagConstraints.NONE, GridBagConstraints.CENTER,X	      0.0, 0.0, 0, 0, 0, 0);XX    currentPlayerColor = new Canvas();X    constrain(this, currentPlayerColor, 0, 2, 1, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    currentPlayerName = new Label();X    constrain(this, currentPlayerName, 1, 2, 1, 1,X	      GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,X	      0.5, 0.0, 2, 2, 2, 2);X    X    statusLine = new Label("", Label.LEFT);X    constrain(this, statusLine, 2, 2, 1, 1,X	      GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER,X	      1.0, 0.0, 2, 2, 2, 2);XX    endPhase = new Button("End Phase");X    constrain(this, endPhase, 3, 2, 1, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    endTurn = new Button("End Turn");X    constrain(this, endTurn, 4, 2, 1, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    constrain(this, new Label("Die:"), 0, 3, 1, 1,X	      GridBagConstraints.NONE, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    constrain(this, new Label("Cards:"), 1, 3, 3, 1,X	      GridBagConstraints.NONE, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);X    X    attackDie = new Label[5];X    Panel p = new Panel();X    p.setLayout(new GridLayout(3, 2, 0, 0));X    for (int i = 0; i < 5; i++) {X      attackDie[i] = new Label("0");X      p.add(attackDie[i]);X    }X    constrain(this, p, 0, 4, 1, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    pileCanvas = new PileCanvas(this);X    constrain(this, pileCanvas, 1, 4, 3, 1,X	      GridBagConstraints.BOTH, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);X    X    tradeCards = new Button("Trade");X    tradeCards.disable();X    constrain(this, tradeCards, 4, 4, 1, 1,X	      GridBagConstraints.NONE, GridBagConstraints.CENTER,X	      0.0, 0.0, 2, 2, 2, 2);XX    pack();X  }XX  // Helper method to simplify using GridBagContraints objects.X  private void constrain(Container container, Component component,X                        int grid_x, int grid_y,X                        int grid_width, int grid_height,X                        int fill, int anchor, double weight_x, double weight_y,X                        int top, int left, int bottom, int right)X  {X    GridBagConstraints c = new GridBagConstraints();X    c.gridx = grid_x; c.gridy = grid_y;X    c.gridwidth = grid_width; c.gridheight = grid_height;X    c.fill = fill; c.anchor = anchor;X    c.weightx = weight_x; c.weighty = weight_y;X    if (top+bottom+left+right > 0)X      c.insets = new Insets(top, left, bottom, right);XX    ((GridBagLayout)container.getLayout()).setConstraints(component, c);X    container.add(component);X  }XX  // AWT methods.XX  public void addNotify()X  {X    super.addNotify();X    statusLine.setFont(new Font(getFont().getName(),X				Font.BOLD,X				getFont().getSize()));X  }X  X  public void paint(Graphics g)X  {X    super.paint(g);XX    Player pl = getCurrentPlayer();X    currentPlayerStats.setText("Countries: " + pl.getTotalCountries() +X			       "  Armies: " + pl.getTotalArmies() +X			       "  Cards: " + pl.getPile().size() +X			       "  Card Bonus: " + cardBonus);X  }X  X  public boolean action(Event event, Object arg)X  {X    if (event.target == quit) {X      System.exit(0);X    }X    else if (event.target == endPhase) {X      switch (currentPhase) {X      case PLACE_ARMIES:X	setPhase(ATTACK_FROM);X	break;X      case ATTACK_FROM:X      case ATTACK_TO:X      case ATTACK_MOVE:X	setPhase(FORTIFY_FROM);X	break;X      case FORTIFY_FROM:X      case FORTIFY_TO:X	nextTurn();X      }X    }X    else if (event.target == endTurn) {X      nextTurn();X    }X    else if (event.target == tradeCards) {X      getCurrentPlayer().tradeCards(cardBonus);X      setArmiesToPlace(getCurrentPlayer().getArmies());X      incrementCardBonus();X      checkCardSet();X      world.repaint();X      pileCanvas.repaint();XX      if ((getCurrentPhase() == FORCED_TRADE) &&X	  (getCurrentPlayer().getPile().size() < 5)) {X	setPhase(PLACE_ARMIES);X      }X    }X    return true;X  }XX  // Game methods.X  X  // Read player names from stdin.X  public void registerPlayers()X  {X    // Default player colors.X    Color playerColors[] = {X      Color.red, Color.orange, Color.yellow, Color.green, Color.cyan, Color.magentaX    };X    X    System.out.println("Enter player names, one per line, empty line to finish.");X    System.out.println("Minimum 2 players, maximum 6.");XX    DataInputStream dis = new DataInputStream(System.in);X    Vector v = new Vector();X    try {X      while (true) {X	System.out.print("Player " + (v.size() + 1) + ": ");X	System.out.flush();X	String line = dis.readLine();X	if (line.length() == 0) {X	  if (v.size() < 2) {X	    System.out.println("Minimum of 2 players required.");X	    System.exit(0);X	  }X	  break;X	}X	Player pl = new Player(this, line, playerColors[v.size()]);X	v.addElement(pl);X	// No more than 6 players.X	if (v.size() == 6) {X	  break;X	}X      }X    }X    catch (IOException e) {X      System.err.println(e.toString());X      System.exit(0);X    }XX    // We'll convert the Vector of players into an array, to avoidX    // many future casts and generally to simplify matters (not toX    // mention efficiency considerations).X    players = new Player[v.size()];X    for (int i = 0; i < players.length; i++) {X      players[i] = (Player)v.elementAt(i);X    }X  }XX  // Initial army placement, turn setup, etc.X  public void setup()X  {X    int initialArmies = 20 + ((6 - players.length) * 5);XX    // Initialize player army reserve count.X    for (int i = 0; i < players.length; i++) {X      players[i].setArmies(initialArmies);X    }XX    // Randomly assign countries to players.X    Country randomCountries[] = world.randomCountryPermutation();X    for (int i = 0; i < randomCountries.length; i += players.length) {X      for (int j = 0; j < players.length; j++) {X	if ((i+j) < randomCountries.length) {X	  players[j].addCountry(randomCountries[i+j]);X	  players[j].decrementArmies(1);X	  randomCountries[i+j].incrementArmies(1);X	}X      }X    }XX    // Evenly distribute remaining armies to those countries ownedX    // by each player.X    for (int i = 0; i < players.length; i++) {X      players[i].distributeArmies();X    }XX    // players[0] has the first turn.X    setCurrentTurn(0);X  }XX  protected Player getCurrentPlayer()X  {X    return players[currentTurn];X  }X  X  protected int getCurrentPhase()X  {X    return currentPhase;X  }XX  protected int getCurrentCountry()X  {X    return currentCountry;X  }XX  protected int getCurrentCountryTarget()X  {X    return currentCountryTarget;X  }X  X  private void setCurrentTurn(int num)X  {X    currentTurn = num;X    currentPlayerColor.setBackground(players[num].getColor());X    currentPlayerName.setText(players[num].getName());X    players[currentTurn].gainBonusArmies();XX    if (getCurrentPlayer().getPile().size() > 4) {X      setPhase(FORCED_TRADE);X    }X    else {X      setPhase(PLACE_ARMIES);X    }XX    checkCardSet();X    repaint();X    pileCanvas.repaint();X  }XX  private void nextTurn()X  {X    // Give the player a card if deserved.X    if (getCurrentPlayer().getCardFlag()) {X      Card c = drawPile.drawTopCard();X      if (c != null) {  // c is null if the pile is emptyX	getCurrentPlayer().addCard(c);X      }X      getCurrentPlayer().clearCardFlag();X    }XX    // Find the next player still in the game.X    int nextTurn = currentTurn;X    while (true) {X      nextTurn = ++nextTurn % players.length;X      if (players[nextTurn].numCountries() > 0) {X	setCurrentTurn(nextTurn);X	break;X      }X    }X  }XX  private void incrementCardBonus()X  {X    if (cardBonus >= 15) {X      cardBonus += 5;X    }X    else if (cardBonus == 12) {X      cardBonus += 3;X    }X    else {X      cardBonus += 2;X    }X    repaint();X  }XX  protected void checkCardSet()X  {X    if (getCurrentPlayer().canTradeCards() && !tradeCards.isEnabled()) {X      tradeCards.enable();X    }X    else if (!getCurrentPlayer().canTradeCards() && tradeCards.isEnabled()) {X      tradeCards.disable();X    }X  }X  X  protected void setArmiesToPlace(int n)X  {X    if (n > 0) {X      statusLine.setText("You may place " + n + " more armies.");X    }X    else if (n == -1) {X      statusLine.setText("You must trade in some cards!");X    }X    else {  // assume have placed all armies, advance phaseX      setPhase(ATTACK_FROM);X    }X  }XX  protected void setAttackRolls(int rolls[])X  {X    for (int i = 0; i < rolls.length; i++) {X      attackDie[i].setText("" + rolls[i]);X    }X  }XX  protected void setPhase(int phase)X  {X    setPhase(phase, -1);X  }XX  protected void setPhase(int phase, int n)X  {X    currentPhase = phase;X    X    switch (phase) {X    case FORCED_TRADE:X      endPhase.disable();X      endTurn.disable();X      pileCanvas.repaint();X      setArmiesToPlace(-1);X      break;X    case PLACE_ARMIES:X      if (!endPhase.isEnabled()) {X	endPhase.enable();X	endTurn.enable();X      }X      setArmiesToPlace(getCurrentPlayer().getArmies());X      checkCardSet();X      break;X    case ATTACK_FROM:X      if (tradeCards.isEnabled()) {  // no trading after placement phase

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -