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

📄 galaxy.java

📁 java版的数据结构的完全代码 免费提供了 学习数据结构的请下载
💻 JAVA
字号:
// Introduced in Chapter 15import java.util.Scanner;/** The game of Galaxy. */public class Galaxy {  /** For reading from the console. */  public static final Scanner INPUT = new Scanner(System.in);  /** Edges linking the stars together. */  private Graph edges;  /** Points earned so far by each player. */  private int[] scores;    /** Number of player controlling each star, or zero for neutral. */  private int[] stars;  /** Build the galaxy and give one star to each player. */  public Galaxy() {    edges = new Graph(20);    for (int[] pair : new int[][]      {{0, 16}, {0, 17}, {0, 19},       {1, 2}, {1, 8},       {2, 4}, {2, 5}, {2, 8},       {3, 8}, {3, 9}, {3, 16},       {4, 5}, {4, 8},       {5, 6}, {5, 7},       {6, 7},       {7, 8}, {7, 10}, {7, 11}, {7, 12}, {7, 18},       {8, 10}, {8, 14},       {9, 10}, {9, 13},       {10, 12}, {10, 13},       {11, 14},       {11, 19},       {12, 14}, {12, 16}, {12, 19},       {13, 15}, {13, 16},       {15, 16}, {15, 18},       {16, 17},       {18, 19}}) {      edges.addUndirectedEdge(pair[0], pair[1]);    }    scores = new int[3];         // Initially all zeroes    stars = new int[20];         // Initially all zeroes    stars[(int)(Math.random() * 20)] = 1;    do {      int star = (int)(Math.random() * 20);      if (stars[star] == 0) {        stars[star] = 2;        return;      }    } while (true);  }  /** Play the game. */  public void play() {    int player = 1;    for (int turn = 0; turn < 18; turn++) {      System.out.println(this);      System.out.print("Player " + player + ", pick a star: ");      int star = INPUT.nextInt();      INPUT.nextLine();         // To clear out input      stars[star] = player;      for (int s : edges.neighbors(star)) {        if (stars[s] == 3 - player) {          scores[player]++;        }      }      player = 3 - player;    }    System.out.println(this);  }    public String toString() {    StringBuilder result = new StringBuilder();    result.append("Star\tOwner\tNeighbors\n");    for (int i = 0; i < 20; i++) {      result.append(i + "\t" + stars[i] + "\t"                    + edges.neighbors(i) + "\n");    }    for (int p = 1; p <= 2; p++) {      result.append("Player " + p + ": " + scores[p] + "\n");    }    return result.toString();  }  /** Create and play the game. */  public static void main(String[] args) {    Galaxy game = new Galaxy();    System.out.println("Welcome to Galaxy.\n");    game.play();  }  }

⌨️ 快捷键说明

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