I am having a problem with my code.
When I add an element to a 2d array, it adds two instead. Here are some outputs to help explain the problem:
Blank game board:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Adding to board: 7, 0
After adding first piece:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 7
7 0 0 0 0 0 0 0
Found a 7 at: 6, 7
Found a 7 at: 7, 0
I initialize a 2d array (I've tried locally and globally) with every element being 0. Then I add a game piece to the board.
Here is my full code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// 2D array representing the board
int gameBoard[7][7];
// Translates chess code to array index
int translate(char letter){
if ( letter == 'a' || letter == '8' ){
return 0;
} else if ( letter == 'b' || letter == '7' ){
return 1;
} else if ( letter == 'c' || letter == '6' ){
return 2;
} else if ( letter == 'd' || letter == '5' ){
return 3;
} else if ( letter == 'e' || letter == '4' ){
return 4;
} else if ( letter == 'f' || letter == '3' ){
return 5;
} else if ( letter == 'g' || letter == '2' ){
return 6;
} else if ( letter == 'h' || letter == '1' ){
return 7;
} else {
return -1;
}
}
// Displays current game board
void showBoard(){
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
cout << gameBoard[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
// Initiates game board
void initBoard(){
for (int i=0; i <= 7; i++){
for (int j=0; j <= 7; j++){
gameBoard[i][j] = 0;
}
}
}
// Adds a piece to the board
void addPiece(string square){
int x = translate(square[1]);
int y = translate(square[0]);
cout << "Adding to board: " << x << ", " << y << endl;
gameBoard[x][y] = 7;
}
int main() {
// Initialize game board with zeros
initBoard();
// Display 2d array (game board)
cout << "Blank game board:\n\n";
showBoard();
// Place first piece on board
string firstPiece = "a1";
addPiece(firstPiece);
// Display 2d array (game board)
cout << "After adding first piece:\n\n";
showBoard();
for (int i = 0; i <= 7; i++){
for (int j = 0; j <= 7; j++){
if (gameBoard[i][j] == 7){
cout << "Found a 7 at: " << i << ", " << j << endl;
}
}
}
}
I can confirm that that "translation" portion is functioning correctly. All that's going on here is translating a1 to 7 and 0 for row and column coordinates.
As you can see, even the program execution agrees that only 1 item is being added to the board. So why do I keep finding two? Any help would be much appreciated, thank you!
I have tried declaring the array (GameBoard) in many different ways and it still adds two when I add only one element. I only want one element to be added to the GameBoard.
This problem doesn't appear for every square I decide to add a piece to. Valid squares are chess board related so a1-h8.
When you declare an array with int array[7]
, the length of the array is 7, so the max index is 6.
int array[] = { 1, 2, 3, 4, 5, 6, 7 };
// index: 0, 1, 2, 3, 4, 5, 6
When you use any index greater than the max index (or less than zero), you are accessing memory that is not yours, this can cause issues such as a crash if this memory contains your code.
To fix this problem, initialize game board with the correct length. A chess board is 8x8, so you would change the line int gameBoard[7][7];
to now read int gameBoard[8][8];
. In my testing, this fixed the problem.