Instructions given: \"In your program, the turn player will enter the column num
ID: 3575637 • Letter: I
Question
Instructions given:
"In your program, the turn player will enter the column number of where she wishes to drop her disc. Your program should display what the grid looks like each turn. Yellow discs should be labeled 'Y' and red discs should be labeled 'R'. An empty cell in the grid should be labeled ' '.
Your program should detect the winning conditions for horizontal and vertical wins. You will receive extra credit if your program can also detect diagonal wins.
Your program should halt when it detects that a player has won or there's a draw."
The code below was provided to me and I need to find out how to check if the player has a win condition in main. I know it has to do with the function check_four_in_line call; I'm just not sure how I should check this in main. Thank you, the code is for connect 4.
#include <iostream>
using namespace std;
class Connect4Grid {
private:
std::vector< std::vector<char> > grid;
bool check_four_in_horizontal(int row, int col1, int col2, char disc) const;
bool check_four_in_vertical(int col, int row1, int row2, char disc) const;
bool check_four_in_diagonal(int row1, int col1, int row2, int col2, char disc) const;
public:
Connect4Grid();
/* Returns true if the line formed from coordinates (row1, column1) and (row2, column2)
* are all cells containing disc as a character value
*/
bool check_four_in_line(int row1, int column1, int row2, int column2, char disc) const;
/* Drops disc with a labeled character value into the column specified by column.
* Returns false if the column is full
*/
bool drop(int column, char disc);
/* Clears the grid */
void clear();
/* Displays the grid in the terminal window */
void draw() const;
};
Connect4Grid::Connect4Grid()
{
grid = std::vector< std::vector<char> >(6, std::vector<char>(7, ' '));
}
bool Connect4Grid::check_four_in_diagonal(int row1, int col1, int row2, int col2, char disc) const
{
if (row1 < row2) {
if (not (row1+3 == row2 and col1+3 == col2)) return false;
return (grid[row1][col1] == disc and
grid[row1+1][col1+1] == disc and
grid[row1+2][col1+2] == disc and
grid[row1+3][col1+3] == disc);
}
if (not (row1-3 == row2 and col1+3 == col2)) return false;
return (grid[row1][col1] == disc and
grid[row1-1][col1+1] == disc and
grid[row1-2][col1+2] == disc and
grid[row1-3][col1+3] == disc);
}
bool Connect4Grid::check_four_in_horizontal(int row, int col1, int col2, char disc) const
{
int i;
for (i = col1; i <= col2; ++i) {
if (grid[row][i] != disc) return false;
}
return true;
}
bool Connect4Grid::check_four_in_vertical(int col, int row1, int row2, char disc) const
{
int i;
for (i = row1; i <= row2; ++i) {
if (grid[i][col] != disc) return false;
}
return true;
}
bool Connect4Grid::check_four_in_line(int row1, int col1, int row2, int col2, char disc) const
{
if (row1 == row2) {
if (abs(col2 - col1) != 3) return false;
return check_four_in_horizontal(row1, std::min(col1, col2), std::max(col1, col2), disc);
} else if (col1 == col2) {
if (abs(row2 - row1) != 3) return false;
return check_four_in_vertical(col1, std::min(row1, row2), std::max(row1, row2), disc);
} else {
if (col1 < col2) {
return check_four_in_diagonal(row1, col1, row2, col2, disc);
} else {
return check_four_in_diagonal(row2, col2, row1, col1, disc);
}
}
}
bool Connect4Grid::drop(int col, char chr)
{
int i;
if (grid[0][col] != ' ') return false;
for (i = 1; i < 6; ++i) {
if (grid[i][col] != ' ') break;
}
grid[i-1][col] = chr;
return true;
}
void Connect4Grid::clear()
{
for (auto& row : grid) {
for (auto& col : row) {
col = ' ';
}
}
}
void Connect4Grid::draw() const
{
int i, j;
std::cout << std::string(4, ' ');
for (i = 0; i < 7; ++i) {
std::cout << i << std::string(3, ' ');
}
std::cout << std::endl;
std::cout << " +---+---+---+---+---+---+---+" << std::endl;
for (i = 0; i < 6; ++i) {
std::cout << i << " |";
for (j = 0; j < 7; ++j) {
std::cout << ' ' << grid[i][j] << " |";
}
std::cout << std::endl;
std::cout << " +---+---+---+---+---+---+---+" << std::endl;
}
}
// I need to find out how to detect a win condition.
int main()
{
Connect4Grid grid;
int column;
while (true) {
grid.draw();
cout << "Enter column index: ";
if (not (cin >> column)) break;
grid.drop(column, 'X');
}
}
Explanation / Answer
Answer:
int count = 0;
int rows = 6;
int cols = 7;
int winner = 0;
CircleButton[][] grid = new CircleButton[cols][rows];
int[][] gridChecker = new int[cols][rows];
boolean locked = false;
boolean gameOn = true;
void setup()
{
smooth();
for (int a = 0; a<cols; a++) {
for (int b = 0; b<rows; b++) {
gridChecker[a][b] = 10;
}
}
int a;
int b;
for ( a = 0; a<cols; a++) {
for (b = 0; b<rows; b++) {
grid[a][b] = new CircleButton(100*a + 50, 100*b + 50, 80, color(255), color(153));
}
}
color buttoncolor = color(204);
color highlight = color(153);
ellipseMode(CENTER);
size(700, 600);
background(255, 204, 0);
stroke(126);
for ( int c = 1; c <= cols; c++) {
line( c*(width/cols), 0, c*(width/cols), height);
}
for (int c = 1; c<=rows; c++) {
line(0, c*(height/rows), width, c*(height/rows));
}
}
void draw()
{
update(mouseX, mouseY);
for (int c = 0; c<cols; c++) {
for (int d = 0; d<rows; d++) {
grid[c][d].display();
}
}
if (checkWin() == true) {
gameOn = false;
PFont font = loadFont("ARCARTER-78.vlw");
textFont(font, 85);
if (winner == 4) {
shadowtext("Red Wins!", width/4, height/2, 3);
}
else if(winner == 8) {
shadowtext("Black Wins!", width/3.5, height/2, 3);
}
shadowtext("Click to play again", width/5.5, height*.3, 1);
}
}
void shadowtext (String s, float x, float y, int o) {
fill(100,100);
text(s, x+o, y+o);
fill(58,12,247);
text(s, x, y);
}
void mousePressed() {
if(gameOn == false) {
gameOn = true;
setup();
}
}
boolean checkWin()
{
int counter;
//horizontal
for (int a=0; a < rows; a++) {
for (int b=0; b < cols-3; b++) {
int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a]) + (gridChecker[b+2][a]) + (gridChecker[b+3][a]);
if (tCheck == 8 || tCheck == 4)
{
winner = tCheck;
return true;
}
}
}
//vertical
for (int a=0; a < rows-3; a++) {
for (int b=0; b < cols; b++) {
int tCheck = (gridChecker[b][a]) + (gridChecker[b][a+1]) + (gridChecker[b][a+2]) + (gridChecker[b][a+3]);
if (tCheck == 8 || tCheck == 4)
{
winner = tCheck;
return true;
}
}
}
//diagonals
for (int a=0; a < rows-3; a++) {
for (int b=0; b < cols-3; b++) {
int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
if (tCheck == 8 || tCheck == 4)
{
winner = tCheck;
return true;
}
}
}
for (int a =0; a < rows-3; a++) {
for (int b=0; b < cols-3; b++) {
int tCheck = (gridChecker[b][a]) + (gridChecker[b+1][a+1]) + (gridChecker[b+2][a+2]) + (gridChecker[b+3][a+3]);
if (tCheck == 8 || tCheck == 4)
{
winner = tCheck;
return true;
}
}
}
for (int a=0; a < rows-3; a++) {
for (int b=3; b < cols; b++) {
int tCheck = (gridChecker[b][a]) + (gridChecker[b-1][a+1]) + (gridChecker[b-2][a+2]) + (gridChecker[b-3][a+3]);
if (tCheck == 8 || tCheck == 4)
{
winner = tCheck;
return true;
}
}
}
return false;
}
void update(int x, int y)
{
if (mousePressed && gameOn) {
for (int c = 0; c<cols; c++) {
for (int d = 0; d<rows; d++) {
if (grid[c][d].pressed() && (grid[c][d].getColor() == color(255)|| grid[c][d].getColor() == color(153)) ) {
if (d == 5 || (gridChecker[c][d + 1] == 1 || gridChecker[c][d+1] == 2)) {
grid[c][d].setHighlight();
count++;
if (count % 2 == 0) {
grid[c][d].setColor(color(255, 0, 0));
gridChecker[c][d] = 1;
}
else {
grid[c][d].setColor(color(0));
gridChecker[c][d] = 2;
}
}
}
}
}
}
if (locked == false && gameOn) {
for (int c = 0; c<cols; c++) {
for (int d = 0; d<rows; d++) {
//grid[c][d].canHighlight();
grid[c][d].update();
}
}
}
else {
locked = false;
}
}
class CircleButton
{
color basecolor;
int x, y;
int size;
color highlightcolor;
color currentcolor;
boolean highlight;
boolean over = false;
boolean pressed = false;
CircleButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
highlightcolor = ihighlight;
currentcolor = icolor;
highlight = true;
}
void setHighlight() {
highlight = false;
}
void update()
{
if (over() && highlight == true ) {
currentcolor = highlightcolor;
}
else if (currentcolor == highlightcolor && !over()) {
currentcolor = color(255);
}
else {
currentcolor = currentcolor;
}
}
boolean pressed()
{
if (over) {
locked = true;
return true;
}
else {
locked = false;
return false;
}
}
boolean overCircle(int x, int y, int diameter)
{
float disX = x - mouseX;
float disY = y - mouseY;
if (sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
return true;
}
else {
return false;
}
}
void setColor(color a) {
currentcolor = a;
}
color getColor() {
return currentcolor;
}
boolean over()
{
if ( overCircle(x, y, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
void display()
{
smooth();
stroke(currentcolor);
fill(currentcolor);
ellipse(x, y, size, size);
}
}