Привет, у меня много проблем, пытаясь заставить эту функцию работать, я попытался создать массив внутри функции, который принимает значения массива основного указателя и редактирует значения перед обновлением массива указателей, вводя временный массив обратно в указатель.
Моя попытка получить массив для правильной передачи не сработала, так как я получаю действительно странно большие или маленькие значения, когда на плате должны быть только 1 или 0.
Оператор печати помещается для проверки правильности ввода массива. Я считаю, что остальная часть моего кода должна быть правильной.
void updateBoard(int* board, int boardRowSize, int boardColSize) {
int i,j,sum,k;
k = boardRowSize * boardColSize;
int array[k];
for(i = 0; i < boardRowSize; i++){
for(j = 0; j < boardColSize; j++){
array[(i * boardColSize) + j] = board[(i * boardColSize) + j];
printf("%d",array[i * boardColSize + j]);
}
printf("\n");
}
for(i = 0; i < boardRowSize; i++){
for(j = 0; j < boardColSize; j++){
sum = 0;
sum = countLiveNeighbor(board,boardRowSize,boardColSize,i,j);
if(board[i * boardColSize + j] = 1){
if(sum == 3 || sum == 2){
array[(i * boardColSize) + j ] = 1;
}else{
array[(i * boardColSize) + j ] = 0;
}
}else if (board[(i * boardColSize) + j] = 0){
if(sum == 3){
array[(i * boardColSize) + j] = 1;
}else{
array[(i * boardColSize) + j] = 0;
}
}
}
}
for(i = 0; i < boardRowSize; i++){
for(j = 0; j < boardColSize; j++){
board[i * boardColSize + j] = array[i * boardColSize + j];
printf("%d",board[i * boardColSize + j]);
}
}
}
следует предположить, что функция countLiveNeighbor () работает как задумано, считая правильное количество живых соседей.
это то, как выглядит массив
00000000000000000000000000000000000
00000000000000000000000000000000000
00000000000000000000000000000000000
00000000000000000000001000000000000
00000000000000000000010100000000000
00000000000000000000111110000000000
00000000000000000000000000000000000
00000000000000000000100010000000000
00000000000000000001100011000000000
00000000000000000010100010100000000
00000000000000000001100011000000000
00000000000000000000100010000000000
00000000000000000000000000000000000
00000000000000000000111110000000000
00000000000000000000010100000000000
00000000000000000000001000000000000
00000000000000000000000000000000000
00000000000000000000000000000000000
Функция CountLiveNeighbor
int countLiveNeighbor(int* board, int boardRowSize, int boardColSize, int row, int col){
int count = 0;
if((row - 1) >= 0 && board[ (row -1) * boardColSize + (col)] == 1){
count++;
}if((row + 1) < boardRowSize && board[ (row + 1) * boardColSize + (col)] == 1){
count++;
}if((col - 1) >= 0 && board[ (row) * boardColSize + (col - 1)] == 1){
count++;
}if((col - 1) < boardColSize && board[ (row) * boardColSize + (col + 1)] == 1){
count++;
}if((row - 1) >= 0 && (col - 1) >= 0 && board[ (row -1) * boardColSize + (col - 1)] == 1){
count++;
}if((row + 1) < boardRowSize && (col - 1) >= 0 && board[(row + 1) * boardColSize + (col - 1)] == 1){
count++;
}if((row + 1) < boardRowSize && (col + 1) < boardColSize && board[(row + 1) * boardColSize + (col + 1)] == 1){
count++;
}if((row - 1) >= 0 && (col + 1) < boardColSize && board[(row - 1) * boardColSize + (col + 1)] == 1){
count++;
}
return count;}
предоставляется функция, используемая для тестирования этой программы:
#include <stdlib.h>
#include <stdio.h>
#include "updateBoard.h"#include "updateBoardGold.h"
int main(){
int row,col,step;
step = 0;
row = 18;
col = 35;
int i,j;
int game_board_stu[18*35] ={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,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,1,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,1,0,1,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,1,1,1,1,1,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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int stuCount,goldCount;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
stuCount = countLiveNeighbor(game_board_stu, row, col, i, j);
goldCount = countLiveNeighbor_gold(game_board_gold,row,col,i,j);
if(stuCount != goldCount){
printf("countLiveNeighbor test failed\n");
return 0;
}
}
}
printf("countLiveNeighbor test passed\n");
step = 0;
while(!aliveStable_gold(game_board_gold, row, col)){
if(step > 10000){
printf("updateBoard test passed\n");
printf("aliveStable test not finished due to too many steps taken\n");
return 0;
}
if(aliveStable(game_board_stu,row,col)){
printf("aliveStable test failed\n");
return 0;
}
updateBoard_gold(game_board_gold,row,col);
updateBoard(game_board_stu,row,col);
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(game_board_stu[i*col+j]!= game_board_gold[i*col+j]){
printf("updateBoard test failed\n");
return 0;
}
}
}
}
if(!aliveStable(game_board_stu,row,col)){
printf("aliveStable test failed\n");
return 0;
}
printf("updateBoard test passed\n");
printf("aliveStable test passed\n");
return 0;
}
Я думаю, что вам нужно инициализировать переменную массива и заменить «==» на «=», как уже упоминалось.
Вот ваш код, который я пытался редактировать. Вы можете редактировать его дальше в соответствии с вашими потребностями.
void updateBoard(int* board, int boardRowSize, int boardColSize) {
int i, j, sum, k;
k = boardRowSize * boardColSize;
int array[20];
memset(array, 0, k*sizeof(int));
for (i = 0; i < boardRowSize; i++) {
for (j = 0; j < boardColSize; j++) {
array[(i * boardColSize) + j] == board[(i * boardColSize) + j];
printf("%d", array[i * boardColSize + j]);
}
printf("\n");
}
for (i = 0; i < boardRowSize; i++) {
for (j = 0; j < boardColSize; j++) {
sum = 0;
sum = countLiveNeighbor(board, boardRowSize, boardColSize, i, j);
if (board[i * boardColSize + j] == 1) {
if (sum == 3 || sum == 2) {
array[(i * boardColSize) + j] = 1;
}
else {
array[(i * boardColSize) + j] = 0;
}
}
else if (board[(i * boardColSize) + j] == 0) {
if (sum == 3) {
array[(i * boardColSize) + j] = 1;
}
else {
array[(i * boardColSize) + j] = 0;
}
}
}
}
for (i = 0; i < boardRowSize; i++) {
for (j = 0; j < boardColSize; j++) {
board[i * boardColSize + j] = array[i * boardColSize + j];
printf("%d", board[i * boardColSize + j]);
}
}
Других решений пока нет …