test.c
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include "game.h"
void menu()//游戏菜单
{
printf("*******************\n");
printf("*****1.进入游戏*****\n");
printf("*****0.退出游戏*****\n");
printf("*******************\n");
}
void game()
{
//存储数据-二维数组
char board[ROW][COL];
//初始化棋盘-初始化成空格
IitBoard(board, ROW, COL);
//打印棋盘-本质打印数组的内容
DisplayBoard(board, ROW, COL);
//玩家走-电脑走
char ret = 0;//接受游戏状态
while (1)
{
//玩家走
playermove(board, ROW, COL);
//打印玩家的
DisplayBoard(board, ROW, COL);
//判断玩家输赢
ret = Iswin(board, ROW, COL);
if (ret != 'c')
{
break;
}
//电脑走
Computermove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = Iswin(board, ROW, COL);
//判断电脑输赢
if (ret != 'c')
{
break;
}
}
if (ret == '*')
printf("玩家赢了\n");
else if (ret == '#')
printf("电脑赢了\n");
else
printf("平局\n");
//DisplayBoard(board, ROW, COL);打印最后的棋盘
}
void test()
{
int input = 0;//放在外面定义
do
{
menu();//循环里面都可以进入菜单
printf("请选择:");
scanf("%d", &input);
//if语句不加break;加了会退出循环
/*if (input == 1)
{
printf("进入游戏\n");
}
else if (input == 0)
{
printf("退出游戏\n");
}
else
{
printf("选择错误,重新选择\n");
}*/
switch(input)
{
case 1:
//printf("进入游戏\n");
game();//游戏函数必须放在void test()前边否则会重复定义
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);//0则假停止循环,非0继续循环
}
int main()
{
srand((unsigned int)time(NULL));//随机数
//menu();//进入游戏菜单
test();//玩家输入
return 0;
}
头文件
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
//头文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//函数的定义
#define ROW 3
#define COL 3
//函数的声明
//初始化棋盘
void IitBoard(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);
//判断输赢
//玩家赢——'*'
//电脑赢--'#'
//平局————‘Q’
//游戏继续--'c'
//判断游戏输赢
char Iswin(char board[ROW][COL], int row, int col);
实施文件game.c
#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)
#include"game.h"
//初始化棋盘-空格
void IitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;//电脑行坐标初始为0
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
//只适用于3*3
//void DisplayBoard(char board[ROW][COL], int row, int col)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// if(i < row - 1)
// printf("---|---|---\n");
// }
//}
//棋盘的打印n*m
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]);//必须要board[i][j],类似%d
//最后一项没有|
if(j<col-1)
printf("|");
}
printf("\n");//一行打完换行
if (i < row - 1)//为了最后一行不打印---|
{
for (j = 0; j < col; j++)
{
printf("---");//打印每一列的每一项,不加board[i][j]不是%啥啥的
//最后一列没有|
if (j < col - 1)
printf("|");//必须要有。|是两个小树杠
}
printf("\n");
}
}
}
//玩家走
void playermove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家下棋\n");
while (1)
{
printf("请输入坐标:");
scanf("%d %d", &x, &y);
//判断坐标合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';// "="
break;
}
else
{
printf("坐标被占用,请重新输入\n");
}
}
else
{
printf("坐标非法请重新输入\n");
}
}
}
//电脑下棋
void Computermove(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;//生成范围就是合法的
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
//判断还有没有空格返回到下面ret
Isfull(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++)
{
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][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
//判断三列
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
return board[1][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];
}
//判断平局
//如果棋盘满了返回1,没满返回0
int ret = Isfull(board, row, col);//小写是因为还在char函数里面
if (ret == 1)
{
return 'Q';
}
//继续
return 'c';
}