-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardPrinter.java
More file actions
150 lines (129 loc) · 5.8 KB
/
BoardPrinter.java
File metadata and controls
150 lines (129 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package ui;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
/**
* Handles the printing of the game board on the console, providing different views based on the user's role,
* such as SPYMASTER or GUESSER. This class formats and displays the board dynamically based on the content
* and size of the game board.
*/
public class BoardPrinter {
/**
* Enum defining the different roles that can view the board, which affects how the board is displayed.
*/
public enum Role {
SPYMASTER, GUESSER
}
/**
* Prints a single card with formatting based on the viewer's role.
*
* @param card The card to be printed.
* @param index The index of the card on the board, used for display purposes.
* @param role The viewer's role which determines the format of the display.
* @return A formatted string representation of the card.
*/
public static CardToString printCard(JsonObject card, int index, Role role) {
String word = card.get("word").getAsString();
String wordFound = card.get("isFound").getAsBoolean() ? "V" : "X";
String formatLine;
if (role == Role.SPYMASTER) {
// Check if the card is associated with a team
if (!card.get("teamName").getAsString().isEmpty()) {
formatLine = "[" + index + "]" + " " + wordFound + " (" + card.get("teamName").getAsString() + ")";
} else {
formatLine = "[" + index + "]" + " " + wordFound;
}
}
else
formatLine = "[" + index + "]" + " " + wordFound;
return new CardToString(word, formatLine);
}
/**
* Displays the entire game board, adjusting the spacing and alignment based on the maximum width of
* cards in each column to ensure a clean layout.
*
* @param cards The list of cards to display.
* @param rows The number of rows in the board layout.
* @param cols The number of columns in the board layout.
* @param role The role of the viewer, affecting how cards are displayed.
*/
public static void displayBoard(JsonArray cards, int rows, int cols, Role role) {
int index = 0;
int[] maxWidths = new int[cols]; // Array to hold the maximum width needed for each column
List<CardToString> formattedCards = new ArrayList<>();
formattedCards = formatCards(cards, role);
// First pass: calculate the maximum width needed for each column
maxWidths = calculateMaxWidths(formattedCards, rows, cols);
index = 0; // Reset index for the second pass
// Second pass: print each row with proper spacing
for (int i = 0; i < rows; i++) {
// Print words with correct padding
for (int j = 0; j < cols; j++) {
if (index < formattedCards.size()) {
CardToString card = formattedCards.get(index++);
System.out.print(formatStringToWidth(card.word, maxWidths[j]));
}
}
System.out.println();
index -= cols; // Reset index to the start of the current row for format lines
// Print format lines with correct padding
for (int j = 0; j < cols; j++) {
if (index < formattedCards.size()) {
CardToString card = formattedCards.get(index++);
System.out.print(formatStringToWidth(card.formatLine, maxWidths[j]));
}
}
System.out.println();
System.out.println(); // Extra newline for spacing between rows
}
}
/**
* Helper method to format strings to a specified width by centering text within the width.
*
* @param input The string to format.
* @param width The width within which to center the string.
* @return The centered string.
*/
private static String formatStringToWidth(String input, int width) {
int padSize = (width - input.length()) / 2;
String paddedString = String.format("%" + (input.length() + padSize) + "s", input);
paddedString = String.format("%-" + width + "s", paddedString);
return paddedString;
}
/**
* Formats a list of cards into a list of string representations suitable for printing.
*
* @param cards The list of cards to format.
* @param role The viewer's role.
* @return A list of formatted card strings.
*/
private static List<CardToString> formatCards(JsonArray cards, Role role) {
List<CardToString> formattedCards = new ArrayList<>();
for (int i = 0; i < cards.size(); i++) {
formattedCards.add(printCard(cards.get(i).getAsJsonObject(), i + 1, role));
}
return formattedCards;
}
/**
* Calculates the maximum widths needed for each column based on the longest card string in each column,
* ensuring proper alignment when printed.
*
* @param formattedCards The formatted card strings.
* @param rows The number of rows in the board layout.
* @param cols The number of columns in the board layout.
* @return An array of maximum widths for each column.
*/
private static int[] calculateMaxWidths(List<CardToString> formattedCards, int rows, int cols) {
int index = 0;
int[] maxWidths = new int[cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols && index < formattedCards.size(); j++) {
CardToString card = formattedCards.get(index++);
int maxCardWidth = Math.max(card.word.length(), card.formatLine.length());
maxWidths[j] = Math.max(maxWidths[j], maxCardWidth + 4); // Add 4 for padding
}
}
return maxWidths;
}
}