Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 28.92 KB | None | 0 0
  1. /* This is the driving engine of the program. It parses the command-line
  2.  * arguments and calls the appropriate methods in the other classes.
  3.  *
  4.  * You should edit this file in three ways:
  5.  * 1) Insert your database username and password in the proper places.
  6.  * 2) Implement the generation of the world by reading the world file.
  7.  * 3) Implement the three functions showPossibleMoves, showPlayerAssets
  8.  *    and showScores.
  9.  */
  10. import java.math.BigDecimal;
  11. import java.net.URL;
  12. import java.sql.*; // JDBC stuff.
  13. import java.util.*;
  14. import java.io.*;  // Reading user input.
  15. import java.util.concurrent.Executor;
  16.  
  17. public class Game
  18. {
  19.     public class Player
  20.     {
  21.         String playername;
  22.         String personnummer;
  23.         String country;
  24.         private String startingArea;
  25.  
  26.         public Player (String name, String nr, String cntry, String startingArea) {
  27.             this.playername = name;
  28.             this.personnummer = nr;
  29.             this.personnummer = nr;
  30.             this.country = cntry;
  31.             this.startingArea = startingArea;
  32.         }
  33.     }
  34.  
  35.     String USERNAME = "USERNAME";
  36.     String PASSWORD = "PASSWORD";
  37.  
  38.     /* Print command optionssetup.
  39.     * /!\ you don't need to change this function! */
  40.     public void optionssetup() {
  41.         System.out.println();
  42.         System.out.println("Setup-Options:");
  43.         System.out.println("        n[ew player] <player name> <personnummer> <country>");
  44.         System.out.println("        d[one]");
  45.         System.out.println();
  46.     }
  47.  
  48.     /* Print command options.
  49.     * /!\ you don't need to change this function! */
  50.     public void options() {
  51.         System.out.println("\nOptions:");
  52.         System.out.println("    n[ext moves] [area name] [area country]");
  53.         System.out.println("    l[ist properties] [player number] [player country]");
  54.         System.out.println("    s[cores]");
  55.         System.out.println("    r[efund] <area1 name> <area1 country> [area2 name] [area2 country]");
  56.         System.out.println("    b[uy] [name] <area1 name> <area1 country> [area2 name] [area2 country]");
  57.         System.out.println("    m[ove] <area1 name> <area1 country>");
  58.         System.out.println("    p[layers]");
  59.         System.out.println("    q[uit move]");
  60.         System.out.println("    [...] is optional\n");
  61.     }
  62.  
  63.     /* Given a town name, country and population, this function
  64.       * should try to insert an area and a town (and possibly also a country)
  65.       * for the given attributes.
  66.       */
  67.     void insertTown(Connection conn, String name, String country, String population) throws SQLException  {
  68.         // TODO: Your implementation here
  69.         String query = "INSERT INTO Towns (name, country, population) VALUES (?, ?, cast(? as INT))";
  70.         PreparedStatement st = conn.prepareStatement(query);
  71.         st.setString(1, name);
  72.         st.setString(2, country);
  73.         st.setString(3, population);
  74.         st.executeUpdate();
  75.  
  76.         // TODO TO HERE
  77.     }
  78.  
  79.     /* Given a city name, country and population, this function
  80.       * should try to insert an area and a city (and possibly also a country)
  81.       * for the given attributes.
  82.       * The city visitbonus should be set to 0.
  83.       */
  84.     void insertCity(Connection conn, String name, String country, String population) throws SQLException {
  85.         // TODO: Your implementation here
  86.         String query = "INSERT INTO Cities (name, country, population) VALUES (?, ?, cast(? as INT))";
  87.         PreparedStatement st = conn.prepareStatement(query);
  88.         st.setString(1, name);
  89.         st.setString(2, country);
  90.         st.setString(3, population);
  91.         st.executeUpdate();
  92.         // TODO TO HERE
  93.     }
  94.  
  95.     /* Given two areas, this function
  96.       * should try to insert a government owned road with tax 0
  97.       * between these two areas.
  98.       */
  99.     void insertRoad(Connection conn, String area1, String country1, String area2, String country2) throws SQLException {
  100.         // TODO: Your implementation here
  101.         String query = "INSERT INTO Roads (area1, country1, area2, country2) VALUES (?, ?, ?, ?)";
  102.         PreparedStatement st = conn.prepareStatement(query);
  103.         st.setString(1, area1);
  104.         st.setString(2, country1);
  105.         st.setString(3, area2);
  106.         st.setString(4, country2);
  107.         st.executeUpdate();
  108.         // TODO TO HERE
  109.     }
  110.  
  111.     /* Given a player, this function
  112.      * should return the area name of the player's current location.
  113.      */
  114.     String getCurrentArea(Connection conn, Player person) throws SQLException {
  115.         // TODO: Your implementation here
  116.         String query = "SELECT locationarea FROM Persons WHERE (?=personnummer AND ?=country )";
  117.         PreparedStatement st = conn.prepareStatement(query);
  118.         st.setString(1, person.personnummer);
  119.         st.setString(2, person.country);
  120.         ResultSet rs = st.executeQuery();
  121.         String area = null;
  122.         while(rs.next()){
  123.             area = rs.getString("locationarea");
  124.         }
  125.         return area;
  126.         // TODO TO HERE
  127.     }
  128.  
  129.     /* Given a player, this function
  130.      * should return the country name of the player's current location.
  131.      */
  132.     String getCurrentCountry(Connection conn, Player person) throws SQLException {
  133.         // TODO: Your implementation here
  134.         String query = "SELECT locationcountry FROM Persons WHERE (?=personnummer AND ?=country )";
  135.         PreparedStatement st = conn.prepareStatement(query);
  136.         st.setString(1, person.personnummer);
  137.         st.setString(2, person.country);
  138.         ResultSet rs = st.executeQuery();
  139.         String country = null;
  140.         while(rs.next()){
  141.             country = rs.getString("locationcountry");
  142.         }
  143.         return country;
  144.         // TODO TO HERE
  145.     }
  146.  
  147.     /* Given a player, this function
  148.       * should try to insert a table entry in persons for this player
  149.      * and return 1 in case of a success and 0 otherwise.
  150.       * The location should be random and the budget should be 1000.
  151.      */
  152.     int createPlayer(Connection conn, Player person) throws SQLException {
  153.         // TODO: Your implementation here
  154.         try {
  155.             String query = "SELECT country, name FROM Areas WHERE country <> '' ORDER BY RANDOM() LIMIT 1";
  156.             PreparedStatement st = conn.prepareStatement(query);
  157.             ResultSet rs = st.executeQuery();
  158.             String country = null;
  159.             String area = null;
  160.             while (rs.next()) {
  161.                 country = rs.getString("country");
  162.                 area = rs.getString("name");
  163.             }
  164.             query = "INSERT INTO Persons (country, personnummer, name, locationcountry, locationarea, budget) VALUES (?, ?, ?, ?, ?, cast(? as INT))";
  165.             st = conn.prepareStatement(query);
  166.             st.setString(1, person.country);
  167.             st.setString(2, person.personnummer);
  168.             st.setString(3, person.playername);
  169.             st.setString(4, country);
  170.             st.setString(5, area);
  171.             st.setString(6, "1000");
  172.             st.executeUpdate();
  173.         }
  174.         catch(Exception e){
  175.             System.err.println("DUPLICATE PERSON!");
  176.             return 0;
  177.         }
  178.         return 1;
  179.  
  180.         // TODO TO HERE
  181.     }
  182.  
  183.  
  184.     /* Given a player and an area name and country name, this function
  185.      * sould show all directly-reachable destinations for the player from the
  186.      * area from the arguments.
  187.      * The output should include area names, country names and the associated road-taxes
  188.       */
  189.     void getNextMoves(Connection conn, Player person, String area, String country) throws SQLException {
  190.         // TODO: Your implementation here
  191.         String query = "SELECT destarea, destcountry, cost FROM NextMoves WHERE (?=area AND ?=country )";
  192.         PreparedStatement st = conn.prepareStatement(query);
  193.         st.setString(1, area);
  194.         st.setString(2, country);
  195.         ResultSet rs = st.executeQuery();
  196.         String darea = null;
  197.         String dcountry = null;
  198.         int cost = 0;
  199.         while(rs.next()){
  200.             darea = rs.getString("destarea");
  201.             dcountry = rs.getString("destcountry");
  202.             cost = rs.getInt("cost");
  203.             System.out.println("To: "+darea+", "+dcountry+" Cost: "+cost);
  204.         }
  205.         // TODO TO HERE
  206.     }
  207.  
  208.     /* Given a player, this function
  209.        * sould show all directly-reachable destinations for the player from
  210.      * the player's current location.
  211.      * The output should include area names, country names and the associated road-taxes
  212.      */
  213.     void getNextMoves(Connection conn, Player person) throws SQLException {
  214.         // TODO: Your implementation here
  215.         // hint: Use your implementation of the overloaded getNextMoves function
  216.         /*String query = "SELECT locationarea, locationcountry FROM Persons WHERE (?=personnummer, ?=country)";
  217.         PreparedStatement st = conn.prepareStatement(query);
  218.         st.setString(1, person.personnummer);
  219.         st.setString(2, person.country);
  220.         ResultSet rs = st.executeQuery();
  221.         String area = null;
  222.         String country = null;
  223.         while(rs.next()) {
  224.             area = rs.getString("locationarea");
  225.             country = rs.getString("locationcountry");
  226.         }*/
  227.         String area = getCurrentArea(conn, person);
  228.         String country = getCurrentCountry(conn, person);
  229.         getNextMoves(conn, person, area, country);
  230.         // TODO TO HERE
  231.     }
  232.  
  233.     /* Given a personnummer and a country, this function
  234.      * should list all properties (roads and hotels) of the person
  235.      * that is identified by the tuple of personnummer and country.
  236.      */
  237.     void listProperties(Connection conn, String personnummer, String country) throws SQLException {
  238.         // TODO: Your implementation here
  239.         String query = "SELECT fromcountry, fromarea, tocountry, toarea, roadtax, name AS hotelname, locationcountry, locationname  FROM (SELECT Roads.fromcountry, Roads.fromarea, Roads.tocountry, Roads.toarea, Roads.roadtax, Hotels.name, Hotels.locationcountry, Hotels.locationname, Hotels.ownerpersonnummer AS hpn, Roads.ownerpersonnummer AS rpn, Hotels.ownercountry AS hcountry, Roads.ownercountry AS rcountry FROM (Roads FULL JOIN Hotels ON (Hotels.ownerpersonnummer=Roads.ownerpersonnummer AND Hotels.ownercountry=Roads.ownercountry))) AS Properties WHERE ((?=rpn AND ?=rcountry) OR (?=hpn AND ?=hcountry))";
  240.         PreparedStatement st = conn.prepareStatement(query);
  241.         st.setString(1, personnummer);
  242.         st.setString(2, country);
  243.         st.setString(3, personnummer);
  244.         st.setString(4, country);
  245.         ResultSet rs = st.executeQuery();
  246.         while(rs.next()){
  247.             System.out.println("Roads: From: "+rs.getString("fromcountry")+", "+rs.getString("fromarea")+" To: "+rs.getString("tocountry")+", "+rs.getString("toarea")+" Roadtax: " + rs.getString("roadtax"));
  248.             System.out.println("Hotels: Name: "+rs.getString("hotelname")+" Location: "+rs.getString("locationcountry")+", "+rs.getString("locationname"));
  249.         }
  250.         // TODO TO HERE
  251.     }
  252.  
  253.     /* Given a player, this function
  254.      * should list all properties of the player.
  255.      */
  256.     void listProperties(Connection conn, Player person) throws SQLException {
  257.         // TODO: Your implementation here
  258.         // hint: Use your implementation of the overlaoded listProperties function
  259.         listProperties(conn, person.personnummer, person.country);
  260.         // TODO TO HERE
  261.     }
  262.  
  263.     /* This function should print the budget, assets and refund values for all players.
  264.      */
  265.     void showScores(Connection conn) throws SQLException {
  266.         // TODO: Your implementation here
  267.         String query = "SELECT * FROM AssetSummary";
  268.         PreparedStatement st = conn.prepareStatement(query);
  269.         ResultSet rs = st.executeQuery();
  270.         while(rs.next()){
  271.             System.out.println("Player: " + rs.getString("personnummer")+ " Budget: "+rs.getString("budget")+ " Assets: " + rs.getString("assets")+" Reclaimable: "+ rs.getString("reclaimable"));
  272.         }
  273.         // TODO TO HERE
  274.     }
  275.  
  276.     /* Given a player, a from area and a to area, this function
  277.      * should try to sell the road between these areas owned by the player
  278.      * and return 1 in case of a success and 0 otherwise.
  279.      */
  280.     int sellRoad(Connection conn, Player person, String area1, String country1, String area2, String country2) throws SQLException {
  281.         // TODO: Your implementation here
  282.         return 0;
  283.         // TODO TO HERE
  284.     }
  285.  
  286.     /* Given a player and a city, this function
  287.      * should try to sell the hotel in this city owned by the player
  288.      * and return 1 in case of a success and 0 otherwise.
  289.      */
  290.     int sellHotel(Connection conn, Player person, String city, String country) throws SQLException {
  291.         // TODO: Your implementation here
  292.         try {
  293.             String query = "DELETE * FROM Hotels WHERE(?=ownerpersonnummer AND ?=ownercountry AND ?=locationname AND ?=locationcountry)";
  294.             PreparedStatement st = conn.prepareStatement(query);
  295.             st.setString(1, person.personnummer);
  296.             st.setString(2, person.country);
  297.             st.setString(3, city);
  298.             st.setString(4, country);
  299.             st.executeUpdate();
  300.         }catch(Exception e){
  301.             System.err.println("Sale failed!");
  302.             return 0;
  303.         } return 1;
  304.         // TODO TO HERE
  305.     }
  306.  
  307.     /* Given a player, a from area and a to area, this function
  308.      * should try to buy a road between these areas owned by the player
  309.      * and return 1 in case of a success and 0 otherwise.
  310.      */
  311.     int buyRoad(Connection conn, Player person, String area1, String country1, String area2, String country2) throws SQLException {
  312.         // TODO: Your implementation here
  313.         try {
  314.             String query = "INSERT INTO Roads(fromcountry, fromarea, tocountry, toarea, ownercountry, ownerpersonnummer, roadtax) VALUES(?, ?, ?, ?, ?, ?, 0)";
  315.             PreparedStatement st = conn.prepareStatement(query);
  316.             st.setString(1, country1);
  317.             st.setString(2, area1);
  318.             st.setString(3, country2);
  319.             st.setString(4, area2);
  320.             st.setString(5, person.country);
  321.             st.setString(6, person.personnummer);
  322.             st.executeUpdate();
  323.         }catch(Exception e){
  324.             System.err.println("Purchase failed!");
  325.             return 0;
  326.         } return 1;
  327.         // TODO TO HERE
  328.     }
  329.  
  330.     /* Given a player and a city, this function
  331.      * should try to buy a hotel in this city owned by the player
  332.      * and return 1 in case of a success and 0 otherwise.
  333.      */
  334.     int buyHotel(Connection conn, Player person, String name, String city, String country) throws SQLException {
  335.         // TODO: Your implementation here
  336.         try {
  337.             String query = "INSERT INTO Hotels(name, locationcountry, locationname, ownercountry, ownerpersonnummer) VALUES(?, ?, ?, ?, ? )";
  338.             PreparedStatement st = conn.prepareStatement(query);
  339.             st.setString(1, name);
  340.             st.setString(2, country);
  341.             st.setString(3, city);
  342.             st.setString(4, person.country);
  343.             st.setString(5, person.personnummer);
  344.             st.executeUpdate();
  345.         }catch(Exception e){
  346.             System.err.println("Purchase failed!");
  347.             return 0;
  348.         } return 1;
  349.         // TODO TO HERE
  350.     }
  351.  
  352.     /* Given a player and a new location, this function
  353.      * should try to update the players location
  354.      * and return 1 in case of a success and 0 otherwise.
  355.      */
  356.     int changeLocation(Connection conn, Player person, String area, String country) throws SQLException {
  357.         // TODO: Your implementation here
  358.         try {
  359.             String query = "UPDATE Persons SET ?=locationarea, ?=locationcountry WHERE (?=personnummer AND ?=personcountry)";
  360.             PreparedStatement st = conn.prepareStatement(query);
  361.             st.setString(1, area);
  362.             st.setString(2, country);
  363.             st.setString(3, person.personnummer);
  364.             st.setString(4, person.country);
  365.             st.executeUpdate();
  366.         }catch(Exception e){
  367.             System.err.println("Change of location failed!");
  368.             return 0;
  369.         } return 1;
  370.  
  371.         // TODO TO HERE
  372.     }
  373.  
  374.     /* This function should add the visitbonus of 1000 to a random city
  375.       */
  376.     void setVisitingBonus(Connection conn) throws SQLException {
  377.         // TODO: Your implementation here
  378.         String query = "SELECT country, name, visitbonus FROM Cities ORDER BY RANDOM() LIMIT 1";
  379.         PreparedStatement st = conn.prepareStatement(query);
  380.         ResultSet rs = st.executeQuery();
  381.         String country = null;
  382.         String name = null;
  383.         int newBonus = 0;
  384.         while(rs.next()) {
  385.             country = rs.getString("country");
  386.             name = rs.getString("name");
  387.             newBonus = rs.getInt("visitbonus") + 1000;
  388.         }
  389.         query = "UPDATE Cities SET visitbonus=? WHERE (?=country, ?=name)";
  390.         st = conn.prepareStatement(query);
  391.         st.setString(1, ""+newBonus);
  392.         st.setString(2, country);
  393.         st.setString(3, name);
  394.         st.executeUpdate();
  395.         // TODO TO HERE
  396.     }
  397.  
  398.     /* This function should print the winner of the game based on the currently highest budget.
  399.       */
  400.     void announceWinner(Connection conn) throws SQLException {
  401.         // TODO: Your implementation here
  402.         // TODO: Your implementation here
  403.         String query = "SELECT personnummer FROM (SELECT * FROM AssetSummary) AS summary WHERE budget = MAX(budget)";
  404.         PreparedStatement st = conn.prepareStatement(query);
  405.         ResultSet rs = st.executeQuery();
  406.         String name = null;
  407.         while(rs.next()) {
  408.             name = rs.getString("personnummer");
  409.         }
  410.         System.out.println("THE WINNER IS: "+name);
  411.         // TODO TO HERE
  412.     }
  413.  
  414.     void play (String worldfile) throws IOException {
  415.  
  416.         // Read username and password from config.cfg
  417.         try {
  418.             BufferedReader nf = new BufferedReader(new FileReader("config.cfg"));
  419.             String line;
  420.             if ((line = nf.readLine()) != null) {
  421.                 USERNAME = line;
  422.                 System.out.println("username= " + USERNAME);
  423.             }
  424.             if ((line = nf.readLine()) != null) {
  425.                 PASSWORD = line;
  426.                 System.out.println("password= " + PASSWORD);
  427.             }
  428.         } catch (Exception e) {
  429.             System.out.println(e.getMessage());
  430.         }
  431.  
  432.         if (USERNAME.equals("USERNAME") || PASSWORD.equals("PASSWORD")) {
  433.             System.out.println("CONFIG FILE HAS WRONG FORMAT");
  434.             return;
  435.         }
  436.  
  437.         try {
  438.             try {
  439.                 Class.forName("org.postgresql.Driver");
  440.             } catch (Exception e) {
  441.                 System.out.println(e.getMessage());
  442.             }
  443.             String url = "jdbc:postgresql://ate.ita.chalmers.se/";
  444.             Properties props = new Properties();
  445.             props.setProperty("user",USERNAME);
  446.             props.setProperty("password",PASSWORD);
  447.  
  448.             final Connection conn = DriverManager.getConnection(url, props);
  449.  
  450.             /* This block creates the government entry and the necessary
  451.              * country and area for that.
  452.              */
  453.             try {
  454.                 PreparedStatement statement = conn.prepareStatement("INSERT INTO Countries (name) VALUES (?)");
  455.                 statement.setString(1, "");
  456.                 statement.executeUpdate();
  457.                 statement = conn.prepareStatement("INSERT INTO Areas (country, name, population) VALUES (?, ?, cast(? as INT))");
  458.                 statement.setString(1, "");
  459.                 statement.setString(2, "");
  460.                 statement.setString(3, "1");
  461.                 statement.executeUpdate();
  462.                 statement = conn.prepareStatement("INSERT INTO Persons (country, personnummer, name, locationcountry, locationarea, budget) VALUES (?, ?, ?, ?, ?, cast(? as NUMERIC))");
  463.                 statement.setString(1, "");
  464.                 statement.setString(2, "");
  465.                 statement.setString(3, "The Government");
  466.                 statement.setString(4, "");
  467.                 statement.setString(5, "");
  468.                 statement.setString(6, "0");
  469.                 statement.executeUpdate();
  470.             } catch (SQLException e) {
  471.                 System.out.println(e.getMessage());
  472.             }
  473.  
  474.             // Initialize the database from the worldfile
  475.             try {
  476.                 BufferedReader br = new BufferedReader(new FileReader(worldfile));
  477.                 String line;
  478.                 while ((line = br.readLine()) != null) {
  479.                     String[] cmd = line.split(" +");
  480.                     if ("ROAD".equals(cmd[0]) && (cmd.length == 5)) {
  481.                         insertRoad(conn, cmd[1], cmd[2], cmd[3], cmd[4]);
  482.                     } else if ("TOWN".equals(cmd[0]) && (cmd.length == 4)) {
  483.                         /* Create an area and a town entry in the database */
  484.                         insertTown(conn, cmd[1], cmd[2], cmd[3]);
  485.                     } else if ("CITY".equals(cmd[0]) && (cmd.length == 4)) {
  486.                         /* Create an area and a city entry in the database */
  487.                         insertCity(conn, cmd[1], cmd[2], cmd[3]);
  488.                     }
  489.                 }
  490.             } catch (Exception e) {
  491.                 System.out.println(e.getMessage());
  492.             }
  493.  
  494.             ArrayList<Player> players = new ArrayList<Player>();
  495.  
  496.             while(true) {
  497.                 optionssetup();
  498.                 String mode = readLine("? > ");
  499.                 String[] cmd = mode.split(" +");
  500.                 cmd[0] = cmd[0].toLowerCase();
  501.                 if ("new player".startsWith(cmd[0]) && (cmd.length == 5)) {
  502.                     Player nextplayer = new Player(cmd[1], cmd[2], cmd[3], cmd[4]);
  503.                     if (createPlayer(conn, nextplayer) == 1) {
  504.                         players.add(nextplayer);
  505.                     }
  506.                 } else if ("done".startsWith(cmd[0]) && (cmd.length == 1)) {
  507.                     break;
  508.                 } else {
  509.                     System.out.println("\nInvalid option.");
  510.                 }
  511.             }
  512.  
  513.             System.out.println("\nGL HF!");
  514.             int roundcounter = 1;
  515.             int maxrounds = 5;
  516.             while(roundcounter <= maxrounds) {
  517.                 System.out.println("\nWe are starting the " + roundcounter + ". round!!!");
  518.                 /* for each player from the playerlist */
  519.                 for (int i = 0; i < players.size(); ++i) {
  520.                     System.out.println("\nIt's your turn " + players.get(i).playername + "!");
  521.                     System.out.println("You are currently located in " + getCurrentArea(conn, players.get(i)) + " (" + getCurrentCountry(conn, players.get(i)) + ")");
  522.                     while (true) {
  523.                         options();
  524.                         String mode = readLine("? > ");
  525.                         String[] cmd = mode.split(" +");
  526.                         cmd[0] = cmd[0].toLowerCase();
  527.                         if ("next moves".startsWith(cmd[0]) && (cmd.length == 1 || cmd.length == 3)) {
  528.                             /* Show next moves from a location or current location. Turn continues. */
  529.                             if (cmd.length == 1) {
  530.                                 String area = getCurrentArea(conn, players.get(i));
  531.                                 String country = getCurrentCountry(conn, players.get(i));
  532.                                 getNextMoves(conn, players.get(i));
  533.                             } else {
  534.                                 getNextMoves(conn, players.get(i), cmd[1], cmd[2]);
  535.                             }
  536.                         } else if ("list properties".startsWith(cmd[0]) && (cmd.length == 1 || cmd.length == 3)) {
  537.                             /* List properties of a player. Can be a specified player
  538.                                or the player himself. Turn continues. */
  539.                             if (cmd.length == 1) {
  540.                                 listProperties(conn, players.get(i));
  541.                             } else {
  542.                                 listProperties(conn, cmd[1], cmd[2]);
  543.                             }
  544.                         } else if ("scores".startsWith(cmd[0]) && cmd.length == 1) {
  545.                             /* Show scores for all players. Turn continues. */
  546.                             showScores(conn);
  547.                         } else if ("players".startsWith(cmd[0]) && cmd.length == 1) {
  548.                             /* Show scores for all players. Turn continues. */
  549.                             System.out.println("\nPlayers:");
  550.                             for (int k = 0; k < players.size(); ++k) {
  551.                                 System.out.println("\t" + players.get(k).playername + ": " + players.get(k).personnummer + " (" + players.get(k).country + ") ");
  552.                             }
  553.                         } else if ("refund".startsWith(cmd[0]) && (cmd.length == 3 || cmd.length == 5)) {
  554.                             if (cmd.length == 5) {
  555.                                 /* Sell road from arguments. If no road was sold the turn
  556.                                    continues. Otherwise the turn ends. */
  557.                                 if (sellRoad(conn, players.get(i), cmd[1], cmd[2], cmd[3], cmd[4]) == 1) {
  558.                                     break;
  559.                                 } else {
  560.                                     System.out.println("\nTry something else.");
  561.                                 }
  562.                             } else {
  563.                                 /* Sell hotel from arguments. If no hotel was sold the turn
  564.                                    continues. Otherwise the turn ends. */
  565.                                 if (sellHotel(conn, players.get(i), cmd[1], cmd[2]) == 1) {
  566.                                     break;
  567.                                 } else {
  568.                                     System.out.println("\nTry something else.");
  569.                                 }
  570.                             }
  571.                         } else if ("buy".startsWith(cmd[0]) && (cmd.length == 4 || cmd.length == 5)) {
  572.                             if (cmd.length == 5) {
  573.                                 /* Buy road from arguments. If no road was bought the turn
  574.                                    continues. Otherwise the turn ends. */
  575.                                 if (buyRoad(conn, players.get(i), cmd[1], cmd[2], cmd[3], cmd[4]) == 1) {
  576.                                     break;
  577.                                 } else {
  578.                                     System.out.println("\nTry something else.");
  579.                                 }
  580.                             } else {
  581.                                 /* Buy hotel from arguments. If no hotel was bought the turn
  582.                                    continues. Otherwise the turn ends. */
  583.                                 if (buyHotel(conn, players.get(i), cmd[1], cmd[2], cmd[3]) == 1) {
  584.                                     break;
  585.                                 } else {
  586.                                     System.out.println("\nTry something else.");
  587.                                 }
  588.                             }
  589.                         } else if ("move".startsWith(cmd[0]) && cmd.length == 3) {
  590.                             /* Change the location of the player to the area from the arguments.
  591.                                If the move was legal the turn ends. Otherwise the turn continues. */
  592.                             if (changeLocation(conn, players.get(i), cmd[1], cmd[2]) == 1) {
  593.                                 break;
  594.                             } else {
  595.                                 System.out.println("\nTry something else.");
  596.                             }
  597.                         } else if ("quit".startsWith(cmd[0]) && cmd.length == 1) {
  598.                             /* End the move of the player without any action */
  599.                             break;
  600.                         } else {
  601.                             System.out.println("\nYou chose an invalid option. Try again.");
  602.                         }
  603.                     }
  604.                 }
  605.                 setVisitingBonus(conn);
  606.                 ++roundcounter;
  607.             }
  608.             announceWinner(conn);
  609.             System.out.println("\nGG!\n");
  610.  
  611.             conn.close();
  612.         } catch (SQLException e) {
  613.             System.err.println(e);
  614.             System.exit(2);
  615.         }
  616.     }
  617.  
  618.     private String readLine(String s) throws IOException {
  619.         System.out.print(s);
  620.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  621.         char c;
  622.         StringBuilder stringBuilder = new StringBuilder();
  623.         do {
  624.             c = (char) bufferedReader.read();
  625.             stringBuilder.append(c);
  626.         } while(String.valueOf(c).matches(".")); // Without the DOTALL switch, the dot in a java regex matches all characters except newlines
  627.  
  628.         System.out.println("");
  629.         stringBuilder.deleteCharAt(stringBuilder.length()-1);
  630.  
  631.         return stringBuilder.toString();
  632.     }
  633.  
  634.     /* main: parses the input commands.
  635.      * /!\ You don't need to change this function! */
  636.     public static void main(String[] args) throws Exception
  637.     {
  638.         String worldfile = args[0];
  639.         Game g = new Game();
  640.         g.play(worldfile);
  641.     }
  642. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement