-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetrisConsoleWriter.cs
145 lines (134 loc) · 5.54 KB
/
TetrisConsoleWriter.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyPersonalTetris
{
public class TetrisConsoleWriter
{
private int tetrisRows;
private int tetrisColumns;
private int infoColumns;
private int consoleRows;
private int consoleColumns;
private char tetrisCharacter;
public TetrisConsoleWriter(int tetrisRow, int tetrisColumns, char tetrisCharacter = '*', int infoColumns = 11)
{
this.tetrisRows = tetrisRow;
this.tetrisColumns = tetrisColumns;
this.tetrisCharacter = tetrisCharacter;
this.infoColumns = infoColumns;
this.consoleRows = 1 + this.tetrisRows + 1;
this.consoleColumns = 1 + this.tetrisColumns + 1 + this.infoColumns + 1;
this.Frame = 0;
this.FramesToMoveFigure = 15;
Console.WindowHeight = this.consoleRows + 1;
Console.WindowWidth = this.consoleColumns;
Console.BufferHeight = this.consoleRows + 1;
Console.BufferWidth = this.consoleColumns;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Title = "Boxing Tetris v1.0";
Console.CursorVisible = false;
}
public int Frame { get; set; }
public int FramesToMoveFigure { get; private set; }
public void DrawAll(ITetrisGame state, ScoreManager scoreManager)
{
this.DrawBorder();
this.DrawGameState(3 + this.tetrisColumns, state, scoreManager);
this.DrawTetrisField(state.TetrisField);
this.DrawCurrentFigure(state.CurrentFigure, state.CurrentFigureRow, state.CurrentFigureCol);
}
public void DrawGameState(int startColumn, ITetrisGame state, ScoreManager scoreManager)
{
this.Write("Level:", 1, startColumn);
this.Write(state.Level.ToString(), 2, startColumn);
this.Write("Score:", 4, startColumn);
this.Write(scoreManager.Score.ToString(), 5, startColumn);
this.Write("Best:", 7, startColumn);
this.Write(scoreManager.HighScore.ToString(), 8, startColumn);
this.Write("Frame:", 10, startColumn);
this.Write(this.Frame.ToString() + "/" + (this.FramesToMoveFigure - state.Level).ToString(), 11, startColumn);
this.Write("Position:", 13, startColumn);
this.Write($"{state.CurrentFigureRow}, {state.CurrentFigureCol}", 14, startColumn);
this.Write("Keys:", 16, startColumn);
this.Write($" ^ ", 18, startColumn);
this.Write($"<v> ", 19, startColumn);
}
public void DrawBorder()
{
Console.SetCursorPosition(0, 0);
string line = "╔";
line += new string('═', this.tetrisColumns);
line += '╦';
line += new string('═', this.infoColumns);
line += '╗';
Console.Write(line);
for (int i = 0; i < this.tetrisRows; i++)
{
string middleLine = "║";
middleLine += new string(' ', this.tetrisColumns);
middleLine += "║";
middleLine += new string(' ', this.infoColumns);
middleLine += "║";
Console.Write(middleLine);
}
string endLine = "╚";
endLine += new string('═', this.tetrisColumns);
endLine += '╩';
endLine += new string('═', this.infoColumns);
endLine += '╝';
Console.Write(endLine);
}
public void WriteGameOver(int score)
{
var row = this.tetrisRows / 2 - 3;
var column = (this.tetrisColumns + 3 + this.infoColumns) / 2 - 5;
var scoreAsString = score.ToString();
scoreAsString = new string(' ', 7 - scoreAsString.Length) + scoreAsString;
Write("╔═════════╗", row, column);
Write("║ Game ║", row + 1, column);
Write("║ over! ║", row + 2, column);
Write($"║ {scoreAsString} ║", row + 3, column);
Write("╚═════════╝", row + 4, column);
}
public void DrawTetrisField(bool[,] tetrisField)
{
for (int row = 0; row < tetrisField.GetLength(0); row++)
{
string line = string.Empty;
for (int col = 0; col < tetrisField.GetLength(1); col++)
{
if (tetrisField[row, col])
{
line += this.tetrisCharacter;
}
else
{
line += " ";
}
}
this.Write(line, row + 1, 1);
}
}
public void DrawCurrentFigure(Tetromino currentFigure, int currentFigureRow, int currentFigureColumn)
{
for (int row = 0; row < currentFigure.Width; row++)
{
for (int col = 0; col < currentFigure.Height; col++)
{
if (currentFigure.Body[row, col])
{
Write(this.tetrisCharacter.ToString(), row + 1 + currentFigureRow, 1 + currentFigureColumn + col);
}
}
}
}
private void Write(string text, int row, int col)
{
Console.SetCursorPosition(col, row);
Console.Write(text);
}
}
}