C中的蛙跳游戏

时间:2014-10-21 02:30:55

标签: c

我正在制作青蛙游戏。中间有一个Lilly垫,每侧有三只青蛙。游戏的目标是让青蛙尽可能地移动。青蛙只能朝着它面向的方向跳跃,它一次只能跳过一只青蛙到达礼来垫。

这应该是看起来像开始游戏

(1:>(2:>(3:> ____<:4)<:5)<:6)

示例移动:

移动青蛙4

(1:>(2:>(3:><:4)____<:5)<:6)

我的问题是我无法弄清楚如何实现一个makeMove()方法,该方法根据用户想要移动的青蛙数量修改青蛙数组列表。

这是我的一部分:

int frogToJump; //The frog the user wishes to jump
const char* string[7] ; //An array for storing the frogs

//The array of frogs
void frogString(){
string[0]= "\n(1:>";
string[1]= "(2:>";
string[2]= "(3:>";
string[3]= " ___ ";
string[4]= "<4:)";
string[5]= "<5:)";
string[6]= "<6:)";


//Prints the array of frogs
for(int i= 0; i< 7; i++){
    printf("%s",string[i]);
 }


//Gets user input for the frog they want to move

printf("\n""\n" "Move Frog: ");
scanf("%d", &frogToJump);

}

2 个答案:

答案 0 :(得分:0)

要解决您的问题,您可以使用两个数组执行此操作:一个包含字符串指针并且您可以交换,一个数组包含位置青蛙。

所以要向前移动一只青蛙(除非它是最左边的青蛙)你先交换字符串指针数组中的指针,然后更新位置数组中的位置。

答案 1 :(得分:0)

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

char history[24][8];//24 : 3*4*2
const char *init_state = ">>> <<<";
const char *end_state  = "<<< >>>";

void print_history(int n){
    int i;
    for(i=0; i <= n; ++i)
        puts(history[i]);
}

void next_state(int i){
    int loc;
    if(strcmp(history[i], end_state)==0){
        print_history(i);
        exit(0);
    }
    for(loc=0; loc < 8; ++loc){
        if(history[i][loc] == '>'){
            if(loc + 2 < 8 && history[i][loc+2] == ' '){
                strcpy(history[i+1], history[i]);
                history[i+1][loc+2] = '>';
                history[i+1][loc] = ' ';
                next_state(i+1);
            }
            if(loc + 1 < 8 && history[i][loc+1] == ' '){
                strcpy(history[i+1], history[i]);
                history[i+1][loc+1] = '>';
                history[i+1][loc] = ' ';
                next_state(i+1);
            }
        } else if(history[i][loc] == '<'){
            if(loc - 2 >= 0 && history[i][loc-2] == ' '){
                strcpy(history[i+1], history[i]);
                history[i+1][loc-2] = '<';
                history[i+1][loc] = ' ';
                next_state(i+1);
            }
            if(loc - 1 >= 0 && history[i][loc-1] == ' '){
                strcpy(history[i+1], history[i]);
                history[i+1][loc-1] = '<';
                history[i+1][loc] = ' ';
                next_state(i+1);
            }
        }
    }
}

int main(void){
    strcpy(history[0], init_state);
    next_state(0);
    return 0;
}