-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
87 lines (74 loc) · 2.51 KB
/
Game.cs
File metadata and controls
87 lines (74 loc) · 2.51 KB
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
84
85
86
87
namespace othello;
public class Game
{
private readonly Player _player1;
private readonly Player _player2;
private int _currentPlayerId = 1;
private long _player1Clock, _player2Clock;
public Board Board { get; }
public Game(Player[] players)
{
if (players[0].Id == 0 || players[1].Id == 0)
{
throw new ArgumentException("Players must have a valid ID.");
}
_player1 = players[0];
_player2 = players[1];
Board = new Board();
}
// Returns true if a move was made; false if game cannot continue
public bool MakeMove()
{
if (Board.IsFinished()) return false;
var currentPlayer = _currentPlayerId == 1 ? _player1 : _player2;
if (Board.GetFlippableDiscsPerPlacement(currentPlayer.Id).Count == 0) //if there is no move that will flip any enemy discs, skip a turn
{
Console.WriteLine("SKIPPED TURN!");
_currentPlayerId = _currentPlayerId == 1 ? 2 : 1;
return false;
}
var startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var move = currentPlayer.GetMove(Board);
var elapsedTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - startTime;
if (currentPlayer.Id == 1)
{
_player1Clock += elapsedTime;
}
else
{
_player2Clock += elapsedTime;
}
var success = Board.PlaceDisc(move, currentPlayer.Id, true);
if (!success)
{
Console.WriteLine($"Invalid move at {move} by player {currentPlayer.Id}.");
return false;
}
Console.WriteLine($"{currentPlayer.Name} placed at {move}.");
_currentPlayerId = _currentPlayerId == 1 ? 2 : 1;
return true;
}
public (long player1Clock, long player2Clock) GetClock()
{
return (_player1Clock, _player2Clock);
}
public new string ToString()
{
var result = $"Basic evaluation: {Board.GetEvaluation()}\n";
var currentPlayer = _currentPlayerId == 1 ? _player1 : _player2;
result += $"Current player: {currentPlayer}\n\n";
result += " ";
for (var i = 0; i < 8; i++)
{
result += $" {i.ToString()}";
}
result += "\n";
var row = 0;
foreach (var line in Board.ToString(_player1, _player2, _currentPlayerId).Split("\n"))
{
result += $"{row} {line}\n";
row++;
}
return result;
}
}