-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinChecker.cs
55 lines (54 loc) · 1.91 KB
/
WinChecker.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
using System;
using System.Collections.Generic;
using System.Text;
namespace TicTacToe
{
class WinChecker
{
public State Check(Board board)
{
if (CheckForWin(board, State.X)) return State.X;
if (CheckForWin(board, State.O)) return State.O;
return State.Undecided;
}
private bool CheckForWin(Board board, State player)
{
for (int row = 0; row < 3; row++)
if (AreAll(board, new Position[] {
new Position(row, 0),
new Position(row, 1),
new Position(row, 2) }, player))
return true;
for (int column = 0; column < 3; column++)
if (AreAll(board, new Position[] {
new Position(0, column),
new Position(1, column),
new Position(2, column) }, player))
return true;
if (AreAll(board, new Position[] {
new Position(0, 0),
new Position(1, 1),
new Position(2, 2) }, player))
return true;
if (AreAll(board, new Position[] {
new Position(2, 0),
new Position(1, 1),
new Position(0, 2) }, player))
return true;
return false;
}
private bool AreAll(Board board, Position[] positions, State state)
{
foreach (Position position in positions)
if (board.GetState(position) != state) return false;
return true;
}
public bool IsDraw(Board board)
{
for (int row = 0; row < 3; row++)
for (int column = 0; column < 3; column++)
if (board.GetState(new Position(row, column)) == State.Undecided) return false;
return true;
}
}
}