C++抽象编程——回溯算法(2)——准备Direction文件

我记我们在提到方块走动的位置的时候,说过它有三种走法。但是那个时候是因为它的右边是墙壁。但是如果我们把它放在空位上,那么是不是它就有了4种走法?也就是 东南西北。这个时候我们就有必要为方块的走法来制定一个接口,实现这一行为。下面的代码不提供分析,具体的代码分析参考: C++抽象编程——接口(2)——接口的用途

Direction.h

/*这个文件处理一些关于方位上的应用*/ 

#ifndef _direction_h
#define _direction_h

#include <string>
/*声明枚举类型*/
enum Direction { NORTH, EAST, SOUTH, WEST };
/*
*函数: leftFrom
*用法: Direction dir2 =  leftFrom(Direction dir)
*-------------------------------
*在你原本的方位上向左转
*/ 
Direction leftFrom(Direction dir);//返回类型是Direction
/*
*函数: rightFrom
*用法: Direction dir2 =  rightFrom(Direction dir)
*-------------------------------
*在你原本的方位上向右转
*/ 
Direction rightFrom(Direction dir);
/*
*函数: directionToString
*用法: string str =  directionToString(Direction dir)
*-------------------------------
*将枚举类型转变为string类型,以便输出 
*/ 
std::string directionToString(Direction dir);

#include "Directionpriv.h" 

#endif

Directionpriv.h

/*
*这部分内容为不希望用户看到的内容,虽然这个枚举类型不是类。也就意味着
*没有私有成员,但是我们接口的目的就是为了隐藏程序的复杂性。只要是你觉
*得不希望用户看到并且篡改,你都可以写一个文件用于隐藏,虽然不存在访问
*的限制。C++中也是允许我们为枚举类型定义一些操作的
*/

/*重载插入运算符,以便其能方便输出*/
std::ostream & operator<<(std::ostream &os, Direction dir);

/*重载++运算符(前)*/
Direction operator++(Direction &dir);

/*重载++运算符(后)*/
Direction operator++(Direction &dir, int);//注意重载后缀运算符的时候,后面加int

Direction.cpp

#include <iostream>
#include "Direction.h"
using namespace std;
/*实现左转*/ 
Direction leftFrom(Direction dir){
    return Direction((dir + 3) % 4);//返回该类型的值 
}
/*实现右转*/ 
Direction rightFrom(Direction dir) {
    return Direction((dir + 1) % 4);
}
/*转变为string类输出*/ 
string directionToString(Direction dir) {
    switch(dir){
        case NORTH: 
            return "NORTH";
            case EAST:
                    return "EAST";
                    case SOUTH:
                        return "SOUTH";
                        case WEST:
                            return "WEST";
                            default:
                                return "???";
    }
}
/* << 运算符的重载 */
ostream & operator<<(ostream &os, Direction dir){
    return os << directionToString(dir);
}
/*++dir 重载*/
Direction operator++(Direction &dir){
    return Direction(dir + 1);
} 
/* dir++ 重载*/
Direction operator++(Direction &dir, int){
    Direction old = dir;
    dir = Direction(dir + 1);
    return old;
}

这里注意前缀跟后缀的区别。

测试代码

#include <iostream>
#include "Direction.h"
using namespace std;
int main(){
    for (Direction dir = NORTH; dir <= WEST; dir++){
        cout << dir << endl;
    }
    return 0;
} 

测试结果:

