-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect4.cs
More file actions
63 lines (59 loc) · 2.18 KB
/
Connect4.cs
File metadata and controls
63 lines (59 loc) · 2.18 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
using System;
class Connect4
{
static void Main(string[] args)
{
//declares the variables that are the lists that contain the data
char[] row1 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row2 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row3 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row4 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row5 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row6 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[] row7 = new char[7] {'-', '-', '-', '-', '-', '-', '-'};
char[][] rows = new char[][] {row1, row2, row3, row4, row5, row6, row7};
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';
while(true){
//prints the board by iterating through each row in the list "rows"
for(int selectedRowIndex = 0; selectedRowIndex <= 6; selectedRowIndex++){
//iterates through the characters in each row, seperating it by 2 spaces
for (int selectedColumnIndex = 0; selectedColumnIndex <= 6; selectedColumnIndex++){
Console.Write(rows[selectedRowIndex][selectedColumnIndex] + " ");
}
Console.WriteLine("");
}
Console.WriteLine("");
//takes user input for which column to fill
Console.WriteLine("Input a column");
string inputPhrase = Console.ReadLine();
int inputColumn;
bool success = Int32.TryParse(inputPhrase, out inputColumn);
//checks if the input reads stop, and if so, it ends code.
if(inputPhrase == "stop"){
return;
//iterates through each row, and checks the value at the requested column, and sets it to a value if it is blank
}
if(success && inputColumn < 8 && inputColumn > 0){
inputColumn = inputColumn - 1;
for (int rowToCheck = 6; rowToCheck >= 0; rowToCheck--){
if(rows[rowToCheck][inputColumn] == '-'){
rows[rowToCheck][inputColumn] = playerTurn;
break;
}
}
//switches turn over to other player
if(playerTurn == 'X'){
playerTurn = 'O';
}
else{
playerTurn = 'X';
}
}
else{
Console.WriteLine("Not a valid column");
}
}
}
}