展开全部
snake.h
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //横坐标
int lSign; //竖坐标
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-m.rSign,lSign-m.lSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+m.rSign,lSign+m.lSign);
}
};
const int maxSize = 5; //初始蛇身长度
class Csnake
{
Cmp firstSign; //蛇头坐标
Cmp secondSign;//蛇颈坐标
Cmp lastSign; //蛇尾坐标
Cmp nextSign; //预备蛇头
int row; //列数
int line; //行数
int count; //蛇身长度
vector<vector<char> > snakeMap;//整个游戏界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//获取指定坐标点上的字符
{
return snakeMap[c.lSign][c.rSign];
}
Csnake(int n)
//初始化游戏界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line*3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmp.rSign == row-1
|| cmp.rSign== 0 || cmp.lSign == line-1 ||
cmp.lSign == 0 );
}
void InitInstance(); //初始化游戏界面
bool UpdataGame(); //更新游戏界面
void ShowGame(); //显示游戏界面
};
#endif // SNAKE_H
snake.cpp
#include <windows.h>
#include <ctime>
#include <iostream>
#include "snake.h"
using namespace std;
//测试成功
void Csnake::InitInstance()
{
snakeMap.resize(line); // snakeMap[竖坐标][横坐标]
for(int i=0;i<line;i++)
{
snakeMap[i].resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//将蛇身坐标压入队列
snakeBody.push(Cmp(m,(line/2)));
//snakeBody[横坐标][竖坐标]
}
//链表头尾
firstSign=snakeBody.back();
secondSign.setPoint(maxSize-1,line/2);
}
//测试成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上键
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下键
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左键
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右键
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//获取用户按键信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//随机产生食物所在坐标
int r,l;
//开始初始已经吃食,产生一个食物
static bool eatFood=true;
//如果吃了一个,才再出现第2个食物
if(eatFood)
{
do
{
//坐标范围限制在(1,1)到(line-2,row-2)对点矩型之间
srand(time(0));
r=(rand()%(row-2))+1; //横坐标
l=(rand()%(line-2))+1;//竖坐标
//如果随机产生的坐标不是蛇身,则可行
//否则重新产生坐标
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇头和社颈的横坐标不相同,执行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否则,如下在原本方向上继续移动
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头
{
//删除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇头
secondSign=firstSign;
//压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//没有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
main.cpp
#include <iostream>
#include "snake.h"
#include <windows.h>
using namespace std;
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}
//我只做了复制粘贴的工作,不好意思
#include <vector>
#include <queue>
using namespace std;
#ifndef SNAKE_H
#define SNAKE_H
class Cmp
{
friend class Csnake;
int rSign; //横坐标
int lSign; //竖坐标
public:
// friend bool isDead(const Cmp& cmp);
Cmp(int r,int l){setPoint(r,l);}
Cmp(){}
void setPoint(int r,int l){rSign=r;lSign=l;}
Cmp operator-(const Cmp &m)const
{
return Cmp(rSign-m.rSign,lSign-m.lSign);
}
Cmp operator+(const Cmp &m)const
{
return Cmp(rSign+m.rSign,lSign+m.lSign);
}
};
const int maxSize = 5; //初始蛇身长度
class Csnake
{
Cmp firstSign; //蛇头坐标
Cmp secondSign;//蛇颈坐标
Cmp lastSign; //蛇尾坐标
Cmp nextSign; //预备蛇头
int row; //列数
int line; //行数
int count; //蛇身长度
vector<vector<char> > snakeMap;//整个游戏界面
queue<Cmp> snakeBody; //蛇身
public:
int GetDirections()const;
char getSymbol(const Cmp& c)const
//获取指定坐标点上的字符
{
return snakeMap[c.lSign][c.rSign];
}
Csnake(int n)
//初始化游戏界面大小
{
if(n<20)line=20+2;
else if(n>30)line=30+2;
else line=n+2;
row=line*3+2;
}
bool isDead(const Cmp& cmp)
{
return ( getSymbol(cmp)=='@' || cmp.rSign == row-1
|| cmp.rSign== 0 || cmp.lSign == line-1 ||
cmp.lSign == 0 );
}
void InitInstance(); //初始化游戏界面
bool UpdataGame(); //更新游戏界面
void ShowGame(); //显示游戏界面
};
#endif // SNAKE_H
snake.cpp
#include <windows.h>
#include <ctime>
#include <iostream>
#include "snake.h"
using namespace std;
//测试成功
void Csnake::InitInstance()
{
snakeMap.resize(line); // snakeMap[竖坐标][横坐标]
for(int i=0;i<line;i++)
{
snakeMap[i].resize(row);
for(int j=0;j<row;j++)
{
snakeMap[i][j]=' ';
}
}
for(int m=1;m<maxSize+1;m++)
{
//初始蛇身
snakeMap[line/2][m]='@';
//将蛇身坐标压入队列
snakeBody.push(Cmp(m,(line/2)));
//snakeBody[横坐标][竖坐标]
}
//链表头尾
firstSign=snakeBody.back();
secondSign.setPoint(maxSize-1,line/2);
}
//测试成功
int Csnake::GetDirections()const
{
if(GetKeyState(VK_UP)<0) return 1; //1表示按下上键
if(GetKeyState(VK_DOWN)<0) return 2; //2表示按下下键
if(GetKeyState(VK_LEFT)<0) return 3; //3表示按下左键
if(GetKeyState(VK_RIGHT)<0)return 4; //4表示按下右键
return 0;
}
bool Csnake::UpdataGame()
{
//-----------------------------------------------
//初始化得分0
static int score=0;
//获取用户按键信息
int choice;
choice=GetDirections();
cout<<"Total score: "<<score<<endl;
//随机产生食物所在坐标
int r,l;
//开始初始已经吃食,产生一个食物
static bool eatFood=true;
//如果吃了一个,才再出现第2个食物
if(eatFood)
{
do
{
//坐标范围限制在(1,1)到(line-2,row-2)对点矩型之间
srand(time(0));
r=(rand()%(row-2))+1; //横坐标
l=(rand()%(line-2))+1;//竖坐标
//如果随机产生的坐标不是蛇身,则可行
//否则重新产生坐标
if(snakeMap[l][r]!='@')
{snakeMap[l][r]='*';}
}while (snakeMap[l][r]=='@');
}
switch (choice)
{
case 1://向上
//如果蛇头和社颈的横坐标不相同,执行下面操作
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);
//否则,如下在原本方向上继续移动
else nextSign=firstSign+(firstSign-secondSign);
break;
case 2://向下
if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 3://向左
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
case 4://向右
if(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);
else nextSign=firstSign+(firstSign-secondSign);
break;
default:
nextSign=firstSign+(firstSign-secondSign);
}
//----------------------------------------------------------
if(getSymbol(nextSign)!='*' && !isDead(nextSign))
//如果没有碰到食物(且没有死亡的情况下),删除蛇尾,压入新的蛇头
{
//删除蛇尾
lastSign=snakeBody.front();
snakeMap[lastSign.lSign][lastSign.rSign]=' ';
snakeBody.pop();
//更新蛇头
secondSign=firstSign;
//压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
snakeMap[firstSign.lSign][firstSign.rSign]='@';
//没有吃食
eatFood=false;
return true;
}
//-----吃食-----
else if(getSymbol(nextSign)=='*' && !isDead(nextSign))
{
secondSign=firstSign;
snakeMap[nextSign.lSign][nextSign.rSign]='@';
//只压入蛇头
snakeBody.push(nextSign);
firstSign=snakeBody.back();
eatFood=true;
//加分
score+=20;
return true;
}
//-----死亡-----
else {cout<<"Dead"<<endl;cout<<"Your last total score is "<<score<<endl; return false;}
}
void Csnake::ShowGame()
{
for(int i=0;i<line;i++)
{
for(int j=0;j<row;j++)
cout<<snakeMap[i][j];
cout<<endl;
}
Sleep(1);
system("cls");
}
main.cpp
#include <iostream>
#include "snake.h"
#include <windows.h>
using namespace std;
int main()
{
Csnake s(20);
s.InitInstance();
//s.ShowGame();
int noDead;
do
{
s.ShowGame();
noDead=s.UpdataGame();
}while (noDead);
system("pause");
return 0;
}
//我只做了复制粘贴的工作,不好意思
2008-01-29
展开全部
好复杂啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-01-29
展开全部
//文件1、snake.cpp
//#include<iostream.h>
#define SNAKE_MOVE 1
//有向方向
enum EMDirect{M_NONE,M_UP_UP,M_DOWN_DOWN,M_LEFT_LEFT,M_RIGHT_RIGHT,
M_UP_LEFT,M_UP_RIGHT,M_LEFT_UP,M_LEFT_DOWN,
M_RIGHT_UP,M_RIGHT_DOWN,M_DOWN_RIGHT,M_DOWN_LEFT};
//无向方向
enum ESDirect{S_NONE,S_UP,S_DOWN,S_LEFT,S_RIGHT};
struct SPoint
{
int x;
int y;
};
class CSnake
{
struct Snake_Struct//定义蛇体
{
ESDirect head;
ESDirect *body;
ESDirect sweap;
};
private:
int length;//蛇的长度
Snake_Struct newSnake;//蛇的新态
Snake_Struct oldSnake;//蛇的原态
EMDirect *array;//输出有向方向
SPoint *pos;//蛇体坐标
private:
EMDirect SCM(ESDirect d1,ESDirect d2);
public:
CSnake(int x=0,int y=0,int n=1);
void Run(void);
void ChangeDirect(ESDirect d);
void AddBody(int n=1);
EMDirect* GetArray(void);
SPoint* GetPos(void);
int GetLength(void);
};
CSnake::CSnake(int x,int y,int n)
{
if(n<1) n=1;
int i;
length=n;
pos=new SPoint[length+2];
pos[0].x=x;pos[0].y=y;
for(i=1;i<length+2;i++)
{
pos[i].x=0;pos[i].y=0;
}
newSnake.head=S_NONE;
oldSnake.head=S_NONE;
newSnake.body=new ESDirect[length];
oldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
{
newSnake.body[i]=S_NONE;
newSnake.body[i]=S_NONE;
}
newSnake.sweap=S_NONE;
oldSnake.sweap=S_NONE;
array=new EMDirect[length+2];
for(i=0;i<length+2;i++)
array[i]=M_NONE;
}
EMDirect CSnake::SCM(ESDirect d1,ESDirect d2)
{
EMDirect res;
switch(d1)
{
case S_NONE:
switch(d2)
{
case S_NONE:
res=M_NONE;
break;
case S_UP:
res=M_UP_UP;
break;
case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_RIGHT:
res=M_RIGHT_RIGHT;
break;
}
break;
case S_UP:
switch(d2)
{
case S_UP:
res=M_UP_UP;
break;
case S_LEFT:
res=M_UP_LEFT;
break;
case S_RIGHT:
res=M_UP_RIGHT;
break;
}
break;
case S_DOWN:
switch(d2)
{
case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_DOWN_LEFT;
break;
case S_RIGHT:
res=M_DOWN_RIGHT;
break;
}
break;
case S_LEFT:
switch(d2)
{
case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_UP:
res=M_LEFT_UP;
break;
case S_DOWN:
res=M_LEFT_DOWN;
break;
}
break;
case S_RIGHT:
switch(d2)
{
case S_RIGHT:
res=M_RIGHT_RIGHT;
break;
case S_UP:
res=M_RIGHT_UP;
break;
case S_DOWN:
res=M_RIGHT_DOWN;
break;
}
break;
}
return res;
}
void CSnake::ChangeDirect(ESDirect d)
{
switch(d)
{
case S_NONE:
newSnake.head=d;
break;
case S_UP:
if(newSnake.head!=S_DOWN) newSnake.head=d;
break;
case S_DOWN:
if(newSnake.head!=S_UP) newSnake.head=d;
break;
case S_LEFT:
if(newSnake.head!=S_RIGHT) newSnake.head=d;
break;
case S_RIGHT:
if(newSnake.head!=S_LEFT) newSnake.head=d;
break;
}
}
void CSnake::Run(void)
{
int i;
// oldSnake.head=newSnake.head;
for(i=0;i<length;i++)
{
oldSnake.body[i]=newSnake.body[i];
}
// oldSnake.sweap=newSnake.sweap;
newSnake.sweap=newSnake.body[length-1];
for(i=length-1;i>0;i--)
{
newSnake.body[i]=newSnake.body[i-1];
}
newSnake.body[0]=newSnake.head;
array[0]=SCM(oldSnake.head,newSnake.head);
for(i=0;i<length;i++)
array[i+1]=SCM(oldSnake.body[i],newSnake.body[i]);
array[length+1]=SCM(oldSnake.sweap,newSnake.sweap);
switch(newSnake.head)
{
case S_UP:
pos[0].y-=SNAKE_MOVE;
break;
case S_DOWN:
pos[0].y+=SNAKE_MOVE;
break;
case S_LEFT:
pos[0].x-=SNAKE_MOVE;
break;
case S_RIGHT:
pos[0].x+=SNAKE_MOVE;
break;
}
for(i=length+1;i>0;i--)
pos[i]=pos[i-1];
}
void CSnake::AddBody(int n)
{
int i;
Snake_Struct saveOldSnake,saveNewSnake;
EMDirect *saveArray;
SPoint *savePos;
savePos=new SPoint[length+2];
for(i=0;i<length+2;i++)
savePos[i]=pos[i];
saveOldSnake.head=oldSnake.head;
saveOldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
saveOldSnake.body[i]=oldSnake.body[i];
saveOldSnake.sweap=oldSnake.sweap;
saveNewSnake.head=newSnake.head;
saveNewSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
saveNewSnake.body[i]=newSnake.body[i];
saveNewSnake.sweap=newSnake.sweap;
saveArray=new EMDirect[length+2];
for(i=0;i<length+2;i++)
saveArray[i]=array[i];
length+=n;
delete[] oldSnake.body;oldSnake.body=NULL;
delete[] newSnake.body;newSnake.body=NULL;
delete[] array;array=NULL;
delete[] pos;pos=NULL;
newSnake.head=S_NONE;
oldSnake.head=S_NONE;
newSnake.body=new ESDirect[length];
oldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
{
newSnake.body[i]=S_NONE;
newSnake.body[i]=S_NONE;
}
newSnake.sweap=S_NONE;
oldSnake.sweap=S_NONE;
array=new EMDirect[length+2];
for(i=0;i<length+2;i++)
array[i]=M_NONE;
pos=new SPoint[length+2];
for(i=0;i<length+2;i++)
{
pos[i].x=0;
pos[i].y=0;
}
newSnake.head=saveNewSnake.head;
oldSnake.head=saveOldSnake.head;
for(i=0;i<length-n;i++)
{
newSnake.body[i]=saveNewSnake.body[i];
oldSnake.body[i]=saveOldSnake.body[i];
}
newSnake.sweap=saveNewSnake.sweap;
oldSnake.sweap=saveOldSnake.sweap;
for(i=0;i<length-n+2;i++)
array[i]=saveArray[i];
for(i=0;i<length-n+2;i++)
pos[i]=savePos[i];
}
EMDirect* CSnake::GetArray(void)
{
return array;
}
SPoint* CSnake::GetPos(void)
{
return pos;
}
int CSnake::GetLength(void)
{
return length+2;
}
/*
void main()
{
CSnake s(10,10,10);
int i;
s.ChangeDirect(S_UP);
s.Run();
s.Run();
s.ChangeDirect(S_LEFT);
s.Run();
s.Run();
s.AddBody();
s.AddBody();
s.AddBody();
s.AddBody();
s.Run();
s.ChangeDirect(S_UP);
s.Run();
for(i=0;i<s.GetLength();i++)
{
cout<<"x:"<<s.GetPos()[i].x<<" y:"<<s.GetPos()[i].y<<endl;
cout<<i<<" "<<s.GetArray()[i]<<endl;
}
}
*/
//----------------------------------/
//文件2、snaker.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by snaker.rc
//
#define IDI_MAIN 101
#define IDB_BITMAP 102
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
//--------------------
文件3、table.h
//#include<iostream.h>
#include"snake.h"
#define FOOD 1
#define BLOCK 2
#define SBLOCK 3
class CTable
{
private:
int width,height;
int foodNumber;
int blockNumber;
CSnake snake;
int **board;
public:
CTable();
~CTable();
bool AddBlock(int x,int y);
bool AddFood(int x,int y);
bool Clear(int x,int y);
void SetTable(int w,int h);
CSnake* GetSnake(void);
int** GetBoard(void);
int GetData(int x,int y);
void SnakeRun(void);
bool ChangeSnakeDirect(ESDirect d);
};
CTable::CTable()
{
width=height=0;
foodNumber=blockNumber=0;
board=NULL;
}
CTable::~CTable()
{
// if(board!=NULL) SAFE_DELETE_ARRAY(board);
}
void CTable::SetTable(int w,int h)
{
int i,j;
width=w;height=h;
board=new int*[height];
for(i=0;i<h;i++)
{
board[i]=new int[width];
for(j=0;j<w;j++)
board[i][j]=0;
}
for(i=0;i<h;i++)
{
board[i][0]=SBLOCK;
board[i][width-1]=SBLOCK;
}
for(i=0;i<w;i++)
{
board[0][i]=SBLOCK;
board[height-1][i]=SBLOCK;
}
}
bool CTable::AddBlock(int x,int y)
{
if(x>=0&&x<width&&y>=0&&y<height&&board[y][x]==0)
{
board[y][x]=BLOCK;
++blockNumber;
return true;
}
return false;
}
bool CTable::AddFood(int x,int y)
{
if(x>=0&&x<width&&y>=0&&y<height&&board[y][x]==0)
{
board[y][x]=FOOD;
++foodNumber;
return true;
}
return false;
}
bool CTable::Clear(int x,int y)
{
board[y][x]=0;
return true;
}
CSnake* CTable::GetSnake(void)
{
return &snake;
}
int** CTable::GetBoard(void)
{
return board;
}
int CTable::GetData(int x,int y)
{
return board[y][x];
}
void CTable::SnakeRun(void)
{
// int i;
// int x,y;
// EMDirect d;
snake.Run();
/*
for(i=0;i<snake.GetLength()+2;i++)
{
x=snake.GetPos()[i].x%width;y=snake.GetPos()[i].y%height;
d=snake.GetArray()[i];
board[x+y*height]=(int)d;
}*/
}
bool CTable::ChangeSnakeDirect(ESDirect d)
{
snake.ChangeDirect(d);
return true;
}
/*
void main()
{
cout<<"ok"<<endl;
}
*/
//----------------
//文件4、table.cpp
//#include<iostream.h>
#include"snake.cpp"
class CTable
{
private:
int width,height;
int number;
CSnake snake;
int *board;
public:
CTable(int w,int h);
void AddBlock(int x,int y);
void AddFood(int x,int y);
CSnake* GetSnake(void);
int* GetBoard(void);
};
CTable::CTable(int w,int h)
{
int i;
width=w;height=h;
board=new int[w*h];
for(i=0;i<w*h;i++)
board[i]=0;
}
void CTable::AddBlock(int x,int y)
{
board[x+y*height]=1;
}
void CTable::AddFood(int x,int y)
{
board[x+y*height]=2;
}
CSnake* CTable::GetSnake(void)
{
return &snake;
}
int* CTable::GetBoard(void)
{
return board;
}
/*
void main()
{
cout<<"ok"<<endl;
}
*/
//------------------------
//文件5、snake.h
//#include<iostream.h>
#define SNAKE_MOVE 1
//有向方向
enum EMDirect{M_NONE,M_UP_UP,M_DOWN_DOWN,M_LEFT_LEFT,M_RIGHT_RIGHT,
M_UP_LEFT,M_UP_RIGHT,M_LEFT_UP,M_LEFT_DOWN,
M_RIGHT_UP,M_RIGHT_DOWN,M_DOWN_RIGHT,M_DOWN_LEFT};
//无向方向
enum ESDirect{S_NONE,S_UP,S_DOWN,S_LEFT,S_RIGHT};
struct SPoint
{
int x;
int y;
};
class CSnake
{
struct Snake_Struct//定义蛇体
{
ESDirect head;
ESDirect *body;
ESDirect sweap;
};
private:
int length;//蛇的长度
Snake_Struct newSnake;//蛇的新态
Snake_Struct oldSnake;//蛇的原态
EMDirect *array;//输出有向方向
SPoint *pos;//蛇体坐标
private:
EMDirect SCM(ESDirect d1,ESDirect d2);
public:
CSnake(int x=0,int y=0,int n=1);
void Run(void);
void ChangeDirect(ESDirect d);
void AddBody(int n=1);
EMDirect* GetArray(void);
SPoint* GetPos(void);
int GetLength(void);
};
CSnake::CSnake(int x,int y,int n)
{
if(n<1) n=1;
int i;
length=n;
pos=new SPoint[length+2];
pos[0].x=x;pos[0].y=y;
for(i=1;i<length+2;i++)
{
pos[i].x=0;pos[i].y=0;
}
newSnake.head=S_NONE;
oldSnake.head=S_NONE;
newSnake.body=new ESDirect[length];
oldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
{
newSnake.body[i]=S_NONE;
newSnake.body[i]=S_NONE;
}
newSnake.sweap=S_NONE;
oldSnake.sweap=S_NONE;
array=new EMDirect[length+2];
for(i=0;i<length+2;i++)
array[i]=M_NONE;
}
EMDirect CSnake::SCM(ESDirect d1,ESDirect d2)
{
EMDirect res;
switch(d1)
{
case S_NONE:
switch(d2)
{
case S_NONE:
res=M_NONE;
break;
case S_UP:
res=M_UP_UP;
break;
case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_RIGHT:
res=M_RIGHT_RIGHT;
break;
}
break;
case S_UP:
switch(d2)
{
case S_UP:
res=M_UP_UP;
break;
case S_LEFT:
res=M_UP_LEFT;
break;
case S_RIGHT:
res=M_UP_RIGHT;
break;
}
break;
case S_DOWN:
switch(d2)
{
case S_DOWN:
res=M_DOWN_DOWN;
break;
case S_LEFT:
res=M_DOWN_LEFT;
break;
case S_RIGHT:
res=M_DOWN_RIGHT;
break;
}
break;
case S_LEFT:
switch(d2)
{
case S_LEFT:
res=M_LEFT_LEFT;
break;
case S_UP:
res=M_LEFT_UP;
break;
case S_DOWN:
res=M_LEFT_DOWN;
break;
}
break;
case S_RIGHT:
switch(d2)
{
case S_RIGHT:
res=M_RIGHT_RIGHT;
break;
case S_UP:
res=M_RIGHT_UP;
break;
case S_DOWN:
res=M_RIGHT_DOWN;
break;
}
break;
}
return res;
}
void CSnake::ChangeDirect(ESDirect d)
{
switch(d)
{
case S_NONE:
newSnake.head=d;
break;
case S_UP:
if(newSnake.head!=S_DOWN) newSnake.head=d;
break;
case S_DOWN:
if(newSnake.head!=S_UP) newSnake.head=d;
break;
case S_LEFT:
if(newSnake.head!=S_RIGHT) newSnake.head=d;
break;
case S_RIGHT:
if(newSnake.head!=S_LEFT) newSnake.head=d;
break;
}
}
void CSnake::Run(void)
{
int i;
// oldSnake.head=newSnake.head;
for(i=0;i<length;i++)
{
oldSnake.body[i]=newSnake.body[i];
}
// oldSnake.sweap=newSnake.sweap;
newSnake.sweap=newSnake.body[length-1];
for(i=length-1;i>0;i--)
{
newSnake.body[i]=newSnake.body[i-1];
}
newSnake.body[0]=newSnake.head;
array[0]=SCM(oldSnake.head,newSnake.head);
for(i=0;i<length;i++)
array[i+1]=SCM(oldSnake.body[i],newSnake.body[i]);
array[length+1]=SCM(oldSnake.sweap,newSnake.sweap);
switch(newSnake.head)
{
case S_UP:
pos[0].y-=SNAKE_MOVE;
break;
case S_DOWN:
pos[0].y+=SNAKE_MOVE;
break;
case S_LEFT:
pos[0].x-=SNAKE_MOVE;
break;
case S_RIGHT:
pos[0].x+=SNAKE_MOVE;
break;
}
for(i=length+1;i>0;i--)
pos[i]=pos[i-1];
}
void CSnake::AddBody(int n)
{
int i;
Snake_Struct saveOldSnake,saveNewSnake;
EMDirect *saveArray;
SPoint *savePos;
savePos=new SPoint[length+2];
for(i=0;i<length+2;i++)
savePos[i]=pos[i];
saveOldSnake.head=oldSnake.head;
saveOldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
saveOldSnake.body[i]=oldSnake.body[i];
saveOldSnake.sweap=oldSnake.sweap;
saveNewSnake.head=newSnake.head;
saveNewSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
saveNewSnake.body[i]=newSnake.body[i];
saveNewSnake.sweap=newSnake.sweap;
saveArray=new EMDirect[length+2];
for(i=0;i<length+2;i++)
saveArray[i]=array[i];
length+=n;
delete[] oldSnake.body;oldSnake.body=NULL;
delete[] newSnake.body;newSnake.body=NULL;
delete[] array;array=NULL;
delete[] pos;pos=NULL;
newSnake.head=S_NONE;
oldSnake.head=S_NONE;
newSnake.body=new ESDirect[length];
oldSnake.body=new ESDirect[length];
for(i=0;i<length;i++)
{
newSnake.body[i]=S_NONE;
newSnake.body[i]=S_NONE;
}
newSnake.sweap=S_NONE;
oldSnake.sweap=S_NONE;
array=new EMDirect[length+2];
for(i=0;i<length+2;i++)
array[i]=M_NONE;
pos=new SPoint[length+2];
for(i=0;i<length+2;i++)
{
pos[i].x=0;
pos[i].y=0;
}
newSnake.head=saveNewSnake.head;
oldSnake.head=saveOldSnake.head;
for(i=0;i<length-n;i++)
{
newSnake.body[i]=saveNewSnake.body[i];
oldSnake.body[i]=saveOldSnake.body[i];
}
newSnake.sweap=saveNewSnake.sweap;
oldSnake.sweap=saveOldSnake.sweap;
for(i=0;i<length-n+2;i++)
array[i]=saveArray[i];
for(i=0;i<length-n+2;i++)
pos[i]=savePos[i];
}
EMDirect* CSnake::GetArray(void)
{
return array;
}
SPoint* CSnake::GetPos(void)
{
return pos;
}
int CSnake::GetLength(void)
{
return length+2;
}
/*
void main()
{
CSnake s(10,10,10);
int i;
s.ChangeDirect(S_UP);
s.Run();
s.Run();
s.ChangeDirect(S_LEFT);
s.Run();
s.Run();
s.AddBody();
s.AddBody();
s.AddBody();
s.AddBody();
s.Run();
s.ChangeDirect(S_UP);
s.Run();
for(i=0;i<s.GetLength();i++)
{
cout<<"x:"<<s.GetPos()[i].x<<" y:"<<s.GetPos()[i].y<<endl;
cout<<i<<" "<<s.GetArray()[i]<<endl;
}
}
*/
//----------------------------------/
//文件2、snaker.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by snaker.rc
//
#define IDI_MAIN 101
#define IDB_BITMAP 102
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
//--------------------
文件3、table.h
//#include<iostream.h>
#include"snake.h"
#define FOOD 1
#define BLOCK 2
#define SBLOCK 3
class CTable
{
private:
int width,height;
int foodNumber;
int blockNumber;
CSnake snake;
int **board;
public:
CTable();
~CTable();
bool AddBlock(int x,int y);
bool AddFood(int x,int y);
bool Clear(int x,int y);
void SetTable(int w,int h);
CSnake* GetSnake(void);
int** GetBoard(void);
int GetData(int x,int y);
void SnakeRun(void);
bool ChangeSnakeDirect(ESDirect d);
};
CTable::CTable()
{
width=height=0;
foodNumber=blockNumber=0;
board=NULL;
}
CTable::~CTable()
{
// if(board!=NULL) SAFE_DELETE_ARRAY(board);
}
void CTable::SetTable(int w,int h)
{
int i,j;
width=w;height=h;
board=new int*[height];
for(i=0;i<h;i++)
{
board[i]=new int[width];
for(j=0;j<w;j++)
board[i][j]=0;
}
for(i=0;i<h;i++)
{
board[i][0]=SBLOCK;
board[i][width-1]=SBLOCK;
}
for(i=0;i<w;i++)
{
board[0][i]=SBLOCK;
board[height-1][i]=SBLOCK;
}
}
bool CTable::AddBlock(int x,int y)
{
if(x>=0&&x<width&&y>=0&&y<height&&board[y][x]==0)
{
board[y][x]=BLOCK;
++blockNumber;
return true;
}
return false;
}
bool CTable::AddFood(int x,int y)
{
if(x>=0&&x<width&&y>=0&&y<height&&board[y][x]==0)
{
board[y][x]=FOOD;
++foodNumber;
return true;
}
return false;
}
bool CTable::Clear(int x,int y)
{
board[y][x]=0;
return true;
}
CSnake* CTable::GetSnake(void)
{
return &snake;
}
int** CTable::GetBoard(void)
{
return board;
}
int CTable::GetData(int x,int y)
{
return board[y][x];
}
void CTable::SnakeRun(void)
{
// int i;
// int x,y;
// EMDirect d;
snake.Run();
/*
for(i=0;i<snake.GetLength()+2;i++)
{
x=snake.GetPos()[i].x%width;y=snake.GetPos()[i].y%height;
d=snake.GetArray()[i];
board[x+y*height]=(int)d;
}*/
}
bool CTable::ChangeSnakeDirect(ESDirect d)
{
snake.ChangeDirect(d);
return true;
}
/*
void main()
{
cout<<"ok"<<endl;
}
*/
//----------------
//文件4、table.cpp
//#include<iostream.h>
#include"snake.cpp"
class CTable
{
private:
int width,height;
int number;
CSnake snake;
int *board;
public:
CTable(int w,int h);
void AddBlock(int x,int y);
void AddFood(int x,int y);
CSnake* GetSnake(void);
int* GetBoard(void);
};
CTable::CTable(int w,int h)
{
int i;
width=w;height=h;
board=new int[w*h];
for(i=0;i<w*h;i++)
board[i]=0;
}
void CTable::AddBlock(int x,int y)
{
board[x+y*height]=1;
}
void CTable::AddFood(int x,int y)
{
board[x+y*height]=2;
}
CSnake* CTable::GetSnake(void)
{
return &snake;
}
int* CTable::GetBoard(void)
{
return board;
}
/*
void main()
{
cout<<"ok"<<endl;
}
*/
//------------------------
//文件5、snake.h
参考资料: 代码贴不下,需要就留言
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询