1. 游戏介绍
三子棋是一种民间传统游戏。分为双方对战,双方依次在9宫格的棋盘上落子,率先在棋盘上将自己的三个棋子走成一条线就视为胜利,也有和棋的局面。
实现
- 游戏可以循环玩下去,而不是玩一把就结束(循环)
- 本文应用多文件的形式写代码,分为:
- test.c —— 测试游戏
- game.c —— 游戏函数的实现
- game.h —— 游戏函数的声明
2. 效果展示
![image-20230510155244980](https://i-blog.csdnimg.cn/blog_migrate/f95fee5ddb0a01641eb2dc55090c2219.png)
3. 游戏代码
test.c
#include "game.h"
void menu()
{
printf("*******************************************\n");
printf("*************** 1. play ***************\n");
printf("*************** 0. exit ***************\n");
printf("*******************************************\n");
}
void game()
{
//棋盘
char board[ROW][COL] = { 0 };
//初始化棋盘
InitBoard(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");
}
int main()
{
int input = 0;
srand((unsigned int)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;
}
game.c
#include "game.h"
//初始化棋盘为空格
void InitBoard(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++)
{
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;
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)
{
int x = 0;
int y = 0;
printf("电脑下棋:>\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int IsFull(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++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
char IsWin(char board[ROW][COL], int row, int col)
{
//赢
int i = 0;
int j = 0;
int count = 0; //相同的次数
//行
for (i = 0; i < row; i++)
{
count = 0;
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] != ' ')
{
count++;
}
}
if (count == col - 1)
{
return board[i][j];
}
}
//列
for (i = 0; i < col; i++)
{
count = 0;
for (j = 0; j < row - 1; j++)
{
if (board[j][i] == board[j + 1][i] && board[j][i] != ' ')
{
count++;
}
}
if (count == row - 1)
{
return board[j][i];
}
}
//对角线
count = 0;
for (i = 0; i < row - 1; i++)
{
if (board[i][i] == board[i + 1][i + 1] && board[i][i] != ' ')
{
count++;
}
}
if (count == row - 1)
{
return board[i][i];
}
count = 0;
for (i = 0, j = col - 1; i < row - 1 && j > 0; i++, j--)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] != ' ')
{
count++;
}
}
if (count == row - 1)
{
return board[i][j];
}
//平局
if (IsFull(board, row, col) == 1)
{
return 'Q';
}
//继续
return 'C';
}
game.h
#pragma once
#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);
//判断输赢
//玩家赢 - '*'
//电脑赢 - '#'
//平局 - 'Q'
//继续 - 'C'
char IsWin(char board[ROW][COL], int row, int col);