FIX: TUI javadox
This commit is contained in:
@@ -4,37 +4,77 @@ import it.polimi.ingsw.gc14.View.IView;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Text-based User Interface (TUI) implementation of {@link IView}.
|
||||||
|
*
|
||||||
|
* <p>Renders the current state of a {@link Game} model directly to the
|
||||||
|
* standard output using Unicode box-drawing characters and fixed-width
|
||||||
|
* ASCII tables (see {@link AsciiTable}).
|
||||||
|
*
|
||||||
|
* <p>The display is split into two side-by-side panels:
|
||||||
|
* <ul>
|
||||||
|
* <li><b>Left panel</b> — board or player status, depending on the render method called.</li>
|
||||||
|
* <li><b>Right panel</b> — menu options legend and the current player's hand.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>The terminal is cleared before each render via a platform-aware
|
||||||
|
* {@code cls} / {@code clear} system call.
|
||||||
|
*
|
||||||
|
* <p>Typical usage:
|
||||||
|
* <pre>{@code
|
||||||
|
* TUI tui = new TUI(game);
|
||||||
|
* tui.setUsername("Alice");
|
||||||
|
* tui.fullRender();
|
||||||
|
* }</pre>
|
||||||
|
*/
|
||||||
public class TUI implements IView {
|
public class TUI implements IView {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link it.polimi.ingsw.gc14.Model.Game } model stored for the game status render.
|
* The {@link Game} model whose state is rendered.
|
||||||
|
* Updated via {@link #update(Game)} whenever the game state changes.
|
||||||
*/
|
*/
|
||||||
private Game model;
|
private Game model;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link it.polimi.ingsw.gc14.Model.Player } username stored for the player hand render.
|
* The username of the local player, used to retrieve and display
|
||||||
|
* that player's hand in the right panel.
|
||||||
|
*
|
||||||
|
* @see #setUsername(String)
|
||||||
*/
|
*/
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for the class {@code TUI}.
|
* Constructs a {@code TUI} bound to the given game model.
|
||||||
* @param model The model that will be displayed.
|
* The username is initialised to an empty string and must be set
|
||||||
|
* separately via {@link #setUsername(String)} before calling any
|
||||||
|
* render method that displays the player's hand.
|
||||||
|
*
|
||||||
|
* @param model the {@link Game} model to display; must not be {@code null}
|
||||||
*/
|
*/
|
||||||
public TUI(Game model) {
|
public TUI(Game model) {
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.username="";
|
this.username = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the {@code Username} of the player, used for the hand player display.
|
* Sets the username of the local player.
|
||||||
* @param username
|
* This value is used by {@link #renderBoard()} and {@link #fullRender()}
|
||||||
|
* to look up the correct player hand via
|
||||||
|
* {@link Game#getPlayerByUsername(String)}.
|
||||||
|
*
|
||||||
|
* @param username the player's username; must match an existing player
|
||||||
|
* in the current {@link Game} model
|
||||||
*/
|
*/
|
||||||
public void setUsername(String username) {
|
public void setUsername(String username) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update {@link it.polimi.ingsw.gc14.Model.Game } model stored for the game status render.
|
* Updates the game model stored in this view.
|
||||||
|
* Should be called whenever the game state changes so that the next
|
||||||
|
* render reflects the latest state.
|
||||||
|
*
|
||||||
|
* @param model the new {@link Game} model; must not be {@code null}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void update(Game model) {
|
public void update(Game model) {
|
||||||
@@ -42,119 +82,136 @@ public class TUI implements IView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default method called to display homepage , implements {@link IView}.
|
* Default render entry point, as required by {@link IView}.
|
||||||
|
* Delegates to {@link #renderBoard()}.
|
||||||
*/
|
*/
|
||||||
public void render()
|
@Override
|
||||||
{
|
public void render() {
|
||||||
renderBoard();
|
renderBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to display players status and board status.
|
* Renders a full view of the game, combining both player status
|
||||||
* In the upper part displays {@link #renderPlayer} information , in a table format.
|
* and board status.
|
||||||
* In the lower part displays {@link #renderBoard} information in the left , menu options and {@code Username} hand in the right.
|
*
|
||||||
|
* <p>Layout:
|
||||||
|
* <ul>
|
||||||
|
* <li><b>Top</b> — player status table produced by
|
||||||
|
* {@link Game#PlayersStamp()}.</li>
|
||||||
|
* <li><b>Bottom-left</b> — board status produced by
|
||||||
|
* {@link Game#BoardStamp()}.</li>
|
||||||
|
* <li><b>Bottom-right</b> — menu options legend and the local
|
||||||
|
* player's hand.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>The terminal is cleared before rendering.
|
||||||
*/
|
*/
|
||||||
public void fullRender()
|
public void fullRender() {
|
||||||
{
|
clearTerminal();
|
||||||
try{
|
List<String> lines = List.of(model.BoardStamp().split("\n"));
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
List<String> lines2 = List.of((printMenuOptions() + "\n" +
|
||||||
ProcessBuilder pb;
|
model.getPlayerByUsername(username)).split("\n"));
|
||||||
if (os.contains("win")) {
|
System.out.println(model.PlayersStamp() + "\n" +
|
||||||
pb = new ProcessBuilder("cmd", "/c", "cls");
|
AsciiTable.sideBySide(lines, lines2, 3));
|
||||||
} else {
|
|
||||||
pb = new ProcessBuilder("clear");
|
|
||||||
}
|
|
||||||
pb.inheritIO().start().waitFor();
|
|
||||||
}
|
|
||||||
catch(Exception e){
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String>lines=List.of(model.BoardStamp().split("\n"));
|
|
||||||
List<String> lines2=List.of((PrintMenuOptions()+"\n"+model.getPlayerByUsername(username)).split("\n"));
|
|
||||||
System.out.println(model.PlayersStamp()+"\n"+ AsciiTable.sideBySide(lines,lines2,3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to display board status.
|
* Renders the board status only.
|
||||||
* Displays turn order ,upper list cards , offer track , lower list cards in the left, menu options and {@code Username} hand in the right.
|
*
|
||||||
|
* <p>Layout:
|
||||||
|
* <ul>
|
||||||
|
* <li><b>Left panel</b> — turn order, upper card row, offer track,
|
||||||
|
* and lower card row, as produced by {@link Game#BoardStamp()}.</li>
|
||||||
|
* <li><b>Right panel</b> — menu options legend followed by the local
|
||||||
|
* player's hand.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>The terminal is cleared before rendering.
|
||||||
*/
|
*/
|
||||||
public void renderBoard()
|
public void renderBoard() {
|
||||||
{
|
clearTerminal();
|
||||||
try{
|
List<String> lines = List.of(model.BoardStamp().split("\n"));
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
List<String> lines2 = List.of((printMenuOptions() + "\nYOUR HAND\n" +
|
||||||
ProcessBuilder pb;
|
model.getPlayerByUsername(username)).split("\n"));
|
||||||
if (os.contains("win")) {
|
System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
|
||||||
pb = new ProcessBuilder("cmd", "/c", "cls");
|
|
||||||
} else {
|
|
||||||
pb = new ProcessBuilder("clear");
|
|
||||||
}
|
|
||||||
pb.inheritIO().start().waitFor();
|
|
||||||
}
|
|
||||||
catch(Exception e){
|
|
||||||
}
|
|
||||||
List<String>lines=List.of(model.BoardStamp().split("\n"));
|
|
||||||
List<String> lines2=List.of((PrintMenuOptions()+"\nYOUR HAND\n"+model.getPlayerByUsername(username)).split("\n"));
|
|
||||||
System.out.println(AsciiTable.sideBySide(lines,lines2,3));
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Method called to display player status.
|
|
||||||
* Displays player prestige value,food value, character deck and building deck , in a table format.
|
|
||||||
*/
|
|
||||||
public void renderPlayer()
|
|
||||||
{
|
|
||||||
try{
|
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
|
||||||
ProcessBuilder pb;
|
|
||||||
if (os.contains("win")) {
|
|
||||||
pb = new ProcessBuilder("cmd", "/c", "cls");
|
|
||||||
} else {
|
|
||||||
pb = new ProcessBuilder("clear");
|
|
||||||
}
|
|
||||||
pb.inheritIO().start().waitFor();
|
|
||||||
}
|
|
||||||
catch(Exception e){
|
|
||||||
}
|
|
||||||
List<String>lines=List.of(model.PlayersStamp().split("\n"));
|
|
||||||
List<String> lines2=List.of(PrintMenuOptions().split("\n"));
|
|
||||||
System.out.println(AsciiTable.sideBySide(lines,lines2,3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to display a message on the terminal.
|
* Renders the player status table only.
|
||||||
|
*
|
||||||
|
* <p>Layout:
|
||||||
|
* <ul>
|
||||||
|
* <li><b>Left panel</b> — prestige, food, character deck, and building
|
||||||
|
* deck for all players, as produced by {@link Game#PlayersStamp()}.</li>
|
||||||
|
* <li><b>Right panel</b> — menu options legend.</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* <p>The terminal is cleared before rendering.
|
||||||
*/
|
*/
|
||||||
public void showMessage(String message)
|
public void renderPlayer() {
|
||||||
{
|
clearTerminal();
|
||||||
|
List<String> lines = List.of(model.PlayersStamp().split("\n"));
|
||||||
|
List<String> lines2 = List.of(printMenuOptions().split("\n"));
|
||||||
|
System.out.println(AsciiTable.sideBySide(lines, lines2, 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints a plain message to standard output.
|
||||||
|
*
|
||||||
|
* @param message the message to display
|
||||||
|
*/
|
||||||
|
public void showMessage(String message) {
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to display a error on the terminal.
|
* Prints an error message to standard output.
|
||||||
|
*
|
||||||
|
* @param message the error message to display
|
||||||
*/
|
*/
|
||||||
public void showError(String message)
|
public void showError(String message) {
|
||||||
{
|
clearTerminal();
|
||||||
|
render();
|
||||||
System.out.println(message);
|
System.out.println(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method used to set up menu options.
|
* Builds and returns the menu options panel as a two-column
|
||||||
* List of runnable command as a legend.
|
* {@link AsciiTable} with rounded borders.
|
||||||
* @return the string format of the menu options.
|
*
|
||||||
|
* <p>The left column lists game action commands (slot choice, draw, pick…),
|
||||||
|
* the right column lists render shortcuts (full, board, players).
|
||||||
|
*
|
||||||
|
* @return the rendered menu table as a multi-line string
|
||||||
*/
|
*/
|
||||||
private String PrintMenuOptions()
|
private String printMenuOptions() {
|
||||||
{
|
var table = new AsciiTable(BorderStyle.ROUNDED, 2);
|
||||||
var table=new AsciiTable(BorderStyle.ROUNDED,2);
|
table.addHeader("Menu Options", "Render Options");
|
||||||
table.addHeader( "Menu Options","Render Options");
|
table.addRow(List.of("0-SlotChoice(pos)", "A-Full Render"));
|
||||||
table.addRow( List.of("0-SlotChoice(pos)","A-Full Render"));
|
table.addRow(List.of("1-DrawUpperTribe(pos)", "B-Board Render"));
|
||||||
table.addRow(List.of("1-DrawUpperTribe(pos)","B-Board Render"));
|
table.addRow(List.of("2-DrawUpperBuilding(pos)", "C-Players Render"));
|
||||||
table.addRow(List.of("2-DrawUpperBuilding(pos)","C-Players Render"));
|
table.addRow(List.of("3-DrawLowerTribe(pos)", ""));
|
||||||
table.addRow(List.of("3-DrawLowerTribe(pos)",""));
|
table.addRow(List.of("4-DrawLowerBuilding(pos)", ""));
|
||||||
table.addRow(List.of("4-DrawLowerBuilding(pos)",""));
|
table.addRow(List.of("5-PickOptionalTribe(pos)", ""));
|
||||||
table.addRow(List.of("5-PickOptionalTribe(pos)",""));
|
table.addRow(List.of("6-PickOptionalBuilding(pos)", ""));
|
||||||
table.addRow(List.of("6-PickOptionalBuilding(pos)",""));
|
table.addRow(List.of("7-NoOptional", ""));
|
||||||
table.addRow(List.of("7-NoOptional",""));
|
table.addRow(List.of("8-NoUpperCard", ""));
|
||||||
table.addRow(List.of("8-NoUpperCard",""));
|
table.addRow(List.of("9-NoLowerCard", ""));
|
||||||
table.addRow(List.of("9-NoLowerCard",""));
|
|
||||||
return table.build();
|
return table.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Clears the terminal using a platform-aware system call.
|
||||||
|
* Uses {@code cls} on Windows and {@code clear} on Unix-like systems.
|
||||||
|
* Failures are silently ignored to avoid interrupting the render flow.
|
||||||
|
*/
|
||||||
|
private void clearTerminal() {
|
||||||
|
try {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
ProcessBuilder pb = os.contains("win")
|
||||||
|
? new ProcessBuilder("cmd", "/c", "cls")
|
||||||
|
: new ProcessBuilder("clear");
|
||||||
|
pb.inheritIO().start().waitFor();
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user