69 lines
1.6 KiB
C#
69 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
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[,] GetShape(String name) {
|
|
if (!content.ContainsKey(name))
|
|
throw new Exception("Invalid name");
|
|
|
|
String s = content[name]["shape"].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 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();
|
|
}
|
|
|
|
|
|
private static JObject GetContent() {
|
|
return JObject.Parse(File.ReadAllText(@"Resources\tetrominoes.json"));
|
|
}
|
|
}
|