描述
用C语言完成一个三子棋小游戏。
步骤
1、首先创建一个二维数组 board ,用 board 数组来储存三子棋的棋盘和元素。
2、写一个函数 Init() 对 board 数组初始化
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(&board[0][0], ' ', row*col*sizeof(board[0][0]));
}
这里用到了 memset() 库函数,头文件为 #include <string.h>, 参数分别为,首地址,需要设置的字符,需要设置的字符个数。
3、写一个函数 DisplayBoard() 打印三子棋棋盘
void DisplayBoard(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++)
{
printf(" %c |", board[i][j]);
}
printf("\n");
if (i < row - 1)
printf("---------------------------\n");
}
}
4、接下来分别写函数 PlayerMove() 和 ComputerMove() 来对二维数组进行操作,模拟下棋的动作
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
//判断是否越界
if (x >= row && x <= 1 && y >= col && y <= 1)
{
printf("输入错误!请重新输入:\n");
continue;
}
//如果用户选择的坐标为空,则将空格改为X,否则提示输入错误
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
else
{
printf("输入错误,请重新输入:\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
5、玩家和电脑操作后,都要进行一次判断是否结束,用 IsWin() 函数来实现
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int a = 0;
char ch = 0;
判断每一行是否一样
//for (i = 0; i < row; i++)
//{
// if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == 'X')
// {
// return 'X';
// }
// if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '0')
// {
// return '0';
// }
//}
//
判断每一列是否一样
//for (i = 0; i < col; i++)
//{
// if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] == 'X')
// {
// return 'X';
// }
// if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] == '0')
// {
// return '0';
// }
//}
//
判断对角线是否满足相等
//if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == 'X')
//{
// return 'X';
//}
//if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == 'X')
//{
// return 'X';
//}
//if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '0')
//{
// return '0';
//}
//if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '0')
//{
// return '0';
//}
for (i = 0; i < row; i++)
{
a = 0;
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i][j + 1] && board[i][j] == '0')
{
a--;
}
if (a == col - 1)
{
return 'X';
}
if (a == -col + 1)
{
return '0';
}
}
}
for (i = 0; i < col; i++)
{
a = 0;
for (j = 0; j < row - 1; j++)
{
if (board[j][i] == board[j + 1][i] && board[j][i] == 'X')
{
a++;
}
if (board[j][i] == board[j + 1][i] && board[j][i] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
}
for (i = 0, j = 0; i < row - 1; i++, j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
for (i = 0, j = col - 1; i < row - 1; i++, j--)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
//若board数组被填满,则为平局返回’Q'
ch = IsFull(board, row, col);
if (ch == 'Q')
{
return 'Q';
}
}
注意!上面代码注释部分是只能判断三子棋是否结束,而没有注释的部分是可以判断任意规模的棋局是否结束,只需要改常量即可!
源代码
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
/*
* 测试程序需要测试三子棋的正常运行
* 郭文峰
* 2018/10/18
*/
void menu()
{
printf("***********************************\n");
printf("******* 1. play *******\n");
printf("******* 0. exit *******\n");
printf("***********************************\n");
}
void game()
{
//创建一个二维数组来存储棋盘
char board[ROW][COL] = { 0 };
int i = 0;
int j = 0;
int ch = 0;
//InitBoard函数用来初始化board数组,并且全部初始化为‘ ’(空格)
InitBoard(board, ROW, COL);
//DisplayBoard函数用来打印棋盘
DisplayBoard(board, ROW, COL);
while (1)
{
//用户选择一个坐标,并进行标记
PlayerMove(board, ROW, COL);
//判断是否赢得游戏
ch = IsWin(board, ROW, COL);
if (ch == 'X')
{
printf("恭喜你!你赢了!\n");
DisplayBoard(board, ROW, COL);
break;
}
else if (ch == 'Q')
{
printf("很惊险!是平局!\n");
DisplayBoard(board, ROW, COL);
break;
}
else if (ch == '0')
{
printf("很抱歉!电脑获胜!\n");
DisplayBoard(board, ROW, COL);
break;
}
DisplayBoard(board, ROW, COL);
printf(" \n");
printf("---------------\n");
printf(" \n");
//电脑随机选择一个坐标进行标记
ComputerMove(board, ROW, COL);
ch = IsWin(board, ROW, COL);
if (ch == 'X')
{
printf("恭喜你!你赢了!\n");
DisplayBoard(board, ROW, COL);
break;
}
else if (ch == '0')
{
printf("很抱歉!电脑获胜!\n");
DisplayBoard(board, ROW, COL);
break;
}
else if (ch == 'Q')
{
printf("很惊险!是平局!\n");
DisplayBoard(board, ROW, COL);
break;
}
DisplayBoard(board, ROW, COL);
}
}
int main(void)
{
int input = 0;
srand((unsigned int)time(NULL));
//菜单函数,打印游戏开始菜单
menu();
do{
printf("请输入您要选的选项:");
//接收用户选择的选项
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误!请重新输入!\n");
break;
}
} while (input);
system("pause");
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
memset(&board[0][0], ' ', row*col*sizeof(board[0][0]));
}
void DisplayBoard(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++)
{
printf(" %c |", board[i][j]);
}
printf("\n");
if (i < row - 1)
printf("---------------------------\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
//判断是否越界
if (x >= row && x <= 1 && y >= col && y <= 1)
{
printf("输入错误!请重新输入:\n");
}
//如果用户选择的坐标为空,则将空格改为X,否则提示输入错误
if (board[x-1][y-1] == ' ')
{
board[x-1][y-1] = 'X';
break;
}
else
{
printf("输入错误,请重新输入:\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
x = rand() % 3;
y = rand() % 3;
if (board[x][y] == ' ')
{
board[x][y] = '0';
break;
}
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int a = 0;
char ch = 0;
判断每一行是否一样
//for (i = 0; i < row; i++)
//{
// if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == 'X')
// {
// return 'X';
// }
// if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '0')
// {
// return '0';
// }
//}
//
判断每一列是否一样
//for (i = 0; i < col; i++)
//{
// if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] == 'X')
// {
// return 'X';
// }
// if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] == '0')
// {
// return '0';
// }
//}
//
判断对角线是否满足相等
//if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == 'X')
//{
// return 'X';
//}
//if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == 'X')
//{
// return 'X';
//}
//if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '0')
//{
// return '0';
//}
//if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '0')
//{
// return '0';
//}
for (i = 0; i < row; i++)
{
a = 0;
for (j = 0; j < col - 1; j++)
{
if (board[i][j] == board[i][j + 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i][j + 1] && board[i][j] == '0')
{
a--;
}
if (a == col - 1)
{
return 'X';
}
if (a == -col + 1)
{
return '0';
}
}
}
for (i = 0; i < col; i++)
{
a = 0;
for (j = 0; j < row - 1; j++)
{
if (board[j][i] == board[j + 1][i] && board[j][i] == 'X')
{
a++;
}
if (board[j][i] == board[j + 1][i] && board[j][i] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
}
for (i = 0, j = 0; i < row - 1; i++, j++)
{
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
for (i = 0, j = col - 1; i < row - 1; i++, j--)
{
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == 'X')
{
a++;
}
if (board[i][j] == board[i + 1][j - 1] && board[i][j] == '0')
{
a--;
}
if (a == row - 1)
{
return 'X';
}
if (a == -row + 1)
{
return '0';
}
}
//若board数组被填满,则为平局返回’Q'
ch = IsFull(board, row, col);
if (ch == 'Q')
{
return 'Q';
}
}
char 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 'C';
}
}
}
return 'Q';
}
game.h
#ifndef __GEME_H__
#define __GEME_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.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);
char IsFull(char board[ROW][COL], int row, int col);
#endif //__GEME_H__