gpt4 book ai didi

c - 15 拼图程序,卡在上下命令上

转载 作者:行者123 更新时间:2023-11-30 16:38:00 25 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main (void){

//Function used to set up your game board
int disp[4][4];
int i, j;
int game_over = 0;
for(i = 0; i < 4; i++) {
for(j = 0;j < 4; j++) {
printf("Enter a value from 1 - 15 for %d %d: ", i, j);
scanf("%d", &disp[i][j]);
}
}
//Function used to display the game board
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf(" _ ");
else if (disp[i][j] < 10)
printf(" %d ",disp[i][j]);
else
printf("%d ",disp[i][j]);
}
}
// Function to show where cursor is
for(i = 0; i < 4; i++){
printf("\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf("");
}
}
//Skeleton to function to controller
do{
char move[10];
printf("Enter a move: ");
scanf("%s", move);

if (strcmp("up", move) == 0){
for(i = 0; i < 4; i++){
printf("");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i >= 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Up", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("UP", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}



else if (strcmp("down", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Down", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("DOWN", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}

else if (strcmp("left", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (j >= 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Left", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("LEFT", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}

else if (strcmp("right", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (j <= 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Right", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("RIGHT", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}

else
printf("Invalid Move!");

//Update the board function
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf(" _ ");
else if (disp[i][j] < 10)
printf(" %d ",disp[i][j]);
else
printf("%d ",disp[i][j]);
}
}
}
while (game_over == 0);

return 0;
}

我在使光标向右或向下移动时遇到问题?我可以向左和向上走,但其他两个不行。我并不是在寻找人来回答整个程序,只是我已经在这个问题上坚持了几个小时,而且我看到的所有其他代码都与我的类似。 I can get it to correctly do up and left but right and down cause a problem and not sure why.

最佳答案

错误是检查逻辑错误,并且您遵循的不良做法是大量重复代码。

好吧,我已经将代码简化为简单的东西。

    ...
else if (strcmp("down", move) == 0 || strcmp("Down", move) == 0 || strcmp("DOWN", move) == 0){
int d = 0;
for(i = 0; i < 4; i++){

for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i+1 < 4){ // <--- notice the change
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
d = 1;
break; // <-- and this
}
else {
printf("%s","Invalid move!");
}
}
if( d == 1)
break;
}
}
else if (strcmp("right", move) == 0 || strcmp("Right", move) == 0 || strcmp("RIGHT", move) == 0){
for(i = 0; i < 4; i++){

for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if ( j+1 < 4){ // <--- notice the change
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
break; // <-- and this
}
else {
printf("%s","Invalid move!");
}
}

}
}
...

您可以检查在 downright 情况下是否存在 break 语句。原因是,当您向右或向下交换 -1 时,您将在循环迭代中再次相遇。

因此,如果没有break,结果将是

_ 1 2 3
4 5 6 7
8 9 1 2
3 4 5 6

>>> right

1 2 3 _
4 5 6 7
8 9 1 2
3 4 5 6

另一件事是不要重复代码。当你这样做时,可读性会更差。维护代码更加困难。更好的是,您可以编写 swap() 逻辑并使用它。而且瓷砖的移动也可以通过另一个函数来完成。

<小时/>

棘手的下行逻辑(Edit-1)。

在 downlogic 的情况下,简单地分解循环将不起作用,因为如果你这样做,那么在下一次迭代中它将有机会再次获得 -1 并尝试将其与下一个交换数量。

关于c - 15 拼图程序,卡在上下命令上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47645402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com