动画控制函数(设置光标位置)需要包含什么头文件?
没用头文件加进去都是错?用linux g++编译器
没用头文件加进去都是错?用linux g++编译器
2015-07-06
/* .h文件 */ #ifndef _maze_h_ #define _maze_h_ #include <stdio.h> #define wall 1 // #typedef 1 wall; #define road 0 // #typedef 0 road; int map[10][11] = { {road,road,road,road,road,road,road,road,road,road,road}, {road,wall,wall,wall,wall,wall,wall,wall,wall,wall,road}, {road,wall,wall,wall,wall,road,wall,wall,road,wall,road}, {road,wall,road,road,road,road,wall,wall,road,wall,road}, {road,wall,wall,wall,wall,road,wall,wall,road,wall,road}, {road,wall,wall,road,road,road,road,road,road,wall,road}, {road,wall,wall,road,wall,wall,road,road,road,wall,road}, {road,wall,road,road,wall,wall,wall,wall,wall,wall,road}, {road,wall,road,wall,wall,wall,wall,wall,wall,wall,road}, {road,road,road,road,road,road,road,road,road,road,road} }; class maze { public: maze(int x,int y):m_X(x),m_Y(y){} void setMazeMap(int x,int y); // void setMazeWall(char c); // void drawMap(); private: int m_X; // int m_Y; // }; enum direction{Up,Down,Left,Right}; class mazer { public: mazer():direct(Up){} void setMazerPosition(int x,int y); void resetMazerPosition(int x,int y); void MazerStart(void); direction getDirection(void); void setDirection(direction D); int getPx(void); int getPy(void); private: direction direct; int m_Px; int m_Py; }; #define moveto(x,y) printf("\033[%d;%dH",(x),(y)) #define clear() printf("\033[2J") #define setRed() printf("\033[31m") #define setBlack() printf("\033[30m") //#define setBlack() printf("\033[30m\033[4m") #endif /* .cpp文件 */ #include <iostream> #include <string> #include <unistd.h> #include <time.h> #include <sys/time.h> #include "maze.h" using namespace std; int main() { clear(); moveto(1,1); //////////////////////////////////////////// maze myMaze(10,11); myMaze.setMazeMap(10,11); mazer myMazer; myMazer.setMazerPosition(10,3); myMazer.MazerStart(); //////////////////////////////////////////// moveto(15,1); setBlack(); return 0; } void maze::setMazeMap(int x,int y) { int i=0,j=0; for(i=0;i<x;i++) { for(j=0;j<y;j++) { if(map[i][j]==wall) cout << "*"; else cout << " "; } cout <<endl; } } void mazer::setMazerPosition(int x,int y) { m_Px = x; m_Py = y; moveto(x,y); setRed(); cout << "P" << endl ; // sleep(1); // n second usleep(500000); //n usecond } void mazer::resetMazerPosition(int x,int y) { moveto(x,y); setBlack(); cout << " " << endl; } int mazer::getPx() { return m_Px; } int mazer::getPy() { return m_Py; } direction mazer::getDirection() { return direct; } void mazer::setDirection(direction D) { direct = D; } void mazer::MazerStart() { int Px,Py; int over=0,step=0; Px = getPx(); Py = getPy(); while(1) { direction D = getDirection(); switch (D) { case Up: resetMazerPosition(Px,Py); setMazerPosition(Px-1,Py); Px = getPx(); Py = getPy(); if(map[Px-1][Py+1-1] != wall) { if((map[Px-1-1][Py-1] !=wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1; else setDirection(Right); } else if(map[Px-1-1][Py-1] == wall) { if(map[Px-1][Py-1-1] != wall)setDirection(Left); else setDirection(Down); } else setDirection(Up); break; case Down: resetMazerPosition(Px,Py); setMazerPosition(Px+1,Py); Px = getPx(); Py = getPy(); if(map[Px-1][Py-1-1] != wall) { if((map[Px+1-1][Py-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1; else setDirection(Left); } else if(map[Px+1-1][Py-1] == wall) { if(map[Px-1][Py+1-1] != wall)setDirection(Right); else setDirection(Up); } else setDirection(Down); break; case Left: resetMazerPosition(Px,Py); setMazerPosition(Px,Py-1); Px = getPx(); Py = getPy(); if(map[Px-1-1][Py-1] != wall) { if((map[Px-1][Py-1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1; else setDirection(Up); } else if(map[Px-1][Py-1-1] == wall) { if(map[Px+1-1][Py-1] != wall)setDirection(Down); else setDirection(Right); } else setDirection(Left); break; case Right: resetMazerPosition(Px,Py); setMazerPosition(Px,Py+1); Px = getPx(); Py = getPy(); if(map[Px+1-1][Py-1] != wall) { if((map[Px-1][Py+1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1; else setDirection(Down); } else if(map[Px-1][Py+1-1] == wall) { if(map[Px-1-1][Py-1] != wall)setDirection(Up); else setDirection(Left); } else setDirection(Right); break; } if(over) { moveto(12,1); cout << "Congratulation! use step " << step <<endl; break; } step ++; } }
/*** .h文件 */ #ifndef _maze_h_ #define _maze_h_ #include <stdio.h> #define wall 1 // #typedef 1 wall; #define road 0 // #typedef 0 road; int map[10][11] = { {road,road,road,road,road,road,road,road,road,road,road}, {road,wall,wall,wall,wall,wall,wall,wall,wall,wall,road}, {road,wall,wall,wall,wall,road,wall,wall,road,wall,road}, {road,wall,road,road,road,road,wall,wall,road,wall,road}, {road,wall,wall,wall,wall,road,wall,wall,road,wall,road}, {road,wall,wall,road,road,road,road,road,road,wall,road}, {road,wall,wall,road,wall,wall,road,road,road,wall,road}, {road,wall,road,road,wall,wall,wall,wall,wall,wall,road}, {road,wall,road,wall,wall,wall,wall,wall,wall,wall,road}, {road,road,road,road,road,road,road,road,road,road,road} }; class maze { public: maze(int x,int y):m_X(x),m_Y(y){} void setMazeMap(int x,int y); // void setMazeWall(char c); // void drawMap(); private: int m_X; // int m_Y; // }; enum direction{Up,Down,Left,Right}; class mazer { public: mazer():direct(Up){} void setMazerPosition(int x,int y); void resetMazerPosition(int x,int y); void MazerStart(void); direction getDirection(void); void setDirection(direction D); int getPx(void); int getPy(void); private: direction direct; int m_Px; int m_Py; }; #define moveto(x,y) printf("\033[%d;%dH",(x),(y)) #define clear() printf("\033[2J") #define setRed() printf("\033[31m") #define setBlack() printf("\033[30m") //#define setBlack() printf("\033[30m\033[4m") #endif
/* .cpp文件 */
#include <iostream>
#include <string>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "maze.h"
using namespace std;
int main()
{
clear();
moveto(1,1);
////////////////////////////////////////////
maze myMaze(10,11);
myMaze.setMazeMap(10,11);
mazer myMazer;
myMazer.setMazerPosition(10,3);
myMazer.MazerStart();
////////////////////////////////////////////
moveto(15,1);
setBlack();
return 0;
}
void maze::setMazeMap(int x,int y)
{
int i=0,j=0;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(map[i][j]==wall)
cout << "*";
else
cout << " ";
}
cout <<endl;
}
}
void mazer::setMazerPosition(int x,int y)
{
m_Px = x;
m_Py = y;
moveto(x,y);
setRed();
cout << "P" << endl ;
// sleep(1); // n second
usleep(500000); //n usecond
}
void mazer::resetMazerPosition(int x,int y)
{
moveto(x,y);
setBlack();
cout << " " << endl;
}
int mazer::getPx()
{
return m_Px;
}
int mazer::getPy()
{
return m_Py;
}
direction mazer::getDirection()
{
return direct;
}
void mazer::setDirection(direction D)
{
direct = D;
}
void mazer::MazerStart()
{
int Px,Py;
int over=0,step=0;
Px = getPx();
Py = getPy();
while(1)
{
direction D = getDirection();
switch (D)
{
case Up:
resetMazerPosition(Px,Py);
setMazerPosition(Px-1,Py);
Px = getPx();
Py = getPy();
if(map[Px-1][Py+1-1] != wall)
{
if((map[Px-1-1][Py-1] !=wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
else setDirection(Right);
}
else if(map[Px-1-1][Py-1] == wall)
{
if(map[Px-1][Py-1-1] != wall)setDirection(Left);
else setDirection(Down);
}
else setDirection(Up);
break;
case Down:
resetMazerPosition(Px,Py);
setMazerPosition(Px+1,Py);
Px = getPx();
Py = getPy();
if(map[Px-1][Py-1-1] != wall)
{
if((map[Px+1-1][Py-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
else setDirection(Left);
}
else if(map[Px+1-1][Py-1] == wall)
{
if(map[Px-1][Py+1-1] != wall)setDirection(Right);
else setDirection(Up);
}
else setDirection(Down);
break;
case Left:
resetMazerPosition(Px,Py);
setMazerPosition(Px,Py-1);
Px = getPx();
Py = getPy();
if(map[Px-1-1][Py-1] != wall)
{
if((map[Px-1][Py-1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
else setDirection(Up);
}
else if(map[Px-1][Py-1-1] == wall)
{
if(map[Px+1-1][Py-1] != wall)setDirection(Down);
else setDirection(Right);
}
else setDirection(Left);
break;
case Right:
resetMazerPosition(Px,Py);
setMazerPosition(Px,Py+1);
Px = getPx();
Py = getPy();
if(map[Px+1-1][Py-1] != wall)
{
if((map[Px-1][Py+1-1] != wall) && ( (Px==1) || (Px==10) || (Py==1) || (Py==11)))over = 1;
else setDirection(Down);
}
else if(map[Px-1][Py+1-1] == wall)
{
if(map[Px-1-1][Py-1] != wall)setDirection(Up);
else setDirection(Left);
}
else setDirection(Right);
break;
}
if(over)
{
moveto(12,1);
cout << "Congratulation! use step " << step <<endl;
break;
}
step ++;
}
}
举报