Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Tetris/Models/TetrominoeParser.cs

60 lines
1.4 KiB
C#
Raw Normal View History

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