From e49cfb2c784653a4902ae761c4eee7d48b7c52db Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 4 May 2022 10:15:16 +0200 Subject: [PATCH] Build Tetrominoe from parser and support print of grid with current Tetrominoe --- Models/Grid.cs | 28 +++++++++++++++++----------- Models/Tetrominoe.cs | 11 ++++++----- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Models/Grid.cs b/Models/Grid.cs index d4adda9..4119fce 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Windows.Media; using Pastel; using Color = System.Drawing.Color; @@ -8,6 +9,8 @@ namespace Tetris.Models; public class Grid { private Color[,] _grid; + public Color[,] CGrid => (Color[,])_grid.Clone(); + public Point MinGrid => new Point(0,0); public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1); @@ -32,6 +35,17 @@ public class Grid { return true; } + + 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; + } + } public override string ToString() { String s = ""; @@ -45,16 +59,8 @@ public class Grid { } 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(); + Grid g = new((Color[,]) _grid.Clone()); + g.PrintTetrominoe(t); + return g.ToString(); } } diff --git a/Models/Tetrominoe.cs b/Models/Tetrominoe.cs index 7cbbdc9..6356927 100644 --- a/Models/Tetrominoe.cs +++ b/Models/Tetrominoe.cs @@ -70,12 +70,13 @@ public class Tetrominoe { // ToDO: ascpect technique test } - public Tetrominoe(Grid grid, String shape, Point coordinates, short orientation, Color color) { + public Tetrominoe(Grid grid, String name) { _grid = grid; - _shape = TetrominoeParser.Get(shape); - _coordinates = coordinates; - _orientation = orientation; - _color = color; + _shape = TetrominoeParser.GetShape(name); + _coordinates = new Point(Convert.ToInt32(Math.Floor(_grid.CGrid.GetLength(0) / 2.0) - Math.Floor(_shape.GetLength(0) / 2.0)), 0); + // ToDo fixme, not rounded to left + _orientation = 0; + _color = TetrominoeParser.GetColor(name); } private bool[,] _rotateLeft(bool[,] shape) {