昨天晚上突发奇想想做一下,然后就真实的感受到了自己的菜,不过终究还是做出来了,虽然丑的一逼,还那么多bug,不过还是很高兴,不会任何东西然后把它写出来还是很不容易了
我用了graphics.h,这个vc没有,我就把它抄下写进去了,,,
graphics.h:
/******************************************************
* EasyX Library for C++ (Ver:v20121115(beta))
* http://www.easyx.cn
*
* graphics.h
* 基于 EasyX.h,实现在 VC 下实现简单绘图。
* 同时,为了兼容 Turbo C/C++ 和 Borland C/C++ 等一系
* 列开发环境中的 BGI 绘图库函数,做了相应扩展。
******************************************************/
#pragma once
#include <easyx.h>
// 兼容 initgraph 绘图模式的宏定义(无实际意义)
#define DETECT 0
#define VGA 0
#define VGAHI 0
// BGI 格式的初始化图形设备,默认 640 x 480
HWND initgraph(int* gdriver, int* gmode, char* path)
{
return initgraph(640, 480, NULL);
}
// 画多边形
void drawpoly(int numpoints, const int *polypoints)
{
polygon(numpoints, polypoints);
}
// 画填充的多边形
void fillpoly(int numpoints, const int *polypoints)
{
fillpolygon(numpoints, polypoints);
}
// 获取最大的宽度值
int getmaxx()
{
return getwidth() - 1;
}
// 获取最大的高度值
int getmaxy()
{
return getheight() - 1;
}
MyGraph.h:
#include <graphics.h>
#include <conio.h>
class Cell
{
public :
int x , y ;
bool vis ;
void setx( int newx )
{
x = newx ;
}
void sety( int newy )
{
y = newy ;
}
void setvis( bool newvis )
{
vis = newvis ;
}
bool getvis()
{
return vis ;
}
int getx()
{
return x ;
}
int gety()
{
return y ;
}
void printcell()
{
fillrectangle( x * 40 , y * 40 , ( x + 1 ) * 40 , ( y + 1 ) * 40 ) ;
}
Cell()
{
x = 0 ;
y = 0 ;
vis = 0 ;
}
};
class Grid
{
public :
int x , y ;
int num , next ;
bool changeable ;
void setx( int newx )
{
x = newx ;
}
void sety( int newy )
{
y = newy ;
}
void setnum( int newnum )
{
num = newnum ;
}
void setchangeable( bool flag )
{
changeable = flag ;
}
void setnext()
{
if( num == 0 ) next = 7 ;
else if( num == 1 ) next = 1 ;
else if( num == 2 ) next = 8 ;
else if( num == 3 ) next = 10 ;
else if( num == 4 ) next = 14 ;
else if( num == 5 ) next = 17 ;
else if( num == 6 ) next = 18 ;
else if( num == 7 ) next = 0 ;
else if( num == 8 ) next = 9 ;
else if( num == 9 ) next = 10 ;
else if( num == 10 ) next = 2 ;
else if( num == 11 ) next = 12 ;
else if( num == 12 ) next = 13 ;
else if( num == 13 ) next = 3 ;
else if( num == 14 ) next = 15 ;
else if( num == 15 ) next = 16 ;
else if( num == 16 ) next = 4 ;
else if( num == 17 ) next = 5 ;
else if( num == 18 ) next = 6 ;
}
int getx()
{
return x ;
}
int gety()
{
return y ;
}
int getnext()
{
return next ;
}
int getnum()
{
return num ;
}
bool getchangeable()
{
return changeable ;
}
void Changenum()
{
num = next ;
setnext() ;
}
Grid()
{
x = 0 ;
y = 0 ;
changeable = 0 ;
num = 0 ;
next = 0 ;
}
};
main:
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <dos.h>
#include "MyGraph.h"
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
int MAX = 50000 ;
bool GameOver = 0 ;
Cell cell[16][9] ;
Grid grid ;
int key1 , key ;
bool CellGrid[19][4][4]={
{
{1,1,1,1}, // 0
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, //1
{1,1,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,0,0,0}, //2
{1,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,0,1,0}, //3
{1,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,1,0,0}, //4
{1,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, //5
{0,1,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,1,1,0}, //6
{0,0,1,1},
{0,0,0,0},
{0,0,0,0}
},
{
{1,0,0,0}, //7
{1,0,0,0},
{1,0,0,0},
{1,0,0,0}
},
{
{1,1,0,0}, //8
{1,0,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, //9
{0,0,1,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,1,0,0}, //10
{0,1,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0}, //11
{1,0,0,0},
{1,1,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, //12
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{1,1,0,0}, //13
{0,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{1,0,0,0}, //14
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,1,1,0}, //15
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}
},
{
{0,1,0,0}, //16
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
},
{
{0,1,0,0}, //17
{1,1,0,0},
{1,0,0,0},
{0,0,0,0}
},
{
{1,0,0,0}, //18
{1,1,0,0},
{0,1,0,0},
{0,0,0,0}
}
};
void ChangeAble()
{
int i , j ;
int x = grid.getx() ;
int y = grid.gety() ;
bool flag = 0 ;
for( i = 0 ; i < 4 ; i ++ )
for( j = 0 ; j < 4 ; j ++ )
{
if( y + i >= 14 || x + j >= 8 ) continue ;
if( cell[y+i][x+j].getvis() && CellGrid[grid.getnext()][i][j] )
{
flag = 1 ;
break ;
}
}
if( flag == 1 ) grid.setchangeable( 0 ) ;
else grid.setchangeable( 1 ) ;
}
bool check( int Dir )
{
int dx[3] = { 0 , -1 , 1 } ;
int dy[3] = { 1 , 0 , 0 } ;
int num = grid.getnum() ;
int x = grid.getx() ;
int y = grid.gety() ;
for( int i = 0 ; i < 4 ; i ++ )
for( int j = 0 ; j < 4 ; j ++ )
{
if( cell[y+i+dy[Dir]][x+j+dx[Dir]].getvis() && CellGrid[num][i][j] ) return false ;
if( CellGrid[num][i][j] && ( x + j + dx[Dir] < 0 || x + j + dx[Dir] > 8 ) ) return false ;
}
return true ;
}
bool jduge()
{
int num = grid.getnum() ;
int x = grid.getx() ;
int y = grid.gety() ;
for( int i = 0 ; i < 4 ; i ++ )
for( int j = 0 ; j < 4 ; j ++ )
{
if( cell[y+i][x+j].getvis() && CellGrid[num][i][j] ) return false ;
}
return true ;
}
int main()
{
int i , j , score = 0 ;
srand( (unsigned)time( NULL ) );
initgraph( 360 , 600 ) ;
for( i = 0 ; i < 15 ; i ++ )
for( j = 0 ; j < 9 ; j ++ )
{
cell[i][j].sety( i ) ;
cell[i][j].setx( j ) ;
cell[i][j].setvis( 0 ) ;
}
for( i = 0 ; i <= 8 ; i ++ )
{
cell[15][i].sety( 15 ) ;
cell[15][i].setx( i ) ;
cell[15][i].setvis( 1 ) ;
}
while( !GameOver )
{
if( score >= 10 ) MAX = 40000 ;
else if( score >= 20 ) MAX = 30000 ;
else if( score >= 30 ) MAX = 20000 ;
else if( score >= 50 ) MAX = 10000 ;
else if( score >= 100 ) MAX = 5000 ;
int temp ;
temp = rand() % 7 ;
grid.setnum( temp ) ;
grid.setx( 0 ) ;
grid.sety( 0 ) ;
grid.setnext() ;
if( !jduge() )
{
GameOver = 1 ;
break ;
}
while( 1 ){
cleardevice();
for( i = 0 ; i < 15 ; i ++ )
for( j = 0 ; j < 9 ; j ++ )
if( cell[i][j].getvis() ) cell[i][j].printcell() ;
for( i = 0 ; i < 4 ; i ++ )
for( j = 0 ; j < 4 ; j ++ )
if( !cell[grid.gety()+i][grid.getx()+j].getvis() && CellGrid[grid.getnum()][i][j] )
cell[grid.gety()+i][grid.getx()+j].printcell() ;
if( !check( 0 ) )
{
bool Flag[16] ;
memset( Flag , 0 , sizeof( Flag ) ) ;
for( i = 0 ; i < 4 ; i ++ )
for( j = 0 ; j < 4 ; j ++ )
{
if( CellGrid[grid.getnum()][i][j] )
cell[grid.gety()+i][grid.getx()+j].setvis( 1 ) ;
}
for( i = 0 ; i < 15 ; i ++ )
{
int flag = 0 ;
for( j = 0 ; j < 9 ; j ++ ){
if( !cell[i][j].getvis() ) flag = 1 ;
if( cell[i][j].getvis() ) Flag[i] = 1 ;
}
if( !flag ){
for( j = 0 ; j < 9 ; j ++ )
cell[i][j].setvis( 0 ) ;
Flag[i] = 0 ;
score ++ ;
}
}
for( i = 14 ; i >= 0 ; i -- )
{
if( Flag[i] == 0 )
{
for( j = i - 1 ; j >= 0 ; j -- )
if( Flag[j] == 1 ) break ;
if( j < 0 ) continue ;
for( int k = 0 ; k < 9 ; k ++ )
{
bool vis = cell[j][k].getvis() ;
cell[i][k].setvis( vis ) ;
cell[j][k].setvis( 0 ) ;
Flag[i] = 1 ;
Flag[j] = 0 ;
}
}
}
break ;
}
int Count = 0 ;
while( !kbhit() )
{
Count ++ ;
if( Count == MAX ) break ;
}
if( Count == MAX )
{
int y = grid.gety() ;
grid.sety( y + 1 ) ;
continue ;
}
key = getch() ;
if( key == 72 ) //up
{
ChangeAble() ;
if( grid.getchangeable() )
grid.Changenum() ;
}
else if( key == 80 ) // down
{
while( check( 0 ) )
{
int y = grid.gety() ;
grid.sety( y + 1 ) ;
}
}
else if( key == 75 ) // left
{
if( check( 1 ) )
{
int x = grid.getx() ;
grid.setx( x - 1 ) ;
}
}
else if( key == 77 ) // right
{
if( check( 2 ) )
{
int x = grid.getx() ;
grid.setx( x + 1 ) ;
}
}
key1 = key ;
}
}
getch();
closegraph();
cout << "你的成绩是:" << score << endl ;
return 0 ;
}