private boolean place(Command command) { if (!command.hasSecondWord() || !command.hasThirdWord()) { System.out.println("Please enter a valid position"); return true; } int x = Integer.parseInt(command.getSecondWord()); int y = Integer.parseInt(command.getThirdWord()); Position pos = new Position(x, y); Piece piece = null; if (validMove(pos)) { if (turn == X) { piece = new X(); } else if (turn == O) { piece = new O(); } board.addPiece(pos, piece); view.update(board); view.display(); return checkGameStatus(pos); } else { System.out.println("Invalid Position"); return place(parser.getCommand()); } }
public ControlGroup(Model model, View view) { this.model = model; this.view = view; ControlInterface controleInterface = new ControlInterface(model, view); ControlBoard controleBoard = new ControlBoard(model, view); ControlWall controleWall = new ControlWall(model, view); view.setActionListener(controleInterface, controleBoard, controleWall); view.display(true); }
/** * This method is used to work according to the observable that received. * * @param observable * @param obj */ @Override public void update(Observable observable, Object obj) { if (observable == view) { if (((obj.getClass()).getName()).equals("presenter.Properties")) { Properties prop = (Properties) obj; model.setProperties(prop); } else { Command command; String line = (String) obj; command = commandMap.get(line.split(" ")[0]); if (command != null) { if (line.split(" ").length > 0) { command.doCommand(line.substring(line.indexOf(' ') + 1)); } else { command.doCommand(""); } } else { view.display("Invalid command"); } } } else if (observable == model) { String line = (String) obj; String[] tempArr = line.split(" "); switch (tempArr[0]) { case "mazeIsReady": Maze3d maze = (Maze3d) model.getUserCommand(line); view.displayMaze(maze); view.displayPosition(maze.getStartPosition()); break; case "displayCrossSectionBy": view.displayCrossSectionBy((int[][]) model.getUserCommand(line)); break; case "saveMaze": view.display("maze is saved successfully in file " + (String) model.getUserCommand(line)); break; case "loadMaze": Maze3d myMaze = (Maze3d) model.getUserCommand(line); view.displayMaze(myMaze); view.displayPosition(myMaze.getStartPosition()); break; case "mazeSize": view.display("size maze in memory is: " + (int) model.getUserCommand(line)); break; case "fileSize": view.display("file size is: " + (int) model.getUserCommand(line)); break; case "solutionIsReady": view.displaySolution(model.getMazeSolution((String) model.getUserCommand(line))); break; case "loadZip": view.display("The maze has loaded from " + (String) model.getUserCommand(line)); break; case "saveZip": view.display("file is saved to " + (String) model.getUserCommand(line)); break; case "move": view.displayMaze(model.getMaze3d((String) model.getUserCommand(line))); view.displayPosition(model.getPositionFromHash((String) model.getUserCommand(line))); break; case "exit": view.display("The system is shutting down"); break; case "null": view.display("Maze " + (String) model.getUserCommand(line) + " is not exist"); break; case "Invalid": switch (tempArr[1]) { case "parameters": view.display("Invalid parameters"); break; case "index": view.display("Invalid index"); break; case "file": view.display("The file " + (String) model.getUserCommand(line) + " is not exist"); break; case "compress": view.display("Cannot collapse the " + (String) model.getUserCommand(line)); break; case "maze": view.display( "There is error with the " + (String) model.getUserCommand(line) + " maze"); case "algorithm": view.display("Invalid algorithm"); break; case "solution": view.display("Solution for " + (String) model.getUserCommand(line) + " is not exist"); break; default: view.display("Invalid parameters"); break; } break; default: view.display("Invalid command"); break; } } }