From b1018d5843c5a12b8f81928ab7b0c905ce5f0da5 Mon Sep 17 00:00:00 2001 From: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> Date: Sun, 26 Jan 2025 18:41:09 +0530 Subject: [PATCH 1/4] added inital comments Signed-off-by: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> --- src/main.cpp | 93 +++++++++++++++++++++++++++++++++++++---------- src/sudoku.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 159 insertions(+), 31 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fb438fe..67285ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ sf::Time elapsed1; sf::Image image("assests/sudoku.png"); sf::Texture drawing("assests/drawing.png"); sf::Texture notDrawing("assests/not-drawing.png"); -sf::Sprite icon(notDrawing); +sf::Sprite pencilIcon(notDrawing); void sudokuGenerator(int k) { Sudoku *suk = new Sudoku(); suk->fillDiagonal(cells); @@ -26,12 +26,16 @@ void sudokuGenerator(int k) { delete suk; } +/** + * Checks if puzzle was solved + */ bool checkForVictory() { Sudoku *suk = new Sudoku(); bool win = suk->isValid(cells); delete suk; return win; } + void pencilAppend(std::string numToAdd, int index) { cells[index].isPencil = true; int found = cells[index].pencilNums.find(numToAdd); @@ -49,6 +53,10 @@ void pencilAppend(std::string numToAdd, int index) { } } void clearSelected(int index) { cells[index].isSelected = false; } + +/** + * Generates a puzzle and stores it in `cells` + */ void fillCell() { for (int a = 0; a < 9; a++) { for (int b = 0; b < 9; b++) { @@ -57,10 +65,14 @@ void fillCell() { } sudokuGenerator(10); } + +/** + * Returns 1D cell index from mouse's relative x y position + */ int getMousPos(sf::RenderWindow &window) { int mouse_x = sf::Mouse::getPosition(window).x / CELL_SIZE; int mouse_y = sf::Mouse::getPosition(window).y / CELL_SIZE; - mouse_y = mouse_y - 3; + mouse_y = mouse_y - offset/CELL_SIZE; if (mouse_y < 0) { return 0; } @@ -74,11 +86,19 @@ void highlight(int num) { cells[i].isHighlight = true; } } + +/** + * Clears highlighting of all cells + */ void clearHighlight() { for (int i = 0; i < 81; i++) { cells[i].isHighlight = false; } } + +/** + * Shows initial play game screen + */ void startScreen(bool startedPlaying, sf::RenderWindow &window) { if (!startedPlaying) { text.setCharacterSize(30); @@ -88,6 +108,10 @@ void startScreen(bool startedPlaying, sf::RenderWindow &window) { window.draw(text); } } + +/** + * Draw board + */ void drawRectangles(sf::RenderWindow &window, bool isWin, bool isStarted) { if (isStarted) { if (isWin) { @@ -99,11 +123,14 @@ void drawRectangles(sf::RenderWindow &window, bool isWin, bool isStarted) { window.draw(text); return; } + sf::RectangleShape rectangle({CELL_SIZE, CELL_SIZE}); - sf::RectangleShape line({5, 1100}); - line.setFillColor(sf::Color::Black); + sf::RectangleShape subgridSeparator({5, 1100}); + subgridSeparator.setFillColor(sf::Color::Black); + for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { + if (cells[i + j * 9].isSelected) { rectangle.setFillColor(sf::Color(99, 99, 99)); } else if (cells[i + j * 9].isHighlight) { @@ -111,12 +138,15 @@ void drawRectangles(sf::RenderWindow &window, bool isWin, bool isStarted) { } else { rectangle.setFillColor(sf::Color::White); } + rectangle.setOutlineThickness(1); rectangle.setOutlineColor(sf::Color::Black); rectangle.setPosition( {float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); rectangle.setTexture(&texture); window.draw(rectangle); + + // draw inner text text.setPosition({float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); if (cells[i + j * 9].canBeChanged && !cells[i + j * 9].isPencil && cells[i + j * 9].number != 0) { @@ -142,28 +172,41 @@ void drawRectangles(sf::RenderWindow &window, bool isWin, bool isStarted) { // random } + // draw subgrid separator if ((i % 3 == 0 && i != 0)) { - line.setSize({5, 1100}); - line.setPosition({float(CELL_SIZE * i - 5), 0}); - window.draw(line); + // vertical line + subgridSeparator.setSize({5, 1100}); + subgridSeparator.setPosition({float(CELL_SIZE * i - 5), 0}); + window.draw(subgridSeparator); } else if ((j % 3 == 0 && j != 0)) { - line.setSize({1100, 5}); - line.setPosition({0, float(CELL_SIZE * j) + offset}); - window.draw(line); + // horizontal line + subgridSeparator.setSize({1100, 5}); + subgridSeparator.setPosition({0, float(CELL_SIZE * j) + offset}); + window.draw(subgridSeparator); } } } } } + +/** + * shows time on the screen + */ void drawTime(sf::RenderWindow &window, sf::Time time, bool isStart) { if (isStart) { - icon.scale({1, 1}); - icon.setPosition({30, 100}); - window.draw(icon); + // show pencil icon + pencilIcon.scale({1, 1}); + pencilIcon.setPosition({30, 100}); + window.draw(pencilIcon); + + + ////// show time /////// text.setPosition({320, 0}); text.setFillColor(sf::Color::White); std::string str = std::to_string(time.asSeconds()); std::string newStr = ""; + + // skip decimal part for (int i = 0; i < str.length(); i++) { if (str[i] == '.') break; @@ -173,16 +216,24 @@ void drawTime(sf::RenderWindow &window, sf::Time time, bool isStart) { window.draw(text); } } + int main() { bool isPencil = false; bool start = false; + bool isWin = false; + srand(time(0)); fillCell(); + sf::Clock clock; clock.stop(); - bool isWin = false; - sf::RenderWindow window = - sf::RenderWindow(sf::VideoMode({9 * 80, 9 * 80 + offset}),"Sudoku",sf::Style::Titlebar| sf::Style::Close); + + sf::RenderWindow window = sf::RenderWindow( + sf::VideoMode({9 * CELL_SIZE, 9 * CELL_SIZE + offset}), + "Sudoku", + sf::Style::Titlebar | sf::Style::Close + ); + window.setIcon(image.getSize(), image.getPixelsPtr()); window.setFramerateLimit(144); int index = -1; @@ -192,12 +243,14 @@ int main() { if (event->is()) { window.close(); } + if (checkForVictory()) { isWin = true; elapsed1 = clock.getElapsedTime(); clock.stop(); } - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !start) { + + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !start) { int x = sf::Mouse::getPosition(window).x; int y = sf::Mouse::getPosition(window).y; if ((x < 400 && x > 300) && (y < 200 && y > 100)) { @@ -208,6 +261,7 @@ int main() { clearHighlight(); } } + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { clearHighlight(); if (index != -1) @@ -216,10 +270,10 @@ int main() { int y = sf::Mouse::getPosition(window).y; if ((x >= 30 && y >= 100 && y <= 200 && x <= 120)) { if (isPencil) { - icon.setTexture(notDrawing); + pencilIcon.setTexture(notDrawing); isPencil = false; } else { - icon.setTexture(drawing); + pencilIcon.setTexture(drawing); isPencil = true; } } @@ -392,6 +446,7 @@ int main() { } } window.clear(); + startScreen(start, window); drawTime(window, elapsed1, start); drawRectangles(window, isWin, start); diff --git a/src/sudoku.cpp b/src/sudoku.cpp index cd8f866..66843f8 100644 --- a/src/sudoku.cpp +++ b/src/sudoku.cpp @@ -21,6 +21,18 @@ struct Cell { }; class Sudoku { public: + + /** + * Checks if given number is used within + * [rowStart, colStart] to [rowStart + 2, colStart + 2] + * can be used to check duplicate numbers in a subgrid + * + * colStart + * ↓ + * row → o o o + * start o o o + * o o o + */ bool unUsedInBox(std::vector &grid, int rowStart, int colStart, int num) { for (int i = 0; i < 3; i++) { @@ -32,6 +44,8 @@ class Sudoku { } return true; } + + // checks if number is already present in the column j bool unUsedInCol(std::vector &grid, int j, int num) { for (int i = 0; i < 9; i++) { if (grid[i + j * 9].number == num) { @@ -40,6 +54,8 @@ class Sudoku { } return true; } + + // checks if number is already present in the row i bool unUsedInRow(std::vector &grid, int i, int num) { for (int j = 0; j < 9; j++) { if (grid[i + j * 9].number == num) { @@ -48,25 +64,34 @@ class Sudoku { } return true; } + + /** + * Returns true if number isn't already present in the same subgrid, row and col + */ bool checkIfSafe(std::vector &grid, int i, int j, int num) { - return (unUsedInRow(grid, i, num) && unUsedInCol(grid, j, num) && + return (unUsedInRow(grid, i, num) && + unUsedInCol(grid, j, num) && unUsedInBox(grid, i - i % 3, j - j % 3, num)); } - int isValid(std::vector &mat) { + + /** + * returns whether solution is valid or not + */ + int isValid(std::vector &grid) { std::vector> rows(10, std::vector(10, 0)); std::vector> cols(10, std::vector(10, 0)); - std::vector> subMat(10, std::vector(10, 0)); + std::vector> subgrid(10, std::vector(10, 0)); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { - // Skip empty cells - if (mat[i + j * 9].number == 0) + // empty cells, not fully solved + if (grid[i + j * 9].number == 0) return false; // Current value - int val = mat[i + j * 9].number; + int val = grid[i + j * 9].number; // Check for duplicates in row if (rows[i][val] == 1) @@ -84,41 +109,67 @@ class Sudoku { // Check for duplicates in sub-grid int idx = (i / 3) * 3 + j / 3; - if (subMat[idx][val] == 1) + if (subgrid[idx][val] == 1) return false; // Mark as seen - subMat[idx][val] = 1; + subgrid[idx][val] = 1; } } return true; } + bool solveSudoku(std::vector &grid, int row, int col) { + + // return true if reached last row and overflown cols if (row == 9 - 1 && col == 9) return true; + /** + * wrap col when cols overflow + * currently here + * ↓ + * 0 1 2 3 4 5 6 7 8 x + * goto → x 1 2 3 4 5 6 7 8 + * here + * + */ if (col == 9) { row++; col = 0; } + /** if no number is filled, goto next column of same row + * + * currently here + * | goto here + * ↓ ↓ + * 0 1 2 3 4 5 6 7 8 + * + * */ if (grid[row + col * 9].number > 0) return solveSudoku(grid, row, col + 1); - + + // check all possible values for a cell for (int num = 1; num <= 9; num++) { if (checkIfSafe(grid, row, col, num)) { - + // number doesn't exists in the respective subgrid, row & col + // add num to that cell and see if it is consistent grid[row + col * 9].number = num; - if (solveSudoku(grid, row, col + 1)) return true; } - + // number doesnt make consistent sudoku, reset the cell grid[row + col * 9].number = 0; } return false; } + + /** + * fill each subgrid by a random number + * A subgrid denoted by index of top left entry + */ void fillBox(std::vector &grid, int row, int col) { int num; for (int i = 0; i < 3; i++) { @@ -131,12 +182,34 @@ class Sudoku { } } + /** + * Fills diagonal subgrids with valid random arrangement of values + * + * col index + * 0 1 2 3 4 5 6 7 8 + * ----------------------- + * 0 x x x | - - - | - - - + * 1 x x x | - - - | - - - + * 2 x x x | - - - | - - - + * ------ - ----- - ------ + * row 3 - - - | x x x | - - - + * index 4 - - - | x x x | - - - + * 5 - - - | x x x | - - - + * ------ - ----- - ------ + * 6 - - - | - - - | x x x + * 7 - - - | - - - | x x x + * 8 - - - | - - - | x x x + * + */ void fillDiagonal(std::vector &grid) { for (int i = 0; i < 9; i = i + 3) { fillBox(grid, i, i); } } + /** + * Removes k number of random cells + */ void removeKDigits(std::vector &grid, int k) { while (k > 0) { From 0017900c80f9e8f68112e655dc5a0ae468b54139 Mon Sep 17 00:00:00 2001 From: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:17:45 +0530 Subject: [PATCH 2/4] Refactored code and added extra keycodes for input Signed-off-by: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> --- src/main.cpp | 448 +++++++++++++++++++++------------------------------ 1 file changed, 185 insertions(+), 263 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 67285ca..01d6a53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,15 +9,33 @@ #define CELL_SIZE 80 #define offset 240 + +typedef sf::Keyboard::Scancode Kcode; + sf::Texture texture("assests/bg.png"); sf::Font font("assests/type.ttf"); sf::Text text(font); std::vector cells; sf::Time elapsed1; -sf::Image image("assests/sudoku.png"); +sf::Image windowIcon("assests/sudoku.png"); sf::Texture drawing("assests/drawing.png"); sf::Texture notDrawing("assests/not-drawing.png"); sf::Sprite pencilIcon(notDrawing); + +// for handling keypress events +// handle both normal key and numberpad key +Kcode numcodes[18] = { + Kcode::Num1, Kcode::Numpad1, + Kcode::Num2, Kcode::Numpad2, + Kcode::Num3, Kcode::Numpad3, + Kcode::Num4, Kcode::Numpad4, + Kcode::Num5, Kcode::Numpad5, + Kcode::Num6, Kcode::Numpad6, + Kcode::Num7, Kcode::Numpad7, + Kcode::Num8, Kcode::Numpad8, + Kcode::Num9, Kcode::Numpad9 +}; + void sudokuGenerator(int k) { Sudoku *suk = new Sudoku(); suk->fillDiagonal(cells); @@ -67,11 +85,11 @@ void fillCell() { } /** - * Returns 1D cell index from mouse's relative x y position + * Returns 1D cell index from mouse's relative (x,y) position */ -int getMousPos(sf::RenderWindow &window) { - int mouse_x = sf::Mouse::getPosition(window).x / CELL_SIZE; - int mouse_y = sf::Mouse::getPosition(window).y / CELL_SIZE; +int getGridIndex(int x, int y) { + int mouse_x = x / CELL_SIZE; + int mouse_y = y / CELL_SIZE; mouse_y = mouse_y - offset/CELL_SIZE; if (mouse_y < 0) { return 0; @@ -80,6 +98,10 @@ int getMousPos(sf::RenderWindow &window) { cells[mouse_x + mouse_y * 9].isSelected = true; return (mouse_x + mouse_y * 9); } + +/** + * Highlights a given number throughout the grid + */ void highlight(int num) { for (int i = 0; i < 81; i++) { if (cells[i].number == num) @@ -99,122 +121,118 @@ void clearHighlight() { /** * Shows initial play game screen */ -void startScreen(bool startedPlaying, sf::RenderWindow &window) { - if (!startedPlaying) { - text.setCharacterSize(30); - text.setFillColor(sf::Color::Cyan); - text.setString("PLAY"); - text.setPosition({300, 100}); - window.draw(text); - } +void startScreen(sf::RenderWindow &window) { + text.setCharacterSize(30); + text.setFillColor(sf::Color::Cyan); + text.setString("PLAY"); + text.setPosition({300, 100}); + window.draw(text); } /** * Draw board */ -void drawRectangles(sf::RenderWindow &window, bool isWin, bool isStarted) { - if (isStarted) { - if (isWin) { - window.clear(); - int timeTaken = (elapsed1.asSeconds()); - text.setFillColor(sf::Color::White); - text.setPosition({200, 300}); - text.setString("YOU WIN!\n" + std::to_string(timeTaken) + " seconds"); - window.draw(text); - return; - } +void drawRectangles(sf::RenderWindow &window, bool isWin) { + if (isWin) { + window.clear(); + int timeTaken = (elapsed1.asSeconds()); + text.setFillColor(sf::Color::White); + text.setPosition({200, 300}); + text.setString("YOU WIN!\n" + std::to_string(timeTaken) + " seconds"); + window.draw(text); + return; + } - sf::RectangleShape rectangle({CELL_SIZE, CELL_SIZE}); - sf::RectangleShape subgridSeparator({5, 1100}); - subgridSeparator.setFillColor(sf::Color::Black); + sf::RectangleShape rectangle({CELL_SIZE, CELL_SIZE}); + sf::RectangleShape subgridSeparator({5, 1100}); + subgridSeparator.setFillColor(sf::Color::Black); - for (int i = 0; i < 9; i++) { - for (int j = 0; j < 9; j++) { + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { - if (cells[i + j * 9].isSelected) { - rectangle.setFillColor(sf::Color(99, 99, 99)); - } else if (cells[i + j * 9].isHighlight) { - rectangle.setFillColor(sf::Color::Magenta); - } else { - rectangle.setFillColor(sf::Color::White); - } + if (cells[i + j * 9].isSelected) { + rectangle.setFillColor(sf::Color(99, 99, 99)); + } else if (cells[i + j * 9].isHighlight) { + rectangle.setFillColor(sf::Color::Magenta); + } else { + rectangle.setFillColor(sf::Color::White); + } - rectangle.setOutlineThickness(1); - rectangle.setOutlineColor(sf::Color::Black); - rectangle.setPosition( - {float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); - rectangle.setTexture(&texture); - window.draw(rectangle); - - // draw inner text - text.setPosition({float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); - if (cells[i + j * 9].canBeChanged && !cells[i + j * 9].isPencil && - cells[i + j * 9].number != 0) { - text.setCharacterSize(64); - text.setFillColor(sf::Color::Black); - std::string String = std::to_string(cells[i + j * 9].number); - String = " " + String; - text.setString(String); - window.draw(text); - } else if (cells[i + j * 9].isPencil) { - text.setCharacterSize(21); - text.setFillColor(sf::Color::Black); - text.setString(cells[i + j * 9].pencilNums); - window.draw(text); - } else if (cells[i + j * 9].number != 0) { - std::string String = std::to_string(cells[i + j * 9].number); - String = " " + String; - text.setString(String); - text.setCharacterSize(64); - text.setFillColor(sf::Color(99, 62, 21)); - window.draw(text); - } else { - // random - } + rectangle.setOutlineThickness(1); + rectangle.setOutlineColor(sf::Color::Black); + rectangle.setPosition( + {float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); + rectangle.setTexture(&texture); + window.draw(rectangle); + + // draw cell text + text.setPosition({float(CELL_SIZE * i), float(CELL_SIZE * j) + offset}); + if (cells[i + j * 9].canBeChanged && !cells[i + j * 9].isPencil && + cells[i + j * 9].number != 0) { + text.setCharacterSize(64); + text.setFillColor(sf::Color::Black); + std::string String = std::to_string(cells[i + j * 9].number); + String = " " + String; + text.setString(String); + window.draw(text); + } else if (cells[i + j * 9].isPencil) { + text.setCharacterSize(21); + text.setFillColor(sf::Color::Black); + text.setString(cells[i + j * 9].pencilNums); + window.draw(text); + } else if (cells[i + j * 9].number != 0) { + text.setCharacterSize(64); + text.setFillColor(sf::Color(99, 62, 21)); + std::string String = std::to_string(cells[i + j * 9].number); + String = " " + String; + text.setString(String); + window.draw(text); + } else { + // random + } - // draw subgrid separator - if ((i % 3 == 0 && i != 0)) { - // vertical line - subgridSeparator.setSize({5, 1100}); - subgridSeparator.setPosition({float(CELL_SIZE * i - 5), 0}); - window.draw(subgridSeparator); - } else if ((j % 3 == 0 && j != 0)) { - // horizontal line - subgridSeparator.setSize({1100, 5}); - subgridSeparator.setPosition({0, float(CELL_SIZE * j) + offset}); - window.draw(subgridSeparator); - } + // draw subgrid separator + if ((i % 3 == 0 && i != 0)) { + // vertical line + subgridSeparator.setSize({5, 1100}); + subgridSeparator.setPosition({float(CELL_SIZE * i - 5), 0}); + window.draw(subgridSeparator); + } else if ((j % 3 == 0 && j != 0)) { + // horizontal line + subgridSeparator.setSize({1100, 5}); + subgridSeparator.setPosition({0, float(CELL_SIZE * j) + offset}); + window.draw(subgridSeparator); } } } } /** - * shows time on the screen + * Displays toolbar */ -void drawTime(sf::RenderWindow &window, sf::Time time, bool isStart) { - if (isStart) { - // show pencil icon - pencilIcon.scale({1, 1}); - pencilIcon.setPosition({30, 100}); - window.draw(pencilIcon); - +void drawToolbar(sf::RenderWindow &window) { + pencilIcon.scale({1, 1}); + pencilIcon.setPosition({30, 100}); + window.draw(pencilIcon); +} - ////// show time /////// - text.setPosition({320, 0}); - text.setFillColor(sf::Color::White); - std::string str = std::to_string(time.asSeconds()); - std::string newStr = ""; - - // skip decimal part - for (int i = 0; i < str.length(); i++) { - if (str[i] == '.') - break; - newStr.append(1, str[i]); - } - text.setString(newStr); - window.draw(text); +/** + * shows time on the screen + */ +void drawTime(sf::RenderWindow &window, sf::Time time) { + text.setPosition({320, 0}); + text.setFillColor(sf::Color::White); + std::string str = std::to_string(time.asSeconds()); + std::string newStr = ""; + + // skip decimal part + for (int i = 0; i < str.length(); i++) { + if (str[i] == '.') + break; + newStr.append(1, str[i]); } + text.setString(newStr); + window.draw(text); } int main() { @@ -229,12 +247,12 @@ int main() { clock.stop(); sf::RenderWindow window = sf::RenderWindow( - sf::VideoMode({9 * CELL_SIZE, 9 * CELL_SIZE + offset}), - "Sudoku", - sf::Style::Titlebar | sf::Style::Close - ); + sf::VideoMode({9 * CELL_SIZE, 9 * CELL_SIZE + offset}), + "Sudoku", + sf::Style::Titlebar | sf::Style::Close + ); - window.setIcon(image.getSize(), image.getPixelsPtr()); + window.setIcon(windowIcon.getSize(), windowIcon.getPixelsPtr()); window.setFramerateLimit(144); int index = -1; while (window.isOpen()) { @@ -250,7 +268,7 @@ int main() { clock.stop(); } - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !start) { + if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && !start) { int x = sf::Mouse::getPosition(window).x; int y = sf::Mouse::getPosition(window).y; if ((x < 400 && x > 300) && (y < 200 && y > 100)) { @@ -260,15 +278,21 @@ int main() { clock.start(); clearHighlight(); } + // skip checking further inputs + continue; } if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { + + // clear highlights and clearHighlight(); if (index != -1) clearSelected(index); int x = sf::Mouse::getPosition(window).x; int y = sf::Mouse::getPosition(window).y; - if ((x >= 30 && y >= 100 && y <= 200 && x <= 120)) { + + // clicked on toolbar + if (x >= 30 && y >= 100 && y <= 200 && x <= 120) { if (isPencil) { pencilIcon.setTexture(notDrawing); isPencil = false; @@ -276,165 +300,59 @@ int main() { pencilIcon.setTexture(drawing); isPencil = true; } + // skip checking further inputs + continue; } - index = getMousPos(window); - if(index!=-1 && !cells[index].canBeChanged){ + + // click on grid (probably) + index = getGridIndex(x, y); + if( + index != -1 && + !cells[index].canBeChanged && + cells[index].number != 0 + ){ highlight(cells[index].number); - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num1)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("1", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 1; - highlight(1); - cells[index].isPencil = false; - cells[index].pencilNums = ""; - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num2)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("2", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 2; - highlight(2); - cells[index].isPencil = false; - cells[index].pencilNums = ""; - - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num3)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("3", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 3; - cells[index].isPencil = false; - highlight(3); - cells[index].pencilNums = ""; - - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num4)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("4", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 4; - cells[index].isPencil = false; - highlight(4); - cells[index].pencilNums = ""; - - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num5)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("5", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 5; - clearSelected(index); - highlight(5); - cells[index].isPencil = false; - cells[index].pencilNums = ""; - - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num6)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("6", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 6; - cells[index].isPencil = false; - highlight(6); - cells[index].pencilNums = ""; - - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num7)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("7", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 7; - cells[index].isPencil = false; - highlight(7); - cells[index].pencilNums = ""; - clearSelected(index); - index = -1; - } + // skip checking further inputs + continue; } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num8)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("8", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 8; - cells[index].isPencil = false; - cells[index].pencilNums = ""; - highlight(8); - - clearSelected(index); - index = -1; + + ////// key events ///// + for (int i = 0; i < 9; i++) { + Kcode numkey = numcodes[i*2]; + Kcode numpadkey = numcodes[i*2+1]; + + if ( + sf::Keyboard::isKeyPressed(numkey) || + sf::Keyboard::isKeyPressed(numpadkey) + ) { + int num = i+1; + if (index != -1 && cells[index].canBeChanged) { + if (isPencil) { + pencilAppend(std::to_string(num), index); + clearSelected(index); + // index = -1; + } else { + cells[index].number = num; + highlight(num); + cells[index].isPencil = false; + cells[index].pencilNums = ""; + clearSelected(index); + // keeping previous index makes it handy to delete accidential entries + // index = -1; + } } + continue; } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num9)) { - if (index != -1 && cells[index].canBeChanged) { - if (isPencil) { - pencilAppend("9", index); - clearSelected(index); - index = -1; - } else { - cells[index].number = 9; - cells[index].isPencil = false; - highlight(9); - cells[index].pencilNums = ""; - clearSelected(index); - index = -1; - } - } - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scancode::Num0)) { + // erase keys + if ( + sf::Keyboard::isKeyPressed(Kcode::Num0) || + sf::Keyboard::isKeyPressed(Kcode::Backspace) || + sf::Keyboard::isKeyPressed(Kcode::Delete) + ) { if (index != -1) { cells[index].number = 0; cells[index].isPencil = false; @@ -446,10 +364,14 @@ int main() { } } window.clear(); - - startScreen(start, window); - drawTime(window, elapsed1, start); - drawRectangles(window, isWin, start); + + if (!start) { + startScreen(window); + } else { + drawToolbar(window); + drawTime(window, elapsed1); + drawRectangles(window, isWin); + } window.display(); } } From ec77bd36d93d7c6ff4fc894e95ce02d497f724ba Mon Sep 17 00:00:00 2001 From: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> Date: Sun, 26 Jan 2025 19:25:18 +0530 Subject: [PATCH 3/4] update readme Signed-off-by: Roopesh O R <62459820+Roopesh2@users.noreply.github.com> --- README.md | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f2f6f06..f83f04d 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,28 @@ ## The Sudoku Game - For now, its just a simple sudoku game with minimal graphics, however I do plan to make it multiplayer/collaborative. +For now, its just a simple sudoku game with minimal graphics, however I do plan to make it multiplayer/collaborative. -# Compile for Linux - ``` - sudo apt update - sudo apt install \ - libxrandr-dev \ - libxcursor-dev \ - libudev-dev \ - libfreetype-dev \ - libflac-dev \ - libvorbis-dev \ - libgl1-mesa-dev \ - libegl1-mesa-dev \ - libfreetype-dev - ``` - ``` - cmake -B build - cmake --build build - ``` +# Running on Linux +### Install dependencies +```bash +sudo apt update && +sudo apt install \ + libxrandr-dev \ + libxcursor-dev \ + libudev-dev \ + libfreetype-dev \ + libflac-dev \ + libvorbis-dev \ + libgl1-mesa-dev \ + libegl1-mesa-dev \ + libfreetype-dev +``` +### Building +```bash +cmake -B build +cmake --build build +``` +### Run the binary +from project directory: +``` +./build/bin/main +``` \ No newline at end of file From 7b80373bfb856f7931b0912ec0bbca67f5326b67 Mon Sep 17 00:00:00 2001 From: Roopesh O R Date: Sun, 26 Jan 2025 19:26:49 +0530 Subject: [PATCH 4/4] Update README.md --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f83f04d..e35c85f 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,15 @@ For now, its just a simple sudoku game with minimal graphics, however I do plan ```bash sudo apt update && sudo apt install \ - libxrandr-dev \ - libxcursor-dev \ - libudev-dev \ - libfreetype-dev \ - libflac-dev \ - libvorbis-dev \ - libgl1-mesa-dev \ - libegl1-mesa-dev \ - libfreetype-dev + libxrandr-dev \ + libxcursor-dev \ + libudev-dev \ + libfreetype-dev \ + libflac-dev \ + libvorbis-dev \ + libgl1-mesa-dev \ + libegl1-mesa-dev \ + libfreetype-dev ``` ### Building ```bash @@ -25,4 +25,4 @@ cmake --build build from project directory: ``` ./build/bin/main -``` \ No newline at end of file +```