Archived
1
0
Fork 0

Merge branch 'core-game' into 'master'

Core game

See merge request tetris-dotnet/tetris!3
This commit is contained in:
Ethanell 2022-05-04 08:20:26 +00:00
commit ba5b820159
5 changed files with 116 additions and 45 deletions

View file

@ -1,38 +1,71 @@
using System.ComponentModel; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
namespace Tetris.Models; namespace Tetris.Models;
public class Party : INotifyPropertyChanged public class Game : INotifyPropertyChanged {
{ private Random _random = new Random();
private int _score;
private string _userName; private string _userName;
public Party(string userName) public string UserName {
{
_userName = userName;
}
public string UserName
{
get => _userName; get => _userName;
set set {
{
_userName = value; _userName = value;
OnPropertyChanged("UserName"); OnPropertyChanged("UserName");
} }
} }
public int Score private int _score;
{
public int Score {
get => _score; get => _score;
set set {
{
_score = value; _score = value;
OnPropertyChanged("Score"); OnPropertyChanged("Score");
} }
} }
private Grid _grid;
public Grid Grid => _grid;
private Tetrominoe? _currentTetrominoe;
public Tetrominoe? CurrentTetrominoe => _currentTetrominoe;
private Tetrominoe? _nextTetrominoe;
public Tetrominoe? NextTetrominoe => _nextTetrominoe;
public Game(string userName, Grid grid) {
_userName = userName;
_grid = grid;
_currentTetrominoe = GetNewTetrominoe();
_nextTetrominoe = GetNewTetrominoe();
}
public bool HitBottom() {
if (_currentTetrominoe == null)
return false;
return !_grid.CanGo(_currentTetrominoe, _currentTetrominoe.Coordinates + new Size(0, 1));
}
public void PrintTetrominoe() {
if (_currentTetrominoe == null)
return;
_grid.PrintTetrominoe(_currentTetrominoe);
_currentTetrominoe = _nextTetrominoe;
_nextTetrominoe = GetNewTetrominoe();
}
public Tetrominoe GetNewTetrominoe() {
List<String> tetrominoes = TetrominoeParser.List();
return new Tetrominoe(_grid, tetrominoes[_random.Next(tetrominoes.Count)]);
}
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) protected virtual void OnPropertyChanged(string propertyName)
@ -40,4 +73,4 @@ public class Party : INotifyPropertyChanged
if (PropertyChanged != null) if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} }
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Windows.Media;
using Pastel; using Pastel;
using Color = System.Drawing.Color; using Color = System.Drawing.Color;
@ -8,6 +9,8 @@ namespace Tetris.Models;
public class Grid { public class Grid {
private Color[,] _grid; private Color[,] _grid;
public Color[,] CGrid => (Color[,])_grid.Clone();
public Point MinGrid => new Point(0,0); public Point MinGrid => new Point(0,0);
public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1); public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1);
@ -32,6 +35,17 @@ public class Grid {
return true; 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() { public override string ToString() {
String s = ""; String s = "";
@ -45,16 +59,8 @@ public class Grid {
} }
public string ToString(Tetrominoe t) { public string ToString(Tetrominoe t) {
Color[,] grid = (Color[,]) _grid.Clone(); Grid g = new((Color[,]) _grid.Clone());
Boolean[,] shape = t.Shape; g.PrintTetrominoe(t);
return g.ToString();
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

@ -70,12 +70,13 @@ public class Tetrominoe {
// ToDO: ascpect technique test // ToDO: ascpect technique test
} }
public Tetrominoe(Grid grid, String shape, Point coordinates, short orientation, Color color) { public Tetrominoe(Grid grid, String name) {
_grid = grid; _grid = grid;
_shape = TetrominoeParser.Get(shape); _shape = TetrominoeParser.GetShape(name);
_coordinates = coordinates; _coordinates = new Point(Convert.ToInt32(Math.Floor(_grid.CGrid.GetLength(0) / 2.0) - Math.Floor(_shape.GetLength(0) / 2.0)), 0);
_orientation = orientation; // ToDo fixme, not rounded to left
_color = color; _orientation = 0;
_color = TetrominoeParser.GetColor(name);
} }
private bool[,] _rotateLeft(bool[,] shape) { private bool[,] _rotateLeft(bool[,] shape) {

View file

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -18,11 +19,11 @@ public class TetrominoeParser {
} }
} }
public static bool[,] Get(String name) { public static bool[,] GetShape(String name) {
if (!content.ContainsKey(name)) if (!content.ContainsKey(name))
throw new Exception("Invalid name"); throw new Exception("Invalid name");
String s = content.GetValue(name).ToString(); String s = content[name]["shape"].ToString();
int width = s.IndexOf('\n'); int width = s.IndexOf('\n');
if (width < 0) if (width < 0)
@ -48,6 +49,15 @@ public class TetrominoeParser {
return g; return g;
} }
public static Color GetColor(String name) {
if (!content.ContainsKey(name))
throw new Exception("Invalid name");
String s = content[name]["color"].ToString();
return ColorTranslator.FromHtml(s);
}
public static List<String> List() { public static List<String> List() {
return content.Properties().Select(p => p.Name).ToList(); return content.Properties().Select(p => p.Name).ToList();
} }

View file

@ -1,9 +1,30 @@
{ {
"I": "xxxx", "I": {
"J": "x--\nxxx", "shape": "xxxx",
"L": "--x\nxxx", "color": "#55ffff"
"O": "xx\nxx", },
"S": "-xx\nxx-", "J": {
"T": "-x-\nxxx", "shape": "x--\nxxx",
"Z": "xx-\n-xx" "color": "#0000ff"
} },
"L": {
"shape": "--x\nxxx",
"color": "#ff5500"
},
"O": {
"shape": "xx\nxx",
"color": "#ffff00"
},
"S": {
"shape": "-xx\nxx-",
"color": "#00ff00"
},
"T": {
"shape": "-x-\nxxx",
"color": "#ff00ff"
},
"Z": {
"shape": "xx-\n-xx",
"color": "#aa0000"
}
}