Archived
1
0
Fork 0

Merge branch 'master' into game-system

This commit is contained in:
Ziedelth 2022-04-07 14:34:31 +02:00
commit c5148820de
4 changed files with 121 additions and 135 deletions

View file

@ -1,49 +1,60 @@
using System.Drawing;
using System;
using System.Drawing;
using Pastel;
using Color = System.Drawing.Color;
namespace Tetris.Models;
namespace Tetris.Models;
public class Grid
{
private readonly Color[,] _grid;
public class Grid {
private 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;
}
public Point MinGrid => new(0, 0);
public Point MaxGrid => new(_grid.GetLength(0) - 1, _grid.GetLength(1) - 1);
public bool CanGo(Tetrominoe tetrominoe, Point point)
{
var shape = tetrominoe.Shape;
public bool CanGo(Tetrominoe tetrominoe, Point point) {
bool[,] shape = tetrominoe.Shape;
if (point.X < MinGrid.X || point.Y < MinGrid.Y)
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;
for (uint x = 0; x < shape.GetLength(0); x++)
for (uint y = 0; y < shape.GetLength(1); y++)
{
var s = point + new Size((int)x, (int)y);
if (shape[x, y] && _grid[s.X, s.Y] != Color.Empty)
return false;
}
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()
{
var 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";
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 += "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();
}
}

View file

@ -1,22 +1,65 @@
using System.ComponentModel;
using System;
using System.ComponentModel;
using System.Drawing;
using Pastel;
namespace Tetris.Models;
namespace Tetris.Models;
public class Tetrominoe
{
private Color _color;
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;
private readonly Grid _grid;
short oldOrientation = _orientation;
_orientation = value;
private short _orientation;
Point newCoords = _coordinates;
//ToDo: move position when hit a wall
if (!_grid.CanGo(this, newCoords)) {
_orientation = oldOrientation;
} else {
OnPropertyChanged("Orientation");
}
}
}
private readonly bool[,] _shape;
private Color _color;
public Color Color {
get => _color;
}
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color)
{
public Tetrominoe(Grid grid, bool[,] shape, Point coordinates, short orientation, Color color) {
_grid = grid;
_shape = shape;
_coordinates = coordinates;
@ -27,112 +70,51 @@ public class Tetrominoe
// ToDO: ascpect technique test
}
public bool[,] Shape
{
get
{
var shape = (bool[,])_shape.Clone();
for (var i = 0; i < _orientation; i++)
shape = _rotateLeft(shape);
return shape;
}
}
public Point Coordinates
{
get => _coordinates;
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];
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<shape.GetLength(1);++j)
rotatedArr[j,(shape.GetLength(0)-1)-i] = shape[i,j];
return rotatedArr;
}
public void RotateLeft()
{
public void RotateLeft() {
Orientation -= 1;
}
public void RotateRight()
{
public void RotateRight() {
Orientation += 1;
}
public void GoRight()
{
public void GoRight() {
Coordinates += new Size(1, 0);
}
public void GoLeft()
{
public void GoLeft() {
Coordinates -= new Size(1, 0);
}
public void GoDown()
{
public void GoDown() {
Coordinates += new Size(0, 1);
}
protected virtual void OnPropertyChanged(string propertyName)
{
protected virtual void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public override string ToString()
{
var s = "";
for (uint y = 0; y < Shape.GetLength(1); y++)
{
public override string ToString() {
String s = "";
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 += "-".Pastel(Color.Black);
s += "\n";
}
return s;
}
}
}

View file

@ -5,17 +5,10 @@
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<TargetFramework>net6.0-windows</TargetFramework>
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\tetris.png" />
<Resource Include="Resources\tetris.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Pastel" Version="3.0.0" />
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
</ItemGroup>

View file

@ -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