Reputation: 1
I'm trying to write a console C++ chess game and whenever I write into my board array at places like 07 it will put info in other places like 10.
I'm also getting buffer overruns warnings on Board[0][0] = 'C';
void Setup(char Board[7][7])
{
// sets all board spaces to a period
for (int a = 0; a < 8; ++a)
{
for (int b = 0; b < 8; ++b)
{
Board[a][b] = '.';
}
}
//place pieces
{
Board[0][0] = 'C';
Board[0][7] = 'C';
Board[7][0] = 'c';
Board[7][7] = 'c';
Board[0][1] = 'H';
Board[0][6] = 'H';
Board[7][1] = 'h';
Board[7][6] = 'h';
Board[0][2] = 'B';
Board[0][5] = 'B';
Board[7][2] = 'b';
Board[7][5] = 'b';
Board[0][3] = 'Q';
Board[0][4] = 'K';
Board[7][3] = 'k';
Board[7][4] = 'q';
for (int p = 0; p < 8; ++p)
{
Board[1][p] = 'P';
Board[6][p] = 'p';
}
}
cout << "setup complete" << endl;
}
void Display(char Board[7][7])
{
for (int a = 0; a < 8; ++a)
{
for (int b = 0; b < 8; ++b)
{
cout << Board[a][b] << " ";
}
cout << endl;
}
}
int main()
{
static char Board[7][7];
int Winner = 0;
Setup(Board);
while (Winner == 0)
{
//turn1
//turn2
Display(Board);
break;
}
//winner
return 0;
}
this outputs:
setup complete
C H B Q K B H P
P P P P P P P P
P . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . p
p p p p p p p p
p h b k q b h c
The pawns are never written into place like 07 but somehow they show up there. Honestly I have no idea what I'm doing wrong.
Upvotes: 0
Views: 257
Reputation: 4104
The problem is considering Baord
as 7x7
array. In provided code char Board[7][7]
should be char Board[8][8]
.
Keep in mind the following, while you are working with array:
[]
is size of the array. e.g. int testArray[10];
Here we defined an array of size 10
which have index from 0
to 9
[]
is index in the array. e.g. testArray[5] = 20;
Here we have changed the value of 6th element
(which is at index 5 as index starts from 0
and from 1
) to 20
.Index in array starts from 0
. So 1st element will be at index 0
, 2nd at index 1
and so on i.e. Nth element
will be at index N-1
Following is corrected code. you can see it working here:
#include <iostream>
#define ARRAY_SIZE 8
using namespace std;
void Setup(char Board[ARRAY_SIZE][ARRAY_SIZE])
{
// sets all board spaces to a period
for (int a = 0; a < ARRAY_SIZE; ++a)
{
for (int b = 0; b < ARRAY_SIZE; ++b)
{
Board[a][b] = '.';
}
}
//place peices
{
Board[0][0] = 'C';
Board[0][7] = 'C';
Board[7][0] = 'c';
Board[7][7] = 'c';
Board[0][1] = 'H';
Board[0][6] = 'H';
Board[7][1] = 'h';
Board[7][6] = 'h';
Board[0][2] = 'B';
Board[0][5] = 'B';
Board[7][2] = 'b';
Board[7][5] = 'b';
Board[0][3] = 'Q';
Board[0][4] = 'K';
Board[7][3] = 'k';
Board[7][4] = 'q';
for (int p = 0; p < ARRAY_SIZE; ++p)
{
Board[1][p] = 'P';
Board[6][p] = 'p';
}
}
cout << "setup complete" << endl;
}
void Display(char Board[ARRAY_SIZE][ARRAY_SIZE])
{
for (int a = 0; a < ARRAY_SIZE; ++a)
{
for (int b = 0; b < ARRAY_SIZE; ++b)
{
cout << Board[a][b] << " ";
}
cout << endl;
}
}
int main()
{
static char Board[ARRAY_SIZE][ARRAY_SIZE];
int Winner = 0;
Setup(Board);
while (Winner == 0)
{
//turn1
//turn2
Display(Board);
break;
}
//winner
return 0;
}
Upvotes: 2
Reputation: 389
When you create an array:
int foo[4];
the number 4 means you will have 4 int
s, which you can access using:
foo[0]
foo[1]
foo[2]
foo[3]
So when you create char Board[7][7]
with two sevens, you get a 7x7 chess board (because 0, 1, 2, 3, 4, 5 and 6 are 7 numbers in total, not 8), and therefore not a 8x8 chess board.
So just replace any Board[7][7]
by Board[8][8]
and you will be fine.
Upvotes: 2