C语言 井字棋游戏

阅读: 评论:0

C语言 井字棋游戏

C语言 井字棋游戏

C语言 井字棋游戏

编写代码实现人机对战的“井字棋游戏”

实现功能如下:

①可查看游戏说明

②可以自由进行多局游戏

③程序健壮性良好、界面美观、清晰

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//输出棋牌以及已落下的棋子
void write(char(play)[11])
{
system(“cls”); //清屏函数
system(“color BD”);
printf("nnntttt C语言课程设计
");
printf("nnttttt编制者:杨少通tt时间:2016年06月22日nn");
printf("ttttt井字棋游戏**nntttttt “);
for (int i = 0;i < 6;i++)
{
for (int j = 0; j < 11; j++)
{
printf(”%c", play[i][j]);
}
printf("ntttttt “);
}
}
//电脑落子,落子的位置为空则落子
char computer(char(play)[11], int position)
{
char chess = ‘X’;
switch (position)
{
case 1: if (play[1][1] == ‘’ || play[1][1] == ’ ') play[1][1] = chess; else return 0; break;
case 2: if (play[1][5] == '
’ || play[1][5] == ’ ') play[1][5] = chess; else return 0; break;
case 3: if (play[1][9] == ‘’ || play[1][9] == ’ ') play[1][9] = chess; else return 0; break;
case 4: if (play[3][1] == '
’ || play[3][1] == ’ ') play[3][1] = chess; else return 0; break;
case 5: if (play[3][5] == ‘’ || play[3][5] == ’ ') play[3][5] = chess; else return 0; break;
case 6: if (play[3][9] == '
’ || play[3][9] == ’ ') play[3][9] = chess; else return 0; break;
case 7: if (play[5][1] == ‘’ || play[5][1] == ’ ') play[5][1] = chess; else return 0; break;
case 8: if (play[5][5] == '
’ || play[5][5] == ’ ') play[5][5] = chess; else return 0; break;
case 9: if (play[5][9] == ‘’ || play[5][9] == ’ ') play[5][9] = chess; else return 0; break;
}
write(play);
return 1;
}
//玩家落子,落子的位置为空则落子
char player(char(*play)[11], char position[])
{
char chess = ‘O’;
switch (position[0])
{
case ‘1’: if (play[1][1] == '
’ || play[1][1] == ’ ') play[1][1] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘2’: if (play[1][5] == ‘’ || play[1][5] == ’ ') play[1][5] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘3’: if (play[1][9] == '
’ || play[1][9] == ’ ') play[1][9] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘4’: if (play[3][1] == ‘’ || play[3][1] == ’ ') play[3][1] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘5’: if (play[3][5] == '
’ || play[3][5] == ’ ') play[3][5] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘6’: if (play[3][9] == ‘’ || play[3][9] == ’ ') play[3][9] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘7’: if (play[5][1] == '
’ || play[5][1] == ’ ') play[5][1] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘8’: if (play[5][5] == ‘’ || play[5][5] == ’ ') play[5][5] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
case ‘9’: if (play[5][9] == '
’ || play[5][9] == ’ ') play[5][9] = chess; else { printf("ntt此处电脑已落子,请输入您的落子位置:"); return 0; } break;
}
write(play);
return 1;
}
//判断是否为平局
int draw(char(play)[11])
{
int n = 0;
if ((play[1][1] == ‘O’ || play[1][1] == ‘X’) && (play[1][5] == ‘O’ || play[1][5] == ‘X’) && (play[1][9] == ‘O’ || play[1][9] == ‘X’))
if ((play[3][1] == ‘O’ || play[3][1] == ‘X’) && (play[3][5] == ‘O’ || play[3][5] == ‘X’) && (play[3][9] == ‘O’ || play[3][9] == ‘X’))
if ((play[5][1] == ‘O’ || play[5][1] == ‘X’) && (play[5][5] == ‘O’ || play[5][5] == ‘X’) && (play[5][9] == ‘O’ || play[5][9] == ‘X’))
n = 1;
else
n = 0;
return n;
}
//判断输赢
int win(char(play)[11])
{
int draw(char(play)[11]); //判断是否是平局的函数
//第一行或第一列获胜的情况
if ((play[1][1] == play[1][5] && play[1][5] == play[1][9] || play[1][1] == play[3][1] && play[3][1] == play[5][1]) && play[1][1] == ‘O’)
{
printf("nttttt 游戏结束!恭喜你,获得了本次游戏的胜利!"); return 1;
}
else
if ((play[1][1] == play[1][5] && play[1][5] == play[1][9] || play[1][1] == play[3][1] && play[3][1] == play[5][1]) && play[1][1] == ‘X’)
{
printf("nttttt游戏结束!很遗憾,电脑获得了胜利!请再接再厉!");return 1;
}
else
if (draw(play))
{
printf("ntttttt游戏结束!平局!请再接再厉!");return 1;
}
//第二列或第二行或两条对角线获胜的情况
if ((play[3][1] == play[3][5] && play[3][5] == play[3][9] || play[1][5] == play[3][5] && play[3][5] == play[5][5] || play[1][1] == play[3][5] && play[3][5] == play[5][9] || play[1][9] == play[3][5] && play[3][5] == play[5][1]) && play[3][5] == ‘O’)
{
printf("nttttt 游戏结束!恭喜你,获得了本次游戏的胜利!"); return 1;
}
else
if ((play[3][1] == play[3][5] && play[3][5] == play[3][9] || play[1][5] == play[3][5] && play[3][5] == play[5][5] || play[1][1] == play[3][5] && play[3][5] == play[5][9] || play[1][9] == play[3][5] && play[3][5] == play[5][1]) && play[3][5] == ‘X’)
{
printf("nttttt游戏结束!很遗憾,电脑获得了胜利!请再接再厉!");return 1;
}
else
if (draw(play))
{
printf("ntttttt游戏结束!平局!请再接再厉!");return 1;
}
//第三行或第四行获胜的情况
if ((play[5][1] == play[5][5] && play[5][5] == play[5][9] || play[1][9] == play[3][9] && play[3][9] == play[5][9]) && play[5][9] == ‘O’)
{
printf("nttttt 游戏结束!恭喜你,获得了本次游戏的胜利!"); return 1;
}
else
if ((play[5][1] == play[5][5] && play[5][5] == play[5][9] || play[1][9] == play[3][9] && play[3][9] == play[5][9]) && play[5][9] == ‘X’)
{
printf("nttttt游戏结束!很遗憾,电脑获得了胜利!请再接再厉!");return 1;
}
else
if (draw(play))
{
printf("ntttttt游戏结束!平局!请再接再厉!");return 1;
}
return 0;
}
//游戏操作说明
int explain()
{
int start;
system(“color 0D”);
printf("nttttt操作说明如下:n");
printf("nttttt 这是一个人机对弈的游戏,由于电脑较为弱n");
printf("ttttt势,所以由电脑先落子。“O”为玩家棋子,“X”n");
printf("ttttt为电脑棋子。棋盘键位如下:nnttttttt");
char explain[6][11] = { { ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ ‘’,‘1’,’’,’|’,’’,‘2’,’’,’|’,’’,‘3’,’’ },{ ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ ‘’,‘4’,’’,’|’,’’,‘5’,’’,’|’,’’,‘6’,’’ },{ ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ ’ ‘,‘7’,’ ‘,’|’,’ ‘,‘8’,’ ‘,’|’,’ ‘,‘9’,’ ’ } };
for (int i = 0;i < 6;i++)
{
for (int j = 0;j < 11;j++)
printf("%c", explain[i][j]);
printf("nttttttt");
}
printf("nttttt 1.继续游戏tt 2.结束游戏nn");
printf("ttttt请输入您要进行的操作:");
for (;😉
{
scanf_s("%d", &start);
if (start == 1 || start == 2)
break;
else
printf("nttttt只有1操作和2操作,请重新输入:");
}
return start;
}
//开始游戏
void game(char(play)[11])
{
int main(); //再次调用main()函数以达到多次游戏的目的
int cpu;
char person[5];
int start;
srand((unsigned)time(NULL)); //时间种子,从1900年1月1日到现在的时间秒数,使rand()产生一个随机数
for (int num = 0;num < 5;num++) //num变量表示电脑和玩家其中一方最多落子五次且只有一方
{
while (cpu = (rand() % 9 + 1)) //控制电脑落子的位置是空的时候,才允许电脑落子
{
if (computer(play, cpu))
break;
}
//确定胜利的一方后结束游戏
if (win(play))
{
break;
}
getchar();
printf("ntt电脑已落子,请输入您的落子位置:");
while (1) //控制玩家落子的位置是空的时候,才允许落子
{
scanf_s("%s", person,5);
if (person[0] > ‘9’ || person[0] < ‘1’ || person[1] != ‘’)
{
printf("ntt只有1到9的位置,请重新输入:");
continue;
}
if (player(play,person))
break;
}
//确定胜利的一方后结束游戏
if (win(play))
{
break;
}
}
printf("nnttttt
游戏已结束****n”);
printf("ntttttt1.再来一局t2.结束游戏");
printf("nnttttt请输入您要进行的操作:");
for (;😉
{
scanf_s("%d", &start);
if (start == 1)
main();
else
if (start == 2)
{
printf("nnttttt游戏已结束n");
break;
}
else
printf("nttttt只有1操作和2操作,请重新输入:");
}
system(“pause>nul&&cls”); //阻止清屏
}
int main()
{
system(“cls”);
void write(char(play)[11]); //输出棋盘的函数
char computer(char(play)[11], int position); //电脑落子的位置
char player(char(play)[11], char position[]); //玩家落子的位置
int win(char(play)[11]); //判断输赢的函数
int explain(); //游戏说明函数
void game(char(pllay)[11]); //游戏函数
char play[6][11] = { { ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ ‘’,’’,’’,’|’,’’,’’,’’,’|’,’’,’’,’’ },{ ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ '’,’’,’’,’|’,’’,’’,’’,’|’,’’,’’,’’ },{ ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ },{ ’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ‘,’|’,’ ‘,’ ‘,’ ’ } };
int start;
printf("tttt C语言课程设计
");
printf("nnttttt编制者:杨少通tt时间:2016年06月22日nn");
printf("ttttt
井字棋游戏
*******nn");
printf("ttttt 1.操作说明tt 2.开始游戏nn");
printf("ttttt请输入您要进行的操作:");
for (;😉 //允许用户的操作按键只有1和2
{
scanf_s("%d", &start);
if (start == 1 || start == 2)
break;
else
printf("nttttt只有1操作和2操作,请重新输入:");
}
switch (start) //查看操作说明或开始游戏
{
case 1:
switch (explain()) //开始游戏或结束游戏
{
case 1:game(play);break;
case 2:printf("nnttttt***************游戏结束!!!***************n");system(“pause>nul&&cls”);break;
}
break;
case 2:game(play);break;
}
return 0;
}

代码均为原创,存在不足还请见谅!如有转载请注明来源: www.dreamload/blog/?p=395&preview=true (洋葱先生)

本文发布于:2024-02-01 08:51:50,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674871435419.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语言   游戏   井字棋
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23