三子棋是一种民间传统游戏,又叫九宫棋、圈圈叉叉、一条龙、井字棋等。将正方形对角线连起来,相对两边依次摆上三个双方棋子,只要将自己的三个棋子走成一条线,对方就算输了。但是,有很多时候会出现和棋的情况。
1.建立菜单开始或退出游戏
2.创建棋盘并初始化棋盘
3.打印棋盘
4.玩家下棋,下棋完毕后判断游戏是否结束
5.电脑下棋,下棋完毕后判断游戏是否结束
6.若结束,打印输赢
//test.c存放主函数,菜单界面和游戏函数,引用"game.h"头文件
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//时间戳,用来生成随机数
do
{
menu();//打印菜单
printf("请输入:>");
scanf("%d", &input);//玩家输入选择
switch (input)
{
case 1:
game();//输入1,进入游戏
break;
case 0:
printf("退出游戏。\n");//输入0,退出程序
break;
default:
printf("输入错误,请重新输入。\n");
break;
}
} while (input);
return 0;
}
void menu()
{
printf("------------ 三子棋 -----------\n\n");
printf("-------------1.play ----------\n");
printf("-------------0.exit ----------\n");
printf("-------------------------------\n");
}
void game()
{
char board[ROW][COL] ;
char ch = '0';
Initboard(board, ROW, COL);//初始化
Displayboard(board, ROW, COL);//打印
while (1)
{
player_move(board, ROW, COL);//玩家移动
system("cls");//清屏,方便观看
ch =is_win(board, ROW, COL);//判断
if (ch != 'c')
{
break;
}
printf("玩家下棋。\n");
Displayboard(board, ROW, COL);//打印玩家下棋后的棋盘
computer_move(board, ROW, COL);//电脑下棋
Displayboard(board, ROW, COL);//打印电脑下棋后的棋盘
ch = is_win(board, ROW, COL);
if (ch != 'c')
{
break;
}
}
printf("游戏结束:\n");
Displayboard(board, ROW, COL);
if (ch == '*')
printf("玩家胜利!\n");
else if (ch == '#')
printf("电脑胜利。\n");
else
printf("平局\n");
Sleep(2000);
system("cls");
}
//game.h声明函数
#include
#include
#include
#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 player_move(char board[ROW][COL], int row, int col);//玩家移动
void computer_move(char board[ROW][COL], int row, int col);//电脑移动
char is_win(char board[ROW][COL], int row, int col);//判断游戏
//game.c存放功能函数,引用"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 (int k = 0; k < row; k++)
{
printf("---");
if (k < row-1)
printf("|");
}
printf("\n");
}
}
打印出来的棋盘效果:
玩家棋子'*',玩家输入坐标例如:1 1
void player_move(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("玩家下棋:\n");
int flag = 1;
do
{
printf("请输入坐标:>");
scanf("%d %d", &i, &j);
if (board[i -1][j -1] == ' '&& i > 0 && j > 0 && i< 4 && j < 4 )
{
flag = 0;
board[i -1][j -1] = '*';
}
else
printf("请重新输入。\n");
} while (flag);
}
电脑棋子'#'
使用rand()函数前记得使用srand()函数
void computer_move(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("电脑下棋。\n");
while (1)
{
x = rand() % row;//生成0-3的随机数
y = rand() % col;
if (board[x][y] == ' ')//没有放置棋子的情况下才可以下棋
{
board[x][y] = '#';
break;
}
}
}
return ‘*’; 表示玩家胜出
return ‘#’; 表示电脑胜出
return ‘q’; 表示平局
否则return 'c'; 继续游戏
char is_win(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][1] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
return board[1][i];
}
if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][i];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][i];
int j = 0;
int flag = 1;
for (i = 0; i < row; i++)//判断棋盘上有没有空格子
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
flag = 0;
}
}
if (flag == 1)
return 'q';//平局
return 'c';//游戏继续
}
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("------------ 三子棋 -----------\n\n");
printf("-------------1.play ----------\n");
printf("-------------0.exit ----------\n");
printf("-------------------------------\n");
}
void game()
{
char board[ROW][COL] ;
char ch = '0';
Initboard(board, ROW, COL);//初始化
Displayboard(board, ROW, COL);//打印
while (1)
{
player_move(board, ROW, COL);//玩家移动
system("cls");
ch =is_win(board, ROW, COL);//判断
if (ch != 'c')
{
break;
}
printf("玩家下棋。\n");
Displayboard(board, ROW, COL);
computer_move(board, ROW, COL);//电脑下棋
Displayboard(board, ROW, COL);
ch = is_win(board, ROW, COL);
if (ch != 'c')
{
break;
}
}
printf("游戏结束:\n");
Displayboard(board, ROW, COL);
if (ch == '*')
printf("玩家胜利!\n");
else if (ch == '#')
printf("电脑胜利。\n");
else
printf("平局\n");
Sleep(2000);
system("cls");
}
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;
}
#include
#include
#include
#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 player_move(char board[ROW][COL], int row, int col);//玩家移动
void computer_move(char board[ROW][COL], int row, int col);//电脑移动
char is_win(char board[ROW][COL], int row, int col);//判断游戏
#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 (int k = 0; k < row; k++)
{
printf("---");
if (k < row-1)
printf("|");
}
printf("\n");
}
}
void player_move(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
printf("玩家下棋:\n");
int flag = 1;
do
{
printf("请输入坐标:>");
scanf("%d %d", &i, &j);
if (board[i -1][j -1] == ' '&& i > 0 && j > 0 && i< 4 && j < 4 )
{
flag = 0;
board[i -1][j -1] = '*';
}
else
printf("请重新输入。\n");
} while (flag);
}
void computer_move(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;
}
}
}
char is_win(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][1] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
return board[1][i];
}
if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
return board[1][i];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
return board[1][i];
int j = 0;
int flag = 1;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
flag = 0;
}
}
if (flag == 1)
return 'q';//平局
return 'c';//游戏继续
}