流程图理清C语言简易三子棋-创新互联
目录
创新互联专注于雨湖网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供雨湖营销型网站建设,雨湖网站制作、雨湖网页设计、雨湖网站官网定制、小程序设计服务,打造雨湖网络公司原创品牌,更为您提供雨湖网站排名全网营销落地服务。一、主要涉及的知识点有:
二、玩家玩三子棋流程
三、判断一次游戏是否结束?
四、代码
1、主函数部分
2、自定义头文件部分
3、自定义函数定义部分
一、主要涉及的知识点
逻辑表达式、for循环、switch选择、do-while循环、while循环、if语句、自定义函数、二维数组、时间戳、rand函数生成随机数字等。
二、玩家玩三子棋流程三、判断一次游戏是否结束?四、代码 1、主函数部分#define _CRT_SECURE_NO_WARNINGS 1
#include#include"game.h"
void game()
{
char board[ROW][COL] = { 0 };
Prim_board(board, ROW, COL);//初始化棋盘
Print_board(board, ROW, COL);//打印棋盘
do //判断谁先走
{
int k = 0;
int i = 0;
printf("****** 1.Player First ******\n");
printf("****** 0.Computer First ******\n");
printf("请选择:>");
scanf("%d", &i);
switch (i)
{
case 1:
printf("玩家先走!\n");//玩家走
goto player;
break;
case 0:
printf("电脑先走!\n");//电脑走
goto computer;
break;
default:
printf("请重新选择!\n");
break;
}
} while (1);
char ret = 0;
while (1)
{
player://玩家走
Player_board(board, ROW, COL);
Print_board(board, ROW, COL);//打印棋盘
ret = Who_win(board, ROW, COL);//判断是否获胜
if (ret != 'C')
{
break;
}
computer:
printf("\n\n电脑走!\n");
Computer_board(board, ROW, COL);
Print_board(board, ROW, COL);//打印棋盘
ret = Who_win(board, ROW, COL);//判断是否获胜
if (ret != 'C')
{
break;
}
}
if (ret == 'Q')
{
printf("平局!\n");
}
else if (ret == '*')
{
printf("恭喜您获胜!");
}
else if (ret == '#')
{
printf("电脑获胜!");
}
else
{
printf("平局");
}
}
void FirtPage()
{
printf("****** 1.Play ******\n");
printf("****** 0.Exit ******\n");
int i = 0;
do
{
printf("\n请选择是否开始:>");
scanf("%d", &i);
switch (i)
{
case 0:
break;
case 1:
game();
break;
default:
printf("请重新输入:>");
break;
}
} while (i);
}
int main()
{
srand((unsigned int)time(NULL));
FirtPage();
return 0;
}
2、自定义头文件部分#define _CRT_SECURE_NO_WARNINGS 1
#include#include#include#define ROW 3
#define COL 3
Prim_board(char board[ROW][COL], int row, int col);
void Print_board(char board[ROW][COL], int row, int col);
void Player_board(char board[ROW][COL], int row, int col);
void Computer_board(char board[ROW][COL], int row, int col);
char Who_win(char board[ROW][COL], int row, int col);
3、自定义函数定义部分#include"game.h"
Prim_board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i< row; i++)
{
for (j = 0; j< col; j++)
{
board[i][j] = ' ';
}
}
}
void Print_board(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 Player_board(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("\n\n玩家,请输入坐标x, y!\n");
do
{
scanf("%d,%d", &x, &y);
if (x >0 && x<= row && y >0 && y<= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐标不为空,请重新输入!\n");
}
}
else
{
printf("坐标非法,请重新输入!\n");
}
} while (1);
}
void Computer_board(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
do
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
} while (1);
}
int 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;
}
}
}
char Who_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][0] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//三列
for (i = 0; i< col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
//对角线
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//平局
int a = 1;
a = IsFull(board, ROW, COL);
if (a == 1)
return 'Q';
return 'C';
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网站名称:流程图理清C语言简易三子棋-创新互联
链接URL:http://ybzwz.com/article/dsoeog.html