📄 country.java
字号:
// CS 582 - Fall 1996 - OSU// Jean-Guy Spetonimport java.awt.*;import java.util.Random;public class Country extends Polygon{ // Country and continent constants. public static final int NUM_COUNTRIES = 42; public static final int NORTH_AMERICA = 0; public static final int SOUTH_AMERICA = 1; public static final int AFRICA = 2; public static final int EUROPE = 3; public static final int ASIA = 4; public static final int AUSTRALIA = 5; public static final int CONTINENT_LO[] = { 0, 9, 13, 19, 26, 38 }; public static final int CONTINENT_HI[] = { 8, 12, 18, 25, 37, 41 }; public static final int ARMY_BONUS[] = { 5, 2, 3, 5, 7, 2 }; // North America public static final int ALASKA = 0; public static final int NORTHWEST_TERRITORIES = 1; public static final int GREENLAND = 2; public static final int ALBERTA = 3; public static final int ONTARIO = 4; public static final int QUEBEC = 5; public static final int WESTERN_US = 6; public static final int EASTERN_US = 7; public static final int MEXICO = 8; public static final int NORTH_AMERICA_LO = 0; public static final int NORTH_AMERICA_HI = 8; // South America public static final int VENEZUELA = 9; public static final int PERU = 10; public static final int BRAZIL = 11; public static final int ARGENTINA = 12; public static final int SOUTH_AMERICA_LO = 9; public static final int SOUTH_AMERICA_HI = 12; // Africa public static final int NORTH_AFRICA = 13; public static final int EGYPT = 14; public static final int CONGO = 15; public static final int EAST_AFRICA = 16; public static final int SOUTH_AFRICA = 17; public static final int MADAGASCAR = 18; public static final int AFRICA_LO = 13; public static final int AFRICA_HI = 18; // Europe public static final int ICELAND = 19; public static final int GREAT_BRITAIN = 20; public static final int SCANDINAVIA = 21; public static final int WESTERN_EUROPE = 22; public static final int NORTHERN_EUROPE = 23; public static final int SOUTHERN_EUROPE = 24; public static final int UKRAINE = 25; public static final int EUROPE_LO = 19; public static final int EUROPE_HI = 25; // Asia public static final int URAL = 26; public static final int SIBERIA = 27; public static final int YAKUTSK = 28; public static final int KAMCHATKA = 29; public static final int AFGHANISTAN = 30; public static final int IRKUTSK = 31; public static final int CHINA = 32; public static final int MONGOLIA = 33; public static final int JAPAN = 34; public static final int MIDDLE_EAST = 35; public static final int INDIA = 36; public static final int SIAM = 37; public static final int ASIA_LO = 26; public static final int ASIA_HI = 37; // Australia public static final int INDONESIA = 38; public static final int NEW_GUINEA = 39; public static final int WESTERN_AUSTRALIA = 40; public static final int EASTERN_AUSTRALIA = 41; public static final int AUSTRALIA_LO = 38; public static final int AUSTRALIA_HI = 41; private static String countryNames[] = { "Alaska", "Northwest Territories", "Greenland", "Alberta", "Ontario", "Quebec", "Western US", "Eastern US", "Mexico", "Venezuela", "Peru", "Brazil", "Argentina", "North Africa", "Egypt", "Congo", "East Africa", "South Africa", "Madagascar", "Iceland", "Great Britain", "Scandinavia", "Western Europe", "Northern Europe", "Southern Europe", "Ukraine", "Ural", "Siberia", "Yakutsk", "Kamchatka", "Afghanistan", "Irkutsk", "China", "Mongolia", "Japan", "Middle East", "India", "Siam", "Indonesia", "New Guinea", "Western Australia", "Eastern Australia" }; private static int adjacencies[][] = { // North America { NORTHWEST_TERRITORIES, ALBERTA, KAMCHATKA }, { ALASKA, GREENLAND, ALBERTA, ONTARIO }, { NORTHWEST_TERRITORIES, ICELAND, ONTARIO, QUEBEC }, { ALASKA, NORTHWEST_TERRITORIES, ONTARIO, WESTERN_US }, { ALBERTA, NORTHWEST_TERRITORIES, GREENLAND, QUEBEC, WESTERN_US, EASTERN_US }, { ONTARIO, GREENLAND, EASTERN_US }, { ALBERTA, ONTARIO, EASTERN_US, MEXICO }, { WESTERN_US, ONTARIO, QUEBEC, MEXICO }, { WESTERN_US, EASTERN_US, VENEZUELA}, // South America { MEXICO, PERU, BRAZIL }, { VENEZUELA, BRAZIL, ARGENTINA }, { VENEZUELA, PERU, ARGENTINA, NORTH_AFRICA }, { PERU, BRAZIL }, // Africa { BRAZIL, EGYPT, CONGO, EAST_AFRICA, WESTERN_EUROPE, SOUTHERN_EUROPE }, { NORTH_AFRICA, EAST_AFRICA, WESTERN_EUROPE, SOUTHERN_EUROPE, MIDDLE_EAST }, { SOUTH_AFRICA, NORTH_AFRICA, EAST_AFRICA }, { SOUTH_AFRICA, CONGO, MADAGASCAR, NORTH_AFRICA, EGYPT, MIDDLE_EAST }, { CONGO, EAST_AFRICA, MADAGASCAR }, { SOUTH_AFRICA, EAST_AFRICA }, // Europe { GREENLAND, SCANDINAVIA, GREAT_BRITAIN }, { ICELAND, WESTERN_EUROPE, NORTHERN_EUROPE, SCANDINAVIA }, { ICELAND, GREAT_BRITAIN, NORTHERN_EUROPE, UKRAINE }, { NORTH_AFRICA, GREAT_BRITAIN, NORTHERN_EUROPE, SOUTHERN_EUROPE }, { GREAT_BRITAIN, SCANDINAVIA, WESTERN_EUROPE, SOUTHERN_EUROPE, UKRAINE }, { NORTH_AFRICA, EGYPT, WESTERN_EUROPE, NORTHERN_EUROPE, UKRAINE, MIDDLE_EAST }, { SCANDINAVIA, NORTHERN_EUROPE, SOUTHERN_EUROPE, MIDDLE_EAST, AFGHANISTAN, URAL }, // Asia { UKRAINE, AFGHANISTAN, SIBERIA, CHINA }, { URAL, CHINA, MONGOLIA, IRKUTSK, YAKUTSK }, { SIBERIA, IRKUTSK, KAMCHATKA }, { ALASKA, YAKUTSK, IRKUTSK, MONGOLIA, JAPAN }, { UKRAINE, MIDDLE_EAST, URAL, CHINA, INDIA }, { SIBERIA, YAKUTSK, KAMCHATKA, MONGOLIA }, { INDIA, AFGHANISTAN, URAL, SIBERIA, MONGOLIA, SIAM }, { CHINA, SIBERIA, IRKUTSK, KAMCHATKA, JAPAN }, { KAMCHATKA, MONGOLIA }, { EGYPT, EAST_AFRICA, SOUTHERN_EUROPE, UKRAINE, AFGHANISTAN, INDIA }, { MIDDLE_EAST, AFGHANISTAN, CHINA, SIAM }, { INDIA, CHINA, INDONESIA }, // Australia { SIAM, NEW_GUINEA, WESTERN_AUSTRALIA }, { INDONESIA, WESTERN_AUSTRALIA, EASTERN_AUSTRALIA }, { INDONESIA, NEW_GUINEA, EASTERN_AUSTRALIA }, { NEW_GUINEA, WESTERN_AUSTRALIA } }; // (unclosed) Polygon points, in x1, y1, x2, y2, etc. order. private static int points[][] = { // North America { 24,61, 60,67, 60,96, 84,114, 81,118, 60,102, 40,97, 30,107, 6,100, 5,72 }, { 60,67, 144,72, 161,59, 187,75, 160,104, 71,104, 60,96 }, { 254,8, 312,5, 336,13, 315,65, 277,82, 269,100, 247,75, 241,43, 216,40, 208,28 }, { 71,104, 138,104, 138,133, 97,133, 81,118, 84,114 }, { 138,104, 160,104, 186,117, 189,125, 189,146, 161,133, 137,133 }, { 189,125, 195,123, 199,114, 197,93, 205,92, 218,106, 225,100, 244,123, 233,140, 216,138, 189,146 }, { 97,133, 150,133, 143,183, 112,176, 95,158 }, { 150,133, 161,133, 189,146, 216,138, 233,140, 225,147, 216,147, 201,167, 188,179, 192,191, 189,193, 183,182, 163,181, 154,191, 142,183 }, { 112,176, 142,183, 154,191, 157,207, 166,208, 169,203, 177,200, 174,213, 193,228, 190,232, 138,206 }, // South America { 210,223, 229,225, 254,240, 220,240, 213,252, 195,244, 198,231 }, { 195,244, 213,252, 207,264, 235,279, 243,308, 200,281, 189,260 }, { 220,240, 254,240, 259,249, 289,260, 289,269, 280,277, 275,300, 259,305, 252,321, 243,308, 235,279, 207,264 }, { 213,289, 243,308, 252,321, 231,338, 216,370, 220,382, 211,382, 204,369 }, // Africa { 385,164, 407,180, 413,207, 413,239, 383,239, 372,234, 345,237, 327,218, 327,201, 353,169 }, { 411,175, 431,180, 447,209, 413,207, 407,180 }, { 383,239, 430,239, 428,271, 394,271 }, { 413,207, 447,209, 457,227, 474,222, 471,240, 453,252, 452,271, 428,271, 430,239, 413,239 }, { 394,271, 452,271, 442,299, 427,322, 407,326, 390,288 }, { 471,276, 474,281, 468,301, 464,304, 459,297, 460,284 }, // Europe { 313,79, 332,78, 336,83, 324,89, 316,86 }, { 356,103, 368,123, 364,127, 355,129, 357,119, 350,106 }, { 423,60, 431,65, 433,91, 425,97, 412,98, 411,90, 419,82, 413,79, 402,98, 401,111, 392,111, 387,101, 379,105,377,93, 408,64 }, { 372,127, 382,147, 378,150, 373,148, 367,159, 359,165, 345,163, 345,147, 361,147, 362,139, 356,135 }, { 387,117, 415,117, 413,134, 395,134, 378,139, 372,127 }, { 395,134, 413,134, 410,156, 394,141, 392,145, 403,155, 399,160, 399,157, 385,144, 382,147, 378,139 }, { 431,65, 456,76, 475,72, 475,160, 461,158, 441,152, 424,155, 410,156, 415,117, 425,97, 433,91 }, // Asia { 475,72, 506,68, 555,131, 538,145, 475,125 }, { 506,68, 517,51, 539,50, 590,32, 610,39, 611,48, 582,123, 575,129, 555,132 }, { 611,48, 642,48, 651,60, 671,53, 688,58, 655,98, 598,79 }, { 688,58, 694,60, 712,59, 716,94, 676,100, 660,116, 671,124, 667,138, 657,147, 638,120 }, { 475,125, 538,145, 526,156, 529,169, 521,182, 484,160, 475,160 }, { 598,79, 655,98, 638,120, 618,129, 597,131, 582,123 }, { 575,129, 581,149, 592,152, 612,144, 629,154, 622,163, 628,176, 624,188, 615,198, 600,198, 586,197, 575,183, 556,189, 542,181, 536,175, 535,170, 529,169, 526,156, 555,131 }, { 582,123, 597,131, 618,129, 638,120, 657,147, 629,154, 612,144, 592,152, 581,149, 575,129 }, { 672,142, 683,146, 675,151, 668,170, 658,172, 648,180, 647,174, 659,166, 670,153 }, { 424,155, 441,152, 461,158, 475,160, 484,160, 521,182, 512,195, 485,188, 485,193, 495,200, 486,210, 461,220, 436,178, 443,164, 423,165, 424,155 }, { 529,169, 535,170, 536,175, 542,181, 556,189, 557,200, 538,215, 538,226, 533,230, 522,200, 518,202, 512,195, 521,182 }, { 575,183, 586,197, 600,198, 593,205, 603,219, 595,230, 582,219, 580,226, 576,227, 576,210, 570,213, 563,198, 557,200, 556,189 }, // Australia { 579,232, 599,256, 623,263, 642,265, 642,271, 616,270, 594,262, 571,236 }, { 649,251, 665,251, 695,257, 699,268, 697,275, 654,262 }, { 658,274, 663,284, 663,330, 647,318, 614,326, 609,299, 639,280 }, { 676,273, 698,305, 699,309, 687,334, 667,334, 663,330, 663,284, 670,285 }, }; // Coordinate to place army numbers. private static int armyCoods[][] = { { 28,89 }, { 110,92 }, { 275,45 }, { 109,125 }, { 156,127 }, { 208,130 }, { 116,162 }, { 165,170 }, { 138,202 }, { 208,238 }, { 208,282 }, { 246,283 }, { 216,330 }, { 370,210 }, { 424,198 }, { 406,258 }, { 440,242 }, { 416,300 }, { 462,295 }, { 322,88 }, { 358,125 }, { 390,96 }, { 354,160 }, { 391,130 }, { 402,146 }, { 442,126 }, { 500,112 }, { 557,89 }, { 631,77 }, { 685,90 }, { 496,158 }, { 606,116 }, { 571,172 }, { 628,145 }, { 663,170 }, { 460,190 }, { 530,200 }, { 583,215 }, { 592,264 }, { 671,266 }, { 638,304 }, { 675,314 } }; // Make an array of countries comprising the entire world. public static Country[] makeWorld() { Country[] world = new Country[NUM_COUNTRIES]; for (int i = 0; i < NUM_COUNTRIES; i++) { world[i] = new Country(i); } return world; } public static String getName(int i) { return countryNames[i]; } // Instance variables. private int num; private Integer id; private String name; private int[] adjacent; private Player owner; private int numArmies; public Country(int c) { num = c; id = new Integer(num); name = countryNames[c]; adjacent = adjacencies[c]; for (int i = 0; i < points[c].length; i += 2) { addPoint(points[c][i], points[c][i+1]); } addPoint(points[c][0], points[c][1]); // close the Polygon } // AWT methods. public void paint(Graphics g) { if (owner != null) { g.setColor(owner.getColor()); } else { g.setColor(Color.pink); } g.fillPolygon(this); // Draw outline, to distinguish borders. g.setColor(Color.black); g.drawPolygon(this); g.drawString("" + numArmies, armyCoods[num][0]+1, armyCoods[num][1]+1); g.setColor(Color.white); g.drawString("" + numArmies, armyCoods[num][0], armyCoods[num][1]); } // Game methods. public int getNum() { return num; } public Integer getID() { return id; } public String getName() { return name; } public Player getOwner() { return owner; } public void setOwner(Player pl) { owner = pl; } public int getArmies() { return numArmies; } public void setArmies(int i) { numArmies = i; } public void decrementArmies(int i) { numArmies -= i; } public void incrementArmies(int i) { numArmies += i; } // Returns true if this is adjacent to the target parameter. public boolean isAdjacentTo(Country target) { for (int i = 0; i < adjacencies[num].length; i++) { if (adjacencies[num][i] == target.num) { return true; } } return false; } public int[] attackCountry(Country target) { int myRolls[] = new int[Math.min(numArmies - 1, 3)]; int hisRolls[] = new int[Math.min(target.numArmies, 2)]; Random rand = new Random(); int sortedRolls[] = new int[5]; // Roll the die. for (int i = 0; i < myRolls.length; i++) { myRolls[i] = ((Math.abs(rand.nextInt())) % 6) + 1; } for (int i = 0; i < hisRolls.length; i++) { hisRolls[i] = ((Math.abs(rand.nextInt())) % 6) + 1; } // Sort the rolls. int t; if ((myRolls.length > 1) && (myRolls[1] > myRolls[0])) { t = myRolls[0]; myRolls[0] = myRolls[1]; myRolls[1] = t; } if ((myRolls.length > 2) && (myRolls[2] > myRolls[1])) { t = myRolls[1]; myRolls[1] = myRolls[2]; myRolls[2] = t; if ((myRolls[1] > myRolls[0])) { t = myRolls[0]; myRolls[0] = myRolls[1]; myRolls[1] = t; } } if ((hisRolls.length > 1) && (hisRolls[1] > hisRolls[0])) { t = hisRolls[0]; hisRolls[0] = hisRolls[1]; hisRolls[1] = t; } sortedRolls[0] = myRolls[0]; sortedRolls[1] = hisRolls[0]; if (myRolls.length > 1) { sortedRolls[2] = myRolls[1]; } if (hisRolls.length > 1) { sortedRolls[3] = hisRolls[1]; } if (myRolls.length > 2) { sortedRolls[4] = myRolls[2]; } // Compute the battle results. for (int i = 0; i < Math.min(myRolls.length, hisRolls.length); i++) { if (myRolls[i] > hisRolls[i]) { target.numArmies--; if (target.numArmies == 0) { // Victory! Player oldOwner = target.owner; target.owner.removeCountry(target); this.owner.addCountry(target); target.numArmies = 1; this.numArmies--; if (oldOwner.numCountries() == 0) { owner.vanquished(oldOwner); } break; } } else { this.numArmies--; } } return sortedRolls; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -