From 6473e22447e6fd6cc82a804eadf25617c6198cfd Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 7 Apr 2022 12:35:51 +0200 Subject: [PATCH 1/3] Setup printable Grid with Tetrominoe and color --- Models/Grid.cs | 20 ++++++++++++++++++-- Models/Tetrominoe.cs | 5 +++-- Tetris.csproj | 4 ++++ Tetris.sln | 10 +++++----- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Models/Grid.cs b/Models/Grid.cs index b98cb0c..d48f5f0 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using Pastel; using Color = System.Drawing.Color; namespace Tetris.Models; @@ -35,10 +36,25 @@ public class Grid { 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 += _grid[x,y].Name + "\t"; + for (uint x = 0; x < _grid.GetLength(0); x++) { + s += "x".Pastel(_grid[x, y]); + } s += "\n"; } return s; } + + 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 b793280..2e6977c 100644 --- a/Models/Tetrominoe.cs +++ b/Models/Tetrominoe.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Drawing; +using Pastel; namespace Tetris.Models; @@ -56,7 +57,7 @@ public class Tetrominoe { private Color _color; public Color Color { - get; + get => _color; } public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) { @@ -110,7 +111,7 @@ public class Tetrominoe { for (uint y = 0; y < Shape.GetLength(1); y++) { for (uint x = 0; x < Shape.GetLength(0); x++) if (Shape[x, y]) - s += "x"; + s += "x".Pastel(_color); else s += " "; s += "\n"; diff --git a/Tetris.csproj b/Tetris.csproj index d9bfab2..d01518f 100644 --- a/Tetris.csproj +++ b/Tetris.csproj @@ -7,4 +7,8 @@ net6.0-windows + + + + 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 From 3c97673d50fb912fb240c89d012f8ed9d65f9f9a Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 7 Apr 2022 13:51:49 +0200 Subject: [PATCH 2/3] Fix boundaries test error --- Models/Grid.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Models/Grid.cs b/Models/Grid.cs index d48f5f0..d4adda9 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -20,9 +20,9 @@ public class Grid { if (point.X < MinGrid.X || point.Y < MinGrid.Y) return false; - else 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++) { Point s = point + new Size((int) x, (int) y); From 4a775ee9a31da1bdf95d9b4b4cabbc8f92c50866 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 7 Apr 2022 13:52:28 +0200 Subject: [PATCH 3/3] Show empty space on Tetrominoe print --- Models/Tetrominoe.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Models/Tetrominoe.cs b/Models/Tetrominoe.cs index 2e6977c..a7b67ae 100644 --- a/Models/Tetrominoe.cs +++ b/Models/Tetrominoe.cs @@ -44,8 +44,7 @@ public class Tetrominoe { _orientation = value; Point newCoords = _coordinates; - //ToDo edit position ? - //ToDo move position when hot a wall + //ToDo: move position when hit a wall if (!_grid.CanGo(this, newCoords)) { _orientation = oldOrientation; @@ -113,7 +112,7 @@ public class Tetrominoe { if (Shape[x, y]) s += "x".Pastel(_color); else - s += " "; + s += "-".Pastel(Color.Black); s += "\n"; } return s;