-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
83 lines (69 loc) · 2.37 KB
/
Game.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
public class Game
{
private readonly Board _board;
private Color _currentPlayer;
public Game()
{
_board = new Board();
_currentPlayer = Color.Red;
}
public void Start()
{
while (true)
{
Console.Clear();
_board.PrintBoard();
Console.WriteLine($" ➤ Player's turn {_currentPlayer}");
if (_board.IsKingInCheck(_currentPlayer))
{
Console.WriteLine(" ➤ Cult!");
}
Console.Write(" ➤ Enter your move (E.g: e2 to e4) ⟹ ");
string? move = Console.ReadLine();
if (string.IsNullOrWhiteSpace(move))
{
continue;
}
try
{
string[] parts = move.ToLower().Split(" to ");
if (parts.Length != 2)
{
Console.WriteLine(" ➤ The move format is invalid.");
continue;
}
Position from = Position.FromString(parts[0].Trim().ToUpper());
Position to = Position.FromString(parts[1].Trim().ToUpper());
Piece piece = _board.GetPieceAt(from);
if (piece == null)
{
Console.WriteLine(" ➤ There is no bead at the origin.");
continue;
}
if (piece.Color != _currentPlayer)
{
Console.WriteLine(" ➤ This piece does not belong to you.");
continue;
}
_board.MovePiece(from, to);
if (_board.IsCheckmate(_currentPlayer == Color.Red ? Color.Green : Color.Red))
{
Console.WriteLine(" ➤ Checkmate");
Console.WriteLine($" ➤ Player {_currentPlayer} won!");
break;
}
_currentPlayer = _currentPlayer == Color.Red ? Color.Green : Color.Red;
}
catch (ArgumentException e)
{
Console.WriteLine($" ➤ Err: {e.Message}");
}
catch (Exception e)
{
Console.WriteLine($" ➤ Unk Err: {e.Message}");
}
Console.WriteLine(" ➤ Press Enter to continue...");
Console.ReadLine();
}
}
}