📄 board.h
字号:
#ifndef BOARD_JTWU_H
#define BOARD_JTWU_H
#include "stdafx.h"
#include "BoardDraw.h"
#include "Agent.h"
#include "List.h"
enum Cell { space_cell, wall_cell, block_cell, agent_cell };
const int MAX_BOARD_ROW = 13;
const int MAX_BOARD_COL = 21;
const int length = 25;
const int ox = 10;
const int oy = 10;
class Board {
public:
Board();
void Add(const Agent& agent);
void Remove(const Agent& agent);
void NextStatus(); //move all agent in the board and form next status
void DrawBoard();
void SetCell(Cell status, Position pos) { m_board[pos.row][pos.col] = status; }
Cell GetCell(Position pos) { return m_board[pos.row][pos.col]; }
void LoadBoard(int (*pint)[MAX_BOARD_COL+2]);
CDC* m_pDC;
private: //method
Position GetNewPosition(Agent agent); //Get next position of agent
int GetX1(Position pos);
int GetX2(Position pos);
int GetX3(Position pos);
int GetX4(Position pos);
private: //data
Cell m_board[MAX_BOARD_ROW+2][MAX_BOARD_COL+2]; //Envirenment
List<Agent> m_agent; //Agents in the board
};
/////////////////////implementation for method
Board::Board() {
m_agent.clear();
int i,j; //edge
for ( i=0; i<MAX_BOARD_ROW+2; i++ ) { //east,west
m_board[i][MAX_BOARD_COL+1] = wall_cell;
m_board[i][0] = wall_cell;
}
for ( j=0; j<MAX_BOARD_COL+2; j++ ) { //south
m_board[MAX_BOARD_ROW+1][j] = wall_cell;
m_board[0][j] = wall_cell;
}
for ( i=1; i<MAX_BOARD_ROW+1; i++ )
for ( j=1; j<MAX_BOARD_COL+1; j++ )
m_board[i][j] = space_cell;
}
void Board::Add(const Agent& agent) {
m_agent.insert(m_agent.size(),agent);
}
void Board::Remove(const Agent& agent) {
int count = m_agent.size();
for ( int i=0; i<count; i++ ) {
Agent tempagent;
m_agent.retrieve(i,tempagent);
if ( tempagent == agent ) {
m_agent.remove(i,tempagent);
return;
}
}
}
void Board::NextStatus() {
int count = m_agent.size();
Position oldpos, newpos;
Agent refagent;
for ( int i=0; i<count; i++ ) {
m_agent.retrieve(i,refagent);
oldpos = refagent.GetCurrentPosition();
newpos = GetNewPosition(refagent);
refagent.GotoNewPosition(newpos);
m_agent.replace(i,refagent);
m_board[oldpos.row][oldpos.col] = space_cell;
m_board[newpos.row][newpos.col] = agent_cell;
}
}
void Board::DrawBoard() {
for ( int i=1; i<MAX_BOARD_ROW+1; i++ ) {
for ( int j=1; j<MAX_BOARD_COL+1; j++ ) {
Position pos(i,j); //get cell(i,j) and draw the cell
Cell flag = space_cell;
flag = GetCell(pos);
BoardDraw obj(m_pDC);
int x1,y1,x2,y2;
x1 = j*length + ox;
y1 = i*length + oy;
x2 = x1 + length;
y2 = y1 + length;
switch(flag) {
case space_cell: //draw space
obj.SetForeColor(0xff0000);
obj.Rectangle(x1,y1,x2,y2);
break;
case wall_cell: //draw wall
obj.DrawWall(x1+1,y1+1,x2-1,y2-1);
break;
case block_cell: //draw block
obj.DrawBlock(x1+1,y1+1,x2-1,y2-1);
break;
case agent_cell: //draw agent
obj.SetForeColor(0xff0000);
obj.Rectangle(x1,y1,x2,y2);
obj.SetForeColor(0x0000ff);
obj.Circle(x1,y1,x2,y2,5);
break;
}
}
}
}
/////////////////// the agent in the board will goto the new position by this method and board status will be modified
void Board::LoadBoard(int (*pint)[MAX_BOARD_COL+2]) {
m_agent.clear();
int i,j; //edge
for ( i=0; i<MAX_BOARD_ROW+2; i++ ) {
for ( j=0; j<MAX_BOARD_COL+2; j++ )
{
Agent agent(i,j);
switch ( pint[i][j] ) {
case -1: //wall
m_board[i][j] = wall_cell;
break;
case 0: //space
m_board[i][j] = space_cell;
break;
case 1: //block
m_board[i][j] = block_cell;
break;
case 2: //agent
m_board[i][j] = agent_cell;
Add(agent);
break;
}
}
}
}
/////////////////// method for deciding which direction to take and return the new position
Position Board::GetNewPosition(Agent agent) {
Position curpos,newpos;
curpos = agent.GetCurrentPosition();
int x1,x2,x3,x4;
x1 = GetX1(curpos);
x2 = GetX2(curpos);
x3 = GetX3(curpos);
x4 = GetX4(curpos);
if ( x1==1 && x2==0 ) newpos = curpos.East();
else if ( x2==1 && x3==0 ) newpos = curpos.South();
else if ( x3==1 && x4==0 ) newpos = curpos.West();
else if ( x4==1 && x1==0 ) newpos = curpos.North();
else {
if ( GetCell(curpos.East()) == space_cell )
newpos = curpos.East();
else if ( GetCell(curpos.West()) == space_cell )
newpos = curpos.West();
else if ( GetCell(curpos.South()) == space_cell )
newpos = curpos.South();
else if ( GetCell(curpos.North()) == space_cell )
newpos = curpos.North();
else
newpos = curpos;
}
return newpos;
} ///:~method for deciding which direction to take and return the new position
/////////////method for caculating x1,x2,x3,x4
int Board::GetX1(Position pos) {
Position pos1,pos2;
Cell c1,c2;
c1 = c2 = space_cell;
pos1 = pos.North();
pos2 = pos1.East();
c1 = GetCell(pos1);
c2 = GetCell(pos2);
int count = 0;
if ( c1==block_cell || c1==wall_cell || c1==agent_cell) count++;
if ( c2==block_cell || c2==wall_cell || c2==agent_cell ) count++;
if ( count >= 1 ) return 1;
else return 0;
}
int Board::GetX2(Position pos) {
Position pos1,pos2;
Cell c1,c2;
c1 = c2 = space_cell;
pos1 = pos.East();
pos2 = pos1.South();
c1 = GetCell(pos1);
c2 = GetCell(pos2);
int count = 0;
if ( c1==block_cell || c1==wall_cell || c1==agent_cell) count++;
if ( c2==block_cell || c2==wall_cell || c2==agent_cell) count++;
if ( count >= 1 ) return 1;
else return 0;
}
int Board::GetX3(Position pos) {
Position pos1,pos2;
Cell c1,c2;
c1 = c2 = space_cell;
pos1 = pos.South();
pos2 = pos1.West();
c1 = GetCell(pos1);
c2 = GetCell(pos2);
int count = 0;
if ( c1==block_cell || c1==wall_cell || c1==agent_cell) count++;
if ( c2==block_cell || c2==wall_cell || c2==agent_cell) count++;
if ( count >= 1 ) return 1;
else return 0;
}
int Board::GetX4(Position pos) {
Position pos1,pos2;
Cell c1,c2;
c1 = c2 = space_cell;
pos1 = pos.West();
pos2 = pos1.North();
c1 = GetCell(pos1);
c2 = GetCell(pos2);
int count = 0;
if ( c1==block_cell || c1==wall_cell || c1==agent_cell) count++;
if ( c2==block_cell || c2==wall_cell || c2==agent_cell) count++;
if ( count >= 1 ) return 1;
else return 0;
} ///:~ method for caculating x1,x2,x3,x4
#endif //:~BOARD_JTWU_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -