-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
76 lines (60 loc) · 2.18 KB
/
Program.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
using Mission4Assignment;
internal class Program
{
private static void Main(string[] args)
{
string choice = "";
bool winner = false;
List<char> board = new List<char> { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
Console.WriteLine("Welcome to the Tic Tac Toe game!");
int currentPlayer = 1;
do
{
// Print the game board
TikTak.GameBoard(board);
// Determine Current Player
Console.WriteLine($"It is Player {currentPlayer}'s turn. Where would you like to place your marker?");
// Validate the Input
choice = ValidateInput(board);
// Update GameBoard
int choiceNumeric = int.Parse(choice);
board[choiceNumeric - 1] = (currentPlayer % 2 == 0 ? 'O' : 'X');
// Check for a winner
winner = TikTak.CheckWinner(board, out char winningPlayer);
if (winner)
{
TikTak.GameBoard(board);
Console.WriteLine($"Player {winningPlayer} wins! Congratulations!");
break;
}
// Check for a draw
if (board.All(cell => cell == 'X' || cell == 'O'))
{
TikTak.GameBoard(board);
Console.WriteLine("It's a draw! No more moves left.");
break;
}
// Alternate Players
currentPlayer = currentPlayer == 1 ? 2 : 1;
} while (!winner);
}
private static string ValidateInput(List<char> board)
{
bool validInput = false;
string choice = "";
do
{
choice = Console.ReadLine();
// Check if the input is valid
if (int.TryParse(choice, out int position) && position >= 1 && position <= 9 && board[position - 1] != 'X' && board[position - 1] != 'O')
{
validInput = true; // Input is valid
}
else
{
Console.WriteLine("Invalid input! Please choose an available number between 1 and 9.");
}
} while (!validInput);
return choice; // Return the valid choice
}
}