三子棋的规则如下:在九宫格棋盘上,只要将自己的三个棋子走成一条线,例如:横线,竖线,对角线,自己也就赢了,对方算输。
实现以下功能:
1.棋盘初始化;
2.打印棋盘;
3.玩家下棋;
4.电脑下棋;
5.判断输赢;
代码如下:
1.game.h函数的定义
#ifndef __GAME__H_
#define __GAME__H_
#define ROW 3
#include
void InitBoard(char board[][ROW], int row, int col);//初始化棋盘
void ShowBoard(char board[][ROW], int row, int col);//打印棋盘
void PlayerrMove(char board[][ROW], int row, int col);//玩家下棋
void ComputerMove(char board[][ROW], int row, int col);//电脑下棋
char Win(char board[][ROW], int row, int col);//判断输赢
#endif
2.源文件test.c用于存放主函数 (测试部分):
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("*******************\n");
printf("******0.exit*******\n");
printf("******1.play*******\n");
printf("*******************\n");
}
void game()
{
char ret = 0;
char board[ROW][ROW] = { 0 };// '0' '\0' 0 NULL
InitBoard(board, ROW, ROW, ' ');
ShowBoard(board, ROW, ROW);
while (1)
{
PlayerMove(board, ROW, ROW);//玩家每走一步,需要判断输赢
ret = IsWin(board, ROW, ROW);
if (ret != ' ')
{
//平局或者一方胜出
break;
}
ShowBoard(board, ROW, ROW);
//电脑走
ComputerMove(board, ROW, ROW);
ret = IsWin(board, ROW, ROW);
if (ret != ' ')
{
//平局或一方胜出
break;
}
ShowBoard(board, ROW, ROW);
}
//判断ret
if (ret == 'x')
{
printf("玩家胜出\n");
}
if (ret == 'O')
{
printf("电脑胜出\n");
}
if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请输入您的选择:-》");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏开始\n");
game();
break;
case 0:
printf("游戏退出\n");
break;
default:
printf("请输入正确的操作\n");
break;
}
}
while (input);
system("pause");
return 0;
}
3.源文件game.c 函数部分:
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void InitBoard(char board[][ROW], int row, int col, char set)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < row; j++)
{
board[i][j] = ' ';
}
}
}
void ShowBoard(char board[][ROW], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
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], int row, int col)
{
int x=0, y=0;//x,y为输入的坐标
while (1)
{
printf("玩家移动\n");
printf("请输入1-3的坐标:\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = 'x';
break;
}
else
{
printf("位置已经被占用,请重新输入\n");
}
}
else
{
printf("坐标不合法\n");
}
}
}
static int IsFull(char board[][ROW], int row, int col)//判断棋盘是否满了
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;//没有满
}
}
}
return 1;//棋盘已满
}
void ComputerMove(char board[][ROW], int row, int col)
{
int x = 0, y= 0;
printf("电脑移动\n");
while (1)
{
x = rand() % ROW;
y = rand() % ROW;
if (board[x][y] == ' ')
{
board[x][y] = 'O';
break;
}
}
}
char IsWin(char board[][ROW], int row, int col)//判断有没有一行或者一列或者对角线一样
{
int i;
for (i = 0; i < row; i++)
{//行
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
//列
if (board[0][i] == board[1][i] && board[1][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[0][0] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1]==board[2][0] && board[0][2] != ' ')
{
return board[0][2];
}
if (IsFull(board, ROW, ROW))
{
return 'Q';//平局
}
}
return ' ';//谁也没赢
}