test.c
#include "game.h"
void menu() {
printf("*************************************\n");
printf("****** 1:开始游戏 0.退出游戏 ******\n");
printf("*************************************\n");
}
void game(){
char ret = 0;
char board[ROW][COL] = { 0 };
initBorad(board,ROW,COL);
displayBoard(board,ROW,COL);
while (1) {
displerMove(board,ROW,COL);
ret = isWin(board, ROW, COL);
if (ret != 'C') {
break;
}
displayBoard(board,ROW,COL);
printf("\n");
ComputerMove(board, ROW, COL);
ret = isWin(board, ROW, COL);
if (ret != 'C') {
break;
}
displayBoard(board, ROW, COL);
printf("\n");
}
if (ret == '*') {
printf("玩家赢了\n");
}
else if(ret == '#') {
printf("电脑赢了\n");
}
else{
printf("平局了\n");
}
displayBoard(board, ROW, COL);
}
int main() {
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请选择:");
scanf_s("%d",&input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
game();
break;
default:
printf("输入有误,请重新选择:");
break;
}
} while (input);
return 0;
}
game.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
void initBorad(char board[ROW][COL], int row, int col);
void displayBoard(char board[ROW][COL], int row, int col);
void displerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char isWin(char board[ROW][COL], int row, int col);
game.c
#include "game.h"
void initBorad(char board[ROW][COL],int row,int col) {
int i,j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
board[i][j] = ' ';
}
}
}
void displayBoard(char board[ROW][COL], int row, int col) {
int i;
for (i = 0; i < row; i++) {
int j;
for (j = 0; j < col; j++) {
printf(" %c ",board[i][j]);
if(j<col-1)
printf("|");
}
printf("\n");
if (i < row - 1) {
int j;
for (j = 0; j < col; j++) {
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void displerMove(char board[ROW][COL], int row, int col) {
printf("玩家下棋>\n");
printf("请输入坐标:>");
int x = 0, y = 0;
while (1) {
scanf_s("%d %d",&x,&y);
if (x > 0 && x <= row && y > 0 && y <= col) {
if (board[x - 1][y - 1] == ' ') {
board[x - 1][y - 1] = '*';
break;
}
else {
printf("该位置已经有棋,请重新输入其他坐标:>");
}
}
else {
printf("输入位置非法,请重新输入坐标:>");
}
}
}
void ComputerMove(char board[ROW][COL],int row,int col) {
printf("电脑下棋>\n");
int x = 0, y = 0;
while (1) {
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
char isWin(char board[ROW][COL], int row, int col) {
int i;
for (i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ') {
return board[i][0];
}
}
for (i = 0; i < col; i++) {
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ') {
return board[0][i];
}
}
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')) {
return board[1][1];
}
int j = 0;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++){
if (board[i][j] == ' ') {
return 'C';
}
}
}
return 'Q';
}
运行效果