Android Open Source - ZorbsCity City Shell View






From Project

Back to project page ZorbsCity.

License

The source code is released under:

GNU General Public License

If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jonathan.geoffroy.zorbscity.view.shell;
//from w  w  w  .  ja  va  2  s .  co m
import java.util.Scanner;

import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Road;
import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.FreeHouse;
import jonathan.geoffroy.zorbscity.model.helpers.Coord2D;
import jonathan.geoffroy.zorbscity.view.CityView;

public class CityShellView extends CityView {
  public static final int ROAD = 0, FREEHOUSE = 1;
  
  public CityShellView(String cityName) {
    super(cityName);
  }

  @Override
  public void run() {
    boolean continued = true;
    Scanner scanner = new Scanner(System.in);
    String userIn;
    String[] userSplitting;
    String userCommand;

    while (continued) {
      userIn = scanner.nextLine();
      userSplitting = userIn.split(" ");
      userCommand = userSplitting[0];
      if(userCommand.equals("add")) {
        add_command(userSplitting);
      }
      else if(userCommand.equals("rm")) {
        rm_command(userSplitting);
      }
      else if(userCommand.equals("show")) {
        show_command();
      }
      else if(userCommand.equals("next")) {
        next_command(userSplitting);
      }
      else if(userCommand.equals("money")) {
        money_command(userSplitting);
      }
      else if (userCommand.equals("help")) {
        help_command();
      }
      else {
        help_command();
      }
    }

    scanner.close();

  }

  /**
   * Command to show the help menu
   */
  private void help_command() {
    System.out.println("add [buildingType] [x][y]");
    System.out.println("rm [x][y]");
    System.out.println("show");
    System.out.println("next [nbSteps]");
    System.out.println("money");
    System.out.println("money [amountToAdd]");
  }

  private void money_command(String[] args) {
    if(args.length == 1) {
      System.out.println("" + city.getMoney());
    }
    else {
      try {
        int moneyToAdd = Integer.parseInt(args[1]);
        city.addMoney(moneyToAdd);
      } catch (NumberFormatException e) {
        e.printStackTrace();
      }
    }

  }

  /**
   * command to reach the next step of the simulator
   * @param args nothing, to do just 1 step, or a number to reach _number_ steps
   */
  private void next_command(String[] args) {
    int nbSteps;
    if(args.length == 1) {
      nbSteps = 1;
    }
    else {
      try {
        nbSteps = Integer.parseInt(args[1]);
      } catch (NumberFormatException e) {
        nbSteps = 1;
      }
    }

    for(int i = 0; i < nbSteps; i++) {
      simulator.nextStep();
    }
  }

  /**
   * Command to show the city
   */
  private void show_command() {
    for(int i = 0; i < city.getHeight(); i++) {
      for(int j = 0; j < city.getWidth(); j++) {
        System.out.print("" + String.format("%3d", city.getMapping(new Coord2D(j, i))) + " ");
      }
      System.out.println();
    }

  }

  /**
   * Command to remove a Structure.
   * You must give the x & y coordinates of structure to remove
   * @param args x & y coordinates
   */
  private void rm_command(String[] args) {
    if(args.length < 3) {
      System.out.println("add x y");
    }
    else {
      try {
        int x = Integer.parseInt(args[1]);
        int y = Integer.parseInt(args[2]);
        simulator.rm(new Coord2D(x, y));
      } catch (NumberFormatException e) {
        e.printStackTrace();
      }
    }

  }

  /**
   * Command to add a structure
   * You must give the structure number, and the x & y coordinates of the structure to add
   * @param args the structure number, x & y coordinates
   */
  private void add_command(String[] args) {
    if(args.length < 4) {
      System.out.println("add x y structureType");
    }
    else {
      try {
        int x = Integer.parseInt(args[1]);
        int y = Integer.parseInt(args[2]);
        int structureType = Integer.parseInt(args[3]);
        switch(structureType) {
        case ROAD:
          simulator.add(new Road(), new Coord2D(x, y));
          break;
        case FREEHOUSE:
          simulator.add(new FreeHouse(), new Coord2D(x, y));
          break;
        }
        simulator.rm(new Coord2D(x, y));
      } catch (NumberFormatException e) {
        e.printStackTrace();
      }
    }

  }


}




Java Source Code List

jonathan.geoffroy.zorbscity.MainActivity.java
jonathan.geoffroy.zorbscity.Main.java
jonathan.geoffroy.zorbscity.ZorbCityGame.java
jonathan.geoffroy.zorbscity.client.GwtLauncher.java
jonathan.geoffroy.zorbscity.model.City.java
jonathan.geoffroy.zorbscity.model.Galaxy.java
jonathan.geoffroy.zorbscity.model.SimulatorEvent.java
jonathan.geoffroy.zorbscity.model.Simulator.java
jonathan.geoffroy.zorbscity.model.TimeLine.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Building.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Construction.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Enterprise.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.House.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Road.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Storage.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Structure.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.enterprises.ClothesFactory.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.enterprises.PowerHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.FreeHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.SimpleHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.houses.TentHouse.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.storages.FoodStorage.java
jonathan.geoffroy.zorbscity.model.cityObjects.buildings.storages.SecondaryStorage.java
jonathan.geoffroy.zorbscity.model.cityObjects.people.DeliveryMan.java
jonathan.geoffroy.zorbscity.model.cityObjects.people.Person.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Cloth.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Power.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Resource.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Wheat.java
jonathan.geoffroy.zorbscity.model.cityObjects.resources.Wool.java
jonathan.geoffroy.zorbscity.model.helpers.Coord2D.java
jonathan.geoffroy.zorbscity.model.helpers.NeededList.java
jonathan.geoffroy.zorbscity.model.helpers.ResourcesList.java
jonathan.geoffroy.zorbscity.model.simulator.events.ConsumeNeededResources.java
jonathan.geoffroy.zorbscity.model.simulator.events.ObjectCreatedEvent.java
jonathan.geoffroy.zorbscity.model.simulator.events.WeaknessEvent.java
jonathan.geoffroy.zorbscity.view.CityView.java
jonathan.geoffroy.zorbscity.view.shell.CityShellView.java