代码里面有一个小bug,看大家能不能发现
text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//三字棋游戏
#include <stdio.h>
void menu()
{
printf("************************************\n");
printf("****** 1.Play 0.Exit ******\n");
printf("************************************\n");
}
void game()
{
int ret = 0;
char board[ROW][COL] = {0};//数组存放棋盘数据
InitBoard(board, ROW, COL);//初始化棋盘
DisplayBoard(board, ROW, COL);//显示棋盘
while (1)
{
Playermove(board, ROW, COL);//玩家下棋
//判断玩家是否能赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
Computermove(board, ROW, COL);//电脑下棋
//判断电脑是否能赢
ret = Iswin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢\n");
}
else if (ret == '#')
{
printf("电脑赢\n");
}
else
{
printf("平局\n");
}
}
int test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
return 1;
break;
case 0:
printf("退出游戏\n");
return 0;
break;
default:
printf("输入错误,请重新输入\n");
}
} while (input);
}
int main()
{
if (1 == test())
game();
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
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 = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);//打印一行的数据
if(j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
void Playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("玩家走:>\n");
printf("请输入坐标:>x y\n");
scanf("%d%d", &x, &y);
//判断坐标是否合理
if (x < row + 2 && y < col + 2)
{
board[x - 1][y - 1] = '*';
DisplayBoard(board, ROW, COL);
break;
}
else
printf("坐标错误,请重新输入");
}
}
void Computermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑走:>\n");
while (1)
{
x = rand() % ROW;
y = rand() % COL;
if (board[x][y] == ' ')
{
board[x][y] = '#';
DisplayBoard(board, ROW, COL);
break;
}
}
}
int Isfull(char board[ROW][COL],int row,int col)//返回表示棋盘满了,返回0表示没满
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;//满了
}
char Iswin(char board[ROW][COL], int row, int col)
{
int i = 0;
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 < row; 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] != ' ')
return board[1][1];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][1];
if (1 == Isfull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
void InitBoard(char board[ROW][COL], int row,int col);
void DisplayBoard(char board[ROW][COL],int row,int col);
void Playermove(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);
标签:int,三字棋,小游戏,board,printf,row,COL,ROW
From: https://blog.51cto.com/u_15906483/6159487