2022-04-06 11:40:33 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
2022-05-18 09:59:04 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Documents;
|
2022-05-04 10:15:16 +02:00
|
|
|
|
using System.Windows.Media;
|
2022-04-07 12:35:51 +02:00
|
|
|
|
using Pastel;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
using Color = System.Drawing.Color;
|
|
|
|
|
|
|
|
|
|
namespace Tetris.Models;
|
|
|
|
|
|
|
|
|
|
public class Grid {
|
|
|
|
|
private Color[,] _grid;
|
|
|
|
|
|
2022-05-18 09:59:04 +02:00
|
|
|
|
public Color[,] CGrid => _grid;
|
2022-05-04 10:15:16 +02:00
|
|
|
|
|
2022-04-06 11:40:33 +02:00
|
|
|
|
public Point MinGrid => new Point(0,0);
|
|
|
|
|
public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1);
|
|
|
|
|
|
|
|
|
|
public Grid(Color[,] grid) {
|
|
|
|
|
_grid = grid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanGo(Tetrominoe tetrominoe, Point point) {
|
|
|
|
|
bool[,] shape = tetrominoe.Shape;
|
|
|
|
|
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-04-07 13:51:49 +02:00
|
|
|
|
else if ((point.X + tetrominoe.Shape.GetLength(0)-1) > MaxGrid.X || (point.Y + tetrominoe.Shape.GetLength(1)-1) > MaxGrid.Y)
|
2022-04-06 11:40:33 +02:00
|
|
|
|
return false;
|
2022-04-07 13:51:49 +02:00
|
|
|
|
|
2022-04-06 11:40:33 +02:00
|
|
|
|
for (uint x = 0; x < shape.GetLength(0); x++)
|
|
|
|
|
for (uint y = 0; y < shape.GetLength(1); y++) {
|
|
|
|
|
Point s = point + new Size((int) x, (int) y);
|
|
|
|
|
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-05-04 10:15:16 +02:00
|
|
|
|
|
2022-05-18 09:59:04 +02:00
|
|
|
|
public bool LineFull() {
|
|
|
|
|
return Enumerable.Range(0, _grid.GetLength(0)).Select(x => _grid[x, MaxGrid.Y]).All(x => x != Color.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearLine() {
|
|
|
|
|
for (int x = 0; x <= MaxGrid.X; x++)
|
|
|
|
|
for (int y = MaxGrid.Y; y > 0; y--) {
|
|
|
|
|
_grid[x, y] = _grid[x, y - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x <= MaxGrid.X; x++)
|
|
|
|
|
_grid[x,0] = Color.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 10:15:16 +02:00
|
|
|
|
public void PrintTetrominoe(Tetrominoe t) {
|
|
|
|
|
bool[,] shape = t.Shape;
|
|
|
|
|
|
|
|
|
|
for (uint x = 0; x < shape.GetLength(0); x++)
|
2022-05-18 09:59:04 +02:00
|
|
|
|
for (uint y = 0; y < shape.GetLength(1); y++) {
|
|
|
|
|
Point s = t.Coordinates + new Size((int) x, (int) y);
|
|
|
|
|
if (shape[x, y])
|
|
|
|
|
_grid[s.X, s.Y] = t.Color;
|
|
|
|
|
}
|
2022-05-04 10:15:16 +02:00
|
|
|
|
}
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
String s = "";
|
|
|
|
|
for (uint y = 0; y < _grid.GetLength(1); y++) {
|
2022-04-07 12:35:51 +02:00
|
|
|
|
for (uint x = 0; x < _grid.GetLength(0); x++) {
|
|
|
|
|
s += "x".Pastel(_grid[x, y]);
|
|
|
|
|
}
|
2022-04-06 11:40:33 +02:00
|
|
|
|
s += "\n";
|
|
|
|
|
}
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2022-04-07 12:35:51 +02:00
|
|
|
|
|
|
|
|
|
public string ToString(Tetrominoe t) {
|
2022-05-04 10:15:16 +02:00
|
|
|
|
Grid g = new((Color[,]) _grid.Clone());
|
|
|
|
|
g.PrintTetrominoe(t);
|
|
|
|
|
return g.ToString();
|
2022-04-07 12:35:51 +02:00
|
|
|
|
}
|
2022-04-06 11:40:33 +02:00
|
|
|
|
}
|