Add Tetrominoe parser from json resource
This commit is contained in:
parent
b4a3a41566
commit
00dd266cf6
4 changed files with 84 additions and 0 deletions
|
@ -70,6 +70,14 @@ public class Tetrominoe {
|
||||||
// ToDO: ascpect technique test
|
// ToDO: ascpect technique test
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tetrominoe(Grid grid, String shape, Point coordinates, short orientation, Color color) {
|
||||||
|
_grid = grid;
|
||||||
|
_shape = TetrominoeParser.Get(shape);
|
||||||
|
_coordinates = coordinates;
|
||||||
|
_orientation = orientation;
|
||||||
|
_color = color;
|
||||||
|
}
|
||||||
|
|
||||||
private bool[,] _rotateLeft(bool[,] shape) {
|
private bool[,] _rotateLeft(bool[,] shape) {
|
||||||
bool[,] rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
bool[,] rotatedArr = new bool[shape.GetLength(1), shape.GetLength(0)];
|
||||||
|
|
||||||
|
|
59
Models/TetrominoeParser.cs
Normal file
59
Models/TetrominoeParser.cs
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Tetris.Models;
|
||||||
|
|
||||||
|
public class TetrominoeParser {
|
||||||
|
private static JObject _content;
|
||||||
|
|
||||||
|
private static JObject content {
|
||||||
|
get {
|
||||||
|
if (_content == null)
|
||||||
|
_content = GetContent();
|
||||||
|
|
||||||
|
return _content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool[,] Get(String name) {
|
||||||
|
if (!content.ContainsKey(name))
|
||||||
|
throw new Exception("Invalid name");
|
||||||
|
|
||||||
|
String s = content.GetValue(name).ToString();
|
||||||
|
|
||||||
|
int width = s.IndexOf('\n');
|
||||||
|
if (width < 0)
|
||||||
|
width = s.Length;
|
||||||
|
int height = s.Count(c => c == '\n')+1;
|
||||||
|
|
||||||
|
bool[,] g = new bool[width, height];
|
||||||
|
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
for (int i = 0; i < s.Length; i++, x++) {
|
||||||
|
char c = Char.ToLower(s[i]);
|
||||||
|
if (c == 'x')
|
||||||
|
g[x, y] = true;
|
||||||
|
else if (c == '-')
|
||||||
|
g[x, y] = false;
|
||||||
|
else if (c == '\n') {
|
||||||
|
x = -1;
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return g;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> List() {
|
||||||
|
return content.Properties().Select(p => p.Name).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static JObject GetContent() {
|
||||||
|
return JObject.Parse(File.ReadAllText(@"Resources\tetrominoes.json"));
|
||||||
|
}
|
||||||
|
}
|
9
Resources/tetrominoes.json
Normal file
9
Resources/tetrominoes.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"I": "xxxx",
|
||||||
|
"J": "x--\nxxx",
|
||||||
|
"L": "--x\nxxx",
|
||||||
|
"O": "xx\nxx",
|
||||||
|
"S": "-xx\nxx-",
|
||||||
|
"T": "-x-\nxxx",
|
||||||
|
"Z": "xx-\n-xx"
|
||||||
|
}
|
|
@ -8,8 +8,16 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||||
<PackageReference Include="Pastel" Version="3.0.0" />
|
<PackageReference Include="Pastel" Version="3.0.0" />
|
||||||
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
|
<PackageReference Include="WriteableBitmapEx" Version="1.6.8" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="Resources\tetrominoes.json" />
|
||||||
|
<EmbeddedResource Include="Resources\tetrominoes.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</EmbeddedResource>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Reference in a new issue