1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include "Sandpiles.h" #include "GUI/SimpleTest.h" using namespace std;
void dropSandOn(Grid<int>& world, int row, int col) { if (!world.inBounds(row, col)) { return ; } if (world[row][col] <= 2) { world[row][col]++; } else { world[row][col] = 0; dropSandOn(world, row + 1, col); dropSandOn(world, row - 1, col); dropSandOn(world, row, col + 1); dropSandOn(world, row, col - 1); } }
PROVIDED_TEST("Dropping into an empty cell only changes that cell.") { Grid<int> before = { { 3, 3, 3 }, { 3, 0, 3 }, { 3, 3, 3 } }; Grid<int> after = { { 3, 3, 3 }, { 3, 1, 3 }, { 3, 3, 3 } };
dropSandOn(before, 1, 1); EXPECT_EQUAL(before, after); }
PROVIDED_TEST("Non-chaining topples work.") { Grid<int> before = { { 0, 0, 0 }, { 1, 3, 1 }, { 0, 2, 0 } }; Grid<int> after = { { 0, 1, 0 }, { 2, 0, 2 }, { 0, 3, 0 } };
dropSandOn(before, 1, 1); EXPECT_EQUAL(before, after); }
PROVIDED_TEST("Two topples chain.") { Grid<int> before = { { 0, 0, 0, 0 }, { 0, 3, 3, 0 }, { 0, 0, 0, 0 } }; Grid<int> after = { { 0, 1, 1, 0 }, { 1, 1, 0, 1 }, { 0, 1, 1, 0 } };
dropSandOn(before, 1, 1); EXPECT_EQUAL(before, after); }
PROVIDED_TEST("out bound.") { Grid<int> before = { { 0, 0, 0 }, { 1, 3, 1 }, { 0, 2, 0 } }; Grid<int> after = { { 0, 0, 0 }, { 1, 3, 1 }, { 0, 2, 0 } };
dropSandOn(before, 3, 3); EXPECT_EQUAL(before, after); }
|