#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <time.h>
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")
#define SIZE 15//棋盘大小
#define EMPTY 0
char borad[SIZE][SIZE];//棋盘
int lhc;
int p;
int win=9;//黑子输赢判断
int wni=9;//白子输赢判断
void Gamerun();
void cern1();
void cern2();
void cern3();
void cern5();
void player1();
void player2();
void pc_player_black();
void pc_player_white();
void bgm();
void pvp();
void pve();
void plintf(int x,int y);
void win_or_loseBlack();
void win_or_loseWhite();
int main()
{
system("color E0");//设置背景颜色
while(1)
{
Gamerun();
printf("再来一把,请选入1,结束按,2");
int o_o;
scanf("%d",o_o);
if(o_o==2)
{
break;//Gamerun的每一次循环是进行一整局游戏
}
}
}
void Gamerun()
{
cern5();
bgm();
cern2();
cern1();
cern3();
if(p==1)
{
pvp();
}
else if(p==0)
{
pve();
}
}
void cern2()
{
int i=0;
int j=0;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
borad[i][j]=EMPTY;
}
}
}
void cern1()
{
for(int i=0;i<=SIZE;i++)//行
{
for(int j=0;j<=SIZE;j++)//列
{
if(i==0&&j==0)//左上角
{
printf("┌ ");
}
else if(i==0&&j==SIZE)//右上角
{
printf("┐");
}
else if(i==SIZE&&j==0)//左下角
{
printf("└ ");
}
else if(i==SIZE&&j==SIZE)//右下角
{
printf("┘");
}
else if(i==0)
{
printf("┬ ");
}
else if(i==SIZE)
{
printf("┴ ");
}
else if(j==0)
{
printf("├ ");
}
else if(j==SIZE)
{
printf("┤");
}
else
{
printf("┼ ");
}
}
printf("\n");
}
}
void cern3()
{
printf("请选择你的棋,黑子,0;白子,1\n");
scanf("%d",&lhc);
if(lhc==0)
{
printf("你是黑子,你先下,屌毛!\n");
}
else if(lhc==1)
{
printf("你是白子,你就等屎吧你!\n");
}
}
void player1()
{
int x;
int y;
while(1)
{
printf("\n请输入您的坐标,坐标之间需要空格间隔开(i行j列)\n");
scanf("%d%d", &x, &y);
if(x>=0&&x<=14&&y>=0&&y<=14)
{
if(borad[x][y]==EMPTY)
{
borad[x][y]='b';
plintf(x,y);
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围\n");
}
}
}
void player2()
{
int x;
int y;
while(1)
{
printf("\n请输入您的坐标,坐标之间需要空格间隔开(i行j列)\n");
scanf("%d%d", &x, &y);
if(x>=0&&x<=14&&y>=0&&y<=14)
{
if(borad[x][y]==EMPTY)
{
borad[x][y]='w';
plintf(x,y);
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围\n");
}
}
}
void pc_player_black()
{
int x,y;
srand(time(0));
x=rand()%SIZE+1;
y=rand()%SIZE+1;
while(1)
{
if(x>=0&&x<=14&&y>=0&&y<=14)
{
if(borad[x][y]==EMPTY)
{
borad[x][y]='b';
plintf(x,y);
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围\n");
}
}
}
void pc_player_white()
{
int x,y;
srand(time(0));
while(1)
{
x=rand()%SIZE+1;
y=rand()%SIZE+1;
if(x>=0&&x<=14&&y>=0&&y<=14)
{
if(borad[x][y]==EMPTY)
{
borad[x][y]='w';
plintf(x,y);
break;
}
else
{
printf("坐标被占用,请重新输入\n");
printf("\n");
}
}
else
{
printf("超出坐标范围\n");
}
}
}
void cern5(void){
while(1){
p=0;
printf("*****作者:刘俊茗*****\n");
printf(" *****欢迎来到*****\n");
printf(" 《超级屌毛五子棋》 \n");
printf("请选择对战模式:输入0进行人机对战,输入1进行人人对战\n");
scanf("%d",&p);
if(p==0){
//pve();
printf("准备好接受超级算法的挑战了吗\n");
break;
}else if(p==1){
//pvp();
printf("输的人请客\n");
break;
}else{
printf("输入不符合规范,请重新输入\n");
}
}
}
void bgm()
{
mciSendString("open op.mp3 alias bkmusic", NULL, 0, NULL);
mciSendString("play bkmusic repeat", NULL, 0, NULL); //循环播放音乐
system("pause");
}
void pvp()
{
int i=2;
while(2)
{
if(i%2==0)
{
printf("轮到黑子了\n");
player1();
win_or_loseBlack();
printf("%d\n",win);
if(win==1)
{
printf("黑子胜!\n");
break;
}
i++;
}
else
{
printf("轮到白子了\n");
player2();
win_or_loseWhite();
printf("%d\n",wni);
if(wni==1)
{
printf("白子胜!\n");
break;
}
i++;
}
}
}
void pve()
{
int i=2;
if(lhc==0)//选黑
{
while(1)
{
if(i%2==0)
{
printf("轮到黑子了\n");
player1();
win_or_loseBlack();
printf("%d\n",win);
if(win==1)
{
printf("鸡哥胜!\n");
break;
}
i++;
}
else
{
printf("轮到白子了\n");
pc_player_white();
win_or_loseWhite();
printf("%d\n",wni);
if(wni==1)
{
printf("白子胜!\n");
break;
}
i++;
}
}
}
else if(lhc==1)
{
while(1)
{
if(i%2!=0)
{
printf("轮到黑子了\n");
pc_player_black();
win_or_loseBlack();
printf("%d\n",win);
if(win==1)
{
printf("鸡哥胜!\n");
break;
}
i++;
}
else
{
printf("轮到白子了\n");
player2();
win_or_loseWhite();
printf("%d\n",wni);
if(wni==1)
{
printf("白子胜!\n");
break;
}
i++;
}
}
}
}
void plintf(int x,int y)
{
//清屏
//system("cls");
for(int i=0;i<=SIZE;i++)//行
{
for(int j=0;j<=SIZE;j++)//列
{
if(i==0&&j==0)//左上角
{
printf("┌ ");
}
else if(i==0&&j==SIZE)//右上角
{
printf("┐");
}
else if(i==SIZE&&j==0)//左下角
{
printf("└ ");
}
else if(i==SIZE&&j==SIZE)//右下角
{
printf("┘");
}
else if(i==0)
{
printf("┬ ");
}
else if(i==SIZE)
{
printf("┴ ");
}
else if(j==0)
{
printf("├ ");
}
else if(j==SIZE)
{
printf("┤");
}
else
{
if( borad[i][j]==EMPTY)
printf("┼ ");
else if(borad[i][j]=='b'&&borad[i][j]!='w')
printf("X ");
else if(borad[i][j]=='w'&&borad[i][j]!='b')
printf("O ");
}
}
printf("\n");
}
}
void win_or_loseBlack()
{
int i = 0;
int j = 0;
for(i=0;i<SIZE;i++)//横排
{
for(j=0;j<SIZE-4;j++)
{
if (borad[i][j] == borad[i][j+1]
&& borad[i][j+1] == borad[i][j+2]
&& borad[i][j+2] == borad[i][j+3]
&& borad[i][j+3] == borad[i][j+4]
&& borad[i][j] !=EMPTY)
{
win=1;
break;
}
}
}
for(j=0;j<SIZE;j++)//竖排
{
for(i=0;i<SIZE;i++)
{
if (borad[i][j] == borad[i + 1][j]
&& borad[i + 1][j] == borad[i + 2][j]
&& borad[i + 2][j] == borad[i + 3][j]
&& borad[i + 3][j] == borad[i + 4][j]
&& borad[i][j] !=EMPTY)
{
win=1;
break;
}
}
}
for(i=0;i<SIZE;i++)
{
if (borad[i][i] == borad[i + 1][i + 1]
&& borad[i + 1][i + 1] == borad[i + 2][i + 2]
&& borad[i + 2][i + 2] == borad[i + 3][i + 3]
&& borad[i + 3][i + 3] == borad[i + 4][i + 4]
&& borad[i][i] !=EMPTY)
{
win=1;
break;
}
if (borad[i][i + 4] == borad[i + 1][i + 3]
&& borad[i + 1][i + 3] == borad[i + 2][i + 2]
&& borad[i + 2][i + 2] == borad[i + 3][i + 1]
&& borad[i + 3][i + 1] == borad[i + 4][i]
&& borad[i][i + 4] !=EMPTY)
{
win=1;
break;
}
}
}
void win_or_loseWhite()
{
int i = 0;
int j = 0;
for(i=0;i<SIZE;i++)//横排
{
for(j=0;j<SIZE-4;j++)
{
if (borad[i][j] == borad[i][j+1]
&& borad[i][j+1] == borad[i][j+2]
&& borad[i][j+2] == borad[i][j+3]
&& borad[i][j+3] == borad[i][j+4]
&& borad[i][j] !=EMPTY)
{
wni=1;
break;
}
}
}
for(j=0;j<SIZE;j++)//竖排
{
for(i=0;i<SIZE;i++)
{
if (borad[i][j] == borad[i + 1][j]
&& borad[i + 1][j] == borad[i + 2][j]
&& borad[i + 2][j] == borad[i + 3][j]
&& borad[i + 3][j] == borad[i + 4][j]
&& borad[i][j] !=EMPTY)
{
wni=1;
break;
}
}
}
for(i=0;i<SIZE;i++)//斜的
{
if (borad[i][i] == borad[i + 1][i + 1]//左斜
&& borad[i + 1][i + 1] == borad[i + 2][i + 2]
&& borad[i + 2][i + 2] == borad[i + 3][i + 3]
&& borad[i + 3][i + 3] == borad[i + 4][i + 4]
&& borad[i][i] !=EMPTY)
{
wni=1;
break;
}
if (borad[i][i + 4] == borad[i + 1][i + 3]//右斜
&& borad[i + 1][i + 3] == borad[i + 2][i + 2]
&& borad[i + 2][i + 2] == borad[i + 3][i + 1]
&& borad[i + 3][i + 1] == borad[i + 4][i]
&& borad[i][i + 4] !=EMPTY)
{
wni=1;
break;
}
}
}
大一新生作业之五子棋对战-。-(bgm版)
最新推荐文章于 2024-12-19 19:34:49 发布