-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeGame.cs
96 lines (78 loc) · 3.41 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicTacToeLowLevelDesign
{
public class TicTacToeGame
{
private readonly int _sizeOfBoard;
private readonly List<Player> _players;
private readonly PlayingBoard _playingBoard;
public TicTacToeGame(string firstPalyerName, string secondPlayerName, int sizeOfBoard)
{
_players = new List<Player>();
//Creating Player 1
PlayingPieceX playingPieceX = new PlayingPieceX();
Player firstPlayer = new Player(firstPalyerName, playingPieceX);
//Creating player 2
PlayingPieceO playingPieceO = new PlayingPieceO();
Player secondPalyer = new Player(secondPlayerName, playingPieceO);
_players.Add(firstPlayer);
_players.Add(secondPalyer);
_sizeOfBoard = sizeOfBoard;
_playingBoard = new PlayingBoard(_sizeOfBoard);
}
public void StartGame()
{
bool isGameOver = false;
_playingBoard.DisplayBoard();
while (!isGameOver)
{
if (!_playingBoard.IsBoardEmpty())
{
isGameOver = true;
Console.WriteLine("Game is tie");
continue;
}
var currentPlayerTurn = _players[0];
Console.WriteLine($"{currentPlayerTurn.Name} turn : Enter row, Enter column");
string enteredPosition = Console.ReadLine();
if (!string.IsNullOrEmpty(enteredPosition))
{
var boardLocations = enteredPosition.Split(',');
if (boardLocations.Length != 2)
{
Console.WriteLine("Please Enter Valid location or in valid format");
continue;
}
else
{
int xLocationIndex = Convert.ToInt32(boardLocations[0]);
int yLocationIndex = Convert.ToInt32(boardLocations[1]);
if (xLocationIndex < 0 || yLocationIndex < 0 || xLocationIndex >= _sizeOfBoard || yLocationIndex >= _sizeOfBoard)
{
Console.WriteLine("Location is not valid");
continue;
}
if (!_playingBoard.Add(xLocationIndex, yLocationIndex, currentPlayerTurn.PlayingPiece.PieceType.ToString()))
{
Console.WriteLine("Entered location already filled");
continue;
}
_playingBoard.DisplayBoard();
if (_playingBoard.IsWinner(xLocationIndex, yLocationIndex, currentPlayerTurn.PlayingPiece.PieceType.ToString()))
{
isGameOver = true;
Console.WriteLine($"{currentPlayerTurn.Name} is winner");
break;
}
_players.RemoveAt(0);
_players.Add(currentPlayerTurn);
}
}
}
}
}
}