/* 主程序,控制游戏流程。 */ int main (int argc, char * argv[]) { int put; int in_x,in_y; int judge=0; /*显示欢迎语句,清空并显示棋盘。*/ printf ("Welcome to play!\n"); board_clear(); board_display(); while (1)//主循环,由内部break语句控制退出。或许有更好的处理方式。 { /*用户输入落子坐标*/ scanf("%d",&in_x); scanf("%d",&in_y); printf("your input number is x=%d, y=%d\n",in_x,in_y); /*调用落子函数,由用户落下一子*/ //这里,落子函数接受的有效坐标为0~2,为了照顾人类习惯,人类输入的坐标有效值为1~3,在此转换 board_put(in_x-1,in_y-1,USER); /*用户落子后显示棋盘*/ board_display(); /*判断是否取胜*/ judge=board_judge(); if (judge!=0)//如果取胜,打印并退出 { printf("User %d win!\n",judge); break; } /*有AI计算落子位置*/ put=ai_think(&in_x,&in_y); printf("AI put: x=%d,y=%d\n",in_x,in_y); /*调用落子函数,由AI落下一子*/ board_put(in_x,in_y,AI); /*AI落子后显示棋盘*/ board_display(); /*判断是否取胜*/ judge=board_judge(); if (judge!=0)//如果取胜,打印并退出 { printf("User %d win!\n",judge); break; } } return 0; }
int main(int argc, char **argv) { // Randomise random unmber generator initial state. srandom(time(0)); int board_size = 4; int **board = board_create(board_size); if (!board) { printf("failed to create board.\n"); exit(0); } board_spawn_tile(board_size, board); board_spawn_tile(board_size, board); int tile = 0; while (1) { board_display(board_size, board); printf("Next move? "); fflush(stdout); int action = read_input(); switch (action) { case GO_LEFT: tilt_board_left(board_size, board); break; case GO_RIGHT: tilt_board_right(board_size, board); break; case GO_UP: tilt_board_up(board_size, board); break; case GO_DOWN: tilt_board_down(board_size, board); break; case GO_NOWHERE: break; case INVALID_INPUT: default: printf("Invalid input. Type l, r, u or d to tilt board.\n"); break; } if (action == INVALID_INPUT) { printf("Invalid input. Type l, r, u or d to tilt board.\n"); } else { tile = board_spawn_tile(board_size, board); } if (tile == 1) { break; } } return 0; }