2022-05-04 10:19:10 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
|
|
|
|
namespace Tetris.Models;
|
|
|
|
|
|
2022-05-04 10:19:10 +02:00
|
|
|
|
public class Game : INotifyPropertyChanged {
|
|
|
|
|
private Random _random = new Random();
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
private string _userName;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-05-04 10:19:10 +02:00
|
|
|
|
public string UserName {
|
2022-04-06 11:40:33 +02:00
|
|
|
|
get => _userName;
|
2022-05-04 10:19:10 +02:00
|
|
|
|
set {
|
2022-04-06 11:40:33 +02:00
|
|
|
|
_userName = value;
|
|
|
|
|
OnPropertyChanged("UserName");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 10:19:10 +02:00
|
|
|
|
private int _score;
|
|
|
|
|
|
|
|
|
|
public int Score {
|
2022-04-06 11:40:33 +02:00
|
|
|
|
get => _score;
|
2022-05-04 10:19:10 +02:00
|
|
|
|
set {
|
2022-04-06 11:40:33 +02:00
|
|
|
|
_score = value;
|
|
|
|
|
OnPropertyChanged("Score");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 10:19:10 +02:00
|
|
|
|
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)]);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2022-04-06 11:40:33 +02:00
|
|
|
|
|
2022-04-06 11:15:55 +02:00
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName)
|
|
|
|
|
{
|
2022-04-06 11:40:33 +02:00
|
|
|
|
if (PropertyChanged != null)
|
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
|
}
|
2022-05-04 10:19:10 +02:00
|
|
|
|
}
|