c - 2D array not updating even after rewriting elements right after
问题描述
CONTEXT:
Hello, I'm trying to print a 7x6 Connect Four board where each section is |___|
with three underscores. I am trying to create each center underscore an element of a 2D array so I can later update it for player moves. I am currently trying to test updating the center underscore, or array element.
CONFLICT:
A successful test would output a selected section as |_X_|
. I tried testing updating an element inside PrintBoard()
with arr[1][1] = 'X';
however it remained as a _
and the section also remained as |___|
. I then retried this in main()
but with board[1][1] = 'X';
to no avail. I get no errors or warnings either.
CODE:
#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
*/
}
解决方案
char[7]
范围 from0
到的有效数组索引<= 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
to7
而不是0
to 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
推荐阅读
- c++ - 使用 QWebView 在本地网络中加载网页
- bash - 默认更改 bash 脚本的目录
- django - 错误:类型对象“库存”没有属性“对象”
- java - Java:参数的值不会改变
- c# - ThreadPool 或 Timers 任务未正确等待
- python - 使用 BeautifulSoup 抓取网站
- android - Android Emulator 不再连接到包服务器的问题
- localization - vert.x 是否提供任何加载本地化文件的方法?像语言环境解析器?
- jquery - 如何在循环中将类添加到 div
- c++ - 将具有数组成员的类传递给使用该数组成员的函数的重载