📄 win32集成.cpp
字号:
// win32集成.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
#include <math.h>
#include <time.h>
#include<stdio.h>//sprintf用
char str[100];
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//导弹打飞机程序
void purse(HDC hdc){
double vd,xd,yd,xdd,ydd,vf,xf,yf,xff,yff,s,dt,s0;
vd=150;vf=100;xd=200;yd=200;xf=200;yf=100;s0=5;dt=0.0001;
s=100;
MoveToEx(hdc,200,250,NULL);LineTo(hdc,200,50);
MoveToEx(hdc,150,200,NULL);LineTo(hdc,300,200);
while(s>s0){
xdd=xd,ydd=yd;
xd=xd+dt*vd*(xf-xd)/s;
yd=yd-dt*vd*(yd-yf)/s;
xff=xf;yff=yf;
xf=xf+dt*vf;
yf=yf;
s=sqrt((xf-xd)*(xf-xd)+(yf-yd)*(yf-yd));
MoveToEx(hdc,xdd,ydd,NULL);
LineTo(hdc,xd,yd);
MoveToEx(hdc,xff,yff,NULL);
LineTo(hdc,xf,yf);
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//(1)函数DrawTree:绘制分形树
int DrawTree(
HDC hDC, //设备描述表句柄
int xStart, int yStart, //树枝或树干的开始位置
double length, //树枝或树干的长
double angle, //树枝或树干的倾斜角度
int num) //递归层次数
{
int xEnd, yEnd;
if(num--==0)return 1;
xEnd = xStart + (int)(length * cos(angle));
yEnd = yStart - (int)(length * sin(angle));
MoveToEx(hDC,xStart, yStart,NULL);LineTo(hDC,xEnd, yEnd);//画树干
DrawTree(hDC, xEnd, yEnd, length*0.6999, angle+0.624, num-1);//画左叉树枝
DrawTree(hDC, xEnd, yEnd, length*0.85999, angle+0.08, num-1);//画中叉树枝
DrawTree(hDC, xEnd, yEnd, length*0.65999,angle-0.6, num-1);//画右叉树枝
return 1;
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//围棋(二人对奕和名局欣赏)
//自定义类WqQP,实现围棋棋盘,下棋子等基本操作
#include <strstrea.h>
#include <math.h>
static int step=0; //步数
static int stepmax=0;
static int SX[200],SY[200];//记录已下棋子坐标
char MJX[362];
char MJY[362];
char wqqs[100]={"沈果孙 vs 杉内寿子" }; // 围棋对局棋手名字
char wqfile[200]= //围棋所在文件路径
// {"c:\\2001fgm\\教程\\I_12\\棋谱记录文件\\Qp5_1.gos"};
{"Qp5_1.gos"};
void transfile(HWND hWnd){
HANDLE hf;//所打开的围棋名局文件
hf=CreateFile(str,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(hf==INVALID_HANDLE_VALUE){
MessageBox(hWnd,"文件1没有打开","文件1没有打开",MB_OK);
return;
}
for(int i=0;i<362;i++){ MJX[i]=' ';MJY[i]=' ';}
int k=0;
char buff[7];
unsigned long temp=7;
while(1){
ReadFile(hf,&buff,sizeof(buff),&temp,NULL);
if(buff[3]<'A'||buff[3]>'S'){stepmax=k; break;}
MJX[k]=buff[3];
MJY[k]=buff[4];
k++;
}
CloseHandle(hf);
}
class WqQP{
protected:
int xPos,yPos; //鼠标坐标
int xP,yP; //棋盘坐标
public:
int QP[21][21];//棋盘:内部从QP[1][1]到QP[19][19],其余为外部
WqQP(){ //构造函数,棋盘初始化
for(int i=1;i<20;i++) for(int j=1;j<20;j++) QP[i][j]=0;
for(int j=0;j<21;j++) QP[0][j]=100;
for(i=0;i<21;i++) QP[i][0]=100;
for(j=0;j<21;j++) QP[20][j]=100;
for(i=0;i<21;i++) QP[i][20]=100;
}
void drawQP(HDC); //画棋盘
void drawBQZ(HDC,int,int);//画黑棋子
void drawWQZ(HDC,int,int);//画白棋子
void drawQZ(HDC,int,int); //画棋子
void drawQZB(HDC xxx,int xx,int yy,int ss);//给当前棋子画红边
void transSB(int,int); //将鼠标坐标换算为棋盘格点坐标
int XX(){return xP;} //得到棋盘坐标
int YY(){return yP;} //得到棋盘坐标
int getQP(int i,int j){return QP[i][j];} //得到棋盘点(i,j)的状态:白子1,黑子-1,空0
void putQP(int i,int j,int n){QP[i][j]=n;}//在棋盘点(i,j)赋值n:白子n=1,黑子n=-1,空n=0
void drawText(HDC xxx,int,int,int,int,char*,int);//在指定位置输出字符串、数字显示,用于提示
int no_QZ(int,int); //(int,int)处已有棋子,或已在棋盘外,不可在此处下子,
void transZB(int,int); //将棋盘格点坐标换算为鼠标坐标
int killQZ(HDC,int); //提走死子
int canplace(HDC hdc,int wb);//判断该点是否能下
void play3(HDC,HWND);//名局欣赏下法
void transQJ(int);//将棋谱中的下法转化为棋盘坐标
//计算机对称下法:(jsj_dc_xx,jsj_dc_yy)为(int,int)的对称点
int jsj_dc_xx(int,int); int jsj_dc_yy(int,int);
//对弈下法:二人对弈play1; 人机对弈play2
void play1(HDC,HWND hWnd);void play2(HDC);
//作业:(0)将棋盘画得更好;(1)定义方法no_qz并应用
// (2)定义方法killQZ并应用;
// (3)定义方法play2并应用
};
//**********类WqQP的成员函数定义:******************************
void WqQP::drawQP(HDC xxx){//绘围棋棋盘过程:drawQP(HDC xxx)
HPEN qppen;
SetBkColor( xxx, RGB(255, 255, 0));
qppen=CreatePen(PS_SOLID,1,RGB(0,0,0));
SelectObject(xxx,qppen);
MoveToEx(xxx,0,0,NULL); LineTo(xxx,0,400);
MoveToEx(xxx,400,0,NULL); LineTo(xxx,400,400);
MoveToEx(xxx,0,400,NULL); LineTo(xxx,400,400);
for(int i=1;i<20;i++){
MoveToEx(xxx,20*i,20,NULL); LineTo(xxx,20*i,380);
}
for(i=1;i<20;i++){
MoveToEx(xxx,20,20*i,NULL); LineTo(xxx,380,20*i);
}
DeleteObject(qppen);
for(i=1;i<20;i++)
for(int j=1;j<20;j++){
if(QP[i][j]==-1) drawBQZ( xxx,i,j);
if(QP[i][j]==1) drawWQZ( xxx,i,j);
}
SetTextColor( xxx, RGB(0, 0, 255 ));
TextOut(xxx,60,430,"贵州大学计算机软件与理论研究所:2000,8",38);
}
void WqQP::transQJ(int ss){
xP=MJX[ss]-'A'+1;
yP=MJY[ss]-'A'+1;
if(ss%2==0) QP[xP][yP]=-1;
else QP[xP][yP]=1;
}
void WqQP::drawWQZ(HDC xxx,int xx,int yy){//画白棋子
HBRUSH h0Brush;//oldbrush;
HPEN hPen,hOldPen;
hPen=CreatePen(PS_SOLID,1,RGB(0,0,0));
hOldPen=(HPEN)SelectObject(xxx,hPen);
h0Brush= CreateSolidBrush(RGB(250,250,250));
// oldbrush=(HBRUSH)SelectObject(xxx,h0Brush);
SelectObject(xxx, h0Brush);
Ellipse(xxx, xx*20-9,yy*20-9,xx*20+9,yy*20+9);
SelectObject(xxx,hOldPen);
DeleteObject(hPen);
SelectObject(xxx,GetStockObject(NULL_BRUSH));// SelectObject(xxx,NULL);
DeleteObject(h0Brush);
}
void WqQP::drawBQZ(HDC xxx,int xx,int yy){//画黑棋子
HBRUSH h0Brush;//oldbrush;
HPEN hPen,hOldPen;
hPen=CreatePen(PS_SOLID,1,RGB(0,0,0));
hOldPen=(HPEN)SelectObject(xxx,hPen);
h0Brush= CreateSolidBrush(RGB(0,0,0));
//oldbrush=(HBRUSH)SelectObject(xxx,h0Brush);
SelectObject(xxx, h0Brush);
Ellipse(xxx, xx*20-9,yy*20-9,xx*20+9,yy*20+9);
SelectObject(xxx,GetStockObject(NULL_BRUSH));
SelectObject(xxx,hOldPen);
DeleteObject(hPen);
DeleteObject(h0Brush);
}
//给当前棋子画红边
void WqQP::drawQZB(HDC xxx,int xx,int yy,int ss){
static int fnPenStyle = PS_SOLID;
static int nPenWidth = 1;
static COLORREF crPenColor = 255;//边的颜色:红
HBRUSH hBrush;
if(ss%2==0) hBrush=CreateSolidBrush(RGB(0,0,0));
else hBrush=CreateSolidBrush(RGB(255,255,255));
HPEN h0Pen,oldpen; h0Pen = CreatePen(fnPenStyle, nPenWidth, crPenColor);
// SelectObject(xxx, h0Pen);
oldpen=(HPEN)SelectObject(xxx,h0Pen);
Ellipse(xxx, xx*20-9,yy*20-9,xx*20+9,yy*20+9);
SelectObject(xxx,oldpen);
DeleteObject(h0Pen);
DeleteObject(hBrush);
}
void WqQP::drawQZ(HDC xxx,int xx,int yy){
//画棋子的边
static int fnPenStyle = PS_SOLID;
static int nPenWidth = 1;
static COLORREF crPenColor = 255;//边的颜色:红
HPEN h0Pen; h0Pen = CreatePen(fnPenStyle, nPenWidth, crPenColor);
SelectObject(xxx, h0Pen);
Ellipse(xxx, xx*20-9,yy*20-9,xx*20+9,yy*20+9);// 绘制棋子
DeleteObject(h0Pen);
}
void WqQP::drawText(HDC xxx,int t,int b,int l,int r,char *z,int x){
RECT rr;//设定重画区
rr.top=t; rr.bottom=b; rr.left=l; rr.right=r;
char szBuffer[160]; ostrstream(szBuffer,160)<<z<<" "<<x<<ends;
DrawText(xxx,szBuffer,strlen(szBuffer),&rr,DT_EXPANDTABS);
}
//将鼠标坐标(xPos,yPos)换算为棋盘格点坐标(xP,yP)
void WqQP::transSB(int xPos,int yPos){
if(xPos % 20 > 10){xPos=xPos+xPos%20;}else {xPos=xPos-xPos%20;}
if(yPos % 20 > 10){yPos=yPos+yPos%20;}else {yPos=yPos-yPos%20;}
xP=(xPos)/20; yP=(yPos)/20;
}
///**
//计算机对称下法:(jsj_dc_xx,jsj_dc_yy)为(int,int)的对称点
int WqQP::jsj_dc_xx(int xx,int yy){
return 21-xx;
}
int WqQP::jsj_dc_yy(int xx,int yy){
return 21-yy;
}
//**/
//二人对弈
void WqQP::play1(HDC hdc,HWND hWnd){
SX[step]=XX();SY[step]=YY();
if(xP>20||yP>20) return;
if(QP[xP][yP]==0){
QP[xP][yP]=-1;
if(step%2==0){//偶数步,从第step=0步开始,下黑棋
if(killQZ(hdc,1)==1){//判断死子
drawWQZ(hdc,SX[step-1],SY[step-1]);
drawBQZ(hdc,XX(),YY());//画黑子
InvalidateRect(hWnd,NULL,1);//提走死子
UpdateWindow(hWnd);
drawQZB(hdc,XX(),YY(),step);//画红边
++step;
}
else if(canplace(hdc,-1)==1){
if(step>1) drawWQZ(hdc,SX[step-1],SY[step-1]);//画大白子,覆盖上次当前棋子红边记号,step=0时不覆盖
drawBQZ(hdc,XX(),YY());//画黑子
++step;
drawQZB(hdc,XX(),YY(),step);//画红边
}
else {
MessageBox(hWnd,"此处下子可是违反规则的哟!","警告",MB_OK);
QP[xP][yP]=0;
}
}
else {//奇数步,下白棋
QP[xP][yP]=1;
if(killQZ(hdc,-1)==1){//判断死子
drawBQZ(hdc,SX[step-1],SY[step-1]);
drawWQZ(hdc,XX(),YY());
InvalidateRect(hWnd,NULL,1);//提走死子
UpdateWindow(hWnd);//提走死子
drawQZB(hdc,XX(),YY(),step);//画红边
++step;
}
else if(canplace(hdc,1)==1){
drawBQZ(hdc,SX[step-1],SY[step-1]);//覆盖上次当前棋子记号
drawWQZ(hdc,XX(),YY());//画白子
++step;
drawQZB(hdc,XX(),YY(),step);//画红边
}
else {
MessageBox(hWnd,"此处下子可是违反规则的哟!","警告",MB_OK);
QP[xP][yP]=0;
}
}
}
}
void WqQP::play3(HDC hdc,HWND hWnd){
InvalidateRect(hWnd,NULL,1);
UpdateWindow(hWnd);
step=0;
while(step<=stepmax){
Sleep(500);
transQJ(step);
SX[step]=XX();SY[step]=YY();
if(step%2==0){
if(killQZ(hdc,1)==1){//判断死子
drawWQZ(hdc,SX[step-1],SY[step-1]);
drawBQZ(hdc,XX(),YY());//画黑子
InvalidateRect(hWnd,NULL,1);//提走死子
UpdateWindow(hWnd);
drawQZB(hdc,XX(),YY(),step);//画红边
++step;
}
else{
if(step>1) drawWQZ(hdc,SX[step-1],SY[step-1]);//画大白子,覆盖上次当前棋子红边记号,step=0时不覆盖
drawBQZ(hdc,XX(),YY());//画黑子
++step;
drawQZB(hdc,XX(),YY(),step);//画红边
}
}
else{
if(killQZ(hdc,-1)==1){
drawBQZ(hdc,SX[step-1],SY[step-1]);
drawWQZ(hdc,XX(),YY());
InvalidateRect(hWnd,NULL,1);//提走死子
UpdateWindow(hWnd);//提走死子
drawQZB(hdc,XX(),YY(),step);//画红边
++step;
}
else{
drawBQZ(hdc,SX[step-1],SY[step-1]);//覆盖上次当前棋子记号
drawWQZ(hdc,XX(),YY());//画白子
++step;
drawQZB(hdc,XX(),YY(),step);//画红边
}
}
}
}
int WqQP::killQZ(HDC hdc,int wb){//k为步数,wb是指提的是白子还是黑子
struct QUEUE{
int x;int y;
};
struct QUEUE que[200];
int front;int rear;
int flag;
int cankill=0;
int di[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int i=XX();int j=YY();
for(int h=0;h<4;h++){
front=0;rear=-1;
flag=0;
int x2=i+di[h][0];int y2=j+di[h][1];
if(QP[x2][y2]==wb){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -