-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleRenderer.cs
49 lines (43 loc) · 1.44 KB
/
ConsoleRenderer.cs
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
using System;
using System.Text;
namespace GameOf2048
{
public class ConsoleRenderer : IGameRenderer
{
public void DrawBoard(int[][] board, int score)
{
Console.Clear();
StringBuilder output = new StringBuilder();
int cellWidth = 8;
int numOfCells = board.Length;
output.Append(CreateLine(cellWidth, numOfCells));
// draw numbers
for (int i = 0; i < board.Length; i++)
{
for (int j = 0; j < board[i].Length; j++)
{
output.Append("|");
string number = board[i][j] > 0 ? board[i][j].ToString() : " ";
output.Append(number.PadLeft(cellWidth - 2));
output.Append(" ");
}
output.Append("|\n");
output.Append(CreateLine(cellWidth, numOfCells));
}
output.Append("\n");
Console.Write(output);
Console.WriteLine($"Score: {score}");
}
private string CreateLine(int cellWidth, int numOfCells)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < numOfCells; i++)
{
string separator = "+";
output.Append(separator.PadRight(cellWidth, '-'));
}
output.Append("+\n");
return output.ToString();
}
}
}