//main.cpp 1 #if defined(UNICODE) && !defined(_UNICODE) 2 #define _UNICODE 3 #elif defined(_UNICODE) && !defined(UNICODE) 4 #define UNICODE 5 #endif 6 7 #include <tchar.h> 8 #include <windows.h> 9 #include "matrix.h" 10 #include "matrix.cpp" 11 #include <stdio.h> 12 #include <pthread.h> 13 #define BKCOLOR RGB(200,200,200) 14 int score=0; 15 int WIDTH=400; 16 int HEIGHT=440; 17 Matrix ma; 18 int width=40;//WIDTH/ma.getN(); 19 int height=40;//HEIGHT/ma.getN(); 20 PAINTSTRUCT ps ; 21 static HBRUSH red_brush =CreateSolidBrush (RGB(255,0,0)); 22 static HBRUSH green_brush =CreateSolidBrush (RGB(0,255,0)); 23 static HBRUSH blue_brush =CreateSolidBrush (RGB(0,0,255)); 24 static HBRUSH yellow_brush =CreateSolidBrush (RGB(255,255,0)); 25 static HBRUSH purple_brush =CreateSolidBrush (RGB(255,0,255)); 26 static HBRUSH white_brush =CreateSolidBrush (BKCOLOR); 27 Point * s_lattice=new Point(0,0),*d_lattice=new Point(0,0); 28 bool paint_path=false; 29 int path_color=0; 30 Point* sou=new Point(); 31 Point* dest=new Point(); 32 Point* mouse=new Point(0,0); 33 Point *position=new Point(0,0); 34 Link<Point *>* path; 35 HBRUSH hBrush ; 36 HDC hdc; 37 POINT pc; 38 RECT client_rect; 39 int score_height; 40 41 /* Declare Windows procedure */ 42 LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); 43 44 /* Make the class name into a global variable */ 45 TCHAR szClassName[ ] = _T("Game"); 46 47 int WINAPI WinMain (HINSTANCE hThisInstance, 48 HINSTANCE hPrevInstance, 49 LPSTR lpszArgument, 50 int nCmdShow) { 51 HWND hwnd; /* This is the handle for our window */ 52 MSG messages; /* Here messages to the application are saved */ 53 WNDCLASSEX wincl; /* Data structure for the windowclass */ 54 55 /* The Window structure */ 56 wincl.hInstance = hThisInstance; 57 wincl.lpszClassName = szClassName; 58 wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */ 59 wincl.style = CS_DBLCLKS; /* Catch double-clicks */ 60 wincl.cbSize = sizeof (WNDCLASSEX); 61 62 /* Use default icon and mouse-pointer */ 63 wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); 64 wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); 65 wincl.hCursor = LoadCursor (NULL, IDC_ARROW); 66 wincl.lpszMenuName = NULL; /* No menu */ 67 wincl.cbClsExtra = 0; /* No extra bytes after the window class */ 68 wincl.cbWndExtra = 0; /* structure or the window instance */ 69 /* Use Windows‘s default colour as the background of the window */ 70 wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; 71 72 /* Register the window class, and if it fails quit the program */ 73 if (!RegisterClassEx (&wincl)) 74 return 0; 75 76 /* The class is registered, let‘s create the program*/ 77 hwnd = CreateWindowEx ( 78 0, /* Extended possibilites for variation */ 79 szClassName, /* Classname */ 80 _T("GLine"), /* Title Text */ 81 WS_OVERLAPPEDWINDOW, /* default window */ 82 CW_USEDEFAULT, /* Windows decides the position */ 83 CW_USEDEFAULT, /* where the window ends up on the screen */ 84 WIDTH, /* The programs width */ 85 HEIGHT, /* and height in pixels */ 86 HWND_DESKTOP, /* The window is a child-window to desktop */ 87 NULL, /* No menu */ 88 hThisInstance, /* Program Instance handler */ 89 NULL /* No Window Creation data */ 90 ); 91 92 /* Make the window visible on the screen */ 93 ShowWindow (hwnd, nCmdShow); 94 95 /* Run the message loop. It will run until GetMessage() returns 0 */ 96 while (GetMessage (&messages, NULL, 0, 0)) { 97 /* Translate virtual-key messages into character messages */ 98 TranslateMessage(&messages); 99 /* Send message to WindowProcedure */ 100 DispatchMessage(&messages); 101 } 102 103 /* The program return-value is 0 - The value that PostQuitMessage() gave */ 104 return messages.wParam; 105 } 106 void select_brush(int color,HDC hdc) { 107 108 switch(color) { 109 case RED: 110 SelectObject (hdc, red_brush); 111 break; 112 case GREEN: 113 SelectObject (hdc, green_brush); 114 break; 115 case BLUE: 116 SelectObject (hdc, blue_brush); 117 break; 118 case YELLOW: 119 SelectObject (hdc, yellow_brush); 120 break; 121 case PURPLE: 122 SelectObject (hdc, purple_brush); 123 break; 124 default: 125 SelectObject (hdc, white_brush); 126 } 127 } 128 void paint_lattice(Point * p,HDC hdc,int color) { //-1 original color 129 int left=p->y*width; 130 int right=(p->y+1)*width; 131 int top=p->x*height+score_height; 132 int bottom=(p->x+1)*height+score_height; 133 if(color!=-1) { 134 select_brush(color,hdc); 135 } else { 136 select_brush(ma.get_color(p),hdc); 137 } 138 Ellipse (hdc,left,top,right,bottom) ; 139 } 140 void mouse_to_position() { 141 position->x=(mouse->y-score_height)/height; 142 position->y=mouse->x/width; 143 } 144 char *get_score_string(char* content){ 145 char *message=new char[50]; 146 sprintf (message, "%s %d .",content,score); 147 return message; 148 } 149 int show_score(HWND hwnd,char* content,char* title) { 150 MessageBox(hwnd,get_score_string(content),title,NULL); 151 } 152 void after_show_path(HWND hwnd) { 153 ma.set_flag(dest,ma.get_color(sou)); 154 ma.set_blank(sou); 155 ma.random_set(2); 156 ma.eliminate(score); 157 InvalidateRect (hwnd,NULL, true) ; 158 if(ma.is_all_not_blank()) { 159 show_score(hwnd,"GAME OVER.Your score is","你输了"); 160 } 161 } 162 struct args { 163 HWND hwnd; 164 HDC hdc; 165 }; 166 void* show_path(void* ar) { 167 path=new Link<Point *>(); 168 ma.get_path(sou,dest,path); 169 struct args* arg=(struct args*) ar; 170 HWND hwnd=arg->hwnd; 171 HDC hdc=arg->hdc; 172 if(path->get_first()==NULL) { 173 return 0; 174 } 175 //path->show_link(); 176 path->seek_to_first(); 177 Point* node=path->get_first(); 178 path_color=ma.get_color(node); 179 s_lattice=node; 180 paint_path=true; 181 while((node=path->get_next())!=NULL) { 182 d_lattice=node; 183 InvalidateRect (hwnd,NULL, true) ; 184 Sleep(300); 185 } 186 paint_path=false; 187 after_show_path(hwnd); 188 } 189 190 void init_GUI(HWND hwnd,HDC hdc) { 191 SetBkColor(hdc,BKCOLOR); 192 for(int i=0; i<ma.getN(); i++) { 193 for(int j=0; j<ma.getN(); j++) { 194 Point* p=new Point(i,j); 195 paint_lattice(p,hdc,-1); 196 delete p; 197 } 198 } 199 char * score=get_score_string("You Score: "); 200 TextOut(hdc,0,score_height/4,score,strlen(score)); 201 } 202 203 void sou_dest(HWND hwnd,HDC hdc) { 204 static pthread_t show_path_pth; 205 static int ret; 206 mouse_to_position(); 207 if(sou!=NULL) { 208 if(ma.get_color(position)==BLANK) { 209 *dest=*position; 210 struct args arg; 211 arg.hdc=hdc; 212 arg.hwnd=hwnd; 213 ret= pthread_create( &show_path_pth, NULL, show_path, &arg ); //参数:创建的线程id,线程参数,线程运行函数的起始地址,运行函数的参数 214 if( ret != 0 ) { //创建线程成功返回0 215 printf("pthread_create error:error_code=%d\n",ret ); 216 } 217 } else { 218 *sou=*position; 219 } 220 } else { 221 if(ma.get_color(position)!=BLANK) { 222 *sou=*position; 223 } 224 } 225 } 226 /* This function is called by the Windows function DispatchMessage() */ 227 228 LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { 229 switch (message) { /* handle the messages */ 230 case WM_CREATE: { 231 ma.init(); 232 GetClientRect (hwnd, &client_rect) ; 233 WIDTH=client_rect.right-client_rect.left ; 234 HEIGHT=client_rect.bottom-client_rect.top ; 235 score_height=HEIGHT/10; 236 width=WIDTH/ma.getN(); 237 height=(HEIGHT-score_height)/ma.getN(); 238 InvalidateRect (hwnd,NULL, true) ; 239 } 240 break; 241 case WM_DESTROY: 242 PostQuitMessage (0); /* send a WM_QUIT to the message queue */ 243 break; 244 case WM_LBUTTONUP: { 245 GetCursorPos(&pc); 246 ScreenToClient(hwnd,&pc); 247 mouse->x=pc.x; 248 mouse->y=pc.y; 249 sou_dest(hwnd,hdc); 250 } 251 break; 252 case WM_RBUTTONUP: { 253 show_score(hwnd,"","得分"); 254 } 255 break; 256 case WM_PAINT: { 257 hdc = BeginPaint (hwnd, &ps) ; 258 init_GUI(hwnd,hdc); 259 if(paint_path) { 260 paint_lattice(s_lattice,hdc,BLANK); 261 paint_lattice(d_lattice,hdc,path_color); 262 } 263 EndPaint (hwnd, &ps) ; 264 } 265 break; 266 default: /* for messages that we don‘t deal with */ 267 return DefWindowProc (hwnd, message, wParam, lParam); 268 } 269 return 0; 270 }
//matrix.h 1 #ifndef MATRIX_H_INCLUDED 2 #define MATRIX_H_INCLUDED 3 #pragma once 4 5 #define BLANK 0 6 #define RED 1 7 #define GREEN 2 8 #define BLUE 3 9 #define YELLOW 4 10 #define PURPLE 5 11 12 class Point { 13 private : 14 15 public: 16 int x; 17 int y; 18 Point(int xx,int yy); 19 Point(); 20 bool operator ==(Point p); 21 Point& operator =(Point p); 22 void show(); 23 }; 24 class node { 25 public : 26 int x; 27 int y; 28 node* father; 29 node(); 30 node(int xx,int yy,node* f); 31 }; 32 template<class T> 33 class LinkNode { 34 private: 35 36 public: 37 T po; 38 LinkNode* next; 39 LinkNode(T pp); 40 LinkNode& operator=(LinkNode nn); 41 void show_node(); 42 }; 43 template <class T> 44 class Link { 45 private: 46 LinkNode<T>* head=NULL; 47 LinkNode<T>* p=NULL; 48 LinkNode<T>* tail=NULL; 49 public: 50 friend class Matrix; 51 void seek_to_first(); 52 T get_first(); 53 T remove_first(); 54 T get_next(); 55 void add_to_first(T t); 56 void add_to_last(T t); 57 void show_link(); 58 }; 59 class Matrix { 60 private: 61 int N=10; 62 int COLOR_NUM=5; 63 int** matrix; 64 int max_random_set_num=5; 65 int get_random_num(int x); 66 void print_matrix() ; 67 bool random_set_one() ; 68 bool is_out(int x,int y) ; 69 public: 70 int getN() ; 71 Matrix() ; 72 void init(); 73 void set_flag(Point* p,int color) ; 74 void set_blank(Point* p); 75 bool is_in_que(int** in,int x,int y) ; 76 void get_path(Point *s,Point* dest,Link<Point *> *&l) ; 77 void random_set(int n) ; 78 int get_color(Point *p) ; 79 int get_color(int x,int y); 80 int sget_color(int dir,int x,int y,int n,int color); 81 void test_line(int x,int y,int dir,int &score); 82 int eliminate(int & score) ; 83 bool is_all_not_blank(); 84 }; 85 #endif // MATRIX_H_INCLUDED
//matrix.cpp 1 #include "matrix.h" 2 3 #include <stdlib.h> 4 #include <time.h> 5 #include <stdio.h> 6 7 8 Point::Point(int xx,int yy) { 9 x=xx; 10 y=yy; 11 } 12 Point::Point() {} 13 bool Point::operator ==(Point p) { 14 return x==p.x&y==p.y; 15 } 16 Point& Point::operator =(Point p) { 17 x=p.x; 18 y=p.y; 19 } 20 void Point::show() { 21 printf("(%d,%d)->",x,y); 22 } 23 24 node::node() {} 25 node::node(int xx,int yy,node* f) { 26 x=xx; 27 y=yy; 28 father=f; 29 } 30 31 template<class T> 32 LinkNode<T>::LinkNode(T pp) { 33 po=pp; 34 next=NULL; 35 } 36 template<class T> 37 LinkNode<T>& LinkNode<T>::operator=(LinkNode<T> nn) { 38 po=nn.p; 39 next=nn.next; 40 } 41 template<class T> 42 void LinkNode<T>::show_node() { 43 po->show(); 44 } 45 46 template<class T> 47 void Link<T>::seek_to_first() { 48 p=head; 49 } 50 template<class T> 51 T Link<T>::get_first() { 52 return head==NULL?NULL:head->po; 53 } 54 template<class T> 55 T Link<T>::remove_first() { 56 if(head==NULL)return NULL; 57 T *temp=&(head->po); 58 head=head->next; 59 return *temp; 60 } 61 template<class T> 62 T Link<T>::get_next() { 63 if(p==tail)return NULL; 64 if(p==NULL)return NULL; 65 if(p->next==NULL)return NULL; 66 p=p->next; 67 return p->po; 68 } 69 template<class T> 70 void Link<T>::add_to_first(T t) { 71 LinkNode<T>* x=new LinkNode<T>(t); 72 x->next=head; 73 head=x; 74 if(tail=NULL) { 75 tail=head; 76 return ; 77 } 78 } 79 template<class T> 80 void Link<T>::add_to_last(T t) { 81 LinkNode<T>* x=new LinkNode<T>(t); 82 if(head==NULL) { 83 head=x; 84 x->next=NULL; 85 tail=x; 86 return ; 87 } 88 tail->next=x; 89 x->next=NULL; 90 tail=x; 91 } 92 template<class T> 93 void Link<T>::show_link() { 94 printf("show_link:\n"); 95 p=head; 96 while(p!=NULL) { 97 p->show_node(); 98 p=p->next; 99 } 100 printf("\n"); 101 } 102 103 104 int Matrix::get_random_num(int x) { 105 int n=rand()%x; 106 return n; 107 } 108 void Matrix::print_matrix() { 109 printf("matrix:\n"); 110 for(int i=0; i<N; i++) { 111 for(int j=0; j<N; j++) { 112 printf("%d",matrix[i][j]); 113 } 114 printf("\n"); 115 } 116 } 117 118 bool Matrix::random_set_one() { 119 int x=get_random_num(N),y=get_random_num(N); 120 if(matrix[x][y]!=BLANK) { 121 return false; 122 } 123 Point *p=new Point(x,y); 124 set_flag(p,get_random_num(COLOR_NUM)+1); 125 return true; 126 } 127 bool Matrix::is_out(int x,int y) { 128 return x<0|x>=N|y<0|y>=N; 129 } 130 131 132 int Matrix::getN() { 133 return N; 134 } 135 Matrix::Matrix() { 136 matrix=new int*[N]; 137 for(int i=0; i<N; i++) { 138 matrix[i]=new int[N]; 139 for(int j=0; j<N; j++) { 140 matrix[i][j]=BLANK; 141 } 142 } 143 srand(time(0)); 144 } 145 146 void Matrix::init() { 147 random_set(N*N/4); 148 //print_matrix(); 149 } 150 void Matrix::set_flag(Point* p,int color) { 151 matrix[p->x][p->y]=color; 152 } 153 void Matrix::set_blank(Point* p) { 154 matrix[p->x][p->y]=BLANK; 155 } 156 157 bool Matrix::is_in_que(int** in,int x,int y) { 158 return false; 159 } 160 161 void Matrix::get_path(Point *s,Point* dest,Link<Point *> *&l) { 162 if(!(get_color(s)!=BLANK&&get_color(dest)==BLANK))return ; 163 static int xc[4]= {-1,1,0,0}; 164 static int yc[4]= {0,0,-1,1}; 165 int xt,yt; 166 Link<node*> que; 167 node* temp=new node(s->x,s->y,NULL); 168 que.add_to_last(temp); 169 node* dest_node; 170 bool flag=true; 171 bool** in=new bool*[N]; 172 for(int i=0; i<N; i++) { 173 in[i]=new bool[N]; 174 for(int j=0; j<N; j++) { 175 in[i][j]=false; 176 } 177 } 178 in[temp->x][temp->y]=true; 179 while(flag&&que.get_first()!=NULL) { 180 temp=que.remove_first(); 181 if(temp==NULL) { 182 break; 183 } 184 for(int i=0; i<4; i++) { 185 xt=temp->x+xc[i]; 186 yt=temp->y+yc[i]; 187 if((!is_out(xt,yt))&&(!in[xt][yt])) { 188 node* new_node=new node(xt,yt,temp); 189 if(xt==dest->x&&yt==dest->y) { 190 dest_node=new_node; 191 flag=false; 192 break; 193 } 194 if(get_color(xt,yt)==BLANK) { 195 in[xt][yt]=true; 196 que.add_to_last(new_node); 197 } 198 } 199 } 200 } 201 if(flag) { 202 return ; 203 } 204 node *n=dest_node; 205 while(n!=NULL) { 206 Point *lp=new Point(n->x,n->y); 207 l->add_to_first(lp); 208 n=n->father; 209 } 210 } 211 void Matrix::random_set(int n) { 212 int i=0; 213 srand(time(0)); 214 int try_time=0; 215 while(i<n&&try_time<N*N*N) { 216 try_time++; 217 if(random_set_one()) { 218 i++; 219 } 220 } 221 return ; 222 } 223 int Matrix::get_color(Point *p) { 224 return matrix[p->x][p->y]; 225 } 226 int Matrix::get_color(int x,int y) { 227 return matrix[x][y]; 228 } 229 int Matrix::sget_color(int dir,int x,int y,int n,int color) { 230 switch(dir) { 231 case 0: 232 y+=n; 233 break; 234 case 1: 235 x+=n; 236 y+=n; 237 break; 238 case 2: 239 x+=n; 240 break; 241 case 3: 242 x+=n; 243 y-=n; 244 } 245 if(is_out(x,y)) { 246 return -1; 247 } 248 if(color!=-1) { 249 matrix[x][y]=color; 250 return -1; 251 } 252 return get_color(x,y); 253 } 254 void Matrix::test_line(int x,int y,int dir,int &score) { 255 int color=get_color(x,y); 256 if(color==BLANK)return ; 257 for(int k=0; k<5; k++) { 258 if(sget_color(dir,x,y,k,-1)!=color) { 259 return ; 260 } 261 } 262 int k=0; 263 for( ;; k++) { 264 if(sget_color(dir,x,y,k,-1)!=color)break; 265 sget_color(dir,x,y,k,BLANK); 266 } 267 score+=k; 268 } 269 int Matrix::eliminate(int & score) { 270 for(int i=0; i<N; i++) { 271 for(int j=0; j<N; j++) { 272 for(int k=0; k<4; k++) { 273 test_line(i,j,k,score); 274 } 275 } 276 } 277 } 278 bool Matrix::is_all_not_blank() { 279 for(int i=0; i<N; i++) { 280 for(int j=0; j<N; j++) { 281 if(matrix[i][j]==BLANK) { 282 return false; 283 } 284 } 285 } 286 return true; 287 }
旧函数:
1 // int tree(node * n,node* father,Point* dest,node * temp) 2 // { 3 // static int xc[4]= {-1,1,0,0}; 4 // static int yc[4]= {0,0,-1,1}; 5 // if(n==NULL) 6 // { 7 // return -1; 8 // } 9 // printf("n:(%d,%d) ",n->x,n->y); 10 // n->father=father; 11 // int xt,yt; 12 // Link<Point *> nei; 13 // printf("neis: "); 14 // for(int i=0; i<4; i++) 15 // { 16 // xt=n->x+xc[i]; 17 // yt=n->y+yc[i]; 18 // if((!is_out(xt,yt))&&nn[xt][yt].father==NULL&&nn[xt][yt].father==temp) 19 // { 20 // 21 // if(xt==dest->x&&yt==dest->y) 22 // { 23 // printf("(%d,%d) ",xt,yt); 24 // nn[xt][yt].father=n; 25 // return 0; 26 // } 27 // if(get_color(xt,yt)==BLANK) 28 // { 29 // printf("(%d,%d) ",xt,yt); 30 // Point *ptemp=new Point(xt,yt); 31 // nei.add_to_first(ptemp); 32 // nn[xt][yt].father=n; 33 // } 34 // } 35 // } 36 // printf("\n"); 37 // nei.seek_to_first(); 38 // Point *pp; 39 // if((pp=nei.get_first())!=NULL){ 40 // if(tree(&nn[pp->x][pp->y],n,dest,temp)==0)return 0; 41 // } 42 // while((pp=nei.get_next())!=NULL){ 43 // if(tree(&nn[pp->x][pp->y],n,dest,temp)==0)return 0; 44 // } 45 // return 1; 46 // } 47 48 49 50 51 52 53 // void get_path(Point *s,Point* dest,Link<Point *> *&l) 54 // { 55 // printf("1\n"); 56 // node* temp=new node; 57 // tree(&nn[s->x][s->y],temp,dest,temp); 58 // printf("\n\nfathers:\n"); 59 // 60 // for(int i=0; i<N; i++) 61 // { 62 // for(int j=0; j<N; j++) 63 // { 64 // if(nn[i][j].father!=NULL){ 65 // printf("(%d",nn[i][j].x); 66 // printf(",%d)",nn[i][j].y); 67 // printf("->(%d",nn[i][j].father->x); 68 // printf(",%d)\n",nn[i][j].father->y); 69 // } 70 // } 71 // printf("\n"); 72 // } 73 // 74 // node* n=&nn[dest->x][dest->y]; 75 // 76 // while(n!=temp) 77 // { 78 // Point *lp=new Point(n->x,n->y); 79 // l->add_to_first(lp); 80 // n=n->father; 81 // } 82 // printf("3\n"); 83 // }
时间: 2024-10-12 17:44:08