初学C语言,经过写完这个游戏发现与众多语言有很大差别的,虽说语法都差不多,但不能直接return数组这种让我觉得写C是一项体力活啊
这个游戏规则为呈现一个九宫格,电脑和人两个玩家对玩,只要有任意一方优先连成一条直线(3个)就算赢
算下来赢法一共就有8种,横线3条竖线3条对角线2条
以下是代码
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
bool isDone (int arr[9], char a) {
int len = 9;
int len1 = 3;
int correct = 0;
bool isWin = false;
for (unsigned int i = 0; i < len; i++) {
if (arr[i] == a) {
correct++;
}
if ((i + 1) % len1 == 0) {
if (correct == len1) {
isWin = true;
break;
} else {
correct = 0;
}
}
}
for (unsigned int i = 0; i < len1; i++) {
correct = 0;
for (unsigned int j = i; j < len; j+=len1) {
if (arr[j] == a) {
correct++;
}
}
if (correct == 3) {
isWin = true;
printf("\n Here 2");
break;
}
}
if (arr[0] == a && arr[4] == a && arr[8] == a) {
isWin = true;
} else if (arr[2] == a && arr[4] == a && arr[6] == a) {
isWin = true;
}
return isWin;
}
int showGrid (int arr[9]) {
int len = 9;
printf("\n");
for (unsigned int i = 0; i < len; i++) {
if (arr[i] != 0) {
printf(" %c ", arr[i]);
} else {
printf(" %d ", i + 1);
}
if ((i + 1) % 3 == 0) {
printf("\n");
}
}
printf("\n");
return 0;
}
int avalible (int arr[9]) {
int arrLen = 9;
int av = 0;
for (unsigned int i = 0; i < arrLen; i++) {
if (arr[i] == 0) {
av++;
}
}
return av;
}
int clear (int arr[9]) {
int len = 9;
for (unsigned int i = 0; i < len; i++) {
arr[i] = 0;
}
return 0;
}
int random1 (int min, int max) {
srand(time(NULL));
return rand()%((max+1)-min) + min;
}
int main (void) {
int grid[9] = {0};
int gridCount = sizeof(grid) / sizeof(grid[0]);
int times = 0;
int enter = 0;
int enterTimes = 0;
int av = 0;
bool done = false;
char a = 65;
char b = 68;
char restart = 0;
showGrid(grid);
printf(" %d", 1 % 3);
do {
times++;
enterTimes = 0;
enter = 0;
if (times % 2 == 0) {
enter = random1(1, 9);
while (grid[enter - 1] != 0) {
if (enter >= gridCount) {
enter = 0;
}
enter++;
}
printf("\n Computer enter the number : %d", enter);
} else {
printf("\n User enter the number : ");
do {
scanf(" %d", &enter);
++enterTimes;
if (grid[enter - 1] > 0) {
printf("\n Error: cannot enter this number. You can enter below number. \n ");
avalible(grid);
for (unsigned int i = 0; i < gridCount; i++) {
if (grid[i] == 0) {
printf(" %d ", i + 1);
}
}
printf("\n Re-enter number please \n ");
}
} while (grid[enter - 1] > 0);
}
if (times % 2 == 0) {
grid[enter - 1] = a;
} else {
grid[enter - 1] = b;
}
showGrid(grid);
av = avalible(grid);
if (av >= 1) {
if (times % 2 == 0) {
done = isDone(grid, a);
} else {
done = isDone(grid, b);
}
if (done) {
if (times % 2 == 0) {
printf("\n Computer win! ");
} else {
printf("\n User win! ");
}
printf("\n Do you wanna restart? (Y/n) ");
scanf(" %c", &restart);
while (restart != 'y' && restart != 'n') {
printf("\n Please input y or n ");
scanf(" %c", &restart);
}
if (restart == 'y') {
clear(grid);
showGrid(grid);
times = 0;
done = false;
} else if (restart == 'n') {
done = true;
}
}
} else {
printf("\n Game end! ");
printf("\n Do you wanna restart? (Y/n) ");
scanf(" %c", &restart);
while (restart != 'y' && restart != 'n') {
printf("\n Please input y or n ");
scanf(" %c", &restart);
}
if (restart == 'y') {
clear(grid);
showGrid(grid);
times = 0;
done = false;
} else if (restart == 'n') {
done = true;
}
}
} while (!done);
return 0;
}
...