Contents
1. ¶
2. 리 ¶
StructuredProgramming StepwiseRefinement . 문를 TopDown 뼈대를 만 , Depth First (리 ) 모듈들 먼 . 부는 UnitTest 를 를 , 부는 UnitTest Code를 먼 만들 램 를 .
3. Version 0.1 - 램 ¶
말 대. '램' . ' 받, 리 .' 는;
~cpp int main () { Input (); Process (); Output (); return 0; } void Input () { } void Process () { } void Output () { }
Process () 는 리 명 못 , 더 바.
~cpp int main () { Input (); ScheduledWalk (); Output (); return 0; } void ScheduledWalk () { }
4. Version 0.2 - Refinement ¶
램 리 1 벨, 를 . 러는 뭐 무를..~ 뼈대므. 러 무루 등 문 는 void 리 return false; . 뼈를 는 더 므.
~cpp void Input() { InputBoardSize(); InputStartRoachPosition(); InputRoachJourney(); } void InputBoardSize() { } void InputStartRoachPosition() { } void InputRoachJourney() { } void ScheduledWalk() { while (!IsFinished()) { Move(); } } BOOL IsFinished() { return IsJourneyEnd() || IsAllBoardChecked(); } void Move() { } void Output() { OutputMoveCount(); OutputBoardStatus(); } void OutputMoveCount() { } void OutputBoardStatus() { }
5. Version 0.3 - Refinement More ¶
는데 15 . 마 면 10내 능듯.~ 는 무런 는 .~
~cpp typedef int BOOL; void Input(); void InputBoardSize(); void InputStartRoachPosition(); void InputRoachJourney(); void ScheduledWalk(); BOOL IsFinished(); BOOL IsJourneyEnd(); BOOL IsAllBoardChecked(); void MoveNext(); void Output(); void OutputMoveCount(); void OutputBoardStatus(); int main() { Input(); ScheduledWalk(); Output(); return 0; } void Input() { InputBoardSize(); InputStartRoachPosition(); InputRoachJourney(); } void InputBoardSize() { } void InputStartRoachPosition() { } void InputRoachJourney() { } void ScheduledWalk() { while (!IsFinished()) { MoveNext(); } } BOOL IsFinished() { return IsJourneyEnd() || IsAllBoardChecked(); } BOOL IsJourneyEnd() { return false; } BOOL IsAllBoardChecked() { return false; } void MoveNext() { } void Output() { OutputMoveCount(); OutputBoardStatus(); } void OutputMoveCount() { } void OutputBoardStatus() { }
(Hierarchy Input-Process-Output) . (림 부 )
6. Version 0.4 - Implementation : Input ¶
10 - 1 55. 보등등;
면 뼈대 됩. (, 더 눌 만, 바 들는데 문 )
면 뼈대 됩. (, 더 눌 만, 바 들는데 문 )
럼. 들.
떤 는 모듈 부 리. 모듈부 를 명 . DFD를 리 데 대 만, 는 맞. 면 를 더 , 를 는 . 따 HIPO 램 Argument 를 는 부 . (낙 를 . 만, 많 린 .~)
대 면 란 .
- - -> 른 . 따 . ( 독립 따 . 는 UnitTest )
- Depth-Module First. -> 는 들 문 , 모듈부 . (데, 대 못 면 Bottom-Up 릴 문.. 는 명 됩. 내 .)
~cpp void InputBoardSize() { }
는 말 대 Board Size 를 Input. 를 받는 부. scanf cin 등 .
~cpp void InputBoardSize() { int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); printf ("%d, %d \n", boardRow, boardCol); }
부 대 른 Visual C++ 뒤, 따 보 동면 본 붙 . 받는. 대 데를 .
데를 받 리는 방는 . 는 리 는 방, 는 변 는 런를 받뒤, 변 변는 방. (scanf 러 방.) 리 는 방 . int 변를 리는 면 를 는 더 리는 .
~cpp typedef struct __IntegerPair { // return int n1; int n2; } IntPair; IntPair InputBoardSize(); // void InputBoardSize() IntPair InputBoardSize() 리 바뀌. 만 들 말 대 '뼈' . IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; 0 return size; }
리 는 볼?
~cpp #include <stdio.h> typedef int BOOL; typedef struct __IntegerPair { int n1; int n2; } IntPair; IntPair InputBoardSize(); void Input(); IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } void Input() { IntPair testReceiver; testReceiver = InputBoardSize(); printf ("Board Size value : %d, %d \n", testReceiver.n1, testReceiver.n2); }
~cpp IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; }
InputRoachJourney() 는 String 문 르 . 말, 메모리 를 는 방 . 배 른 , .
~cpp void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
방 . char [] journey 를 모르 문. 만 journey 받 메모리러를 . 만, 능 따 ' 돌는 ' 를 냥 남봅. 면 배 대보 더 리 등 . , 문 대 InputRoachJourney () 내 느 능. 는 Structured Programming 는 목, 부 문 대는 .
Input 부 Implementation 1 료 모 러.
~cpp #include <stdio.h> typedef char* PSTR; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; void Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void Input() { IntPair testReceiver; testReceiver = InputBoardSize(); printf ("Board Size value : %d, %d \n", testReceiver.n1, testReceiver.n2); testReceiver = InputStartRoachPosition(); printf ("Start Position value : %d, %d \n", testReceiver.n1, testReceiver.n2); char testJourney[MAX_JOURNEY_LENGTH]=""; InputRoachJourney(testJourney); printf ("Journey : %s \n", testJourney); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
부 는 . 들 느 노(?) 는 모 보.
1. 는
2. . 변 마 받.
3. return 대 . 따 .
4. 뒤 return 대 printf cout 등 보.
2. . 변 마 받.
3. return 대 . 따 .
4. 뒤 return 대 printf cout 등 보.
Input 낸 데들 main 끌립. ScheduedWalk 모듈 데를 보내 .
~cpp #include <stdio.h> typedef int BOOL; typedef char* PSTR; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); int main() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); ScheduledWalk(); Output(); return 0; } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); return inputData; } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
Input 부 .
7. Version 0.5 - Implementation : ScheduledWalk ¶
모듈 - ScheduledWalk() . Depth First . 리 TestDrivenDevelopment 를 미봅.
( , 는 TestFirstProgramming, UnitTest)
( , 는 TestFirstProgramming, UnitTest)
~cpp BOOL IsFinished() { // Test Code. 램 료 는 . char journey = "111122222"; // 데를 '111122222' 를 받 int currentPosition = 1; // 는 번(0,1,..) , '1' . 는 9. assert (IsJourneyEnd(journey, currentPosition) == false); // , 면 '' . // return IsJourneyEnd() || IsAllBoardChecked(); return true; } BOOL IsJourneyEnd() { // . return true; } BOOL IsAllBoardChecked() { // 됨. return true; }
보면, assert 부 러 . 러면 assert 부 . , 는 Test 를 만뒤, Test 를 를 면 . 뭔 동 면,
- 문 모듈 .
- Test Code 를 본.
- 모듈 본.
를 .
~cpp BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; }
를 .
~cpp BOOL IsFinished() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); // return IsJourneyEnd() || IsAllBoardChecked(); return true; }
IsAllBoardChecked 대 를 , .
~cpp BOOL IsFinished() { /* ---------------- IsJourney Test Case ---------------- char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); ---------------- IsJourney Test Case ---------------- */ /* ---------------- IsAllBoardChecked Test Case ---------------- int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); ---------------- IsAllBoardChecked Test Case ---------------- */ char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 10; // for true condition int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } int maxRow = 10; int maxCol = 10; return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) < (UINT)currentPosition; } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[i*maxRow+j] <= 0) return false; } } return true; }
2 34; .
3. .
MoveNext 대 를 봅.
~cpp void MoveNext() { GetMoveVector(); MoveRoach(); IncrementBoardBlockCount(); } void GetMoveVector() { } void MoveRoach() { } void IncrementBoardBlockCount() { }
Prototype 를 봤. 대는 .~
~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void ScheduledWalk(); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void MoveNext(); void GetMoveVector(); void MoveRoach(); void IncrementBoardBlockCount(); void Output(); void OutputMoveCount(); void OutputBoardStatus(); . . .
부 대 는 .
1. 능 매 는 모 /모듈 대 .
2. 모듈 는 Test Case 를 보.
3. Test Case .
4. . Test Case 부 .
5. Test Case 를 보. ( 따 . . 만 는 .)
2. 모듈 는 Test Case 를 보.
3. Test Case .
4. . Test Case 부 .
5. Test Case 를 보. ( 따 . . 만 는 .)
GetMoveVector 대 .
~cpp void MoveNext() { testGetMoveVector(); /* GetMoveVector(journey, currnetPosition); MoveRoach(); IncrementBoardBlockCount(); */ } IntPair GetMoveVector(char* journey, int currentPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); }
MoveRoach 대 Test Case .
~cpp void MoveRoach() { } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); }
Test Case 대 만는 MoveRoach 를 봅. 본 Requirement 대범를 면
대 를 만, 대 면 . 대 Test Case를 면 됩.
대 를 만, 대 면 . 대 Test Case를 면 됩.
~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = currentRoachPosition.n1 + moveVector.n1; updatedRoachPosition.n2 = currentRoachPosition.n2 + moveVector.n2; return updatedRoachPosition; }리 를 만는 봅.
러면 범를 대 부 대 Test Case 를 봅.
~cpp void testMoveRoach() { . . . // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxCol, maxRow); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); }방 IncrementBoardBlockCount 를 .
~cpp void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); }
3 52부 4 ;
4 5. ~
4 5. ~
~cpp IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; /* 부 . */ return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "222"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); }
MoveNext 부 test case 를 pass . 미 GetMoveVector, MoveRoach, IncrementBoardBlockCount 들 는 만 면 됩.
~cpp IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[1*maxRow+1] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[2*maxRow+2] == 1); }
대 ScheduledWalk (). 따른 는데 러 . 럴?
{{{~cpp int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; // '22222' 를 받 . int maxCol = 10; // 는 10, 10 int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; // Roach 는 0,0 startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); // 동 대는 printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); // 5 . 데 바는 6 . } 럴? 무를 먼 봐 . 1. ScheduledWalk 는 totalMoveCount 르는 . 2. totalMoveCount 르는 while 루 리는 . 3. while 루 리는 IsFinished() 바르 는 . 4. IsFinished() 못는 IsJourneyEnd, IsAllBoardChecked 못는 . 만, 모듈들 Test Case를 만 . 럼 Test Case 를 봐? {{{~cpp void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; // <---- 배 9. 0번부 므 8번 . assert (IsJourneyEnd(journey, currentPosition) == false); // <--- true! currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } }}} 면, 린 맞 만들 IsJourneyEnd 린 . 를 , . 리 램 돌봅. 리 Test Case를 . {{{~cpp void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[0*maxRow+0] == 1); assert (board[0*maxRow+1] == 1); assert (board[0*maxRow+2] == 1); assert (board[0*maxRow+3] == 1); assert (board[0*maxRow+4] == 1); assert (board[0*maxRow+5] == 1); } }}} === Version 0.6 - Implementation : Output & === 료면 ScheduledWalk 료. 럼 Output . Acceptance TestCase 를 1 료 . {{{~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row 들. int n2; // Col 들. } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void testInput(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void testIsJourney(); void testIsAllBoardCheck(); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testIsFinished(); void testMoveNext(); void testScheduledWalk(); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; return 0; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } void testInput() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[0*maxRow+0] == 1); assert (board[0*maxRow+1] == 1); assert (board[0*maxRow+2] == 1); assert (board[0*maxRow+3] == 1); assert (board[0*maxRow+4] == 1); assert (board[0*maxRow+5] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { testIsJourney(); testIsAllBoardCheck(); return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[i*maxRow+j] <= 0) return false; } } return true; } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[1*maxRow+1] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[2*maxRow+2] == 1); } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1) % maxRow; updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2) % maxCol; return updatedRoachPosition; } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxRow*i+j]); } printf ("\n"); } } }}} === Test & === . ["RandomWalk2/TestCase"] 는 ["AcceptanceTest"] 는 들 봅. {{{~cpp F:\WorkingTemp\ScheduledWalk\Debug>ScheduledWalk 5 5 0 0 22224444346 999 11 2 1 1 1 2 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 본 대 : 2 1 1 1 1 1 0 0 0 2 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 }}} . 맞봅. 마막 6 대 동 마 1 대 동럼 . , 바 는 는 6번 대 동부. 동방벡를 는 루 보면 GetMoveVector 므 부 번 받. {{{~cpp IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } }}} .. Vector 는 문 보. 면 Roach를 동는 Position MoveRoach 부 보. (는 동방 는 동는 맞 . board 배 update 받는를 먼 머릿 .) 림 는 는 Debugger Trace, break point 를 . 만, 를 놓 문 부 느 림 . {{{~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1) % maxRow; updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2) % maxCol; return updatedRoachPosition; } }}} 부 대 는데; (디 르 부 못 면 Debugger 를 .) 바 index -1 . 러므 Modular . row 1, col 0 row 0, col -1 더 루를 면 row 1, col -1 . -1 대 modular 5를 면? 4 . , 1, 4 리. main () test code 모 바. {{{~cpp int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ /* InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; */ testScheduledWalk(); return 0; } }}} MoveRoach 를 봤. {{{~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } }}} assert 문 놓 부 대 ok. {{{~cpp 5 5 0 0 22224444346 999 11 2 1 1 1 1 1 0 0 0 2 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Press any key to continue }}} Test Case 대 ok. 를 . ["RandomWalk2/TestCase"] 대 ok. ["RandomWalk2/TestCase2"] Test1,2,3 대 ok. . 럼 더 미 ? 복 .. Test4 대는 {{{~cpp 5 4 0 0 2224444666 999 10 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 }}} 리 못 . 는 대 {{{~cpp 10 1 1 1 1 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 }}} ["RandomWalk2/TestCase"] 멈면 뻔 . 는 램 를 높. Quality Assurance . 만 본면, 배 뒤. 면 따른 러 능 높. 면, 배 는 IncrementBoardBlockCount() 러능 높. {{{~cpp void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; // <--- maxRow maxCol . } }}} ? 런 램 내 많 . 2 동배 1 배 문. .. ( 밖 1 배 부들 Search '*maxRow+' 봤. 13데 더;;) 는 - 런 변 부 리 만들는 . -_-; OO Language 면 1 배 2 배 를 만들 는 . 문를 배 내 . 는 C 만들 배. {{{~cpp int _2to1(int row, int col, int maxCol) { return maxCol*row + col; } }}} ["NewTestsForOldBugs"] . === 들 만는 === (["RandomWalk2/TestCase"], ["RandomWalk2/TestCase2"]) 를 만는 . {{{~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row 들. int n2; // Col 들. } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void InputEndCode(); void testInput(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void testIsJourney(); void testIsAllBoardCheck(); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testIsFinished(); void testMoveNext(); void testScheduledWalk(); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int _2to1(int row, int col, int maxCol) { return maxCol*row+col; } int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; return 0; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } void testInput() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardCol, &boardRow); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[_2to1(0,0,maxCol)] == 1); assert (board[_2to1(0,1,maxCol)] == 1); assert (board[_2to1(0,2,maxCol)] == 1); assert (board[_2to1(0,3,maxCol)] == 1); assert (board[_2to1(0,4,maxCol)] == 1); assert (board[_2to1(0,5,maxCol)] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { testIsJourney(); testIsAllBoardCheck(); return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[_2to1(i,j,maxCol)] <= 0) return false; } } return true; } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[_2to1(0,0,maxCol)] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[_2to1(1,1,maxCol)] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[_2to1(2,2,maxCol)] == 1); } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxCol + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxCol*i+j]); } printf ("\n"); } } }}} 더 . 메 복보. 리 AcceptanceTest 대는 동 . === 마무리 === ==== main.cpp ==== {{{~cpp #include "ScheduledWalkTestCase.h" #include "ScheduledWalk.h" int main() { /* ------------- Acceptance Test ------------- AcceptanceTestAll (); ------------- Acceptance Test ------------- */ AcceptanceTestAll (); // <--- For Acceptance Test UnitTestAll(); // ScheduledWalkMain(); // <--- Main Routine return 0; } void ScheduledWalkMain() { InputData inputData; IntPair startRoachPosition; int maxRow; int maxCol; int* board; char journey[MAX_JOURNEY_LENGTH]=""; int totalMoveCount = 0; inputData = Input(); maxRow = inputData.boardSize.n1; maxCol = inputData.boardSize.n2; board = CreateBoard(maxRow, maxCol); startRoachPosition = inputData.roachPosition; strcpy(journey, inputData.journey); totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); DestroyBoard(board); } }}} ==== ScheduledWalk.h ==== {{{~cpp #ifndef _SCHEDULEDWALK_H_ #define _SCHEDULEDWALK_H_ #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row 들. int n2; // Col 들. } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void InputEndCode(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int _2to1(int row, int col, int maxCol); int* CreateBoard(int maxRow, int maxCol); void DestroyBoard(int* board); #endif }}} ==== ScheduledWalk.cpp ==== {{{~cpp #include "ScheduledWalk.h" int _2to1(int row, int col, int maxCol) { return maxCol*row+col; } int* CreateBoard(int maxRow, int maxCol) { int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); return board; } void DestroyBoard(int* board) { delete board; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardCol, &boardRow); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[_2to1(i,j,maxCol)] <= 0) return false; } } return true; } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxCol + roachPosition.n2]++; } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxCol*i+j]); } printf ("\n"); } } }}} ==== ScheduledWalkTestCase.h ==== {{{~cpp #include <assert.h> #include <stdio.h> #include "ScheduledWalk.h" void testScheduledWalk1_1(); void testScheduledWalk1_2(); void testScheduledWalk1_3(); void testScheduledWalk1_4(); void testScheduledWalk2_1(); void testScheduledWalk2_2(); void testScheduledWalk2_3(); void testScheduledWalk2_4(); void testInput(); void testIsFinished(); void testIsJourney(); void testIsAllBoardCheck(); void testMoveNext(); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testScheduledWalk(); void AcceptanceTestAll(); void UnitTestAll(); void testScheduledWalking(int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey, int expectedTotalMoveCount, int* expectedBoardArray); void excuteTest (void (*func)(void), PSTR message); }}} ==== ScheduledWalkTestCase.cpp ==== {{{~cpp #include "ScheduledWalkTestCase.h" void testInput() { InputData inputData; printf ("Input data ... :"); inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); assert (totalMoveCount == 5); assert (board[_2to1(0,0,maxCol)] == 1); assert (board[_2to1(0,1,maxCol)] == 1); assert (board[_2to1(0,2,maxCol)] == 1); assert (board[_2to1(0,3,maxCol)] == 1); assert (board[_2to1(0,4,maxCol)] == 1); assert (board[_2to1(0,5,maxCol)] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[_2to1(0,0,maxCol)] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[_2to1(1,1,maxCol)] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[_2to1(2,2,maxCol)] == 1); } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextM