r/programminghelp • u/Unfunny-dot-exe • Nov 09 '25
Java counsole print function is printing on rows twice java
this code takes in a 2d array and sets the highlight and text color of the bottom half char to the colors corrasponding to the int then prints but when it dose every other (odd) line is printed twice
package util;
import java.util.ArrayList;
public class Draw {
private static ArrayList<Color> colorList = new ArrayList<>();
private static StringBuilder preview;
private static StringBuilder rend;
public static void init(Color... colors) {
colorList.clear();
for (Color c : colors) {
colorList.add(c);
}
int maxDigits = String.valueOf(colorList.size() - 1).length();
preview = new StringBuilder();
rend = new StringBuilder();
}
public static void render(int[][] ren) {
rend.setLength(0);
if (ren.length % 2 == 0) {
for (int i = 0; i < ren.length; i += 2) {
for (int j = 0; j < ren[0].length; j++) {
String bg = colorList.get(ren[i][j]).getBackground();
String fg = colorList.get(ren[i + 1][j]).getText();
rend.append(bg).append(fg).append("▄").append("\u001B[0m");
}
rend.append("\n");
}
}
else {
for (int j = 0; j < ren[0].length; j++) {
String fg = colorList.get(ren[0][j]).getText();
rend.append(fg).append("▄").append("\u001B[0m");
}
rend.append("\n");
for (int i = 1; i < ren.length; i += 2) {
for (int j = 0; j < ren[0].length; j++) {
String bg = colorList.get(ren[i][j]).getBackground();
String fg = colorList.get(ren[i + 1][j]).getText();
rend.append(bg).append(fg).append("▄").append("\u001B[0m");
}
rend.append("\n");
}
}
System.out.println(preview);
System.out.println(rend);
}
public static ArrayList<Color> getColors() {
return colorList;
}
}
/*
╔═════════╤═══╤════════╗
║ black ┃ 0 ┃ floor ║
╟─────────╀───╀────────╢
║ red ┃ 1 ┃ enemy ║
╟─────────╀───╀────────╢
║ green ┃ 2 ┃ player ║
╟─────────╀───╀────────╢
║ yellow ┃ 3 ┃ chest ║
╟─────────╀───╀────────╢
║ blue ┃ 4 ┃ water ║
╟─────────╀───╀────────╢
║ magenta ┃ 5 ┃ trap ║
╟─────────╀───╀────────╢
║ cyan ┃ 6 ┃ door ║
╟─────────╀───╀────────╢
║ white ┃ 7 ┃ wall ║
╚═════════╧═══╧════════╝
*/
/*
public static void render(int[][] ren){
String rend = "";
if(ren.length % 2 == 0)
{
for(int i = 0; i < ren.length; i+=2){
for(int j = 0; j < ren[0].length; j++){
rend += "\u001B[4"+ren[i][j]+"m"+"\u001B[3"+ren[i+1][j]+"m"+"▄"+"\u001B[0m";}
rend += "\n";
}
}
else
{
for(int j = 0; j < ren[0].length; j++){
rend += "\u001B[3"+ren[0][j]+"m"+"▄"+"\u001B[0m";}
rend += "\n";
for(int i = 1; i < ren.length; i+=2){
for(int j = 0; j < ren[0].length; j++){
rend += "\u001B[4"+ren[i][j]+"m"+"\u001B[3"+ren[i+1][j]+"m"+"▄"+"\u001B[0m";}
rend += "\n";
}
}
//
System.out.println("\u001b[40m0\u001b[41m1\u001b[42m2\u001b[43m3\u001b[44m4\u001b[45m5\u001b[46m6\u001b[47m7\u001B[0m");
System.out.println(rend);
}
*/
3
Upvotes
1
u/omgpassthebacon Nov 14 '25
I would suggest a few things:
main()anywhere, so I have no idea how you intend for this to be used. If your main is in some other class, you should include that so we can see how you are calling the various functions in this class.\nseveral times. Perhaps this is not what you expected?Seriously; write some tests for these functions. If you are going to write Java code, learn how to write tests. JUnit is your friend. I'm not trying to be mean; all employed developers learn how to write tests and do it religiously. It's the best way to write good, working software.