This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
82 lines (67 loc) · 2.84 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
77
78
79
80
81
82
public static class TicTacToe {
public static Player[] Players { get; set; } = new Player[2];
public static Player ActivePlayer { get; set; } = Players[0];
public static bool GameOver { get; set; } = false;
public static int WindowWidth { get; set; } = 60;
public static void Main() {
// Set Console Properties -------------------------/
Console.Title = "Tic-Tac-Toe";
// Console.WindowWidth = WindowWidth; // Only works on Windows
Console.CursorVisible = false;
// Display Title Page -------------------------------------------------/
Console.Clear();
Title title = new();
title.Render();
// Display Instructions -------/
Utilities.Center("Welcome to Tic-Tac-Toe!");
Console.WriteLine();
Utilities.Center("Players take turns claiming spaces on a board.");
Utilities.Center("The first player to claim three in a row wins!");
Console.WriteLine();
Utilities.Center("Press any key to continue...");
Console.ReadKey(true);
Console.Clear();
// --------------------------------------------------------------------/
// Initialize Players -----------------------------/
title.Render();
Players[0] = new();
Players[1] = new();
ActivePlayer = Players[0];
// Game Loop ----------------------------------------------------------/
Board board = new();
while (!GameOver) {
Console.Clear();
// Display Title & Instructions ---------------/
title.Render();
Utilities.Center($"{ActivePlayer.Name}'s turn!");
Utilities.Bar();
// Display Board ------------------------------/
board.Render();
// Check for Win Condition --------------------/
board.CheckForWin();
// Get Player Input ---------------------------/
board.Select();
}
//---------------------------------------------------------------------/
}
}
public class Player {
public static int PlayerCount { get; set; } = 0;
public string Name { get; set; } = $"Player {++PlayerCount}";
public char Symbol { get; set; }
public Player() {
Console.Write($"Enter a name for {Name}: ");
Name = Console.ReadLine()!;
Name ??= $"Player {PlayerCount}";
Symbol = (PlayerCount == 1) ? 'X' : 'O';
}
}
public static class Utilities {
public static void Center(string text) {
Console.SetCursorPosition((TicTacToe.WindowWidth - text.Length) / 2, Console.CursorTop);
Console.WriteLine(text);
}
public static void Bar() {
Console.WriteLine("------------------------------------------------------------");
}
}