Я сделал эту программу на C, где объект R
размещен на сетке, и он должен двигаться, принимая данные с клавиатуры. Например, вот что происходит, если вы нажмете N.
0 1 2
0 - - - R - - - - -
1 R - - PRESS N -> GO UP -> - - - PRESS N AGAIN -> - - -
2 - - - - - - R - -
Так что R заставляет его идти вверх. Объект должен двигаться вокруг , поэтому, например, когда он находится в точке [A0] [B0], ему нужно спуститься полностью вниз [A2] [B0]. Смотри выше.
Он будет двигаться вверх, вниз, влево и вправо.
Сейчас я создаю функцию, которая заставит ее двигаться вверх , но у меня много проблем: иногда она случайно зависает до 2: 0 и 0: 0 без перехода вверх, и когда он равен A = 2, вместо того, чтобы подняться на 1, он становится равным 0, хотя я установил его на 2-1 (чтобы подняться, он должен вычесть 1).
Я не понимаю, что вызывает эти проблемы, какой-либо совет?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define X 3
#define Y 3
struct coords{
int a;
int b;
};
typedef struct coords cord;
// Print the array
char printArray(char row[][Y], size_t one, size_t two, struct coords cord)
{
row[cord.a][cord.b] = 'X';
// output column heads
printf("%s", " [0] [1] [2]");
// output the row in tabular format
for (size_t i = 0; i < one; ++i) {
printf("\nrow[%lu] ", i);
for (size_t j = 0; j < two; ++j) {
printf("%-5c", row[i][j]);
}
}
}
int moveUp(struct coords * cord);
int main(void)
{
struct coords cord;
char row[X][Y] =
{ { '-', '-', '-'},
{ '-', '-', '-'},
{ '-', '-', '-'} };
srand(time(NULL));
cord.a = (rand() % 3);
cord.b = (rand() % 3);
printf("\nValori rand: A %d, B %d\n", cord.a, cord.b);
// output the row
//printf("\nrobot:%c\n", robot);
puts("The array is:");
printf("\n");
printArray(row, X, Y, cord);
row[cord.a][cord.b] = '-';
//printArray(row, X, Y, &m, &n);
char h;
while(h != '3'){
switch (h) {
case 'N':
moveUp(&cord);
printArray(row, X, Y, cord);
row[cord.a][cord.b] = '-';
break;
}
scanf("%s", &h);
}
printf("\n");
}
int moveUp(struct coords * cord)
{
cord->a - 1;
if (cord->a == 2){
cord->a - 1;
} else if (cord->a == 1){
cord->a - 1;
} else if (cord->a == 0){
cord->a + 2;
}
/*
if (cord->a == 0) {
cord-> a = 2;
} else {
cord->a - 1;
}
*/
printf("\n A = %d, B = %d\n", cord->a, cord->b);
}