我现在出现的问题是可以出现游戏,但是按wasd无法移动,请问应该怎么办?我觉得part1应该是对的,主要是part2里面出现问题。希望能帮我改1下。图片是part2让我们做的和一上来给的东西,那个不能变。
/////////////////////////////////////////////////////////////////////
//////////////////////////// Part 1 /////////////////////////////////
/////////////////////////////////////////////////////////////////////
// You do not need to edit the main function to complete this section.
// You only need to edit the code below.
// You need to malloc some memory (hint: most likely requiring multiple
// malloc's), set all values in the board to be default to NULL, then
// return a pointer to the board.
Board newBoard() {
Board board = (Board)malloc(sizeof(char)*(SIZE*SIZE*6*2));
int i,j;
for (i = 0; i < SIZE; i++) {
board[i] = (char **)malloc(sizeof(char) * SIZE * 6*2); // ATTETION! need to malloc twice
for (j = 0; j < SIZE; j++) {
board[i][j] = NULL;
}
}
return board; // not ideal
}
// The pointer to your board needs to be free'd. Before you free "board"
// you also need to make sure you free any other memory you've malloced
// that are contained within board
void freeBoard(Board board) {
int i,j;
for (i = 0; i < SIZE; i++) { // need to free twice
free(board[i]);
}
free(board);
}
// The second argument passed into the program is the "input string". For
// more information on this please see the assignment specification.
int populateBoard(char ***board, int argc, char* argv[]) {
// check input standard
double sq = sqrt((double)argc);
if (sq != floor(sq)) {
return FALSE;
}
size = (int)sq;
int slen = 0;
while (slen < argc) {
if (strcmp("0",argv[slen]) <= 0 && strcmp("9",argv[slen]) >= 0) {
slen++;
} else if (strcmp("A",argv[slen]) == 0) {
slen++;
} else if (strcmp("B",argv[slen]) == 0) {
slen++;
} else {
break;
}
}
if (argc != slen) {
printf("Please enter a valid populate string.\n");
return FALSE;
}
// board assignment
int i,j,k;
char *inputData[12] = {"0","1","2","3","4","5","6","7","8","9","A","B"};
char *hexData[12] = {NULL,"0x2","0x4","0x8","0x10","0x20","0x40","0x80","0x100","0x200","0x400","0x800"};
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
for (k = 0; k < 12; k++) {
if(strcmp(inputData[k],argv[i*size + j]) == 0) {
board[i][j] = hexData[k];
break;
}
}
}
}
return TRUE;
}
// THIS FUNCTION IS ALREADY COMPLETE, IT IS HERE PURELY FOR DEMONSTRATION
// The second argument passed into the program is the "seed" for the
// random number generator (that you don't have to worry about)
int getSeed(int argc, char* argv[]) {
int ret = 0;
if (argc == 3) {
ret = atoi(argv[2]);
}
return ret;
}
/////////////////////////////////////////////////////////////////////
//////////////////////////// Part 2 /////////////////////////////////
/////////////////////////////////////////////////////////////////////
// You do not need to edit the main function to complete this section.
// You only need to edit the code below.
// You are welcome to write/define more functions of your own and include
// them in this section. In fact - we encourage you to write more of your
// own functions down here.
// Function moves all of the cells to the very bottom
// of the board. During the process any two cells that are
// adjacent in the vertical direction should be merged.
// The function returns TRUE if ANY cells were moved, and FALSE if no
// cells were moved
int moveUp(Board board) {
int flag = weatherCanMove(board,'w'); // check weather can move
if (flag) {
int i,j,pos;
for (j = 0; j < size; j++) { // move
pos = 0;
for (i = 0; i < size; i++) {
if (pos == i && board[i][j] != NULL) {
pos++;
} else if (pos != i && board[i][j] != NULL) {
board[pos][j] = board[i][j];
board[i][j] = NULL;
pos++;
}
}
}
merge(board,'w'); // merge
return TRUE;
}
return FALSE;
}
// Function moves all of the cells to the very bottom
// of the board. During the process any two cells that are
// adjacent in the vertical direction should be merged.
// The function returns TRUE if ANY cells were moved, and FALSE if no
// cells were moved
int moveDown(Board board) {
int flag = weatherCanMove(board,'s');
if (flag) {
int i,j,pos;
for (j = 0; j < size; j++) {
pos = size - 1;
for (i = size - 1; i >= 0; i--) {
if (pos == i && board[i][j] != NULL) {
pos--;
} else if (pos != i && board[i][j] != NULL) {
board[pos][j] = board[i][j];
board[i][j] = NULL;
pos--;
}
}
}
merge(board,'s');
return TRUE;
}
return FALSE;
}
// Function moves all of the cells to the very left hand
// side of the board. During the process any two cells that are
// adjacent in the horizontal direction should be merged.
// The function returns TRUE if ANY cells were moved, and FALSE if no
// cells were moved
int moveLeft(Board board) {
int flag = weatherCanMove(board,'a');
if (flag) {
int i,j,pos;
for (i = 0; i < size; i++) {
pos = 0;
for (j = 0; j < size; j++) {
if (pos == j && board[i][j] != NULL) {
pos++;
} else if (pos != j && board[i][j] != NULL) {
board[i][pos] = board[i][j];
board[i][j] = NULL;
pos++;
}
}
}
merge(board,'a');
return TRUE;
}
return FALSE;
}
// Function moves all of the cells to the very right hand
// side of the board. During the process any two cells that are
// adjacent in the horizontal direction should be merged.
// The function returns TRUE if ANY cells were moved, and FALSE if no
// cells were moved
int moveRight(Board board) {
int flag = weatherCanMove(board,'d');
if (flag) {
// ���ƶ�,˫ָ��yyds
int i,j,pos;
for (i = 0; i < size; i++) {
pos = size - 1;
for (j = size - 1; j >= 0; j--) {
if (pos == j && board[i][j] != NULL) {
pos--;
} else if (pos != j && board[i][j] != NULL) {
board[i][pos] = board[i][j];
board[i][j] = NULL;
pos--;
}
}
}
merge(board,'d');
return TRUE;
}
return FALSE;
}
// The following are the functions I added
// Funtion: check whether the "number" can be moved
// For different directions
// check whether there is an empty position in the middle or whether it can be merged
int weatherCanMove(Board board, char direction) {
int i,j;
if (direction == 'a') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[i][j] == NULL && board[i][j+1] != NULL) {
return TRUE;
} else if (board[i][j] != NULL && board[i][j+1] != NULL && strcmp(board[i][j+1],board[i][j]) == 0) {
return TRUE;
}
}
}
} else if (direction == 'd') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[i][j] != NULL && board[i][j+1] == NULL) {
return TRUE;
} else if (board[i][j] != NULL && board[i][j+1] != NULL && strcmp(board[i][j+1],board[i][j]) == 0) {
return TRUE;
}
}
}
} else if (direction == 'w') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[j][i] == NULL && board[j+1][i] != NULL) {
return TRUE;
} else if (board[j][i] != NULL && board[j+1][i] != NULL && strcmp(board[j][i],board[j+1][i]) == 0) {
return TRUE;
}
}
}
} else if (direction == 's') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[j][i] != NULL && board[j+1][i] == NULL) {
return TRUE;
} else if (board[j][i] != NULL && board[j+1][i] != NULL && strcmp(board[j][i],board[j+1][i]) == 0) {
return TRUE;
}
}
}
}
return FALSE;
}
// Function: merge in the specified direction
int merge(Board board, char direction) {
int flag = FALSE;
int i,j,k;
if (direction == 'a') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[i][j] != NULL && board[i][j+1] != NULL && strcmp(board[i][j+1],board[i][j]) == 0) {
board[i][j] = add(board[i][j]);
flag = TRUE;
for (k = j + 1; k < size - 1; k++) {
board[i][k] = board[i][k+1];
}
board[i][size - 1] = NULL;
}
}
}
} else if (direction == 'd') {
for (i = 0; i < size; i++) {
for (j = size - 1; j >0; j--) {
if (board[i][j] != NULL && board[i][j-1] != NULL && strcmp(board[i][j-1],board[i][j]) == 0) {
board[i][j] = add(board[i][j]);
flag = TRUE;
for (k = j - 1; k > 0; k--) {
board[i][k] = board[i][k-1];
}
board[i][0] = NULL;
}
}
}
} else if (direction == 'w') {
for (i = 0; i < size; i++) {
for (j = 0; j < size - 1; j++) {
if (board[j][i] != NULL && board[j+1][i] != NULL && strcmp(board[j][i],board[j+1][i]) == 0) {
board[j][i] = add(board[j][i]);
flag = TRUE;
for (k = j + 1; k < size - 1; k++) {
board[k][i] = board[k+1][i];
}
board[size - 1][j] = NULL;
}
}
}
} else if (direction == 's') {
for (i = 0; i < size; i++) {
for (j = size - 1; j > 0; j--) {
if (board[j][i] != NULL && board[j-1][i] != NULL && strcmp(board[j][i],board[j-1][i]) == 0) {
board[j][i] = add(board[j][i]);
flag = TRUE;
for (k = j-1; k > 0; k--) {
board[k][i] = board[k-1][i];
}
board[0][j] = NULL;
}
}
}
}
return flag;
}
// Funtion: calculate the combined value
char *add(char *str) {
char *data[11] = {"0x2","0x4","0x8","0x10","0x20","0x40","0x80","0x100","0x200","0x400","0x800"};
int i = 0;
for (i = 0; i < 10; i++) {
if (strcmp(str,data[i]) == 0) {
return data[i+1];
}
}
return NULL;
}
但是按wasd无法移动
设一个断点在读键盘输入的地方
0
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。