Parwesh Bhaggan
Parwesh Bhaggan

Reputation: 29

Gomoku winning condition c++ using pointers and arrays

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

Upvotes: -1

Views: 298

Answers (1)

Botje
Botje

Reputation: 31080

EDIT: You need to change your datastructure from a char[][] to a boardBox[][]. In your constructor, you need to fix up each boardBox' neighbours array as follows:

goboard::goboard() {
    for (int r = 0; r < gridX; r++) {
        for (int c = 0; c < gridY; c++) { 
            boardBox& box = grid[r][c];
            box.neighbours[0] = get_box(r-1, c);
            box.neighbours[1] = get_box(r-1, c+1);
            // and so on
        }
    }
}

boardBox* goboard::get_grid(int row, int col) {
   return in_board(row, col) ? &grid[row][col] : nullptr;
}

where get_grid returns a pointer to the boardBox 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:

int count_direction(boardBox *box, int direction, char symb) {
    int count = 0;
    while (box) {
        if (box->playerColor != symb)
            break;
        box = box->neighbours[direction];
    }
    return count;
}

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:

boardBox *box = get_grid(row, col);
int count_diagonal1 = count_direction(box, 3, symb)   // down and to the right
                    + count_direction(box, 7, symb) // up and to the left
                    - 1;  // count_direction visits (row,col) twice!

Implementing the other directions and determining the win condition from the counts is left to you :)

Upvotes: 1

Related Questions