-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cs
More file actions
90 lines (80 loc) · 2.5 KB
/
Board.cs
File metadata and controls
90 lines (80 loc) · 2.5 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
88
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Strategy_Pattern
{
public class Board
{
private Figure[] _board;
public Board()
{
_board = new Figure[9];
}
public Board(Board board)
{
_board = board._board.Clone() as Figure[];
}
public void ResetBoard()
{
for (var i = 0; i < _board.Length; ++i)
_board[i] = Figure.EMPTY;
}
public bool IsLegal(uint pos)
{
return _board[pos] == Figure.EMPTY;
}
public void SetFigure(uint pos, Figure figure)
{
_board[pos] = figure;
}
public void Display()
{
Console.WriteLine();
var copy = new string[_board.Length];
for (var i = 0; i < _board.Length; ++i)
{
string symb = " ";
switch (_board[i])
{
case Figure.ZERO:
symb = "O";
break;
case Figure.CROSS:
symb = "X";
break;
}
copy[i] = symb;
}
Console.WriteLine($"\t{copy[0]} | {copy[1]} | {copy[2]}");
Console.WriteLine($"\t{copy[3]} | {copy[4]} | {copy[5]}");
Console.WriteLine($"\t{copy[6]} | {copy[7]} | {copy[8]}");
Console.WriteLine();
}
public Winner Winner(Figure human, Figure computer)
{
int[,] WINNING_ROWS = new int[8, 3]{
{ 0, 1, 2},
{ 3, 4, 5},
{ 6, 7, 8},
{ 0, 3, 6},
{ 1, 4, 7},
{ 2, 5, 8},
{ 0, 4, 8},
{ 2, 4, 6}};
for (int row = 0; row < 8; ++row)
{
if ((_board[WINNING_ROWS[row, 0]] != Figure.EMPTY) &&
_board[WINNING_ROWS[row, 0]] == _board[WINNING_ROWS[row, 1]] &&
_board[WINNING_ROWS[row, 1]] == _board[WINNING_ROWS[row, 2]])
{
return human == _board[WINNING_ROWS[row, 0]] ? Strategy_Pattern.Winner.HUMAN : Strategy_Pattern.Winner.COMPUTER;
}
}
if (_board.Any(a => a == Figure.EMPTY))
return Strategy_Pattern.Winner.NO_ONE;
else
return Strategy_Pattern.Winner.TIE;
}
}
}