Merge branch 'master' into game-system
This commit is contained in:
commit
c5148820de
4 changed files with 121 additions and 135 deletions
|
@ -1,32 +1,31 @@
|
||||||
using System.Drawing;
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
|
using Pastel;
|
||||||
|
using Color = System.Drawing.Color;
|
||||||
|
|
||||||
namespace Tetris.Models;
|
namespace Tetris.Models;
|
||||||
|
|
||||||
public class Grid
|
public class Grid {
|
||||||
{
|
private Color[,] _grid;
|
||||||
private readonly 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;
|
_grid = grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point MinGrid => new(0, 0);
|
public bool CanGo(Tetrominoe tetrominoe, Point point) {
|
||||||
public Point MaxGrid => new(_grid.GetLength(0) - 1, _grid.GetLength(1) - 1);
|
bool[,] shape = tetrominoe.Shape;
|
||||||
|
|
||||||
public bool CanGo(Tetrominoe tetrominoe, Point point)
|
|
||||||
{
|
|
||||||
var shape = tetrominoe.Shape;
|
|
||||||
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
|
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
|
||||||
return false;
|
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;
|
return false;
|
||||||
|
|
||||||
for (uint x = 0; x < shape.GetLength(0); x++)
|
for (uint x = 0; x < shape.GetLength(0); x++)
|
||||||
for (uint y = 0; y < shape.GetLength(1); y++)
|
for (uint y = 0; y < shape.GetLength(1); y++) {
|
||||||
{
|
Point s = point + new Size((int) x, (int) y);
|
||||||
var s = point + new Size((int)x, (int)y);
|
|
||||||
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
|
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -34,16 +33,28 @@ public class Grid
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString() {
|
||||||
{
|
String s = "";
|
||||||
var s = "";
|
for (uint y = 0; y < _grid.GetLength(1); y++) {
|
||||||
for (uint y = 0; y < _grid.GetLength(1); y++)
|
for (uint x = 0; x < _grid.GetLength(0); x++) {
|
||||||
{
|
s += "x".Pastel(_grid[x, y]);
|
||||||
for (uint x = 0; x < _grid.GetLength(0); x++)
|
}
|
||||||
s += _grid[x, y].Name + "\t";
|
|
||||||
s += "\n";
|
s += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,22 +1,65 @@
|
||||||
using System.ComponentModel;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using Pastel;
|
||||||
|
|
||||||
namespace Tetris.Models;
|
namespace Tetris.Models;
|
||||||
|
|
||||||
public class Tetrominoe
|
public class Tetrominoe {
|
||||||
{
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
private Color _color;
|
|
||||||
|
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;
|
private Point _coordinates;
|
||||||
|
public Point Coordinates {
|
||||||
|
get => _coordinates;
|
||||||
|
set {
|
||||||
|
if (! _grid.CanGo(this, value))
|
||||||
|
return;
|
||||||
|
_coordinates = value;
|
||||||
|
OnPropertyChanged("Coordinates");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private readonly Grid _grid;
|
private short _orientation = 0;
|
||||||
|
public short Orientation {
|
||||||
|
get => _orientation;
|
||||||
|
set {
|
||||||
|
if (value > 3)
|
||||||
|
value = 0;
|
||||||
|
else if (value < 0)
|
||||||
|
value = 3;
|
||||||
|
|
||||||
private short _orientation;
|
short oldOrientation = _orientation;
|
||||||
|
_orientation = value;
|
||||||
|
|
||||||
private readonly bool[,] _shape;
|
Point newCoords = _coordinates;
|
||||||
|
//ToDo: move position when hit a wall
|
||||||
|
|
||||||
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color)
|
if (!_grid.CanGo(this, newCoords)) {
|
||||||
{
|
_orientation = oldOrientation;
|
||||||
|
} else {
|
||||||
|
OnPropertyChanged("Orientation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Color _color;
|
||||||
|
public Color Color {
|
||||||
|
get => _color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) {
|
||||||
_grid = grid;
|
_grid = grid;
|
||||||
_shape = shape;
|
_shape = shape;
|
||||||
_coordinates = coordinates;
|
_coordinates = coordinates;
|
||||||
|
@ -27,112 +70,51 @@ public class Tetrominoe
|
||||||
// ToDO: ascpect technique test
|
// ToDO: ascpect technique test
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool[,] Shape
|
private bool[,] _rotateLeft(bool[,] shape) {
|
||||||
{
|
bool[,] rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
||||||
get
|
|
||||||
{
|
|
||||||
var shape = (bool[,])_shape.Clone();
|
|
||||||
for (var i = 0; i < _orientation; i++)
|
|
||||||
shape = _rotateLeft(shape);
|
|
||||||
return shape;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Point Coordinates
|
for (int i=shape.GetLength(0)-1;i>=0;--i)
|
||||||
{
|
for (int j=0;j<shape.GetLength(1);++j)
|
||||||
get => _coordinates;
|
rotatedArr[j,(shape.GetLength(0)-1)-i] = shape[i,j];
|
||||||
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];
|
|
||||||
|
|
||||||
return rotatedArr;
|
return rotatedArr;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RotateLeft()
|
public void RotateLeft() {
|
||||||
{
|
|
||||||
Orientation -= 1;
|
Orientation -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RotateRight()
|
public void RotateRight() {
|
||||||
{
|
|
||||||
Orientation += 1;
|
Orientation += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoRight()
|
public void GoRight() {
|
||||||
{
|
|
||||||
Coordinates += new Size(1, 0);
|
Coordinates += new Size(1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoLeft()
|
public void GoLeft() {
|
||||||
{
|
|
||||||
Coordinates -= new Size(1, 0);
|
Coordinates -= new Size(1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GoDown()
|
public void GoDown() {
|
||||||
{
|
|
||||||
Coordinates += new Size(0, 1);
|
Coordinates += new Size(0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void OnPropertyChanged(string propertyName)
|
protected virtual void OnPropertyChanged(string propertyName) {
|
||||||
{
|
|
||||||
if (PropertyChanged != null)
|
if (PropertyChanged != null)
|
||||||
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString() {
|
||||||
{
|
String s = "";
|
||||||
var s = "";
|
for (uint y = 0; y < Shape.GetLength(1); y++) {
|
||||||
for (uint y = 0; y < Shape.GetLength(1); y++)
|
|
||||||
{
|
|
||||||
for (uint x = 0; x < Shape.GetLength(0); x++)
|
for (uint x = 0; x < Shape.GetLength(0); x++)
|
||||||
if (Shape[x, y])
|
if (Shape[x, y])
|
||||||
s += "x";
|
s += "x".Pastel(_color);
|
||||||
else
|
else
|
||||||
s += " ";
|
s += "-".Pastel(Color.Black);
|
||||||
s += "\n";
|
s += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,17 +5,10 @@
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<UseWPF>true</UseWPF>
|
<UseWPF>true</UseWPF>
|
||||||
<TargetFramework>net6.0-windows</TargetFramework>
|
<TargetFramework>net6.0-windows</TargetFramework>
|
||||||
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Resources\tetris.png" />
|
<PackageReference Include="Pastel" Version="3.0.0" />
|
||||||
<Resource Include="Resources\tetris.png">
|
|
||||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
|
||||||
</Resource>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
|
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
10
Tetris.sln
10
Tetris.sln
|
@ -2,7 +2,7 @@
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tetris", "Tetris.csproj", "{1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tetris", "Tetris.csproj", "{1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}"
|
||||||
EndProject
|
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
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
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}.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.ActiveCfg = Release|Any CPU
|
||||||
{1F3B4D12-6E23-49A6-BC9C-5E274CA63B57}.Release|Any CPU.Build.0 = 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
|
{2924F978-4A50-4B23-BCEC-6AB822F236AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{2924F978-4A50-4B23-BCEC-6AB822F236AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{9F341BB7-7176-4DD4-8CE3-528C31F616F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{2924F978-4A50-4B23-BCEC-6AB822F236AE}.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}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
Reference in a new issue