I am currently working on a Gomoku game in c++. But I'm stuck with the winning conditions. I'm new to c++. I need to add the winning conditions using pointers for this game. Please I need help. I don't know how to start with it. So far I can only insert the player moves into the arrays. I need the pointers to determine the 8 directions for the winning condition.
Winning conditions are: 5 in a row horizontally, vertically, diagonally (but this must also be changed to 3 in a row, 4 in a row etc)
the header file
// file goboard.h
class boardBox{
public:
char PlayerColor; //z or W // 7 0 1
boardBox* neighbours[8]; // 6 2
boardBox( ); // 5 4 3
};//boardBox
class goboard {
private:
boardBox* entrance;
int height, width;
static const int gridX = 6;
static const int gridY = 7;
char grid[gridX][gridY];
// TODO
public:
void ask_turn(char symb);
goboard ( );
goboard (int height, int width);
~goboard ( );
void showBoard( );
void generate();
// TODO
};//goboard
this file linked with the header file
// file goboard.cc
#include <iostream>
#include "goboard.h"
using namespace std;
goboard::goboard ( ) {
// TODO
}//goboard::goboard
goboard::~goboard ( ) {
// TODO
}//goboard::~goboard
void goboard::generate()
{
cout << "Board shows like this." << endl;
int number = 1;
for(int x = 0; x < gridX; x++)
{
for(int y = 0; y < gridY; y++)
{
grid[x][y] = '.';
}
}
}
void goboard::showBoard( ) {
printf("\n................\n");
for(int x = 0; x < gridX; x++)
{
for(int y = 0; y < gridY; y++)
{
printf("%c |", grid[x][y]);
}
printf("\n");
}
cout<<endl;
// TODO
}//goboard::showBoard
void goboard::ask_turn(char symb) //symb is symbol Z or W
{
int input;
int input2;
while( true )
{
cout<<"Where would you like to play?"<<endl;
cin>>input;
cin>>input2;
int index = input;
int index2 = input2;
int row = index;
int col = index2;
char grid_position = grid[row][col];
if(grid_position == 'Z' || grid_position == 'W')
{
puts("That grid position is already take!");
}else{
grid[row][col] = symb;
break;
}
}
}
// TODO
The main file
// file hoofd.cc
#include <iostream>
#include <string>
#include "gobord.h"
#define GRID_SIZE 3
using namespace std;
int main (int argc, char *argv[] ) {
goboard Goboard;
Goboard.generate();
char symb = 'Z';
char symb1 = 'W';
while(true){
Goboard.showBoard( );
Goboard.ask_turn(symb);
Goboard.showBoard();
Goboard.ask_turn(symb1);
}
return 0;
}//main
EDIT: You need to change your datastructure from a
char[][]
to aboardBox[][]
. In your constructor, you need to fix up eachboardBox
'neighbours
array as follows:where
get_grid
returns a pointer to theboardBox
or a null pointer if that cell is out of bounds.A win condition happens right after a move, so logically the just-placed piece must be part of the five-in-a-row in either direction.
Let's first implement a function that follows a certain direction and counts the number of pieces of a given symbol in that direction:
This follows the pointers in the
neighbours
array until we hit the end of the board, denoted by a null pointer.We can use this as follows to count the number of pieces along a diagonal:
Implementing the other directions and determining the win condition from the counts is left to you :)