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

📄 game.java

📁 大富翁游戏monopoli
💻 JAVA
字号:
import java.util.*;import java.io.*;public class Game {  private Board board;  private Player[] players;  private String filename;  private int moves;  public Game(String fn) throws IOException, FileNotFoundException, TooManyPlayersException {    filename = fn;    makeBoard();    makePlayers(board.getGo());  }  public void makeBoard() throws IOException, FileNotFoundException {board = new Board(filename);}  public void makePlayers(int pos) throws IOException, FileNotFoundException, TooManyPlayersException {    players = new Player[4];    StringTokenizer st = new StringTokenizer(filename, ".");    String base = st.nextToken();     String suffix = st.nextToken();    for(int i=0 ; i < 4 ; i++) {       players[i] = new Player(base+(i+1)+"."+suffix, pos);       board.setEstate(players[i].getEstate(), players[i].id());    }  }  public void move(int player) {    int n, p, np, toll=0, bonus=0, id = ((player==5)?3:player-1);    Allotment a;    n = players[id].nextDiceToss();    if (n!=0) { // il giocatore non è in prigione       p = players[id].getPosition();       np = (p+n)%40;       a = board.getAllotment(np);        if (a instanceof GoToJail) {          np = board.getJail();          players[id].toggleCaptiveStatus();       }       if (((a.owner() != 0) && (!a.isOwnedBy(player))) ||            ((a.owner() == 0) && ((a instanceof Company)||(a instanceof Tax))))                toll = a.toll();       int go = board.getGo(); // è passato dal via       if (((go-p>0)&&(go-p<n))||((np-go>0)&&(np-go<n)))          bonus = board.getAllotment(go).toll();       players[id].setSum(players[id].getSum()+toll+bonus);       players[id].setPosition(np);    }  }  public void buildAHouse(int player, int[] where) throws CannotBuildOnDifferentLotsException, CannotBuildOnNotOwnedLotsException, CannotBuildWrongConfigurationException {    Allotment a1 = board.getAllotment(where[0]-1),               a2 = board.getAllotment(where[1]-1),               a3 = board.getAllotment(where[2]-1);    // sono dello stesso lotto    if (!((((ColoredLot)a1).getColor() == ((ColoredLot)a2).getColor()) &&          (((ColoredLot)a2).getColor() == ((ColoredLot)a3).getColor())))          throw new CannotBuildOnDifferentLotsException();        // sono dello stesso proprietario e non è own    if (Math.cbrt(((ColoredLot)a1).getLotsOwner(((ColoredLot)a1).getColor())) != player) throw new CannotBuildOnNotOwnedLotsException();        if (a1 instanceof Lot) {      board.setAllotment(where[0]-1, new LotWithAHouse(a1.getName(), ((ColoredLot)a1).getColor(), a1.owner()));      board.setAllotment(where[1]-1, new LotWithAHouse(a2.getName(), ((ColoredLot)a2).getColor(), a2.owner()));      board.setAllotment(where[2]-1, new LotWithAHouse(a3.getName(), ((ColoredLot)a3).getColor(), a3.owner()));    } else    if (a1 instanceof LotWithAHouse) {      board.setAllotment(where[0]-1, new LotWithTwoHouses(a1.getName(), ((ColoredLot)a1).getColor(), a1.owner()));      board.setAllotment(where[1]-1, new LotWithTwoHouses(a2.getName(), ((ColoredLot)a2).getColor(), a2.owner()));      board.setAllotment(where[2]-1, new LotWithTwoHouses(a3.getName(), ((ColoredLot)a3).getColor(), a3.owner()));    } else    if (a1 instanceof LotWithTwoHouses) {      board.setAllotment(where[0]-1, new LotWithThreeHouses(a1.getName(), ((ColoredLot)a1).getColor(), a1.owner()));      board.setAllotment(where[1]-1, new LotWithThreeHouses(a2.getName(), ((ColoredLot)a2).getColor(), a2.owner()));      board.setAllotment(where[2]-1, new LotWithThreeHouses(a3.getName(), ((ColoredLot)a3).getColor(), a3.owner()));    } else throw new CannotBuildWrongConfigurationException();    players[((player==5)?3:player-1)].setSum(players[((player==5)?3:player-1)].getSum()-300);  }      public void buildAHotel(int player, int[] where) throws CannotBuildOnDifferentLotsException, CannotBuildOnNotOwnedLotsException, CannotBuildWrongConfigurationException {    Allotment a1 = board.getAllotment(where[0]-1),               a2 = board.getAllotment(where[1]-1),               a3 = board.getAllotment(where[2]-1);    // sono dello stesso lotto    if (!((((ColoredLot)a1).getColor() == ((ColoredLot)a2).getColor()) &&          (((ColoredLot)a2).getColor() == ((ColoredLot)a3).getColor())))         throw new CannotBuildOnDifferentLotsException();        // sono dello stesso proprietario e non è own    if (Math.cbrt(((ColoredLot)a1).getLotsOwner(((ColoredLot)a1).getColor())) != player) throw new CannotBuildOnNotOwnedLotsException();        if (a1 instanceof LotWithThreeHouses) {      board.setAllotment(where[0]-1, new LotWithAnHotel(a1.getName(), ((ColoredLot)a1).getColor(), a1.owner()));      board.setAllotment(where[1]-1, new LotWithAnHotel(a2.getName(), ((ColoredLot)a2).getColor(), a2.owner()));      board.setAllotment(where[2]-1, new LotWithAnHotel(a3.getName(), ((ColoredLot)a3).getColor(), a3.owner()));    } else throw new CannotBuildWrongConfigurationException();    players[((player==5)?3:player-1)].setSum(players[((player==5)?3:player-1)].getSum()-3000);  }  public static void main(String[] args) throws Exception {    Game m = new Game("test-input.txt");    for (Player p : m.players) System.out.println(p);    System.out.println(m.board);    for (int i=0; i<7; i++) {      for (Player p : m.players) {        m.move(p.id());        if ((i==2) && (p.id()==2)) {           try {             m.buildAHouse(p.id(), new int[]{6, 8, 9});           } catch(Exception e) {System.out.println("### ci sono già 3 case e non ti appartiene!!!"); e.printStackTrace();}           try {             m.buildAHotel(p.id(), new int[]{24, 23, 21});           } catch(Exception e) {System.out.println("### ci sono solo 2 case non puoi costruire un hotel ");e.printStackTrace();}        }        if ((i==3) && (p.id()==5)) {           try {             m.buildAHouse(p.id(), new int[]{6, 8, 9});           } catch(Exception e) {System.out.println("### non li possiedi ");e.printStackTrace();}           try {             m.buildAHotel(p.id(), new int[]{1, 2, 3});           } catch(Exception e) {System.out.println("### c'è solo una casa ");e.printStackTrace();}           m.buildAHouse(p.id(), new int[]{1, 2, 3});        }        System.out.println(p);      }      System.out.println(m.board);    }    m.board.backup("backup.txt");    m.players[0].setSum(m.players[0].getSum()+1000);    m.buildAHotel(1, new int[]{6, 8, 9});    System.out.println(m.players[0]);    Board b2 = (Board)m.board.clone();    b2.setFilename("backup.txt");    b2.write();  }}

⌨️ 快捷键说明

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