2022-04-06 11:15:55 +02:00
|
|
|
|
using System.ComponentModel;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
namespace Tetris.Models;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public class Tetrominoe
|
|
|
|
|
{
|
|
|
|
|
private Color _color;
|
|
|
|
|
|
|
|
|
|
private Point _coordinates;
|
|
|
|
|
|
|
|
|
|
private readonly Grid _grid;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
private short _orientation;
|
|
|
|
|
|
|
|
|
|
private readonly bool[,] _shape;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool[,] Shape
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var shape = (bool[,])_shape.Clone();
|
|
|
|
|
for (var i = 0; i < _orientation; i++)
|
2022-04-06 11:40:33 +02:00
|
|
|
|
shape = _rotateLeft(shape);
|
|
|
|
|
return shape;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
|
|
|
|
public Point Coordinates
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
get => _coordinates;
|
2022-04-06 11:15:55 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!_grid.CanGo(this, value))
|
2022-04-06 11:40:33 +02:00
|
|
|
|
return;
|
|
|
|
|
_coordinates = value;
|
|
|
|
|
OnPropertyChanged("Coordinates");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
|
|
|
|
public short Orientation
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
get => _orientation;
|
2022-04-06 11:15:55 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
if (value > 3)
|
|
|
|
|
value = 0;
|
|
|
|
|
else if (value < 0)
|
|
|
|
|
value = 3;
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
var oldOrientation = _orientation;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
_orientation = value;
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
var newCoords = _coordinates;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
//ToDo edit position ?
|
|
|
|
|
//ToDo move position when hot a wall
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
|
|
|
|
if (!_grid.CanGo(this, newCoords))
|
2022-04-06 11:40:33 +02:00
|
|
|
|
_orientation = oldOrientation;
|
2022-04-06 11:15:55 +02:00
|
|
|
|
else
|
2022-04-06 11:40:33 +02:00
|
|
|
|
OnPropertyChanged("Orientation");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public Color Color { get; }
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
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];
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
|
|
|
|
return rotatedArr;
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
|
|
|
|
public void RotateLeft()
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
Orientation -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public void RotateRight()
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
Orientation += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public void GoRight()
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
Coordinates += new Size(1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public void GoLeft()
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
Coordinates -= new Size(1, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public void GoDown()
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
Coordinates += new Size(0, 1);
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
var s = "";
|
|
|
|
|
for (uint y = 0; y < Shape.GetLength(1); y++)
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
for (uint x = 0; x < Shape.GetLength(0); x++)
|
|
|
|
|
if (Shape[x, y])
|
|
|
|
|
s += "x";
|
|
|
|
|
else
|
|
|
|
|
s += " ";
|
|
|
|
|
s += "\n";
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
|
2022-04-06 11:40:33 +02:00
|
|
|
|
return s;
|
|
|
|
|
}
|
2022-04-06 11:15:55 +02:00
|
|
|
|
}
|