-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
178 lines (158 loc) · 5.55 KB
/
Board.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Linq;
namespace ColoringGame
{
public class Board
{
public int Size { get; }
public int StreakLength { get; }
private BoardField[] Fields { get; }
private Player Player1 { get; }
private Player Player2 { get; }
private PlayerNumber CurrentPlayer { get; set; }
private List<int[]> LoosingSequences { get; }
public Board(int size, int streakLength, double alpha)
{
// Size = 5;
// StreakLength = 3;
// Fields = new BoardField[] { BoardField.Empty, BoardField.Player1, BoardField.Empty, BoardField.Player1, BoardField.Player2 };
// CurrentPlayer = PlayerNumber.Player2;
Size = size;
StreakLength = streakLength;
Fields = Enumerable.Repeat(BoardField.Empty, size).ToArray();
LoosingSequences = GenerateLoosingSequences();
Player1 = new HumanPlayer(PlayerNumber.Player1);
Player2 = new AiPlayer2(PlayerNumber.Player2, alpha, size, StreakLength, LoosingSequences);
CurrentPlayer = PlayerNumber.Player1;
}
private List<int[]> GenerateLoosingSequences()
{
var result = new List<int[]>();
var sequenceStart = 0;
var step = 0;
var generate = true;
while (generate)
{
sequenceStart = 0;
step++;
var listLength = result.Count;
while (sequenceStart + step * (StreakLength - 1) < Size)
{
result.Add(
Enumerable.Repeat(sequenceStart, StreakLength)
.Select((v, i) => v + i * step)
.ToArray());
sequenceStart++;
}
generate = listLength != result.Count;
}
return result;
}
public GameStatus PerformGame()
{
var gameStatus = GetGameStatus();
while (gameStatus == GameStatus.InProgress)
{
if (CurrentPlayer == PlayerNumber.Player1)
{
Play(Player1, PlayerNumber.Player2);
}
else
{
Play(Player2, PlayerNumber.Player1);
}
gameStatus = GetGameStatus();
PrintGame(gameStatus);
}
return gameStatus;
}
private void PrintGame(GameStatus gameStatus)
{
Console.WriteLine($"Current game status {gameStatus}");
Console.WriteLine("Current board state: ");
Console.ForegroundColor = ConsoleColor.White;
for (var i = 0; i < Fields.Length; ++i)
{
Console.BackgroundColor = GetColor(Fields[i]);
Console.Write($"{i + 1,4} ");
Console.ResetColor();
if ((i + 1) % StreakLength == 0)
{
Console.WriteLine();
}
}
Console.ResetColor();
Console.WriteLine();
}
private ConsoleColor GetColor(BoardField boardField)
{
return boardField switch
{
BoardField.Player1 => ConsoleColor.Blue,
BoardField.Player2 => ConsoleColor.Red,
_ => ConsoleColor.Black,
};
}
private void Play(Player currentPlayer, PlayerNumber nextPlayer)
{
var fieldIndex = currentPlayer.SelectField(Fields.ToArray());
if (Fields[fieldIndex] == BoardField.Empty)
{
Fields[fieldIndex] = (BoardField)currentPlayer.PlayerNumber;
}
else
{
throw new InvalidOperationException(
$"Player {currentPlayer.PlayerNumber} tried to play on field {fieldIndex} with value {Fields[fieldIndex]}");
}
CurrentPlayer = nextPlayer;
}
private GameStatus GetGameStatus()
{
if (LoosingSequences.Where(s => s.All(v => Fields[v] == BoardField.Player1)).FirstOrDefault() is int[] sequence1)
{
Console.WriteLine("Player 1 created loosing sequence:");
Console.ForegroundColor = ConsoleColor.White;
foreach (var i in sequence1)
{
Console.Write($"{i + 1,4} ");
}
Console.WriteLine();
return GameStatus.Player2Won;
}
else if (LoosingSequences.Where(s => s.All(v => Fields[v] == BoardField.Player2)).FirstOrDefault() is int[] sequence2)
{
Console.WriteLine("Player 2 created loosing sequence:");
Console.ForegroundColor = ConsoleColor.White;
foreach (var i in sequence2)
{
Console.Write($"{i + 1,4} ");
}
Console.WriteLine();
return GameStatus.Player1Won;
}
else if (Fields.All(v => v != BoardField.Empty))
{
return GameStatus.Draw;
}
else
{
return GameStatus.InProgress;
}
}
}
public enum BoardField
{
Empty = 0,
Player1 = 1,
Player2 = 2,
}
public enum GameStatus
{
InProgress = 0,
Player1Won = 1,
Player2Won = 2,
Draw = 3,
}
}