~cpp 
#include <iostream>
#include <assert.h>

using namespace std;

typedef struct __IntPair {
	int n1;
	int n2;
} IntPair;

class Board
{
public:
	int m_nMaxCol;
	int m_nMaxRow;

	IntPair m_nRoachPos;

	Board () {
		for (int i=0;i<100;i++) {
			for (int j=0;j<100;j++) {
				boardArray[i][j] = 0;
			}
		}
	}
	

	int boardArray[100][100];

	void setSize (int nCol, int nRow) { 
		m_nMaxCol = nCol;
		m_nMaxRow = nRow;
	}

	void printBoardStatus () { 
		for (int i=0;i<m_nMaxRow;i++) {
			for (int j=0;j<m_nMaxCol;j++) {
				cout << boardArray[i][j] << " ";
			}
			cout <<endl;
		}
	}
	void updateCell () {  }
	
	int isCheckedAllCells () {return 0; }

	void setRoachPosition(int nRow, int nCol) {
		m_nRoachPos.n1 = nRow;
		m_nRoachPos.n2 = nCol;
	}

	IntPair getRoachPosition () {
		return m_nRoachPos;
	}

};

class Journey
{
public:

	int journeyArray[100];
	int nCurrentPosition;
	int journeySize;

	Journey (char* buffer) {
		journeySize = strlen(buffer);

		for (int i=0;i<journeySize;i++) {
			journeyArray[i] = buffer[i] - '0';
		}
		nCurrentPosition = -1;
	}

	~Journey () {   }

	int getNextJourney () {
		nCurrentPosition ++;
		if (journeySize <= nCurrentPosition) return -1;

		return journeyArray[nCurrentPosition];
	}

};

class Roach
{
public:
	Journey* m_pJourney;
	Board* m_pBoard;
	
	int m_nMoveCount;

	Roach () {

	}

	Roach (Journey* pJourney) { m_pJourney = pJourney; 
		m_nMoveCount=0;	
	}

	~Roach () {   }

	void goAboard (Board* pBoard, int nStartRow, int nStartCol) {
		m_pBoard = pBoard;

		m_pBoard->setRoachPosition(nStartRow, nStartCol);
	}

	void run () {
		while (!isFinished ()) {
			move (m_pJourney->getNextJourney ());
		}
	}

	void move (int nLocation) {

	}
	
	int isFinished () {
		return 1;
	}

	void printMoveCount () {
		cout << "move count : " << m_nMoveCount << endl;
	}
};

class Inputer
{
public:
	int m_nBoardCol;
	int m_nBoardRow;

	int m_nStartCol;
	int m_nStartRow;

	Journey* journey;
	void getData () {
		cin >> m_nBoardRow >> m_nBoardCol;
		cin >> m_nStartRow >> m_nStartCol;

		char buffer[100];
		scanf ("%s", buffer);

	}

	int getBoardCol () { return m_nBoardRow; }
	int getBoardRow () { return m_nBoardCol; }
	int getStartCol () { return 0; }
	int getStartRow () { return 0; }
	Journey* getJourney () { return journey; }
};

class Game
{
public:
	Inputer* inputer; 
	Board board;
	Roach* roach;

	Game () {   }
	~Game () {   }

	void setInputer (Inputer* pInputer) {
		inputer = pInputer;
	}

	void start () {
		roach = new Roach (inputer->getJourney ());
		board.setSize (inputer->getBoardRow (), inputer->getBoardCol ());

		roach->goAboard (&board, inputer->getStartRow(), inputer->getStartCol());
		roach->run ();

		roach->printMoveCount ();
		board.printBoardStatus ();
	}
};

void testJourney () {
	char buffer[100] = "112";
	Journey* journey = new Journey(buffer);

	int actual; 

	int expectedSet[5] = {1,1,2,-1,-1};

	for (int i=0;i<5;i++) {
		actual = journey->getNextJourney();
		assert (actual == expectedSet[i]);
	}

	cout << "Journey.getNextJourney() Ok \n";
}

void testRoach () {
	Board board;
	board.setSize(5,5);
	Roach roach;
	roach.goAboard(&board, 1, 2);
	assert (board.getRoachPosition ().n1 == 1);
	assert (board.getRoachPosition ().n2 == 2);
	cout << "Roach.goAboard () Ok \n";
}

int main ()
{
	/*
	Game game;
	Inputer inputer;

	inputer.getData();
	game.setInputer (&inputer);

	game.start ();
	*/
	testJourney();
	testRoach();


	return 0;
}
Retrieved from http://wiki.zeropage.org/wiki.php/RandomWalk2/ClassPrototype
last modified 2021-02-07 05:27:50