C++を学んで対象に向かって蛇を貪る(作風一貫::怠け者)

9907 ワード

#include 
#include 
#include 
using namespace std;

class data
{
private:
	int x;
	int  y;
public:
	data(int i, int ff) :x(i), y(ff){ ; }
	virtual~data(){ cout << "data................
"; } virtual int getInt(){ return x; } virtual int getY(){ return y; } virtual void setint(int i){ x = i; } virtual void setY(int ff){ y = ff; } virtual void print() = 0; }; class she_s :public data { public: she_s(int x, int ff) :data(x,ff){} ~she_s(){ cout << "she_s...........
"; } void print() { cout << "x==" << getInt() << "y==" << getY(); } }; class shiru:public data { public: shiru(int x, int y) :data(x,y){} ~shiru(){ cout << "shiru...............
"; } void print() { cout << "x==" << this->getInt() << "y ==" <SetL(0); head->SetNode(0); end = head; head->initConut(); head->ADDConut(); } else { node *pnew = new node(p); end->SetNode(pnew); pnew->SetL(end); end = pnew; head->ADDConut(); } } } node *getShiTou(){ return head; } node *getWei(){ return end; } node * getshi(){ return shi; } void Setweiba(node *p){ end = p; } void init_shiwu() { if (!shi) { data *d = new shiru(7, 9); shi = new node(d); shi->initConut(); shi->ADDConut(); return; } } ~l() { cout << "L........." << endl; if (head) { while (head->getNode() != NULL) { head = head->getNode(); delete (head->getL()); head->SetL(0); } delete head; head = end = NULL; } if (shi) { delete shi; shi = NULL; } } private: node *head; node *end; node *shi; }; class re { public: re() :map(0), sp(300), ch(0){ quit = false; } void initmap() { map = (char **)calloc(21, sizeof(char*)); for (int i = 0; i < 21; i++) { map[i] = (char*)calloc(21, sizeof(char)); } if (!map) exit(0); for (int i = 0; i < 20; i++) { map[0][i] = '1'; } for (int i = 0; i < 20; i++) { map[i][0] = '1'; } for (int i = 0; i < 20; i++) { map[i][19] = '1'; } for (int i = 0; i < 20; i++) { map[19][i] = '1'; } } int getsp(){ return sp; } void Setsp(int i){ sp = i; } void show_map() { cout << "\t C++ !!" << endl; cout << "

"; for (int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { if (map[i][j] == '1') cout << "■"; if (map[i][j] == '2') cout << "○"; if (map[i][j] == '3') cout << "●"; if (map[i][j] == '6') cout << "◎"; else if (!map[i][j]) cout << " "; } switch (i) { case 3: cout << "\t :" << p.getShiTou()->getConut()/5; break; case 5: cout << "\t :" << p.getShiTou()->getConut(); break; case 7: cout << "\t :" << getsp(); break; case 9: cout << "\t :" <getConut() -3)* 10; break; case 11: cout << "\t :X" << p.getshi()->getData()->getInt() << " Y :"<getData()->getY(); break; case 17: cout << "\t ESL"; break; case 18: cout << "\t "; break; } cout << endl; } } void init_shiwu() { srand(time(0)); while (1) { int x = rand() % 18 + 1; int y = rand() % 18 + 1; if (map[x][y] != '1') { node *temp = p.getWei(); while (temp) { if (x == temp->getData()->getInt() && y == temp->getData()->getY()) break; temp = temp->getL(); } if (temp) continue; else { p.getshi()->getData()->setint(x); p.getshi()->getData()->setY(y); map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3'; return; } } } } void read_map() { node *ps =p.getWei(); int numer = p.getShiTou()->getConut(); int conut=0; while (ps) { if (!conut) { ++conut; map[ps->getData()->getInt()][ps->getData()->getY()] = '6'; } else if (conut == numer-1) { map[ps->getData()->getInt()][ps->getData()->getY()] = '2'; conut++; } else { ++conut; map[ps->getData()->getInt()][ps->getData()->getY()] = '3'; } ps = ps->getL(); } map[p.getshi()->getData()->getInt()][p.getshi()->getData()->getY()] = '3'; } void Sethead(const int i) { node *ps =p.getShiTou() ; if (!ps) return; map[ps->getData()->getInt()][ps->getData()->getY()] = '\0'; ps->getData()->setint(ps->getNode()->getData()->getInt()); ps->getData()->setY(ps->getNode()->getData()->getY()); map[ps->getData()->getInt()][ps->getData()->getY()] = '2'; int x = p.getWei()->getData()->getInt(); int y = p.getWei()->getData()->getY(); switch (i) { case 1: case 2: while (1) { ps = ps->getNode(); if (ps->getNode() == NULL) break; ps->getData()->setint(ps->getNode()->getData()->getInt()); ps->getData()->setY(ps->getNode()->getData()->getY()); map[ps->getData()->getInt()][ps->getData()->getY()] = '3'; } if (i == 1) { ps->getData()->setint(x-1); ps->getData()->setY(y); map[ps->getData()->getInt()][ps->getData()->getY()] = '6'; } else { ps->getData()->setint(x + 1); ps->getData()->setY(y); map[ps->getData()->getInt()][ps->getData()->getY()] = '6'; } break; case 3: case 4: while(1) { ps = ps->getNode(); if (ps->getNode() == NULL) break; ps->getData()->setint(ps->getNode()->getData()->getInt()); ps->getData()->setY(ps->getNode()->getData()->getY()); map[ps->getData()->getInt()][ps->getData()->getY()] = '3'; } if (i == 3) { ps->getData()->setint(x); ps->getData()->setY(y-1); map[ps->getData()->getInt()][ps->getData()->getY()] = '6'; } else { ps->getData()->setint(x ); ps->getData()->setY(y+1); map[ps->getData()->getInt()][ps->getData()->getY()] = '6'; } break; } } void chishiwu() { node*ps = p.getWei(); if (ps->getData()->getInt() == p.getshi()->getData()->getInt() && ps->getData()->getY() == p.getshi()->getData()->getY()) { p.getShiTou()->ADDConut(); init_shiwu();// addshe_s(); if ((p.getShiTou()->getConut() - 3) % 5 == 0) { Setsp(sp - 50); } } } void addshe_s() { int x = p.getWei()->getData()->getInt(); int y = p.getWei()->getData()->getY(); data *pp = new she_s(x, y); node*ps = new node(pp); node *temp = p.getWei(); temp->SetNode(ps); ps->SetL(temp); p.Setweiba(ps); ps = ps->getL(); while (ps!=p.getShiTou()) { ps->getData()->setint(ps->getL()->getData()->getInt()); ps->getData()->setY(ps->getL()->getData()->getY()); ps = ps->getL(); } } bool simang() { int x = p.getWei()->getData()->getInt(); int y = p.getWei()->getData()->getY(); if (x==0 || x == 19 || y == 0 || y == 19) return true; node *ps = p.getWei()->getL(); node *pa= p.getShiTou(); while (ps != pa) { if (ps->getData()->getInt() == x&&ps->getData()->getY() == y) return true; if (pa->getData()->getInt() == x&&pa->getData()->getY() == y) return true; ps = ps->getL(); if (ps == pa) break; pa = pa->getNode(); if (ps = ps) break; } return false; } void Run(); void Sleep(int num) { clock_t st, en; st = clock(); while (1) { en = clock(); if ((double)(en - st) >= num) { break; } } } ~re() { for (int i = 0; i < 21; i++) { delete []map[i]; } delete []map; cout << "re......." << endl; } private: char **map; //■●○ l p; int sp; bool quit; unsigned char ch; }; void re::Run() { system("color 02"); initmap();// p.init_she();// p.init_shiwu();// read_map();// show_map();// while (1) { if (_kbhit()) { ch = _getch(); if (_kbhit()) { quit = false; Sleep(20); } } switch (ch) { case 'H':// H Sethead(1); break; case 'P':// P Sethead(2); break; case 'K':// K Sethead(3); break; case 'M':// M Sethead(4); break; } chishiwu(); system("cls"); show_map();// if (simang() || ch == 27 ) { cout << "
"; break; } if (quit) { Sleep(getsp()); } quit = true; } } int main111() { while (1) { re *r=new re; r->Run(); delete r; r = NULL; fflush(stdin); system("pause"); } system("pause"); return 0; }