一、代码
1、主函数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void game()
{
//定义棋盘
char board[ROW][COL];
//初始化棋盘
Initdboard(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);//实现过程中没有写ret=,直接调用了
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 menu()
{
printf("*************************************\n");
printf("--------->> 1、play game <<--------\n");
printf("--------->> 0、exit <<--------\n");
printf("*************************************\n");
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
2、game函数
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//棋盘初始化函数
void Initdboard(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)
{
int j = 0;
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;
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;//实现过程中这两句放在了while外面,导致电脑只能下两次
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int 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[0][2] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
int ree = Isfull(board, row, col);
if (ree == 1)
{
return 'Q';
}
return 'C';
}
头文件和函数定义
#pragma once
#define ROW 3
#define COL 3
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//初始化棋盘函数定义
void Initdboard(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 Isfull(char board[ROW][COL], int row, int col);
二、运行结果