-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.cs
More file actions
58 lines (50 loc) · 1.48 KB
/
TicTacToe.cs
File metadata and controls
58 lines (50 loc) · 1.48 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
using System;
public class TicTacToe
{
public static void Main(string[] args){
//declares the variable that is the list that contains the data
char[] grid = new char[9] {'-', '-', '-', '-', '-', '-', '-', '-', '-'};
Console.WriteLine("Play by inputting a number from 1-7, each number correlating to each column.");
//starts the game, each loop is one turn.
char playerTurn = 'X';
bool running = true;
int timesIterated = 0;
while(running){
//prints the board by iterating through each row in the list "rows"
printGrid(grid);
}
Console.WriteLine("");
//takes user input for which column to fill
Console.WriteLine("Input the selected tile");
string inputPhrase = Console.ReadLine();
int inputNumber;
bool success = Int32.TryParse(inputPhrase, out inputNumber);
//checks if the input reads stop, and if so, it ends code.
if(inputPhrase == "stop"){
running = false;
}
if(success && inputNumber < 10 && inputNumber > 0 && grid[inputNumber - 1] == '-'){
grid[inputNumber - 1] = playerTurn;
//switches turn over to other player
if(playerTurn == 'X'){
playerTurn = 'O';
}
else{
playerTurn = 'X';
}
timesIterated ++;
}
else{
Console.WriteLine("Not a valid grid space");
}
}
}
public static class PrintGrid(char[] grid){
for (int row = 0; row < 3; row++){
for (int column = 0; column < 3; column++){
Console.Write(grid[row * 3 + column] + " ");
}
Console.WriteLine("");
}
}
}