diff --git a/Models/Grid.cs b/Models/Grid.cs index 1e1504f..d4adda9 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -1,49 +1,60 @@ -using System.Drawing; +using System; +using System.Drawing; +using Pastel; +using Color = System.Drawing.Color; -namespace Tetris.Models; +namespace Tetris.Models; -public class Grid -{ - private readonly Color[,] _grid; +public class Grid { + private Color[,] _grid; - public Grid(Color[,] grid) - { + 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 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; + public bool CanGo(Tetrominoe tetrominoe, Point point) { + bool[,] shape = tetrominoe.Shape; if (point.X < MinGrid.X || point.Y < MinGrid.Y) return false; - - if (point.X + tetrominoe.Shape.GetLength(0) > MaxGrid.X || point.Y + tetrominoe.Shape.GetLength(1) > MaxGrid.Y) + + else if ((point.X + tetrominoe.Shape.GetLength(0)-1) > MaxGrid.X || (point.Y + tetrominoe.Shape.GetLength(1)-1) > MaxGrid.Y) return false; for (uint x = 0; x < shape.GetLength(0); x++) - 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; - } - + 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; } - public override string ToString() - { - var s = ""; - for (uint y = 0; y < _grid.GetLength(1); y++) - { - for (uint x = 0; x < _grid.GetLength(0); x++) - s += _grid[x, y].Name + "\t"; + public override string ToString() { + String s = ""; + for (uint y = 0; y < _grid.GetLength(1); y++) { + for (uint x = 0; x < _grid.GetLength(0); x++) { + s += "x".Pastel(_grid[x, y]); + } s += "\n"; } - return s; } -} \ No newline at end of file + + public string ToString(Tetrominoe t) { + Color[,] grid = (Color[,]) _grid.Clone(); + Boolean[,] 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; + } + + return new Grid(grid).ToString(); + } +} diff --git a/Models/Tetrominoe.cs b/Models/Tetrominoe.cs index 5d6227b..a7b67ae 100644 --- a/Models/Tetrominoe.cs +++ b/Models/Tetrominoe.cs @@ -1,22 +1,65 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.Drawing; +using Pastel; -namespace Tetris.Models; +namespace Tetris.Models; -public class Tetrominoe -{ - private Color _color; +public class Tetrominoe { + public event PropertyChangedEventHandler PropertyChanged; + private Grid _grid; + + private bool[,] _shape; + public bool[,] Shape { + get { + bool[,] shape = (bool[,]) _shape.Clone(); + for (int i = 0; i < _orientation; i++) + shape = _rotateLeft(shape); + return shape; + } + } + private Point _coordinates; + public Point Coordinates { + get => _coordinates; + set { + if (! _grid.CanGo(this, value)) + return; + _coordinates = value; + OnPropertyChanged("Coordinates"); + } + } + + private short _orientation = 0; + public short Orientation { + get => _orientation; + set { + if (value > 3) + value = 0; + else if (value < 0) + value = 3; - private readonly Grid _grid; + short oldOrientation = _orientation; + _orientation = value; - private short _orientation; + Point newCoords = _coordinates; + //ToDo: move position when hit a wall + + if (!_grid.CanGo(this, newCoords)) { + _orientation = oldOrientation; + } else { + OnPropertyChanged("Orientation"); + } + } + } - private readonly bool[,] _shape; + private Color _color; + public Color Color { + get => _color; + } - public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) - { + public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) { _grid = grid; _shape = shape; _coordinates = coordinates; @@ -27,112 +70,51 @@ public class Tetrominoe // ToDO: ascpect technique test } - public bool[,] Shape - { - get - { - var shape = (bool[,])_shape.Clone(); - for (var i = 0; i < _orientation; i++) - shape = _rotateLeft(shape); - return shape; - } - } - - public Point Coordinates - { - get => _coordinates; - set - { - if (!_grid.CanGo(this, value)) - return; - _coordinates = value; - OnPropertyChanged("Coordinates"); - } - } - - public short Orientation - { - get => _orientation; - set - { - if (value > 3) - value = 0; - else if (value < 0) - value = 3; - - var oldOrientation = _orientation; - _orientation = value; - - var newCoords = _coordinates; - //ToDo edit position ? - //ToDo move position when hot a wall - - if (!_grid.CanGo(this, newCoords)) - _orientation = oldOrientation; - else - OnPropertyChanged("Orientation"); - } - } - - public Color Color { get; } - - public event PropertyChangedEventHandler PropertyChanged; - - private bool[,] _rotateLeft(bool[,] shape) - { - var rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)]; - - for (var i = shape.GetLength(0) - 1; i >= 0; --i) - for (var j = 0; j < shape.GetLength(1); ++j) - rotatedArr[j, shape.GetLength(0) - 1 - i] = shape[i, j]; + private bool[,] _rotateLeft(bool[,] shape) { + bool[,] rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)]; + + for (int i=shape.GetLength(0)-1;i>=0;--i) + for (int j=0;jenable true net6.0-windows - Resources\icon.ico - - - Always - - - - + diff --git a/Tetris.sln b/Tetris.sln index 58649b9..47ba5a3 100644 --- a/Tetris.sln +++ b/Tetris.sln @@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tetris", "Tetris.csproj", "{1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestTetris", "..\TestTetris\TestTetris\TestTetris.csproj", "{9F341BB7-7176-4DD4-8CE3-528C31F616F4}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestTetris", "..\TestTetris\TestTetris.csproj", "{2924F978-4A50-4B23-BCEC-6AB822F236AE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -14,9 +14,9 @@ Global {1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}.Debug|Any CPU.Build.0 = Debug|Any CPU {1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}.Release|Any CPU.ActiveCfg = Release|Any CPU {1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}.Release|Any CPU.Build.0 = Release|Any CPU - {9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Release|Any CPU.Build.0 = Release|Any CPU + {2924F978-4A50-4B23-BCEC-6AB822F236AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2924F978-4A50-4B23-BCEC-6AB822F236AE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2924F978-4A50-4B23-BCEC-6AB822F236AE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2924F978-4A50-4B23-BCEC-6AB822F236AE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal