今天我们来写一个三子棋小游戏,主要运用的知识是函数调用,数组,循环等。
首先是棋盘菜单的打印:
void menu()
{
printf("**********************\n");
printf("**********************\n");
printf("**** 1.play ****\n");
printf("**** 0.exit ****\n");
printf("**** made by wenbo****\n");
printf("**********************\n");
printf("**********************\n");
}
如果玩家玩完儿一局还想玩,可以再次进行,所以此时使用
do
{
}while()
来使这个游戏可以再次进行:
int main()
{
int input = 0;
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;
}
下一步,需要把棋盘给制作出来,如示意图:
由示意图可得代码:
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)//在最后一行不需要打印分隔线
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("---");
if (j != col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
此时,棋盘已经打印完毕,接下来就可以开始下棋了。这时我们要明白一个事情,就是必须没下一次棋,打印一次棋盘供玩家查看。代码如下?:
void Player_move(char board[ROW][COL], int row, int col)
//玩家下棋
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入坐标\n");
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 Computer_move(char board[ROW][COL], int row, int col)
//电脑下棋
{
printf("电脑走\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
//调用随机数值库函数
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
此时,如果哪一方首先取得了游戏的胜利,就可以结束游戏。
所以接下来应该设置比赛输赢的条件,如果完成条件,既可以结束游戏。
输赢条件和平局条件的判断:
char iswin(char board[ROW][COL], int row, int col)
//判断输赢
{
for (int i = 0; i < row; i++)
{
if (board[i][1] == board[i][0] && board[i][1] == board[i][2] && board[i][1] == '*')
return board[i][1];
if (board[i][1] == board[i][0] && board[i][1] == board[i][2] && board[i][1] == '#')
return board[i][1];
}//判断水平的棋子是否相等
for (int j = 0; j < col; j++)
{
if (board[1][j] == board[0][j] && board[1][j] == board[2][j] && board[1][j] == '*')
return board[1][j];
if (board[1][j] == board[0][j] && board[1][j] == board[2][j] && board[1][j] == '#')
return board[1][j];
}//判断纵行的棋子是否相等
if (board[1][1] == board[0][0] && board[1][1] == board[2][2] && board[1][1] == '*')
return board[1][1];
if (board[1][1] == board[0][0] && 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 (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#')
return board[1][1];
//判断交叉的棋子是否相等
int ret = isdraw(board, ROW, COL);
if (ret == 1)
return 'Q';
else
return 'C';
}
int isdraw(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
最后对整个代码进行整理:
game.c
#define _CRT_SECURE_NO_WARNINGS #include"game.h" void menu() { ?? ?printf("**********************\n"); ?? ?printf("**********************\n"); ?? ?printf("**** ? ?1.play ? ?****\n"); ?? ?printf("**** ? ?0.exit ? ?****\n"); ?? ?printf("**** made by wenbo****\n"); ?? ?printf("**********************\n"); ?? ?printf("**********************\n"); } 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)//在最后一行不需要打印分隔线 ?? ??? ?{ ?? ??? ??? ?int j = 0; ?? ??? ??? ?for (j = 0; j < col; j++) ?? ??? ??? ?{ ?? ??? ??? ??? ?printf("---"); ?? ??? ??? ??? ?if (j != col - 1) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?printf("|"); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ? printf("\n"); ?? ??? ?} ?? ??? ? ?? ?} } void Player_move(char board[ROW][COL], int row, int col) //玩家下棋 { ?? ?int x = 0; ?? ?int y = 0; ?? ?while (1) ?? ?{ ?? ??? ?printf("请输入坐标\n"); ?? ??? ?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 Computer_move(char board[ROW][COL], int row, int col) //电脑下棋 { ?? ?printf("电脑走\n"); ?? ?while (1) ?? ?{ ?? ??? ?int x = rand() % row; ?? ??? ?int y = rand() % col; ?? ??? ?//调用随机数值库函数 ?? ??? ?if (board[x][y] == ' ') ?? ??? ?{ ?? ??? ??? ?board[x][y] = '#'; ?? ??? ??? ?break; ?? ??? ?} ?? ?} } char iswin(char board[ROW][COL], int row, int col) //判断输赢 { ?? ?for (int i = 0; i < row; i++) ?? ?{ ?? ??? ?if (board[i][1] == board[i][0] && board[i][1] == board[i][2] && board[i][1] == '*') ?? ??? ??? ?return board[i][1]; ?? ??? ?if (board[i][1] == board[i][0] && board[i][1] == board[i][2] && board[i][1] == '#') ?? ??? ??? ?return board[i][1]; ?? ?}//判断水平的棋子是否相等 ?? ?for (int j = 0; j < col; j++) ?? ?{ ?? ??? ?if (board[1][j] == board[0][j] && board[1][j] == board[2][j] && board[1][j] == '*') ?? ??? ??? ?return board[1][j]; ?? ??? ?if (board[1][j] == board[0][j] && board[1][j] == board[2][j] && board[1][j] == '#') ?? ??? ??? ?return board[1][j]; ?? ?}//判断纵行的棋子是否相等 ?? ?if (board[1][1] == board[0][0] && board[1][1] == board[2][2] && board[1][1] == '*') ?? ??? ?return board[1][1]; ?? ?if (board[1][1] == board[0][0] && 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 (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#') ?? ??? ?return board[1][1]; ?? ?//判断交叉的棋子是否相等 ?? ?int ret = isdraw(board, ROW, COL); ?? ?if (ret == 1) ?? ??? ?return 'Q'; ?? ?else ?? ??? ?return 'C';
} int isdraw(char board[ROW][COL], int row, int col) { ?? ?for (int i = 0; i < row; i++) ?? ?{ ?? ??? ?for (int j = 0; j < col; j++) ?? ??? ?{ ?? ??? ??? ?if (board[i][j] == ' ') ?? ??? ??? ?{ ?? ??? ??? ??? ?return 0; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?return 1; } ?
time.c
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include"game.h" void game() { ?? ?//三子棋的过程 ?? ?char board[ROW][COL];//棋盘数组 ?? ?InitBoard(board, ROW, COL);//初始化棋盘 - board的元素都成空格 ?? ?DisplayBoard(board, ROW, COL); ?? ?/*PlayerMove(board,ROW,COL )*/ ?? ?//玩家下棋//电脑下棋 ?? ?char ret = 0; ?? ?while (1) ?? ?{ ?? ??? ?Player_move(board, ROW, COL); ?? ??? ?DisplayBoard(board, ROW, COL); ?? ??? ?ret = iswin(board, ROW, COL); ?? ??? ?if (ret != 'C') ?? ??? ??? ?break; ?? ??? ?Computer_move(board, ROW, COL); ?? ??? ?DisplayBoard(board, ROW, COL); ?? ??? ?if (ret != 'C') ?? ??? ??? ?break; ?? ?}
?? ?if (ret == '*') ?? ?{ ?? ??? ?printf("恭喜你,你赢了!\n"); ?? ?} ?? ?else if (ret == '#') ?? ?{ ?? ??? ?printf("真笨,没有加人工智能的电脑你都能输\n"); ?? ??? ?printf("请接受关机惩罚!\n");
?? ?} ?? ?else if (ret == 'Q') ?? ?{ ?? ??? ?printf("真笨,没有加人工智能的电脑你都能平局\n"); ?? ??? ? ?? ?} } int main() { ?? ?int input = 0; ?? ?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.h
#pragma once #define _CRT_SECURE_NO_WARNINGS #define ROW 3 #define COL 3 #include<stdio.h>?? ? #include<stdlib.h> #include<time.h> void menu(); void InitBoard(char board[ROW][COL], int row, int col); //打印棋盘,并且将棋盘的空位输入空格。 void DisplayBoard(char board[ROW][COL], int row, int col); //将填入数据后的棋盘再次打印。 void Player_move(char board[ROW][COL], int row, int col); //玩家走棋 void Computer_move(char board[ROW][COL], int row, int col); //电脑走棋 char iswin(char board[ROW][COL], int row, int col); //判断输赢 int isdraw(char board[ROW][COL], int row, int col); //惩罚规则?
好的,今天的三子棋小游戏就分享到这儿,如果有错误,欢迎大家指正,感谢。?
|