上下文:
您好,我正在尝试打印一张7x6连接四个板,其中每个部分都是带有三个下划线的|___|
。我正在尝试创建每个中心,下划线一个二维数组的元素,这样我就可以在以后更新它以供玩家移动。我目前正在尝试更新中心下划线或数组元素。
冲突:
成功的测试会将选定的部分输出为|_X_|
。我尝试用arr[1][1] = 'X';
更新PrintBoard()
中的一个元素,但是它仍然是一个_
,该节也仍然是|___|
。然后我在main()
中重试了这一点,但是board[1][1] = 'X';
没有用。我也没有收到任何错误或警告。
代码:
#include <stdio.h>
void PrintBoard(char arr[6][7]);
int main()
{
// Declaration of 7x6 2D board array: board[row][col]
char board[6][7];
// Sets all board array elements to '_'
PrintBoard(board);
/* board[1][1] = 'X'; // NOT WORKING, ELEMENTS REMAIN AS `_` */
return 0;
}
void PrintBoard(char arr[6][7])
{
int vertCnt = 0; // Counts vertical lines (8 per row, separates sections)
int undCnt = 0; // Counts underscores (3 per section)
int rowCnt = 0; // Counts rows (6 total)
int colCnt = 0; // Count columns (7 total)
// Print game title
printf(" ~~ CONNECT FOUR ~~\n\n");
for (int rowCnt = 0; rowCnt <= 6; rowCnt++)
{
// If current row is not the first, start it on a new line
if (rowCnt > 0)
{
printf("\n");
}
// Creation of row: |___|___|___|___|___|___|___|
for (int vertCnt = 0; vertCnt < 8; vertCnt++)
{
printf("|");
// Only print `_` three times as long as there have been 7 total or less vertical lines printed
for (int undCnt = 0; undCnt < 3 && vertCnt <= 6; undCnt++)
{
// Print left and right sections as `_`
if(undCnt != 1)
{
printf("_");
}
// Assign middle section to board array and prints it as `_`
else if(undCnt == 1)
{
// If printing left underscore, increment column count
if(colCnt < 7){colCnt++;}
// Assign middle section to 2D board array
arr[rowCnt][colCnt] = '_';
printf("%c", arr[rowCnt][colCnt]);
// Test to rewrite random array element
arr[1][1] = 'X'; // NOT WORKING. ELEMENTS REMAIN AS `_`
// After last (7th) column reached, reset to 0
if(colCnt == 7){colCnt = 0;}
}
}
}
}
// Print column numbers
printf("\n 1 2 3 4 5 6 7\n\n");
/* HOW A CLEAN BOARD SHOULD LOOK:
~~ CONNECT FOUR ~~ <--- GAME TITLE
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___| <--- BOARD
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
1 2 3 4 5 6 7 <--- COLUMN NUMBERS
*/
}
发布于 2018-10-07 14:41:02
char[7]
的有效数组索引范围从0
到<= 6
。
您越界访问数组char arr[6][7]
:
for (int rowCnt = 0; rowCnt <= 6; rowCnt++)
计数到6
,其中它只应计数到5
和构造
if (colCnt < 7) { colCnt++; }
arr[rowCnt][colCnt] = '_'; // eventually arr[6][something] gets
printf("%c", arr[rowCnt][colCnt]); // written to --> corupted stack.
if (colCnt == 7) { colCnt = 0; }
有效地将colCnt
从1
连接到7
,而不是从0
连接到6
。
你把事情完全复杂化了.
#include <stddef.h>
#include <stdio.h>
enum { board_width = 7, board_height = 6 };
void board_print(char arr[board_height][board_width]);
int main(void)
{
char board[board_height][board_width] = { 0 };
board_print(board);
board[1][1] = 'X';
board_print(board);
}
void board_print(char arr[board_height][board_width])
{
puts(" ~~ CONNECT FOUR ~~\n");
for (size_t row = 0; row < board_height; ++row) {
for (size_t col = 0; col < board_width; ++col) {
printf("|_%c_", arr[row][col] ? arr[row][col] : '_');
}
puts("|");
}
puts(" 1 2 3 4 5 6 7\n");
}
输出:
~~ CONNECT FOUR ~~
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
1 2 3 4 5 6 7
~~ CONNECT FOUR ~~
|___|___|___|___|___|___|___|
|___|_X_|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
1 2 3 4 5 6 7
https://stackoverflow.com/questions/52685970
复制相似问题