Detecting Empty Squares in Chess Program

Hi. I'm working on a text-based chess game for blind players. I'm trying to figure out how to check for empty squares. I have several different classes for the pieces, derived from one abstract Piece class. The board is a class containing an array of 32 pointers to pieces. Each one has an index between 0 and 63, and their row and column are based off of that. There is nothing in the other 32 squares. I'm trying to figure out how to detect empty squares so I can display the board and use the information in game play. For display, it will show the board like a list of pieces and positions, skipping over empty squares. I'm thinking the index needs to stay constant so it can be used to refer to specific pieces, but after using the index to set the row and column, they can change. I'm thinking of using a for loop to display the board, but I'm not quite sure how to create it without being able to detect and skip past empty squares, so I need to add an isEmptySquare function to the Board class. It return will true if the square is empty. Can anyone suggest a way to fill in this function to make it work? I've thought about it, and I think this might involve a while or for loop, but other than that, I don't know. My Board class header and methods are below. Let me know if I need to add more information.

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
#ifndef BOARD_H
#define BOARD_H

#include "piece.h"
#include "bishop.h"
#include "king.h"
#include "knight.h"
#include "pawn.h"
#include "queen.h"
#include "rook.h"

class Board
{
public:
   Board();
   ~Board();
   void setPieceColor();
   void display();
   int getPieceAtPosition(int row, int col);
   void setPieceAtPosition(int index, Piece &piece);
   bool isEmptySquare();

private:
   Piece * pieces[32];
};

#endif

#include "board.h"
#include<iostream>

Board::Board()
{
   pieces[0] = new Rook(0, 0);
   pieces[1] = new Knight(0, 1);
   pieces[2] = new Bishop(0, 2);
   pieces[3] = new Queen(0, 3);
   pieces[4] = new King(0, 4);
   pieces[5] = new Bishop(0, 5);
   pieces[6] = new Knight(0, 6);
   pieces[7] = new Rook(0, 7);
   int col = 0;
   for (int i = 8; i < 16; i++)
   {
      pieces[i] = new Pawn(1, col);
      col++;
}
   col = 0;
   for (int i = 48; i < 56; i++)
   {
      pieces[i] = new Pawn(6, col);
      col++;
   }
   pieces[56] = new Rook(7, 0);
   pieces[57] = new Knight(7, 1);
   pieces[58] = new Bishop(7, 2);
   pieces[59] = new Queen(7, 3);
   pieces[60] = new King(7, 4);
   pieces[61] = new Bishop(7, 5);
   pieces[62] = new Knight(7, 6);
   pieces[63] = new Rook(7, 7);
}

Board::~Board()
{
   for (int i = 0; i < 64; i++)
   {
      pieces[i] = NULL;
}
   delete[] &pieces;
}

void Board::setPieceColor()
{
   for (int i = 0; i < 64; i++)
   {
      if (i < 16)
      {
         pieces[i]->setColor(true);
      }
      else if (i > 46)
      {
         pieces[i]->setColor(false);
      }
}
}

int Board::getPieceAtPosition(int row, int col)
{
   int index = row * 8 + col;
   return index;
}

void Board::setPieceAtPosition(int index, Piece &piece)
{
   piece.setRow(index / 8);
   piece.setCol(index % 8);
}

bool Board::isEmptySquare()
{
   
   {

   }
}

void Board::display()
{
   for (int i = 0; i < 64; i++)
   {
      pieces[i]->display();
   }
}
You seem to have a basic confusion in your code between 32 pointers and 64 board positions. Even in your problem description you say "a class containing an array of 32 pointers to pieces. Each one has an index between 0 and 63". You need to sort that out. Either make the array hold all 64 board positions as your code mostly assumes, or redesign it to just hold pointers to the active pieces (if that's what you had in mind) in which case the problem of detecting empty board positions may become interesting.
Last edited on
I was doing it the way my professor suggested. He suggested that my board should use 0 to 63 for the index and have 32 pointers to pieces. I have a Board constructor and other functions written the way he suggested here:
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
Board::Board()
{
   pieces[0] = new Rook(0, 0);
   pieces[1] = new Knight(0, 1);
   pieces[2] = new Bishop(0, 2);
   pieces[3] = new Queen(0, 3);
   pieces[4] = new King(0, 4);
   pieces[5] = new Bishop(0, 5);
   pieces[6] = new Knight(0, 6);
   pieces[7] = new Rook(0, 7);
   int col = 0;
   for (int i = 8; i < 16; i++)
   {
      pieces[i] = new Pawn(1, col);
      col++;
}
   col = 0;
   for (int i = 48; i < 56; i++)
   {
      pieces[i] = new Pawn(6, col);
      col++;
   }
   pieces[56] = new Rook(7, 0);
   pieces[57] = new Knight(7, 1);
   pieces[58] = new Bishop(7, 2);
   pieces[59] = new Queen(7, 3);
   pieces[60] = new King(7, 4);
   pieces[61] = new Bishop(7, 5);
   pieces[62] = new Knight(7, 6);
   pieces[63] = new Rook(7, 7);
}

Board::~Board()
{
   for (int i = 0; i < 64; i++)
   {
      pieces[i] = NULL;
}
   delete[] &pieces;
}

void Board::setPieceColor()
{
   for (int i = 0; i < 64; i++)
   {
      if (i < 16)
      {
         pieces[i]->setColor(true);
      }
      else if (i > 46)
      {
         pieces[i]->setColor(false);
      }
}
}

int Board::getPieceAtPosition(int row, int col)
{
   int index = row * 8 + col;
   return index;
}

void Board::setPieceAtPosition(int index, Piece &piece)
{
   piece.setRow(index / 8);
   piece.setCol(index % 8);
}
Topic archived. No new replies allowed.