c++ - Allocated memory release before end of program -
the program i'm writing sudoku puzzle. each puzzle 2d array of struct, each struct int , bool. bool represents if number in each element of array clue or not, clues cannot changed. sorry huge blocks of code. pared down as possible easy readability.
there missing function showboardans sudoku.h. same showboard, in different colors.
after first time program displays puzzle (using showboard sudoku.h), appears both puzzle , comp deleted memory. after entering number answer (say 1 x, 1 y , 1 answer), when board printed again, either holds garbage or not print except outline.
when debugging using visual studio, puzzle , comp start hold garbage rather numbers program assigned. far can tell, happens after program goes main sudoku after displaying puzzle first time.
#ifndef sudoku_h #define sudoku_h #include <iostream> #include <windows.h> // header file needed change text color of console using namespace std; class sudoku { protected: struct puzz { int ans; bool isclue; }; public: sudoku() {}; // default constructor sudoku(puzz [][9]); // constructor ~sudoku() {}; // destructor bool iscorrect(int [][9], puzz [][9]); // checks puzzle determine accuracy void showboard(puzz [][9]) const; void showboardans(puzz [][9]) const; }; /* constructor each puzzle array initalized here complete numbers. */ sudoku::sudoku(puzz arr[9][9]) { showboard(arr); } bool sudoku::iscorrect(int comp[][9], puzz arr[9][9]) { bool correct = false; // init false (int x = 0; x < 9; x++) { (int y = 0; y < 9; y++) { if (comp[x][y] != arr[x][y].ans) return correct = false; else correct = false; } } return correct; } /* showboard displays incomplete puzzle user first time see it. after have started entering answers, showboardans called show user different text colors. */ void sudoku::showboard(puzz arr[][9]) const { hwnd console = getconsolewindow(); rect r; getwindowrect(console, &r); movewindow(console, r.left, r.top, 800, 450, true); handle hconsole; int color = 7; hconsole = getstdhandle(std_output_handle); setconsoletextattribute(hconsole, color); int x, y; cout << " "; (int c = 1 ; c <= 9; c++) { cout << " " << c; } cout << "\n _____________________________________\n" << endl; (x = 0; x < 9; x++) { cout << x+1 << " | "; (y = 0; y < 9; y++) { // assign colors, if element clue, green, // otherwise, 0s white. if (arr[x][y].isclue == true) { color = 10; setconsoletextattribute(hconsole, color); cout << arr[x][y].ans; color = 7; setconsoletextattribute(hconsole, color); cout << " | "; } else { color = 15; setconsoletextattribute(hconsole, color); cout << arr[x][y].ans; color = 7; setconsoletextattribute(hconsole, color); cout << " | "; } } cout << "\n _____________________________________\n" << endl; } } #endif #include <iostream> #include "sudoku.h" using namespace std; class easy : protected sudoku { private: int comp[9][9]; // comp completed puzzle puzz puzzle[9][9]; // puzzle user sees sudoku easypuzzle; public: easy(); easy(int); ~easy() { delete [] &puzzle; } void addnum(int, int, int); // add number solve puzzle bool solve(); void showpuzz(); }; easy::easy() { (int x = 0; x < 9; x++) { (int y = 0; y < 9; y++) { comp[x][y] = 0; puzzle[x][y].ans = 0; puzzle[x][y].isclue = false; } } } /* default constructor initializes arrays hold completed , partial puzzles. */ easy::easy(int a) { int comp[][9] = { {3, 7, 6, 1, 4, 2, 9, 5, 8}, {5, 8, 4, 6, 7, 9, 2, 1, 3}, {9, 1, 2, 8, 3, 5, 4, 7, 6}, {6, 9, 1, 2, 5, 4, 3, 8, 7}, {8, 4, 5, 3, 6, 7, 1, 9, 2}, {2, 3, 7, 9, 1, 8, 5, 6, 4}, {4, 6, 9, 5, 8, 3, 7, 2, 1}, {7, 2, 8, 4, 9, 1, 6, 3, 5}, {1, 5, 3, 7, 2, 6, 8, 4, 9} }; puzz puzzle[][9] = { {{0, false}, {0, false}, {0, false}, {0, false}, {4, true}, {0, false}, {9, true}, {0, false}, {0, false}}, {{0, false}, {8, true}, {0, false}, {6, true}, {7, true}, {0, false}, {0, false}, {0, false}, {0, false}}, {{9, true}, {0, false}, {2, true}, {8, true}, {0, false}, {0, false}, {4, true}, {0, false}, {0, false}}, {{0, false}, {9, true}, {1, true}, {0, false}, {0, false}, {0, false}, {0, false}, {0, false}, {0, false}}, {{0, false}, {4, true}, {0, false}, {3, true}, {6, true}, {0, false}, {0, false}, {0, false}, {2, true }}, {{0, false}, {0, false}, {0, false}, {0, false}, {0, false}, {0, false}, {5, true}, {0, false}, {4, true }}, {{0, false}, {0, false}, {0, false}, {0, false}, {0, false}, {0, false}, {7, true}, {0, false}, {1, true }}, {{0, false}, {2, true}, {8, true}, {0, false}, {0, false}, {1, true}, {0, false}, {3, true}, {0, false}}, {{1, true}, {0, false}, {3, true}, {7, true}, {0, false}, {6, true}, {8, true}, {0, false}, {0, false}} }; sudoku easypuzzle(puzzle); } /* addnum lets user select x , y coordinates of enter new number and, after checking make sure not 1 of hint numbers, update puzzle array */ void easy::addnum(int x, int y, int answer) { // subtract 1 both x , y @ correct subscript. x-= 1; y-= 1; if (puzzle[x][y].isclue == true) { cout << "cannot change puzzle clue. please try again." << endl; return; } else { puzzle[x][y].ans = answer; easypuzzle.showboardans(puzzle); }} #include <iostream> #include "sudoku.h" #include "easy.h" #include <windows.h> using namespace std; int menu(); // menu function prototype void easypuzz(easy); // constants menu choices const int easy = 1, med = 2, hard = 3, quit = 4; int main() { int choice; cout << "\t\t\twelcome sudoku!\n\n" << endl; choice = menu(); // calls menu function if (choice == 1) { cout << "you have chosen easy puzzle. when doing puzzle, " << "will enter x and\ny coordinate change answer. if " << "enter coordinate clue, receive error. " << "if you'd check see if you've solved puzzle " << "type s instead of coordinates.\n" << endl; easy epuzzle(1); //epuzzle.showpuzz(); easypuzz(epuzzle); } else if (choice == 2) { cout << "you have chosen medium puzzle. when doing puzzle, " << "will enter x and\ny coordinate change answer. if " << "enter coordinate clue, receive error." << "if you'd check see if you've solved puzzle " << "type s instead of coordinates.\n"<< endl; medium mpuzzle; medpuzz(mpuzzle); } else if (choice == 3) { cout << "you have chosen hard puzzle. when doing puzzle, " << "will enter x and\ny coordinate change answer. if " << "enter coordinate clue, receive error. " << "you can hint entering h instead of number." << "if you'd check see if you've solved puzzle " << "type s instead of coordinates.\n"<< endl; hard hpuzzle; hardpuzz(hpuzzle); } else if (choice == 4) { cout << "thank playing!" << endl; exit(0); }
Comments
Post a Comment