-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
179 lines (162 loc) · 5.33 KB
/
Board.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
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
//Dale Cutshall, Linh Dang
//CS 305, Spring 2019
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Connect4
{
/// <summary>
// The main functions of the program. Can print out the board, check for winning condition, and shows the updated player moves.
// After the game is over the player can decide if they want to play again.
/// </summary>
[Serializable]
public class Board
{
public int rows;
public int columns;
public int win = 4;
public int[,] board;
public Board()
{
board = new int[100, 100];
}
//Function to show the player's move on the board. Checks to make sure it is the last empty spot.
public void dropDisc(Player currentPlayer, int dropChoice)
{
int length = rows;
int turn = 0;
do
{
if (board[length, dropChoice] != 1 && board[length, dropChoice] != 2)
{
board[length, dropChoice] = currentPlayer.playerID;
turn = 1;
}
else
length -=1;
} while (turn != 1);
}
//Prints out the connect four grid
public void displayBoard()
{
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= columns; j++)
{
if (board[i, j] != 1 && board[i, j] != 2)
board[i, j] = 0;
Console.Write(board[i, j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
}
//Gettter and Setter for rows and columns
public int Rows
{
get
{
return rows;
}
set
{
rows = value;
}
}
public int Columns
{
get
{
return columns;
}
set
{
columns = value;
}
}
public bool PlayAgain()
{
String again;
again = Console.ReadLine();
if (again[0] == 'y' || again[0] =='Y')
{
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= columns; j++)
{
board[i, j] = 0;
Console.Write(board[i, j]);
Console.Write(" ");
}
Console.WriteLine();
}
Console.WriteLine();
return true;
}
return false;
}
//Function that checks for the winning condition
//Diagonally check still needs some work
public bool checkfour(Player currentplayer)
{
//horizontalCheck
for(int j = 0; j < rows-3; j++)
{
for(int i = 0; i < columns; i++)
{
if (board[i, j] == currentplayer.playerID &&
board[i, j + 1] == currentplayer.playerID &&
board[i, j + 2] == currentplayer.playerID &&
board[i, j + 3] == currentplayer.playerID)
{
return true;
}
}
}
//vertical check
for(int i =0; i < columns - 3; i++)
{
for(int j =0; j < rows; j++)
{
if(board[i, j] == currentplayer.playerID &&
board[i + 1, j] == currentplayer.playerID &&
board[i + 2, j] == currentplayer.playerID &&
board[i + 3, j] == currentplayer.playerID)
{
return true;
}
}
}
for(int i =3; i <columns; i++)
{
for(int j = 0; i < rows - 3; j++)
{
if(board[i, j] == currentplayer.playerID &&
board[i - 1, j + 1] == currentplayer.playerID &&
board[i - 2, j + 2] == currentplayer.playerID &&
board[i - 3, j + 3] == currentplayer.playerID)
{
return true;
}
}
}
for(int i =3; i < columns; i++)
{
for(int j = 3; j < rows; j++)
{
if(board[i, j] == currentplayer.playerID &&
board[i - 1, j - 1] == currentplayer.playerID &&
board[i - 2, j - 2] == currentplayer.playerID &&
board[i - 3, j - 3] == currentplayer.playerID)
{
return true;
}
}
}
return false;
}
}
}