現在有21根火柴,2人輪流取,每人每次可以取走1~4根,不可多取,也不能不取,誰取最後一根火柴誰輸。編寫一個程序進行人機對弈,要求人先取,計算機後取;計算機一方爲 常勝將軍。
int main(int argc, const char *argv[]) {
int computer, people, spare = 21;
printf("-------------------------------\n");
printf("---------------begin----------------\n");
printf("-------------------------------\n");
while (1) {
printf("---------------- 目前還有火柴%d根--------------\n", spare);
printf("People:");
scanf("%d", &people);
if (people < 1 || people > 4 || people > spare) {
printf("輸入違規,取得數目有問題!\n\n");
continue;
}
spare -= people;
if (spare == 0) {
printf("computer win !Game Over!");
break;
}
computer = 5 - people;
spare -= computer;
printf("Computer: %d \n", computer);
if (spare == 0) {
printf("\nPeople win!Game Over!\n");
break;
}
}
return 0;
}