C语言 五子棋2
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#define len 15 int chessmessage[len][len];
int worb = ;
int count = ;
int m; void star_print(); //打印游戏开始函数
void movegb(char key); //光标的移动
void renjiboyi(); //人机博弈
void shuangrenboyi(); //双人博弈
void initial(); //初始化棋盘数据
void borad_print(); //打印棋盘数据
int chessjudge(int x, int y); //判断能否下子
int chessjudgewin(int x, int y, int worb); //判断输赢
int chesscomputer(); //电脑下棋
int chessscore(int x, int y, int worb); //棋盘权值函数
void print(int m); struct zuobiao
{
int x;
int y;
}weizhi = { , }; void gotoxy(int x, int y) //建立坐标函数
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
} void star_print()
{
printf("\n\n\n\n\n");
printf(" ********************* 【五子棋】 *******************\n\n\n");
printf(" *********************【游戏说明】 ******************\n");
printf(" \n");
printf(" 【1】: 黑子由机器使用 \n");
printf(" -------------------------------------------------\n");
printf(" 【2】: 白子由人使用 \n");
printf(" \n");
printf(" ****************************************************\n");
printf("\n\n 【人机博弈按:1】 【双人博弈按:2】 【退出按:Esc】");
} void borad_print()
{ int i, j, k; print(m); for (j = ; j<len; j++)
{
for (i = ; i<len; i++)
{ if (chessmessage[j][i] == )
printf("●");
else if (chessmessage[j][i] == )
printf("○");
else if (j == && i == )
printf("┏");
else if (j == && i == len - )
printf("┓");
else if (j == len - && i == )
printf("┗");
else if (j == len - && i == len - )
printf("┛");
else if (j == && i != && i != len - )
printf("┯");
else if (j != && j != len - && i == )
printf("┠");
else if (j != && j != len - && i == len - )
printf("┨");
else if (j == len - && i != && i != len - )
printf("┷");
else if (j != && j != len - && i != && i != len - )
printf("┼"); if (j> && j<len - && i<len - )
printf("—"); if (j == && i != len - )
printf("━");
else if (j == len - && i != len - )
printf("━"); } printf("\n"); if (j >= && j<len - )
{
for (k = ; k<len; k++)
{
if (k == || k == len - )
printf("┃ ");
else printf("│ ");
} if (j == )
printf(" 【 操作 - PS 】");
if (j == )
printf(" 【 从玩按:n 】");
if (j == )
printf(" 【返回按:Backspace】");
if (j == )
printf(" 【退出按:Esc】");
if(j == )
printf(" 【 空格下子 】");
if(j == )
printf(" 【上,下,左,右】");
if(j == )
printf(" 【 移动光标 】"); }
printf("\n"); }
} void print(int x)
{
if (x == )
printf("\n\n 【五子棋--人机博弈】\n\n\n");
else printf("\n\n 【五子棋--双人博弈】\n\n\n");
} void movegb(char key)/*光标的移动*/
{
switch (key)
{ case 0x48:if (weizhi.y>)weizhi.y--; if (weizhi.y<)weizhi.y = len + ; break;
/*光标向上移动*/ case 0x50:if (weizhi.y<len + )weizhi.y++; if (weizhi.y>len + )weizhi.y = ; break;
/*光标向下移动*/ case 0x4B:if (weizhi.x>-)weizhi.x--; if (weizhi.x<)weizhi.x = len - ; break;
/*光标向左移动*/ case 0x4D:if (weizhi.x<len)weizhi.x++; if (weizhi.x>len - )weizhi.x = ; break;
/*光标向右移动*/
}
gotoxy( * weizhi.x, * weizhi.y);
} void initial()
{
int i, j; for (j = ; j<len; j++) //重置对局数据
{
for (i = ; i<len; i++)
{
chessmessage[i][j] = ;
}
} worb = ; count = ; system("cls"); borad_print(); weizhi.x = ;
weizhi.y = ; gotoxy(weizhi.x, weizhi.y); } int chessjudge(int x, int y)
{
if (chessmessage[x][y] != )
return ; return ;
} int chessjudgewin(int x, int y, int worb)
{
int row, col, count; row = x, col = y; count = ;
while (chessmessage[row][col] == worb && col < len)
{
count++;
col++;
}
col = y - ;
while (chessmessage[row][col] == worb && col >= )
{
count++;
col--;
}
if (count >= )
return ; row = x, col = y; count = ;
while (chessmessage[row][col] == worb && row < len)
{
count++;
row++;
}
row = x - ;
while (chessmessage[row][col] == worb && row >= )
{
count++;
row--;
}
if (count >= )
return ; row = x, col = y; count = ;
while (chessmessage[row][col] == worb && col < len && row < len)
{
count++;
col++;
row++;
}
col = y - , row = x - ;
while (chessmessage[row][col] == worb && col >= && row >= )
{
count++;
col--;
row--;
}
if (count >= )
return ; row = x, col = y; count = ;
while (chessmessage[row][col] == worb && col < len && row >= )
{
count++;
col++;
row--;
}
col = y - , row = x + ;
while (chessmessage[row][col] == worb && col >= && row < len)
{
count++;
col--;
row++;
}
if (count >= )
return ; return ; } int chesscomputer()
{
int i, j, cscore, cscorel, pscore, pscorel, row1, col1, row2, col2, x, y; cscore = pscore = ; row1 = row2 = col1 = col2 = ;
if (count == )
{
srand((unsigned)time(NULL));
row1 = rand() % ;
col1 = rand() % ;
count++; while (chessjudge(row1, col1))
{
row1 = rand() % ;
col1 = rand() % ;
} chessmessage[row1][col1] = ;
worb = - worb; x = abs(weizhi.x - col1);
y = abs(weizhi.y - - row1); if (weizhi.x - col1>)
for (i = ; i < x; i++)
movegb(0x4B);
else for (i = ; i < x; i++)
movegb(0x4D); if (weizhi.y - - row1>)
for (i = ; i < y; i++)
movegb(0x48);
else for (i = ; i < y; i++)
movegb(0x50); printf("●"); } else {
for (i = ; i < len; i++)
{
for (j = ; j < len; j++)
{
if (!chessjudge(i, j))
{
cscorel = chessscore(i, j, ); if (cscorel > cscore)
{
cscore = cscorel;
row1 = i;
col1 = j;
} pscorel = chessscore(i, j, ); if (pscorel > pscore)
{
pscore = pscorel;
row2 = i;
col2 = j;
}
}
}
} if (count == )
{
chessmessage[row1][col1] = ;
worb = - worb;
count++;
x = abs(weizhi.x - col1);
y = abs(weizhi.y - - row1); if (weizhi.x - col1>)
for (i = ; i < x; i++)
movegb(0x4B);
else for (i = ; i < x; i++)
movegb(0x4D); if (weizhi.y - - row1>)
for (i = ; i < y; i++)
movegb(0x48);
else for (i = ; i < y; i++)
movegb(0x50); printf("●"); } else{ if (cscore >= pscore)
{
chessmessage[row1][col1] = ;
worb = - worb;
count++;
x = abs(weizhi.x - col1);
y = abs(weizhi.y - - row1); if (weizhi.x - col1>)
for (i = ; i < x; i++)
movegb(0x4B);
else for (i = ; i < x; i++)
movegb(0x4D); if (weizhi.y - - row1>)
for (i = ; i < y; i++)
movegb(0x48);
else for (i = ; i < y; i++)
movegb(0x50); printf("●"); if (chessjudgewin(row1, col1, ))
{
gotoxy(, );
printf("【 黑子胜利 】");
return ;
}
} else
{
chessmessage[row2][col2] = ;
worb = - worb;
count++;
x = abs(weizhi.x - col2);
y = abs(weizhi.y - - row2); if (weizhi.x - col2>)
for (i = ; i < x; i++)
movegb(0x4B);
else for (i = ; i < x; i++)
movegb(0x4D); if (weizhi.y - - row2>)
for (i = ; i < y; i++)
movegb(0x48);
else for (i = ; i < y; i++)
movegb(0x50);
printf("●"); if (chessjudgewin(row2, col2, ))
{
gotoxy(, );
printf("【 黑子胜利 】");
return ;
}
}
}
} return ;
} int chessscore(int x, int y, int worb) //棋盘权值函数
{
int count3, count4, sumscore = ;
char u, d, l, r, lu, ld, ru, rd; //u上 d下 l左 r右 lu左斜上 ld左斜下 ru右斜上 rd右斜下
int row, col, count1, count2; //1表示横向 2表示竖向 3表示斜上方向 4表示斜下方向 count1 = count2 = count3 = count4 = ;
l = r = u = d = lu = ld = ru = rd = ; chessmessage[x][y] = worb; //模拟下棋 /*---------------------------------------①判断横向棋子并记录空格子---------------------------*/
row = x, col = y + ;
while (chessmessage[row][col] == worb && col < len)
{
count1++;
col++;
}
while (col < len && chessmessage[row][col] == )
{
r++;
col++;
}
row = x, col = y - ;
while (chessmessage[row][col] == worb && col >= )
{
count1++;
col--;
}
while (col >= && chessmessage[row][col] == )
{
l++;
col--;
}
/*------------------------------------------------------------------------------------------*/ /*-------------------------------------②判断竖向棋子并记录空格子---------------------------*/
row = x + , col = y;
while (chessmessage[row][col] == worb && row < len)
{
count2++;
row++;
}
while (row < len && chessmessage[row][col] == )
{
d++;
row++;
}
row = x - , col = y;
while (chessmessage[row][col] == worb && row >= )
{
count2++;
row--;
}
while (row >= && chessmessage[row][col] == )
{
u++;
row--;
}
/*------------------------------------------------------------------------------------------*/ /*------------------------------------③判断斜上向棋子并记录空格子--------------------------*/
row = x - , col = y + ;
while (chessmessage[row][col] == worb && col < len && row >= )
{
count3++;
col++;
row--;
}
while (col < len && row >= && chessmessage[row][col] == )
{
ru++;
col++;
row--;
}
row = x + , col = y - ;
while (chessmessage[row][col] == worb && col >= && row < len)
{
count3++;
col--;
row++;
}
while (col >= && row < len && chessmessage[row][col] == )
{
ld++;
col--;
row++;
}
/*------------------------------------------------------------------------------------------*/ /*-----------------------------------④判断斜下向棋子并记录空格子---------------------------*/
row = x + , col = y + ;
while (chessmessage[row][col] == worb && col < len && row < len)
{
count4++;
col++;
row++;
}
while (col < len && row < len && chessmessage[row][col] == )
{
rd++;
col++;
row++;
}
row = x - , col = y - ;
while (chessmessage[row][col] == worb && col >= && row >= )
{
count4++;
col--;
row--;
}
while (col >= && row >= && chessmessage[row][col] == )
{
lu++;
col--;
row--;
}
/*------------------------------------------------------------------------------------------*/ chessmessage[x][y] = ; if (count1 >= || count2 >= || count3 >= || count4 >= ) //判断五子
{
return sumscore = ;
} if ((count1 == && count2 == && l >= && r >= && u >= && d >= ) ||
(count1 == && count3 == && l >= && r >= && ru >= && ld >= ) ||
(count1 == && count4 == && l >= && r >= && rd >= && lu >= ) ||
(count2 == && count3 == && u >= && d >= && ru >= && ld >= ) ||
(count2 == && count4 == && u >= && d >= && lu >= && rd >= ) ||
(count3 == && count4 == && ru >= && ld >= && lu >= && rd >= )) //判断双活四
{
return sumscore = ;
} if ((count1 == && l >= && r >= ) || (count2 == && u >= && d >= ) ||
(count3 == && ru >= && ld >= ) || (count4 == && lu >= && rd >= )) //判断活四
{
return sumscore = ;
} if ((count1 == && ((l == && r >= ) || (l >= && r == ))) || (count2 == && ((u == && d >= ) || (u >= && d == ))) ||
(count3 == && ((ld == && ru >= ) || (ld >= && ru == ))) || (count4 == && ((lu == && rd >= ) || (lu >= && rd == ))))
{
sumscore = sumscore + ; //成四
} if( ( count1 == && count2 == && l >= && r >= && u >= && d >=) ||
( count1 == && count3 == && l >= && r >= && ru >= && ld >= ) ||
( count1 == && count4 == && l >= && r >= && rd >= && lu >= ) ||
( count2 == && count3 == && u >= && d >= && ru >= && ld >= ) ||
( count2 == && count4 == && u >= && d >= && lu >= && rd >= ) ||
( count3 == && count4 == && ru >= && ld >= && lu >= && rd >=) ) //判断双活三
{
return sumscore + ;
} if ((count1 == && l >= && r >= ) || (count2 == && u >= && d >= ) ||
(count3 == && ru >= && ld >= ) || (count4 == && lu >= && rd >= ))
{
sumscore = sumscore + ;
return sumscore;
} //判断活三 if ((count1 == && count2 == && l >= && r >= && u >= && d >= ) ||
(count1 == && count3 == && l >= && r >= && ru >= && ld >= ) ||
(count1 == && count4 == && l >= && r >= && rd >= && lu >= ) ||
(count2 == && count3 == && u >= && d >= && ru >= && ld >= ) ||
(count2 == && count4 == && u >= && d >= && lu >= && rd >= ) ||
(count3 == && count4 == && ru >= && ld >= && lu >= && rd >= ))
{
sumscore = sumscore + ; //判断双活二
} if ((count1 == && l >= && r >= ) || (count2 == && u >= && d >= ) ||
(count3 == && ru >= && ld >= ) || (count4 == && lu >= && rd >= ))
{
sumscore = sumscore + ; //单活二
} return sumscore; } void renjiboyi()
{
int c = ,w = ;
char key; m = ; while ()
{ initial();
w = ; while ()
{ key = _getch(); if(w == )
{
weizhi.x = ;
weizhi.y = ;
gotoxy(weizhi.x, weizhi.y);
w++;
} if (key == ) //退出游戏
{
exit();
}
else if (key == ) //返回主界面
{
break;
}
else if (key == ) //重新玩
{
break;
}
else if (key == )
{
if (count == len * len)
{
gotoxy(, );
printf("【 平局 】");
count = ;
break;
}
else if (worb == )
{ if (chessjudge(weizhi.y - , weizhi.x) == )
{
printf("○"); chessmessage[weizhi.y - ][weizhi.x] = worb; count++; if (chessjudgewin(weizhi.y - , weizhi.x, worb))
{
gotoxy(, );
printf("【 白子胜利 】"); key = _getch(); while(key != && key != && key != )
key = _getch(); if(key == || key == )
break;
else exit();
} else
{
worb = - worb; c = chesscomputer(); if(c)
{
key = _getch(); while(key != && key != && key != )
{
key = _getch();
} if(key == || key == )
break;
else exit();
}
} } } }
else //光标移动
{
key = _getch();
movegb(key);
} } if (key == )
break; }
} void shuangrenboyi()
{
int w = ;
char key;
m = ; while()
{
initial();
w = ; while()
{
key = _getch(); if(w == )
{
weizhi.x = ;
weizhi.y = ;
gotoxy(weizhi.x, weizhi.y);
w++;
} if (key == ) //退出游戏
{
exit();
}
else if (key == ) //返回主界面
{
break;
}
else if (key == ) //重新玩
{
break;
}
else if(key == )
{
if(worb == )
{ if(chessjudge(weizhi.y-,weizhi.x) == )
{
chessmessage[weizhi.y-][weizhi.x] = ;
printf("○");
count++;
if(chessjudgewin(weizhi.y-,weizhi.x,worb))
{
gotoxy(, );
printf("【 白子胜利 】"); key = _getch(); while(key != && key != && key != )
key = _getch(); if(key == || key == )
break;
else exit(); } worb = - worb;
} } else
{
if(chessjudge(weizhi.y-,weizhi.x) == )
{
chessmessage[weizhi.y-][weizhi.x] = ;
printf("●");
count++;
if(chessjudgewin(weizhi.y-,weizhi.x,worb))
{
gotoxy(, );
printf("【 黑子胜利 】"); key = _getch(); while(key != && key != && key != )
key = _getch(); if(key == || key == )
break;
else exit();
} worb = - worb;
}
} }
else
{
key = _getch(); movegb(key);
} } if (key == )
break;
} } int main()
{
int choice = ; system("title 【五子棋】 --- [14003101班-冉云杰] 制作");
system("color f2");
system("mode con cols=83 lines=35"); while ()
{
star_print(); printf("\n\n\n 【请输入你的选择】:"); while ()
{
choice = _getch(); if (choice == )
{
exit();
} else if (choice == ) //人机博弈
{
renjiboyi();
break;
}
else if (choice == ) //双人博弈
{
shuangrenboyi();
break;
} } system("cls"); } return ;
}
C语言 五子棋2的更多相关文章
- c语言五子棋
#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <conio.h> ...
- C语言 五子棋
#include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> ...
- C 语言控制台实现五子棋项目
花了一天时间实现了控制台五子棋项目,把项目贴上来.也算是告一段落了. 为了进一步了解C语言编程,熟悉优秀的编码风格,提升编码能力,丰富项目经验.所以在编程初期选择了控制台小游戏<单机五子棋> ...
- 用c语言实现简单的五子棋
用c语言实现简单的五子棋 这个小游戏是从零开始的实现的,框架灵感来自于小游戏<走迷宫>. 游戏代码配置: 二维数组+简单逻辑+getch读取键盘+windows函数(刷屏,改颜色,改窗口大 ...
- C/C++编程笔记:C语言开发经典游戏项目《五子棋》,内含源码
上一节给大家分享了扫雷游戏的源代码,本篇文章当然也不会让大家失望,专门针对C语言入门或者学习了部分知识之后的小伙伴来练手的游戏项目——<五子棋大战>,本期并不是使用的easyX,因为考虑到 ...
- 使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网)
使用Java语言编写一个五子棋UI界面并实现网络对战功能(非局域网) 一,前期准备 1,Java IDE(Eclipse)与JDK的安装与配置jdk-15.0.1-免配置路径版提取码:earu免安装版 ...
- C语言实现五子棋简单功能
/******************************************************************** C-4.29-1: 实现五子棋游戏 操作说明:用方向键或者& ...
- 笔记:C语言图形基本函数及实例五子棋游戏
初始化图形界面 int gdriver; int gmode; gdriver = DETECT; initgraph(&gdriver,&gmode,"" ); ...
- C语言实现五子棋
可以称得上史上最简单的五子棋版本了. 可以使用curses库来改进页面和下棋方式. 并且对于输入的坐标没有进行鉴别,如果输入的坐标超过棋盘大小,就会段错误退出. 我改进了一点,但是还是没有完全避免这个 ...
随机推荐
- spark(一)
一.spark 学习 1. spark学习的三种地方: (1)Spark.apache.org 官方文档 (2)spark的源代码的官方网站 https://github.com/apache/ ...
- html5 video,audio控制播放多次,请求/监测全屏状态
audio 播放 Element.play(); 如果在播放中,调用play,从头播放,则需要先load,同时绑定结束方法,如 Element.addEventListener('ended',f ...
- shell函数使用
函数定义格式: 和js有点类似,不过在 shell 中 function 关键字是可选的. 如: sum { // shell 语句 } function sum() { // shell 语句 } ...
- Git之git push不手动输入用户名和密码
每次git push时都要输入用户名和密码,感觉很啰嗦,总结了网上的解决办法,有的发现不可以(原因未知),记录一个对我自己可用的方式,我的是windows. 1:添加环境变量 2:在%HOME%目录下 ...
- centos7安装ZABBIX 3.0+ 邮件报警【OK】
设置主机名: vi /etc/hosts 10.0.0.252 zabbix-server hostnamectl set-hostname 关闭防火墙: systemctl stop firew ...
- tp between
$a = array( 'time' => array('between',[c,d]) ); c<= time <= d
- LightOJ 1364 树形DP
52张扑克牌,问拿到指定数量的4个花色的最少次数期望是多少,其中拿到joker必须马上将其视作一种花色,且要使后续期望最小. 转移很容易想到,主要是两张joker的处理,一个状态除了普通的4个方向的转 ...
- MongoDB - MongoDB CRUD Operations, Query Documents, Query for Null or Missing Fields
Different query operators in MongoDB treat null values differently. The examples on this page use th ...
- 【BZOJ】1537: [POI2005]Aut- The Bus
[算法]DP+线段树求区间max(二维偏序) [题解] 状态转移方程:f[i]=max(f[j]+v[i]),x[j]<x[i]&&y[j]<y[i]. 观察j的条件限制显 ...
- 【BZOJ】3991: [SDOI2015]寻宝游戏 虚树+DFS序+set
[题意]给定n个点的带边权树,对于树上存在的若干特殊点,要求任选一个点开始将所有特殊点走遍后返回.现在初始没有特殊点,m次操作每次增加或减少一个特殊点,求每次操作后的总代价.n,m<=10^5. ...