Archived
1
0
Fork 0

Add grid line full, clear line and game hit top functions

This commit is contained in:
Ethanell 2022-05-18 09:59:04 +02:00
parent ba5b820159
commit 917806d0bd
2 changed files with 39 additions and 6 deletions

View file

@ -52,6 +52,23 @@ public class Game : INotifyPropertyChanged {
return !_grid.CanGo(_currentTetrominoe, _currentTetrominoe.Coordinates + new Size(0, 1));
}
public bool HitTop() {
if (_currentTetrominoe == null)
return false;
else if (_grid.CanGo(_currentTetrominoe, _currentTetrominoe.Coordinates + new Size(0, 1)))
return false;
return _grid.MinGrid.Y == _currentTetrominoe.Coordinates.Y;
}
public bool LineFull() {
return _grid.LineFull();
}
public void ClearLine() {
_grid.ClearLine();
}
public void PrintTetrominoe() {
if (_currentTetrominoe == null)
return;

View file

@ -1,5 +1,7 @@
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Documents;
using System.Windows.Media;
using Pastel;
using Color = System.Drawing.Color;
@ -9,7 +11,7 @@ namespace Tetris.Models;
public class Grid {
private Color[,] _grid;
public Color[,] CGrid => (Color[,])_grid.Clone();
public Color[,] CGrid => _grid;
public Point MinGrid => new Point(0,0);
public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1);
@ -36,15 +38,29 @@ public class Grid {
return true;
}
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;
}
public void PrintTetrominoe(Tetrominoe t) {
bool[,] shape = t.Shape;
for (uint x = 0; x < shape.GetLength(0); x++)
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;
}
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;
}
}
public override string ToString() {