// 1) print the "board" with no formatting and no loop // 2) print the "board" with no formatting // 3) print the "board" with formatting // 4) prompt for a position, toggle the position // 5) implement everything except "jump" logic // 6) design and implement the "jump" logic #include #include using std::cout; using std::cin; using std::string; #if 0 // 1) print the "board" with no formatting and no loop int main() { cout << "A\n"; cout << "B C\n"; cout << "D E F\n"; cout << "G H I J\n"; cout << "K L M N O\n"; } // A // B C // D E F // G H I J // K L M N O // 2a) print the "board" with no formatting (one loop) int main() { char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; int i = 0; int row_number = 1; int column_offset = 0; while (row_number < 6) { cout << positions[i] << ' '; i = i + 1; column_offset = column_offset + 1; if (column_offset == row_number) { cout << '\n'; row_number = row_number + 1; column_offset = 0; } } } // A // B C // D E F // G H I J // K L M N O // 2b) print the "board" with no formatting (two loops) int main() { char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; int i = 0, row = 0, col = 0; while (row < 5) { while (col <= row) { cout << positions[i] << ' '; col = col + 1; i = i + 1; } col = 0; row = row + 1; cout << '\n'; } } // A // B C // D E F // G H I J // K L M N O // 3) print the "board" with formatting int main() { char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; int i = 0, row = 0, col = 0; while (row < 5) { for (int j=0; j < 4 - row; j = j + 1) cout << " "; while (col <= row) { cout << '<' << positions[i] << "> "; col = col + 1; i = i + 1; } col = 0; row = row + 1; cout << "\n\n"; } } // 4) prompt for a position, toggle the position char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; bool pegs_present[15] = { true,true,true,true,true,true,true,true,true,true,true, true,true,true,true }; void draw_board() { int i = 0, row = 0, col = 0; cout << '\n'; while (row < 5) { for (int j=0; j < 4 - row; j = j + 1) cout << " "; while (col <= row) { if (pegs_present[i]) cout << '<' << positions[i] << "> "; else cout << ' ' << positions[i] << " "; col = col + 1; i = i + 1; } col = 0; row = row + 1; cout << "\n\n"; } } int main() { char letter; int posit; while (true) { draw_board(); cout << "Position: "; cin >> letter; if (letter < 'a' || letter > 'o') continue; posit = letter - 'a'; if (pegs_present[posit]) positions[posit] = positions[posit] + 32; else positions[posit] = positions[posit] - 32; pegs_present[posit] = ! pegs_present[posit]; } } Input: e e Input: e // 5) implement everything except "jump" logic // 5) add "Select initial hole:" and "Input: from to", toggle_peg() char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; bool pegs_present[15] = { true,true,true,true,true,true,true,true,true,true,true, true,true,true,true }; void draw_board() { int i = 0, row = 0, col = 0; cout << '\n'; while (row < 5) { for (int j=0; j < 4 - row; j = j + 1) cout << " "; while (col <= row) { if (pegs_present[i]) cout << '<' << positions[i] << "> "; else cout << ' ' << positions[i] << " "; col = col + 1; i = i + 1; } col = 0; row = row + 1; cout << "\n\n"; } } void toggle_peg( int i ) { if (pegs_present[i]) positions[i] = positions[i] + 32; else positions[i] = positions[i] - 32; pegs_present[i] = ! pegs_present[i]; } int main() { char from, to; draw_board(); cout << "Select initial hole: "; cin >> from; toggle_peg( from - 'a' ); while (true) { draw_board(); cout << "Move (from to): "; cin >> from >> to; if (from < 'a' || from > 'o' || to < 'a' || to > 'o') continue; toggle_peg( from - 'a' ); toggle_peg( to - 'a' ); } } Select initial hole: m m Move (from to): o m o Move (from to): n j j n o Move (from to): // 6) design and implement the "jump" logic char positions[15] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' }; bool pegs_present[15] = { true,true,true,true,true,true,true,true,true,true,true, true,true,true,true }; // { a, b, c } = { from, to, jumped position } int jump_table[36][3] = { {1,4,2}, {1,6,3}, {2,7,4}, {2,9,5}, {3,8,5}, {3,10,6}, {4,6,5}, {4,1,2}, {4,11,7}, {4,13,8}, {5,14,9}, {5,12,8}, {6,4,5}, {6,13,9}, {6,15,10}, {6,1,3}, {7,2,4}, {7,9,8}, {8,3,5}, {8,10,9}, {9,2,5}, {9,7,8}, {10,8,9}, {10,3,6}, {11,13,12}, {11,4,7}, {12,5,8}, {12,14,13}, {13,11,12}, {13,15,14}, {13,6,9}, {13,4,8}, {14,12,13}, {14,5,9}, {15,13,14}, {15,6,10} }; int peg_count = 15; void toggle_peg( int i ) { if (pegs_present[i]) positions[i] = positions[i] + 32; else positions[i] = positions[i] - 32; pegs_present[i] = ! pegs_present[i]; } string attempt_move( char from, char to ) { if (from < 'a' || from > 'o' || to < 'a' || to > 'o') return "INVALID INPUT"; int one = from - 'a' + 1, two = to - 'a' + 1; for (int i=0; i < 36; i = i + 1) if (one == jump_table[i][0] && two == jump_table[i][1]) if (pegs_present[ jump_table[i][0]-1 ] && pegs_present[ jump_table[i][2]-1 ] && ( ! pegs_present[ jump_table[i][1]-1 ])) { toggle_peg( jump_table[i][0]-1 ); // source_peg toggle_peg( jump_table[i][2]-1 ); // jumped_peg toggle_peg( jump_table[i][1]-1 ); // destination peg_count = peg_count - 1; return "VALID MOVE"; } return "NOT A LEGAL MOVE"; } void draw_board() { int i = 0, row = 0, col = 0; cout << '\n'; while (row < 5) { for (int j=0; j < 4 - row; j = j + 1) cout << " "; while (col <= row) { if (pegs_present[i]) cout << '<' << positions[i] << "> "; else cout << ' ' << positions[i] << " "; col = col + 1; i = i + 1; } col = 0; row = row + 1; cout << "\n\n"; } } int main() { char from, to; draw_board(); cout << "Select initial hole: "; cin >> from; toggle_peg( from - 'a' ); peg_count = 14; string message = "VALID MOVE"; while (true) { if (message != "VALID MOVE") cout << '\n' << message << '\n'; draw_board(); if (peg_count == 1) break; cout << "Move (from to): "; cin >> from >> to; message = attempt_move( from, to ); } } Select initial hole: d d Move (from to): a d a b Move (from to): d a NOT A LEGAL MOVE a b Move (from to): p j INVALID INPUT a b Move (from to): f a b c f Move (from to): #endif