Contents
1. 개글 ¶
데블2002때 개던 StructuredProgramming 기법 부 끝까 는 모 보드립다. 그가 발고, 그 그를 를 그대로 봅다. 그대로 따라보 듯. 단, 과 러 겁먹만 다면. ^^;
Spec 과 Test Case 는 RandomWalk2 다.
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() { }
기까가 난 데블2002 때 그렸던 HIPO 대를 드로 긴 것다.
답 벽 답 며, HIPO 바로 로그램 까 길는 리가 다. (냐. Top-Down Design 결과가 벽다라고 말는 까. 만, 문 반 로그램 디, 밑그림 그리고 는데 는 명다. )
답 벽 답 며, HIPO 바로 로그램 까 길는 리가 다. (냐. Top-Down Design 결과가 벽다라고 말는 까. 만, 문 반 로그램 디, 밑그림 그리고 는데 는 명다. )
(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. 개~
MoveNext 대 Test Case를 다. 그러면 들 각내고, 갑다. MoveNext 들 GetMoveVector MoveRoach, IncrementBoardBlockCount 들 됩다.
~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