diff --git a/Models/Game.cs b/Models/Game.cs index e236529..abbb891 100644 --- a/Models/Game.cs +++ b/Models/Game.cs @@ -1,38 +1,71 @@ -using System.ComponentModel; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; namespace Tetris.Models; -public class Party : INotifyPropertyChanged -{ - private int _score; - +public class Game : INotifyPropertyChanged { + private Random _random = new Random(); + private string _userName; - public Party(string userName) - { - _userName = userName; - } - - public string UserName - { + public string UserName { get => _userName; - set - { + set { _userName = value; OnPropertyChanged("UserName"); } } - public int Score - { + private int _score; + + public int Score { get => _score; - set - { + set { _score = value; 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 tetrominoes = TetrominoeParser.List(); + return new Tetrominoe(_grid, tetrominoes[_random.Next(tetrominoes.Count)]); + } + public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) @@ -40,4 +73,4 @@ public class Party : INotifyPropertyChanged if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } -} \ No newline at end of file +} diff --git a/Models/Grid.cs b/Models/Grid.cs index 976b479..4119fce 100644 --- a/Models/Grid.cs +++ b/Models/Grid.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Windows.Media; using Pastel; using Color = System.Drawing.Color; @@ -8,7 +9,7 @@ namespace Tetris.Models; public class Grid { private Color[,] _grid; - public Color[,] CGrid => (Color[,]) _grid.Clone(); + public Color[,] CGrid => (Color[,])_grid.Clone(); public Point MinGrid => new Point(0,0); public Point MaxGrid => new Point(_grid.GetLength(0)-1, _grid.GetLength(1)-1); @@ -34,6 +35,17 @@ public class Grid { 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() { String s = ""; @@ -47,16 +59,8 @@ public class Grid { } 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(); + Grid g = new((Color[,]) _grid.Clone()); + g.PrintTetrominoe(t); + return g.ToString(); } } diff --git a/Models/Tetrominoe.cs b/Models/Tetrominoe.cs index 7cbbdc9..6356927 100644 --- a/Models/Tetrominoe.cs +++ b/Models/Tetrominoe.cs @@ -70,12 +70,13 @@ public class Tetrominoe { // ToDO: ascpect technique test } - public Tetrominoe(Grid grid, String shape, Point coordinates, short orientation, Color color) { + public Tetrominoe(Grid grid, String name) { _grid = grid; - _shape = TetrominoeParser.Get(shape); - _coordinates = coordinates; - _orientation = orientation; - _color = color; + _shape = TetrominoeParser.GetShape(name); + _coordinates = new Point(Convert.ToInt32(Math.Floor(_grid.CGrid.GetLength(0) / 2.0) - Math.Floor(_shape.GetLength(0) / 2.0)), 0); + // ToDo fixme, not rounded to left + _orientation = 0; + _color = TetrominoeParser.GetColor(name); } private bool[,] _rotateLeft(bool[,] shape) { diff --git a/Models/TetrominoeParser.cs b/Models/TetrominoeParser.cs index 80834dc..0291595 100644 --- a/Models/TetrominoeParser.cs +++ b/Models/TetrominoeParser.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.IO; using System.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)) throw new Exception("Invalid name"); - String s = content.GetValue(name).ToString(); + String s = content[name]["shape"].ToString(); int width = s.IndexOf('\n'); if (width < 0) @@ -48,6 +49,15 @@ public class TetrominoeParser { 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 List() { return content.Properties().Select(p => p.Name).ToList(); } diff --git a/Resources/tetrominoes.json b/Resources/tetrominoes.json index e5b24cd..afe81e9 100644 --- a/Resources/tetrominoes.json +++ b/Resources/tetrominoes.json @@ -1,9 +1,30 @@ { - "I": "xxxx", - "J": "x--\nxxx", - "L": "--x\nxxx", - "O": "xx\nxx", - "S": "-xx\nxx-", - "T": "-x-\nxxx", - "Z": "xx-\n-xx" -} \ No newline at end of file + "I": { + "shape": "xxxx", + "color": "#55ffff" + }, + "J": { + "shape": "x--\nxxx", + "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" + } +}