#include #include #include #include #include const int size=13,gameSpeed=500; class node { private: int x; int y; node *next; public: node(); void changeX(int X); void changeY(int Y); void changeNext(node *N); int getX(); int getY(); node *getNext(); }; node::node() { x=0; y=0; next=NULL; } int node::getX() { return x; } int node::getY() { return y; } void node::changeX(int X) { x=X; } void node::changeY(int Y) { y=Y; } void node::changeNext(node *N) { next=N; } node *node::getNext() { return next; } class linkList { protected: node *head; public: linkList();//制造一个有头结点的线性表 int getLength(); node *getNode(int i);//i:0-n int getIndex(node *e);//i:1-n,没有返回0 node *getPriorNode(node *e); node *getNextNode(node *e); void insert(int i,node *e);//i:1-(n+1) void Delete(int i); }; linkList::linkList() { head=new node; head->changeNext(NULL); } int linkList::getLength() { node *p=head->getNext(); int i=0; while(p) { i++; p=p->getNext(); } return i; } node *linkList::getNode(int i) { node *p=head; int j=0; while(p&&jgetNext(); j++; } return p; } int linkList::getIndex(node *e) { node *p=head->getNext(); int i=1; while(p) { if(p->getX()==e->getX()&&p->getY()==e->getY()) { return i; } i++; p=p->getNext(); } return 0; } node *linkList::getPriorNode(node *e) { node *p=NULL; int i=getIndex(e); if(i!=0) { p=getNode(i-1); } return p; } node *linkList::getNextNode(node *e) { node *p=NULL; int i=getIndex(e); if(i!=0) { p=getNode(i+1); } return p; } void linkList::insert(int i,node *e) { node *p=getNode(i-1); e->changeNext(p->getNext()); p->changeNext(e); } void linkList::Delete(int i) { node *p=getNode(i-1),*q=p->getNext(); p->changeNext(p->getNext()->getNext()); delete q; } class snake:public linkList { private: char direction; node *lastTail; public: snake(); char getDirection(); void go(); void changeDirection(char D); node *getLastTail(); }; node *snake::getLastTail() { return lastTail; } void snake::changeDirection(char D) { direction=D; } void snake::go() { node *e=new node; e->changeNext(NULL); switch(direction) { case 'w': e->changeX(head->getNext()->getX()-1); e->changeY(head->getNext()->getY()); insert(1,e); break; case 's': e->changeX(head->getNext()->getX()+1); e->changeY(head->getNext()->getY()); insert(1,e); break; case 'a': e->changeX(head->getNext()->getX()); e->changeY(head->getNext()->getY()-1); insert(1,e); break; case 'd': e->changeX(head->getNext()->getX()); e->changeY(head->getNext()->getY()+1); insert(1,e); break; } lastTail=new node; lastTail->changeX(getNode(getLength())->getX()); lastTail->changeY(getNode(getLength())->getY()); lastTail->changeNext(NULL); Delete(getLength()); } char snake::getDirection() { return direction; } snake::snake():linkList() { direction='d'; head=new node; head->changeNext(new node); head->getNext()->changeNext(new node); head->getNext()->getNext()->changeNext(new node); head->getNext()->getNext()->getNext()->changeNext(NULL); head->changeX(0); head->changeY(0); head->getNext()->changeX(size/2); head->getNext()->changeY(size/2); head->getNext()->getNext()->changeX(size/2); head->getNext()->getNext()->changeY(size/2-1); head->getNext()->getNext()->getNext()->changeX(size/2); head->getNext()->getNext()->getNext()->changeY(size/2-2); lastTail=NULL; } class food:public linkList { public: food(); }; food::food():linkList() { head=new node; head->changeNext(new node); head->getNext()->changeNext(new node); head->getNext()->getNext()->changeNext(NULL); head->changeX(0); head->changeY(0); do{ head->getNext()->changeX(rand()%(size-1)+1); head->getNext()->changeY(rand()%(size-1)+1); }while(0);//*** do{ head->getNext()->getNext()->changeX(rand()%(size-1)+1); head->getNext()->getNext()->changeY(rand()%(size-1)+1); }while(0); } class border:public linkList { public: border(); }; border::border():linkList() { head=new node; head->changeX(0); head->changeY(0); node *p=head; for(int i=0;ichangeNext(new node); p=p->getNext(); p->changeX(0); p->changeY(i); } for(i=1;ichangeNext(new node); p=p->getNext(); p->changeX(i); p->changeY(size+1); } for(i=size;i>=0;i--) { p->changeNext(new node); p=p->getNext(); p->changeX(size+1); p->changeY(i); } for(i=size;i>0;i--) { p->changeNext(new node); p=p->getNext(); p->changeX(i); p->changeY(0); } p->changeNext(NULL); } class map { private: int m[size+2][size+2]; public: map(); void refresh(); void display(snake *S,food *F,border *B,float score); bool detectCollision(linkList *S,linkList *O); }; void map::refresh() { for(int i=0;i<size+2;i++) { for(int j=0;j<size+2;j++) { m[i][j]=0; } } } map::map() { for(int i=0;i<size+2;i++) { for(int j=0;jgetNode(1); if(O->getIndex(p)!=0) { return 1; } else { return 0; } } void map::display(snake *S,food *F,border *B,float score) { system("cls"); for(int i=1;igetLength();i++) { m[S->getNode(i)->getX()][S->getNode(i)->getY()]=1; } for(i=1;igetLength();i++) { m[F->getNode(i)->getX()][F->getNode(i)->getY()]=2; } for(i=1;igetLength();i++) { m[B->getNode(i)->getX()][B->getNode(i)->getY()]=4; } cout<<"score="<<score*10<<endl; for(i=0;i<size+2;i++) { for(int j=0;j<size+2;j++) { if(m[i][j]==0) { cout<<" "; } else if(m[i][j]==1) { cout<<"* "; } else if(m[i][j]==2) { cout<<"o "; } else if(m[i][j]==4) { cout<<"+ "; } } cout<<endl; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值