-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (174 loc) · 5.9 KB
/
Copy pathProgram.cs
File metadata and controls
206 lines (174 loc) · 5.9 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TicTacToe
{
class Program
{
private static int threeInARowVert = 0;
private static int threeInARowVert1 = 0;
private static int threeInARowVert2 = 0;
private static int threeInARowHorizon = 0;
private static int threeInARowHorizon1 = 0;
private static int threeInARowHorizon2 = 0;
private static int leftDiagonalMatch = 0;
private static int rightDiagonalMatch = 0;
private static int isInt;
private static string marker = "X";
private static string selection;
private static bool hasWon = false;
private static bool player1 = false;
private static List<int> selectedNums = new List<int>();
private static string[,] ticTacToeArr = new string[3, 3]
{
{ "1", "2", "3" }, // 00 01 02
{ "4", "5", "6" }, // 10 11 12
{ "7", "8", "9" } // 20 21 22
};
static void Main(string[] args)
{
SetupGame();
while (hasWon == false)
{
SetMarker();
AskQuestion();
MakeSelection();
Console.Clear();
SetupGame();
}
DisplayWinner();
Console.Read();
}
private static void SetupGame()
{
Console.WriteLine("|-----------|");
threeInARowVert = 0;
threeInARowVert1 = 0;
threeInARowVert2 = 0;
rightDiagonalMatch = 0;
// Row
for (int i = 0, k = 2; i < ticTacToeArr.GetLength(0); i++, k--)
{
Console.Write("| ");
threeInARowHorizon = 0;
threeInARowHorizon1 = 0;
threeInARowHorizon2 = 0;
leftDiagonalMatch = 0;
// Column
for (int j = 0; j < ticTacToeArr.GetLength(1); j++)
{
if (ticTacToeArr[0, j] == marker) threeInARowHorizon++;
if (ticTacToeArr[1, j] == marker) threeInARowHorizon1++;
if (ticTacToeArr[2, j] == marker) threeInARowHorizon2++;
if (j == 0 && ticTacToeArr[i, j] == marker) threeInARowVert++;
if (j == 1 && ticTacToeArr[i, j] == marker) threeInARowVert1++;
if (j == 2 && ticTacToeArr[i, j] == marker) threeInARowVert2++;
Console.Write(ticTacToeArr[i, j] + " | ");
}
if (ticTacToeArr[i, k] == marker) rightDiagonalMatch++;
Console.WriteLine("");
Console.WriteLine("|-----------|");
}
if (HasMatch()) hasWon = true;
Console.WriteLine("");
}
private static void DisplayWinner()
{
if (player1)
{
Console.WriteLine("Player 1 WINS!");
}
else
{
Console.WriteLine("Player 2 WINS!");
}
}
private static bool HasMatch() {
return threeInARowHorizon == 3
|| threeInARowHorizon1 == 3
|| threeInARowHorizon2 == 3
|| threeInARowVert == 3
|| threeInARowVert1 == 3
|| threeInARowVert2 == 3
|| leftDiagonalMatch == 3
|| rightDiagonalMatch == 3;
}
private static void SetMarker()
{
player1 = !player1;
if (player1)
{
Console.WriteLine("Player 1");
}
else
{
Console.WriteLine("Player 2");
}
marker = player1 ? "X" : "O";
}
private static void AskQuestion()
{
do
{
Console.WriteLine("Select your number: ");
selection = Console.ReadLine();
int.TryParse(selection, out isInt);
if (isInt == 0)
{
Console.WriteLine("Incorrect input.");
Console.WriteLine("");
}
else
{
int parsedInt = int.Parse(selection);
if (parsedInt > ticTacToeArr.Length || selectedNums.Contains(parsedInt))
{
isInt = 0;
Console.WriteLine("");
Console.WriteLine("Please choose a number between 1 and 9 that has not already been selected.");
Console.WriteLine("");
}
else {
selectedNums.Add(parsedInt);
}
}
}
while (isInt == 0);
}
private static void MakeSelection()
{
switch (selection)
{
case "1":
ticTacToeArr[0, 0] = marker;
break;
case "2":
ticTacToeArr[0, 1] = marker;
break;
case "3":
ticTacToeArr[0, 2] = marker;
break;
case "4":
ticTacToeArr[1, 0] = marker;
break;
case "5":
ticTacToeArr[1, 1] = marker;
break;
case "6":
ticTacToeArr[1, 2] = marker;
break;
case "7":
ticTacToeArr[2, 0] = marker;
break;
case "8":
ticTacToeArr[2, 1] = marker;
break;
case "9":
ticTacToeArr[2, 2] = marker;
break;
};
}
}
}