#include<gl/glut.h>
#include <string>
#include<math.h>
#include<stdlib.h>
#include"Renju.h"
#include<iostream>
#include <afxwin.h>
#include<ctime>
using namespace std;
Renju RB_GLOBAL;
status S;
const int n = 20;
const GLfloat R = 0.06f;
const GLfloat Pi = 3.1415926536f;
//半径12px,直径24px,边界为20px
//半径0.06f,直径0.12f,边界0.1f
GLfloat getCoord(int x) ;
int CHECK(int x, int y) ;
int CHECK2(int x, int y) ;
string ToString(Arr2 POS, int type , int cmd);
void place(status s);
int JudgeWin(Arr2 pos);
status count();
Arr2 posi;
void myDisplay(void)
{
int tmp=0;
int day = 200;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.8f,0.2f);
glRectf(-0.90f,-0.90f,0.90f,0.90f);
glLineWidth(3.0f);
glBegin(GL_LINE_LOOP); //棋盘边框
glColor3f(0.0f,0.0f,0.0f);
glVertex2f(-0.84f,-0.84f);
glVertex2f(-0.84f,0.84f);
glVertex2f(0.84f,0.84f);
glVertex2f(0.84f,-0.84f);
glEnd();
glLineWidth(1.0f);
glBegin(GL_LINES); //棋盘内框
for(tmp=1; tmp<= 13 ; tmp++)
{
GLfloat x =-0.84 + (0.12*tmp);
glVertex2f(x , -0.84f);
glVertex2f(x , 0.84f);
glVertex2f(-0.84f , x);
glVertex2f( 0.84f , x);
}
glEnd();
glPointSize(6.0f);
glBegin(GL_POINTS);
glVertex2f(-0.84f+(0.12*3) ,-0.84f+(0.12*3));
glVertex2f(-0.84f+(0.12*3) ,-0.84f+(0.12*11));
glVertex2f(-0.84f+(0.12*7) ,-0.84f+(0.12*7));
glVertex2f(-0.84f+(0.12*11) ,-0.84f+(0.12*3));
glVertex2f(-0.84f+(0.12*11) ,-0.84f+(0.12*11));
glEnd();
for(int i = 0 ; i<15 ; i++)
{
for(int j = 0 ; j<15 ; j++)
{
if(RB_GLOBAL.position[i][j]!=0)
{
glBegin(GL_POLYGON);
if(RB_GLOBAL.position[i][j]== -1)
{
glColor3f(1.0f,1.0f,1.0f);
for(tmp=0; tmp<n; ++tmp)
{
glVertex2f(R*cos(2*Pi/n*tmp)- 0.84f + i * 0.12f, R*sin(2*Pi/n*tmp) + 0.84f - j * 0.12f);
}
}
else
{
glColor3f(0.0f,0.0f,0.0f);
for(tmp=0; tmp<n; ++tmp)
{
glVertex2f(R*cos(2*Pi/n*tmp) - 0.84f + i * 0.12f, R*sin(2*Pi/n*tmp) + 0.84f - j * 0.12f);
}
}
glEnd();
}
}
}
glFlush();
}
void mymouse(int button,int state,int x,int y)//鼠标响应部分
{
if(state==GLUT_DOWN&&button==GLUT_LEFT_BUTTON)
{
GLfloat posx ,posy;
posx = getCoord(x);
posy = getCoord(y);
int locx = 7, locy = 7;
if(posx>0)
{
posx += 0.06f;
locx += posx/0.12f ;
}
else
{
posx -= 0.06f;
locx += posx/0.12f + 1;
}
if(posy>0)
{
posy += 0.06f;
locy += posy/0.12f;
}
else
{
posy -= 0.06f;
locy += posy/0.12f + 1;
}
//cout<<locx<<"\t"<<locy<<endl;
if(locx == 15)
locx = 14;
if(locy == 15)
locy = 14;
if(RB_GLOBAL.position[locx][locy] != 0)
{
MessageBox(NULL,"出错啦!","这个位置已经有棋子了!",MB_OK);
return;
}
RB_GLOBAL.latest[0] = locx;
RB_GLOBAL.latest[1] = locy;
RB_GLOBAL.latest[2] = RB_GLOBAL.currentPlayer;
RB_GLOBAL.position[locx][locy] = RB_GLOBAL.currentPlayer;
RB_GLOBAL.currentPlayer = -RB_GLOBAL.currentPlayer;
for(int i = 0 ; i<15 ; i++)
for(int j = 0 ; j<15 ; j++)
posi.POS[i][j] = RB_GLOBAL.position[i][j];
myDisplay();
//AI Start From Here
cout<<RB_GLOBAL.latest[0]+" "+RB_GLOBAL.latest[1]<<endl;
cout<<"横\t"+ToString(posi,1,RB_GLOBAL.latest[1])<<endl;
cout<<"竖\t"+ToString(posi,2,RB_GLOBAL.latest[0])<<endl;
cout<<"右下\t"+ToString(posi,3,abs(RB_GLOBAL.latest[1]-RB_GLOBAL.latest[0]))<<endl;
cout<<"左下\t"+ToString(posi,4,RB_GLOBAL.latest[1]+RB_GLOBAL.latest[0])<<endl;
if(JudgeWin(posi)!=0)
{
MessageBox(NULL,"恭喜你你赢了!\n请退出","恭喜你!",MB_OK);
exit(0);
}
S = count(); //计算最佳位置
place(S); //放置并重绘
for(int i = 0 ; i<15 ; i++)
for(int j = 0 ; j<15 ; j++)
posi.POS[i][j] = RB_GLOBAL.position[i][j];
if(JudgeWin(posi)!=0)
{
MessageBox(NULL,"电脑获胜!\n请退出","恭喜电脑!",MB_OK);
exit(0);
}/**/
myDisplay();
}
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("090824214-CG-AI-高川宏");
glutDisplayFunc(&myDisplay);
glutMouseFunc(mymouse);
glutMainLoop();
return 0;
}
GLfloat getCoord(int x)
{
return x / 400.0f * 2.0f - 1.0f;
}
int JudgeWin(Arr2 POS)
{
int pos[15][15];
for(int i = 0 ; i < 15 ; i++)
{
for(int j = 0 ; j <15 ; j++)
{
pos[i][j] = POS.POS[i][j];
}
}
//horizontal
for(int i = 0 ; i < 15 ; i++) //行
{
for(int i1 = 0 ; i1 < 10 ; i1++) //列
{
//System.out.println(i+" "+i1+" "+position[i][i1]);
if( pos[i][i1]!=0 &&
pos[i][i1] == pos[i][i1+1] &&
pos[i][i1] ==pos[i][i1+2] &&
pos[i][i1] == pos[i][i1+3] &&
pos[i][i1] == pos[i][i1+4])
{
return pos[i][i1];
}
}
}
//vertical
for(int i = 0 ; i < 15 ; i++) //列
{
for(int i1 = 0 ; i1 < 10 ; i1++) //行
{
if( pos[i1][i]!=0 &&
pos[i1][i] == pos[i1+1][i] &&
pos[i1][i] == pos[i1+2][i] &&
pos[i1][i] == pos[i1+3][i] &&
pos[i1][i] == pos[i1+4][i])
{
return pos[i1][i];
}
}
}
//slash
for(int i = 0 ; i <= 10 ; i++)
{
for(int i1 = 0 ; i1 <= 10 ; i1++)
{
if( pos[i][i1]!=0 &&
pos[i][i1] == pos[i+1][i1+1] &&
pos[i][i1] == pos[i+2][i1+2] &&
pos[i][i1] == pos[i+3][i1+3] &&
pos[i][i1] == pos[i+4][i1+4] )
{
return pos[i][i1];
}
}
}
for(int i = 14 ; i >= 4 ; i--)
{
for(int i1 = 0 ; i1 <= 10 ; i1++)
{
if( pos[i][i1] != 0 &&
pos[i][i1] == pos[i-1][i1+1] &&
pos[i][i1] == pos[i-2][i1+2] &&
pos[i][i1] == pos[i-3][i1+3] &&
pos[i][i1] == pos[i-4][i1+4] )
{
return pos[i][i1];
}
}
}
return 0;
}
void place(status s)
{
int x = RB_GLOBAL.latest[0],y= RB_GLOBAL.latest[1];
int tmp;
if( RB_GLOBAL.currentStep == 0)
{
srand((unsigned)time(0));
tmp = rand() % 8 + 1;
if(tmp == 1)
{
x--;y--;
}
if(tmp == 2)
{
x--;
}
if(tmp == 3)
{
x--;y++;
}
if(tmp == 4)
{
y--;
}
if(tmp == 5)
{
y++;
}
if(tmp == 6)
{
x++;y--;
}
if(tmp == 7)
{
x++;
}
if(tmp == 8)
{
x++;y++;
}
if(x<0)
x=-x;
if(y<0)
y=-y;
S.positionx = x;S.positiony = y;
}
else
x=S.positionx;y=S.positiony;
RB_GLOBAL.position[x][y]=RB_GLOBAL.currentPlayer;
RB_GLOBAL.currentStep++;
RB_GLOBAL.currentPlayer = -RB_GLOBAL.currentPlayer;
for(int i = 0 ; i<15; i++)
{
for(int j = 0 ; j<15 ; j++)
{
cout<<(char)((RB_GLOBAL.position[i][j]==-1)?'x':RB_GLOBAL.position[i][j]);
}cout<<endl;
}
myDisplay();
}
status count()
{
status maxv;
maxv.value = -1;
status value;
for(int x = 0 ; x <= 14 ; x++)
{
for(int y = 0 ; y <= 14 ; y++)
{
if(x==5&&y==7)
{
int __temp = 1+1;
}
if(RB_GLOBAL.position[x][y] != 0)
continue;
value.value =CHECK(x,y);
value.positionx = x;
value.positiony = y;
if(maxv.value < value.value)
{
maxv.positionx = x;
maxv.positiony = y;
maxv.value = value.value;
}
}
}
cout<<maxv.positionx<<" "<<maxv.positiony<<endl;
return maxv;
}
int CHECK(int x, int y)
{
// TODO Auto-generated method stub
int tmppos [15][15];
for(int i = 0 ; i <=14 ; i++)
for(int j = 0 ; j <= 14 ; j++)
tmppos[j][i]=RB_GLOBAL.position[i][j];
for(int i = 0 ; i <=14 ; i++)
for(int j = 0 ; j <= 14 ; j++)
posi.POS[i][j] = tmppos[j][i];
int value = 0;
string p,q;
int j = -1;
string tmp;
for(int pLoop = 0 ; pLoop<2 ; pLoop++)
{
if(pLoop==0)
{
tmppos[x][y] = RB_GLOBAL.currentPlayer; //共检验两次,第一次检验自己方的优势点
posi.POS[x][y] = RB_GLOBAL.currentPlayer;
}
else
{
tmppos
OpenGL c++ 人工智能 五子棋
3星 · 超过75%的资源 需积分: 22 74 浏览量
2012-01-13
12:34:03
上传
评论 1
收藏 26.08MB RAR 举报
gch0214
- 粉丝: 0
- 资源: 1
最新资源
- HCIP-Datacom笔记 (1).pdf
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
- 介绍离散性制造行业的MES系统流程
- Arduino IDE压缩包版本,2024年4月26日,最新版本
- 基于IDEA-CCNL/Randeng-Pegasus-238M-Summary-Chines微调的中文文本摘要任务源码+数据集
- 自动驾驶-状态估计和定位之直方图滤波(Histogram+Filter)定位应用和源码.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