游戏介绍
三子棋又名井字棋,顾名思义与五子棋规则相同。当一方棋子的一行、一列、或斜对角线的三个棋子连成一条直线,则该方获胜。本次实现的三子棋由玩家对战电脑。
本次游戏的实现分为以下三个文件:
1.game.c? ? //游戏功能的实现
2.game.h? ?//游戏函数的头文件,声明函数
3.主程序.c
写一个菜单函数
打印游戏开始时的选项,如果选择1则开始游戏,选择0则退出游戏,选择其他数字则重新进行选择。这段代码放在:主程序.c
//打印游戏菜单
void menu()
{
printf("******************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
将这个函数放在main()中,然后进行选择:
int main()
{
int input = 0;
srand((unsigned int)time(NULL)); //这是随机数生成
do
{
menu(); //打印菜单
scanf("%d", &input);
switch (input)
{
case 1:
game(); //进行游戏
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
}
} while (input);
return 0;
}
初始化并打印棋盘
在game.h中定义两个变量,分别是棋盘的行和列。并且声明InitBoard函数和DisplayBoard函数,功能是初始化棋盘和打印棋盘。
#define ROW 3 //定义行为3
#define COL 3 //定义列为3
void InitBoard(char board[ROW][COL],int row,int col); //初始化棋盘
void DisplayBoard(char board[ROW][COL], int row, int col); //打印棋盘
在game函数中实现这两个函数
void game()
{
char board[ROW][COL]; //棋盘数组
InitBoard(board,ROW,COL);
DisplayBoard(board, ROW, COL);
}
具体功能在game.c中实现,代码如下:
//初始化棋盘,将棋盘每个数值初始化为空格
void InitBoard(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;
int j = 0;
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");
}
}
玩家下棋和电脑下棋函数
在game.h中继续声明两个函数,分别为PlayerMove()和ComputerMove()
#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 boaed[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
当然在game函数中要实现,规则就是使用一个while循环,玩家下一个,然后打印棋盘,判断有没有三子连线,如果有就退出,如果没有就继续让电脑下一个,电脑下完继续判断,如果有三子连线就退出,如果没有则会继续循环。玩家下棋使用“*”,电脑下棋使用“#”。
void game()
{
char board[ROW][COL];//棋盘数组
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");
}
}
玩家下棋函数(放在game.c):
玩家通过输入坐标下棋,如果输入坐标的位置是空格的话可以下,如果已经下过来会显示重新输入坐标。
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf("玩家走->\n");
int x = 0;
int y = 0;
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");
}
}
}
电脑下棋函数(放在game.c):
电脑自动生成随机坐标,在有空格的位置下棋。
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;
}
}
}
当然此时还不能运行,头文件包含不能忘!!
这是game.h
#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 boaed[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); //添加一个判断输赢的函数
这是主程序.c和game.c都要包含的
#include <stdio.h>
#include "game.h"
判断棋盘是否为满格函数
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][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
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[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];
}
if (IsFull(board, row, col))
{
return 'Q';
}return 'C';
}
}
总结
主程序.c文件代码:
#include <stdio.h>
#include "game.h"
void menu()
{
printf("******************\n");
printf("***** 1.play *****\n");
printf("***** 0.exit *****\n");
printf("******************\n");
}
void game()
{
char board[ROW][COL];//棋盘数组
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();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误,请重新输入!\n");
}
} while (input);
return 0;
}
game.h文件代码:
#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 boaed[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);
game.c文件代码:
#include <stdio.h>
#include "game.h"
void InitBoard(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;
int j = 0;
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][COL], int row, int col)
{
printf("玩家走->\n");
int x = 0;
int y = 0;
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;
}
}
}
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][0] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
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[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];
}
if (IsFull(board, row, col))
{
return 'Q';
}return 'C';
}
}
到此结束,一个简单的三子棋小游戏就做好啦~~嘿嘿
|