From 0b5114290e1d27969972d5e09e41f2fd640d35bd Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 6 Apr 2022 11:40:33 +0200 Subject: [PATCH] Add Tetrominoe with shape rotation and movement support Add Grid with basic collision verification --- Models/Game.cs | 37 +++++++++++++ Models/Grid.cs | 44 ++++++++++++++++ Models/Tetrominoe.cs | 120 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 Models/Game.cs create mode 100644 Models/Grid.cs create mode 100644 Models/Tetrominoe.cs diff --git a/Models/Game.cs b/Models/Game.cs new file mode 100644 index 0000000..a72d73c --- /dev/null +++ b/Models/Game.cs @@ -0,0 +1,37 @@ +using System; +using System.ComponentModel; + +namespace Tetris.Models; + +public class Party : INotifyPropertyChanged { + public event PropertyChangedEventHandler PropertyChanged; + + private String _userName; + + public String UserName { + get => _userName; + set { + _userName = value; + OnPropertyChanged("UserName"); + } + } + + private int _score = 0; + + public int Score { + get => _score; + set { + _score = value; + OnPropertyChanged("Score"); + } + } + + public Party(String userName) { + _userName = userName; + } + + protected virtual void OnPropertyChanged(string propertyName) { + if (PropertyChanged != null) + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } +} diff --git a/Models/Grid.cs b/Models/Grid.cs new file mode 100644 index 0000000..b98cb0c --- /dev/null +++ b/Models/Grid.cs @@ -0,0 +1,44 @@ +using System; +using System.Drawing; +using Color = System.Drawing.Color; + +namespace Tetris.Models; + +public class Grid { + private 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 bool CanGo(Tetrominoe tetrominoe, Point point) { + bool[,] shape = tetrominoe.Shape; + 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) + 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); + if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty) + return false; + } + + return true; + } + + 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"; + s += "\n"; + } + return s; + } +} diff --git a/Models/Tetrominoe.cs b/Models/Tetrominoe.cs new file mode 100644 index 0000000..b793280 --- /dev/null +++ b/Models/Tetrominoe.cs @@ -0,0 +1,120 @@ +using System; +using System.ComponentModel; +using System.Drawing; + +namespace Tetris.Models; + +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; + + short oldOrientation = _orientation; + _orientation = value; + + Point newCoords = _coordinates; + //ToDo edit position ? + //ToDo move position when hot a wall + + if (!_grid.CanGo(this, newCoords)) { + _orientation = oldOrientation; + } else { + OnPropertyChanged("Orientation"); + } + } + } + + private Color _color; + public Color Color { + get; + } + + public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) { + _grid = grid; + _shape = shape; + _coordinates = coordinates; + _orientation = orientation; + _color = color; + // ToDo: UML: Sequand, classe, déploiement + // ToDo: pb techniques + // ToDO: ascpect technique test + } + + 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;j