Archived
1
0
Fork 0

Merge remote-tracking branch 'origin/master' into game-system

# Conflicts:
#	Models/Grid.cs
This commit is contained in:
Ziedelth 2022-05-04 10:42:01 +02:00
commit 010a5e7349
5 changed files with 115 additions and 46 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)

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;
@ -35,6 +36,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 = "";
for (uint y = 0; y < _grid.GetLength(1); y++) { for (uint y = 0; y < _grid.GetLength(1); y++) {
@ -47,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"
}
} }