Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Tetris/Models/Grid.cs

49 lines
1.2 KiB
C#
Raw Normal View History

2022-04-06 11:15:55 +02:00
using System.Drawing;
2022-04-06 11:15:55 +02:00
namespace Tetris.Models;
2022-04-06 11:15:55 +02:00
public class Grid
{
private readonly Color[,] _grid;
2022-04-06 11:15:55 +02:00
public Grid(Color[,] grid)
{
_grid = grid;
}
2022-04-06 11:15:55 +02:00
public Point MinGrid => new(0, 0);
public Point MaxGrid => new(_grid.GetLength(0) - 1, _grid.GetLength(1) - 1);
public bool CanGo(Tetrominoe tetrominoe, Point point)
{
var shape = tetrominoe.Shape;
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
return false;
2022-04-06 11:15:55 +02:00
if (point.X + tetrominoe.Shape.GetLength(0) > MaxGrid.X || point.Y + tetrominoe.Shape.GetLength(1) > MaxGrid.Y)
return false;
2022-04-06 11:15:55 +02:00
for (uint x = 0; x < shape.GetLength(0); x++)
2022-04-06 11:15:55 +02:00
for (uint y = 0; y < shape.GetLength(1); y++)
{
var s = point + new Size((int)x, (int)y);
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
return false;
}
return true;
}
2022-04-06 11:15:55 +02:00
public override string ToString()
{
var s = "";
for (uint y = 0; y < _grid.GetLength(1); y++)
{
for (uint x = 0; x < _grid.GetLength(0); x++)
2022-04-06 11:15:55 +02:00
s += _grid[x, y].Name + "\t";
s += "\n";
}
2022-04-06 11:15:55 +02:00
return s;
}
2022-04-06 11:15:55 +02:00
}