-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeGame.cs
50 lines (44 loc) · 1.42 KB
/
TicTacToeGame.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TicTacToe.Players;
namespace TicTacToe
{
public class TicTacToeGame
{
public TicTacToeGame(IPlayer firtsPlayer, IPlayer secondPlayer)
{
this.FirtsPlayer = firtsPlayer;
this.SecondPlayer = secondPlayer;
this.WinnerLogic = new GameWinnerLogic();
}
public IPlayer FirtsPlayer { get; }
public IPlayer SecondPlayer { get; }
public GameWinnerLogic WinnerLogic { get; }
public GameResult Play()
{
Board board = new Board();
IPlayer currentPlayer = this.FirtsPlayer;
SymbolOnBoard symbol = SymbolOnBoard.X;
while (!this.WinnerLogic.GameOver(board))
{
var move = currentPlayer.Play(board, symbol);
board.PlaceSymbol(move, symbol);
if (currentPlayer == this.FirtsPlayer)
{
currentPlayer = this.SecondPlayer;
symbol = SymbolOnBoard.O;
}
else
{
currentPlayer = this.FirtsPlayer;
symbol = SymbolOnBoard.X;
}
}
var winner = this.WinnerLogic.GetWinner(board);
return new GameResult(winner, board);
}
}
}