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;
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<String> 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));
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Windows.Media;
using Pastel;
using Color = System.Drawing.Color;
@ -8,6 +9,8 @@ namespace Tetris.Models;
public class Grid {
private Color[,] _grid;
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);
@ -32,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 = "";
@ -45,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();
}
}

View file

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

View file

@ -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<String> List() {
return content.Properties().Select(p => p.Name).ToList();
}

View file

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