广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言如何实现数独程序
  • 131
分享到

C语言如何实现数独程序

2023-07-05 08:07:39 131人浏览 薄情痞子
摘要

这篇文章主要介绍“C语言如何实现数独程序”,在日常操作中,相信很多人在C语言如何实现数独程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现数独程序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧

这篇文章主要介绍“C语言如何实现数独程序”,在日常操作中,相信很多人在C语言如何实现数独程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现数独程序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

程序截图

C语言如何实现数独程序

C语言如何实现数独程序

C语言如何实现数独程序

简单说明

随机生成数独的算法见力扣上对应题目的题解,我用的是递归回溯法

力扣原题

先随机放入 11 个数就能生成一个数独然后求数独的解最后选择要显示的数字再显示出来。这里还用到了洗牌算法选择要随机显示的数字。

代码实现

TCW_GUI.h

 // 程序:数独// 编译环境:Visual Studio 2019,EasyX_20211109  #pragma once#include<graphics.h>#include<string>#include<list>#include<functional>#include<stdio.h>#pragma comment( lib, "MSIMG32.LIB")#define TCW_GUI_BUTTON_MYSELF 0#ifdef UNICODE#define STRING std::wstring#define SPRINTF swprintf_s#define SSCANF swscanf_s#else#define STRING std::string#define SPRINTF sprintf#define SSCANF sscanf#endif // UNICODE namespace TCW_GUI{enum class State{general = 0,touch = 1,press = 2,release = 3,forbidden = 4};enum class DrawDecision{text = 0,graph = 1}; class Vec2{public:double x, y;Vec2() :x(0), y(0) {}Vec2(double xx, double yy) :x(xx), y(yy) {};Vec2 operator+(Vec2 num){return Vec2(x + num.x, y + num.y);}Vec2 operator-(Vec2 num){return Vec2(x - num.x, y - num.y);}Vec2 operator/(double num){return Vec2(x / num, y / num);}Vec2 operator*(double num){return Vec2(x * num, y * num);}}; class Rect{public:Rect() :size(), position() {}Rect(Vec2 position, Vec2 size) :size(size), position(position) {}Vec2 size;Vec2 position;bool isInRect(Vec2 point){Vec2 left_top = position - size / 2.0;Vec2 right_buttom = position + size / 2.0;if (point.x >= left_top.x && point.y >= left_top.y &&point.x <= right_buttom.x && point.y <= right_buttom.y)return true;return false;}}; class Button{private:double textsize = 20;double textareasize = 0.9;Vec2 defaultsize;Vec2 defaulttext;State nowstate = State::general;DrawDecision drawdecision = DrawDecision::text;void DrawButton_General();void DrawButton_Touch();void DrawButton_Press();void DrawButton_Forbidden();bool isPress = false;public:Button() :boundingbox(), buttontext(){settextstyle(textsize, 0, TEXT("微软雅黑"));Vec2 defaultsize = Vec2(textwidth(TEXT("...")) / textareasize, textheight(TEXT("...")) / textareasize);Vec2 defaulttext = Vec2(textwidth(TEXT("...")), textheight(TEXT("...")));}Button(Rect boundingbox, STRING buttontext, std::function<int(void*)> releaseFunc, void* releaseParam) :boundingbox(boundingbox), buttontext(buttontext), releaseFunc(releaseFunc), releaseParam(releaseParam){drawdecision = DrawDecision::text;settextstyle(textsize, 0, TEXT("微软雅黑"));Vec2 defaultsize = Vec2(textwidth(TEXT("...")) / textareasize, textheight(TEXT("...")) / textareasize);Vec2 defaulttext = Vec2(textwidth(TEXT("...")), textheight(TEXT("...")));}Button(Rect boundingbox, STRING graphsrc_nORMal, STRING graphsrc_touch, STRING graphsrc_press,std::function<int(void*)> releaseFunc, void* releaseParam) :boundingbox(boundingbox), graphsrc_normal(graphsrc_normal), graphsrc_touch(graphsrc_touch), graphsrc_press(graphsrc_press),releaseFunc(releaseFunc), releaseParam(releaseParam){drawdecision = DrawDecision::graph;}STRING buttontext;STRING graphsrc_normal;STRING graphsrc_touch;STRING graphsrc_press; Rect boundingbox;std::function<int(void*)> releaseFunc = nullptr;void* releaseParam = nullptr;void DrawButton();void DrawButton_Text(State state);void DrawButton_Graph(State state);void receiver(ExMessage* msg);void ForbidButton() { this->nowstate = State::forbidden; }// 禁用按钮void RefreshButton() { this->nowstate = State::general; }// 恢复按钮void SetTextSize(double size){textsize = size;defaultsize = Vec2(textsize * 1.5 / textareasize, textsize / textareasize);defaulttext = Vec2(textsize * 1.5, textsize);}void SetTextAreaSize(double size){textareasize = size;defaultsize = Vec2(textsize * 1.5 / textareasize, textsize / textareasize);defaulttext = Vec2(textsize * 1.5, textsize);}}; class ButtonManager{std::list<Button> buttonlist;public:Button* AddButton(Button button);void ReceiveMessage(ExMessage* msg);void DrawButton();}; void Rectangle_TCW(Vec2 left_top, Vec2 right_buttom){rectangle(left_top.x, left_top.y, right_buttom.x, right_buttom.y);} void Fillrectangle_TCW(Vec2 left_top, Vec2 right_buttom){fillrectangle(left_top.x, left_top.y, right_buttom.x, right_buttom.y);} void Outtextxy_TCW(Vec2 position, const TCHAR* str){outtextxy(position.x, position.y, str);} void Button::DrawButton_Text(State state){LOGFONT log;COLORREF textcol;COLORREF linecol;COLORREF fillcol;int bkmode;gettextstyle(&log);bkmode = getbkmode();textcol = gettextcolor();linecol = getlinecolor();fillcol = getfillcolor(); // 默认数值settextstyle(textsize, 0, TEXT("微软雅黑"));settextcolor(BLACK);setbkmode(TRANSPARENT);setlinecolor(BLACK);setfillcolor(WHITE);switch (state){case TCW_GUI::State::general:break;case TCW_GUI::State::touch:setfillcolor(RGB(240, 240, 240));break;case TCW_GUI::State::press:setfillcolor(RGB(240, 240, 240));break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:settextcolor(RGB(128, 128, 128));break;default:break;}Vec2 size_button = Vec2(this->boundingbox.size * textareasize);Vec2 size_text = Vec2(textwidth(this->buttontext.c_str()), textsize); if (boundingbox.size.x > defaultsize.x && boundingbox.size.y > defaultsize.y)// 比最小值大{Rectangle_TCW(this->boundingbox.position - this->boundingbox.size / 2.0,this->boundingbox.position + this->boundingbox.size / 2.0);Fillrectangle_TCW(this->boundingbox.position - this->boundingbox.size / 2.0,this->boundingbox.position + this->boundingbox.size / 2.0);if (size_button.x >= size_text.x && size_button.y >= size_text.y)// 字数没超{switch (state){case TCW_GUI::State::general:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;case TCW_GUI::State::touch:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;case TCW_GUI::State::press:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0 + Vec2(2, 2), buttontext.c_str());break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;default:break;}}else// 字数超了{int Wordnum = size_button.x / textwidth(buttontext.c_str()) * buttontext.size() - 2;STRING realstr = buttontext.substr(0, wordnum);realstr += TEXT("...");Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, realstr.c_str());switch (state){case TCW_GUI::State::general:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, realstr.c_str());break;case TCW_GUI::State::touch:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, realstr.c_str());break;case TCW_GUI::State::press:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0 + Vec2(2, 2), realstr.c_str());break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, realstr.c_str());break;default:break;}}}else// 比最小值小{Rectangle_TCW(this->boundingbox.position - this->defaultsize / 2.0,this->boundingbox.position + this->defaultsize / 2.0);Fillrectangle_TCW(this->boundingbox.position - this->defaultsize / 2.0,this->boundingbox.position + this->defaultsize / 2.0);if (defaulttext.x >= size_text.x && defaulttext.y >= size_text.y)// 字宽比三个点小{switch (state){case TCW_GUI::State::general:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;case TCW_GUI::State::touch:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;case TCW_GUI::State::press:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0 + Vec2(2, 2), buttontext.c_str());break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0, buttontext.c_str());break;default:break;}}else// 字宽比三个点大{switch (state){case TCW_GUI::State::general:Outtextxy_TCW(this->boundingbox.position - defaulttext / 2.0, TEXT("..."));break;case TCW_GUI::State::touch:Outtextxy_TCW(this->boundingbox.position - defaulttext / 2.0, TEXT("..."));break;case TCW_GUI::State::press:Outtextxy_TCW(this->boundingbox.position - size_text / 2.0 + Vec2(2, 2), TEXT("..."));break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:Outtextxy_TCW(this->boundingbox.position - defaulttext / 2.0, TEXT("..."));break;default:break;}}} settextstyle(&log);settextcolor(textcol);setbkmode(bkmode);setlinecolor(linecol);setfillcolor(fillcol);} void Button::DrawButton_Graph(State state){IMAGE img;switch (state){case TCW_GUI::State::general:loadimage(&img, (this->graphsrc_normal).c_str());break;case TCW_GUI::State::touch:loadimage(&img, (this->graphsrc_touch).c_str());break;case TCW_GUI::State::press:loadimage(&img, (this->graphsrc_press).c_str());break;case TCW_GUI::State::release:break;case TCW_GUI::State::forbidden:loadimage(&img, (this->graphsrc_normal).c_str());break;default:break;}IMAGE* srcimg = &img;HDC dstDC = GetImageHDC(NULL);HDC srcDC = GetImageHDC(srcimg);int w = srcimg->getwidth();int h = srcimg->getheight(); // 结构体的第三个成员表示额外的透明度,0 表示全透明,255 表示不透明。BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };// 使用 windows GDI 函数实现半透明位图Vec2 left_top = this->boundingbox.position - this->boundingbox.size / 2.0;AlphaBlend(dstDC, left_top.x, left_top.y, boundingbox.size.x, boundingbox.size.y, srcDC, 0, 0, w, h, bf);} void Button::DrawButton_General(){switch (this->drawdecision){case TCW_GUI::DrawDecision::text:DrawButton_Text(State::general);break;case TCW_GUI::DrawDecision::graph:DrawButton_Graph(State::general);break;default:break;}} void Button::DrawButton_Touch(){switch (this->drawdecision){case TCW_GUI::DrawDecision::text:DrawButton_Text(State::touch);break;case TCW_GUI::DrawDecision::graph:DrawButton_Graph(State::touch);break;default:break;}} void Button::DrawButton_Press(){switch (this->drawdecision){case TCW_GUI::DrawDecision::text:DrawButton_Text(State::press);break;case TCW_GUI::DrawDecision::graph:DrawButton_Graph(State::press);break;default:break;}} void Button::DrawButton_Forbidden(){switch (drawdecision){case TCW_GUI::DrawDecision::text:DrawButton_Text(State::forbidden);break;case TCW_GUI::DrawDecision::graph:DrawButton_Graph(State::forbidden);break;default:break;}} void Button::DrawButton(){switch (this->nowstate){case State::general:DrawButton_General();break;case State::touch:DrawButton_Touch();break;case State::press:DrawButton_Press();break;case State::release:DrawButton_Touch();if (releaseFunc != nullptr){if (releaseParam == TCW_GUI_BUTTON_MYSELF)releaseFunc(this);else releaseFunc(releaseParam);}this->nowstate = State::touch;break;case State::forbidden:DrawButton_Forbidden();break;default:break;}} void Button::receiver(ExMessage* msg){if (this->nowstate == State::forbidden)return;// 先 general 后 touch 再 press 一个 release 后重新 generalif (!isPress && !this->boundingbox.isInRect(Vec2(msg->x, msg->y))){this->nowstate = State::general;}else if (!isPress && this->boundingbox.isInRect(Vec2(msg->x, msg->y))){if (!msg->lbutton)this->nowstate = State::touch;else if (this->nowstate == State::touch){isPress = true;this->nowstate = State::press;}}else if (isPress && this->boundingbox.isInRect(Vec2(msg->x, msg->y))){if (!msg->lbutton){isPress = false;this->nowstate = State::release;}else this->nowstate = State::press;}else if (isPress && !this->boundingbox.isInRect(Vec2(msg->x, msg->y))){if (!msg->lbutton){isPress = false;this->nowstate = State::general;}else this->nowstate = State::press;}} Button* ButtonManager::AddButton(Button button){this->buttonlist.push_back(button);return &buttonlist.back();} void ButtonManager::ReceiveMessage(ExMessage* msg){for (Button& button : this->buttonlist){button.receiver(msg);}} void ButtonManager::DrawButton(){for (Button& button : this->buttonlist){button.DrawButton();}}}

main.cpp

// 程序:C语言数独// 编译环境:Visual Studio 2019,EasyX_20211109 #include"TCW_GUI.h"#include<time.h>#define WIDTH 640#define HEIGHT 480 class Vec2{public:double xx, yy;Vec2(double xx = 0, double yy = 0) :xx(xx), yy(yy) {}Vec2 operator+(Vec2 ano){return Vec2(xx + ano.xx, yy + ano.yy);}Vec2 operator-(Vec2 ano){return Vec2(xx - ano.xx, yy - ano.yy);}Vec2 operator/(double num){return Vec2(xx / num, yy / num);}}; class BoundingBox{public:Vec2 size, position;BoundingBox(Vec2 size, Vec2 position) :size(size), position(position) {}BoundingBox() = default;Vec2 GetLeftTop(){return position - size / 2.0;}bool isInBoundingBox(Vec2 point){return (point.xx > position.xx - size.xx / 2.0 && point.xx < position.xx + size.xx / 2.0 &&point.yy>position.yy - size.yy / 2.0 && point.yy < position.yy + size.yy / 2.0);}};  template<typename T>void WashCard(T* map, int len){for (int i = len; i > 0; i--){int exchange = rand() % i;T temp = map[i - 1];map[i - 1] = map[exchange];map[exchange] = temp;}} bool isvalid(char(* const map)[9][9], char c, int row_i, int col_i){// 尚未放入,判断能否放入for (int i = 0; i < 9; ++i){if (abs((*map)[row_i][i]) == c) return false;else if (abs((*map)[i][col_i]) == c) return false;else if (abs((*map)[3 * (row_i / 3) + i / 3][3 * (col_i / 3) + i % 3]) == c) return false;}return true;} // 递归回溯算法 yydsbool solver(char(* const map)[9][9], int rstart){for (int row = rstart; row < 9; ++row){for (int col = 0; col < 9; ++col){if ((*map)[row][col] == 0){for (char c = 1; c <= 9; ++c){if (isvalid(map, c, row, col)){(*map)[row][col] = c;if (solver(map, row)) return true;else (*map)[row][col] = 0;}}return false;}}}return true;} void GenerateSudoku(char(* const map)[9][9]){memset(*map, 0, sizeof(*map));char arr[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };WashCard(arr, 9);for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){(*map)[i][j] = arr[i * 3 + j];}}WashCard(arr, 9);for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){(*map)[3 + i][3 + j] = arr[i * 3 + j];}}WashCard(arr, 9);for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){(*map)[6 + i][6 + j] = arr[i * 3 + j];}}solver(map, 0);} void GetShowMap(const char(*map)[9][9], char(* const ShowMap)[9][9], bool(* const Auxiliary)[9][9], int showNum){memset(*Auxiliary, 0, sizeof(bool) * 81);memset(*ShowMap, 0, sizeof(char) * 81);char arr[81];for (int i = 0; i < 81; i++)arr[i] = i;WashCard(arr, 81);for (int i = 0; i < showNum; i++){(*ShowMap)[arr[i] / 9][arr[i] % 9] = (*map)[arr[i] / 9][arr[i] % 9];(*Auxiliary)[arr[i] / 9][arr[i] % 9] = true;}} void Rectangle_MR(Vec2 left_top, Vec2 right_bottom){rectangle((int)(left_top.xx + 0.5), (int)(left_top.yy + 0.5), (int)(right_bottom.xx + 0.5), (int)(right_bottom.yy + 0.5));} void Fillrectangle_MF(Vec2 left_top, Vec2 right_bottom){fillrectangle((int)(left_top.xx + 0.5), (int)(left_top.yy + 0.5), (int)(right_bottom.xx + 0.5), (int)(right_bottom.yy + 0.5));} void Outtextxy_MO(Vec2 pericenter, LPCTSTR str){int width = textwidth(str);int height = textheight(str);outtextxy((int)(pericenter.xx - width / 2.0 + 0.5), (int)(pericenter.yy - height / 2.0 + 0.5), str);} void DrawMap(const char(*showMap)[9][9], const bool(*Auxiliary)[9][9], bool isSelected, int indexX, int indexY, BoundingBox boundingbox){Vec2 grid = Vec2(boundingbox.size.xx / 9.0, boundingbox.size.yy / 9.0);Vec2 beginPoint = boundingbox.GetLeftTop();settextstyle(grid.yy * 0.8, 0, TEXT("consolas"));settextcolor(BLACK);setlinestyle(PS_SOLID, 2);setlinecolor(BLACK);setfillcolor(WHITE);setbkmode(TRANSPARENT);for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if (isSelected && i == indexY && j == indexX)setfillcolor(RED);else if ((*Auxiliary)[i][j])setfillcolor(WHITE);else setfillcolor(RGB(127, 127, 127));Fillrectangle_MF(beginPoint + Vec2(j * grid.xx, i * grid.yy), beginPoint + Vec2((j + 1) * grid.xx, (i + 1) * grid.yy));if ((*showMap)[i][j] != 0){TCHAR arr[16];if ((*showMap)[i][j] < 0)settextcolor(LIGHTRED);else settextcolor(BLACK);SPRINTF(arr, TEXT("%d"), abs((*showMap)[i][j]));Outtextxy_MO(beginPoint + Vec2((j + 0.5) * grid.xx, (i + 0.5) * grid.yy), arr);}}}setlinestyle(PS_SOLID, 3);for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){Rectangle_MR(beginPoint + Vec2(j * 3 * grid.xx, i * 3 * grid.yy), beginPoint + Vec2((j + 1) * 3 * grid.xx, (i + 1) * 3 * grid.yy));}}} bool DetectedSudoku(const char(*map)[9][9]){bool nightHash[9][9] = { false };bool rowHash[9][9] = { false };bool colHash[9][9] = { false };for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if ((*map)[i][j] == 0)continue;else if ((*map)[i][j] < 0)return false;if (nightHash[i / 3 * 3 + j / 3][(*map)[i][j] - 1] == true)return false;else nightHash[i / 3 * 3 + j / 3][(*map)[i][j] - 1] = true;if (rowHash[i][(*map)[i][j] - 1] == true)return false;else rowHash[i][(*map)[i][j] - 1] = true;if (colHash[j][(*map)[i][j] - 1] == true)return false;else colHash[j][(*map)[i][j] - 1] = true;}}return true;} void StartScene();void RandomScene();void CustomMode();  int main(){initgraph(WIDTH, HEIGHT);BeginBatchDraw();srand((unsigned int)time(NULL));StartScene();closegraph();return 0;} void StartScene(){cleardevice();ExMessage msg;bool isExit = false; TCW_GUI::ButtonManager manager;double buttonheight = (HEIGHT * 3 / 4.0 - 40) / 3.0;double buttonwidth = WIDTH / 3;manager.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(WIDTH / 2, HEIGHT / 4 + 10 + buttonheight / 2),TCW_GUI::Vec2(buttonwidth, 50)), TEXT("自定义模式"), [](void*) {CustomMode(); return 0; }, nullptr));manager.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(WIDTH / 2, HEIGHT / 4 + 20 + buttonheight * 1.5),TCW_GUI::Vec2(buttonwidth, 50)), TEXT("随机模式"), [](void*) { RandomScene(); return 0; }, nullptr));manager.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(WIDTH / 2, HEIGHT / 4 + 40 + buttonheight * 2.5),TCW_GUI::Vec2(buttonwidth, 50)), TEXT("退出游戏"), [](void* param) {*(bool*)param = true; return 0; }, &isExit));Vec2 pericenter(WIDTH / 2, HEIGHT / 8);while (!isExit){if (peekmessage(&msg, EM_MOUSE)){manager.ReceiveMessage(&msg);manager.DrawButton();}settextstyle((int)(HEIGHT / 4.0 * 0.8 + 0.5), 0, TEXT("楷体"));settextcolor(WHITE);Outtextxy_MO(pericenter, TEXT("数独"));FlushBatchDraw();}cleardevice();} void RandomScene(){cleardevice();char map[9][9];memset(map, 0, sizeof(map));GenerateSudoku(&map);char showMap[9][9] = { 0 };bool Auxiliary[9][9] = { false };int CountOfNum = 27;int CanFillNum = 81 - CountOfNum;GetShowMap(&map, &showMap, &Auxiliary, CountOfNum); ExMessage msg;bool isExit = false;bool isSelected = false;bool isWin = false;int indexX, indexY; TCW_GUI::ButtonManager command;command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(60, 50),TCW_GUI::Vec2(100, 40)), TEXT("返回"), [](void* param) {*(bool*)param = true; return 0; }, &isExit));command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(60, 100),TCW_GUI::Vec2(100, 40)), TEXT("设置数字数量"), [](void* param){TCHAR temp[3];SPRINTF(temp, TEXT("%d"), *(int*)param);InputBox(temp, 3, TEXT("请输入要设置的数字数量:"), TEXT("输入框"), temp);int tempNum;SSCANF(temp, TEXT("%d"), &tempNum);if (tempNum >= 17 && tempNum < 81)*(int*)param = tempNum;return 0;}, &CountOfNum));command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(60, 150),TCW_GUI::Vec2(100, 40)), TEXT("重置"), [&](void* param){GenerateSudoku(&map);GetShowMap(&map, &showMap, &Auxiliary, CountOfNum);CanFillNum = 81 - CountOfNum;isWin = false;return 0;}, nullptr));TCW_GUI::ButtonManager manager;BoundingBox Rect(Vec2(300, 300), Vec2(WIDTH / 2.0, HEIGHT / 2.0));char Answer[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };for (int i = 0; i < 9; i++){TCHAR arr[16];SPRINTF(arr, TEXT("%d"), i + 1);manager.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(WIDTH - HEIGHT / 10, HEIGHT / 10 * (i + 1)),TCW_GUI::Vec2(HEIGHT / 11, HEIGHT / 11)), arr, [&](void* param){if (isSelected){// 检查是否正确if (showMap[indexY][indexX] > 0)CanFillNum++;showMap[indexY][indexX] = 0;char tempNum = *(char*)param;if (!isvalid(&showMap, tempNum, indexY, indexX))tempNum = -tempNum;else CanFillNum--;showMap[indexY][indexX] = tempNum;isSelected = false;if (CanFillNum == 0){isWin = true;}else isWin = false;}return 0;}, Answer + i));} while (!isExit){if (peekmessage(&msg, EM_MOUSE)){command.ReceiveMessage(&msg);manager.ReceiveMessage(&msg);if (msg.lbutton && Rect.isInBoundingBox(Vec2(msg.x, msg.y))){Vec2 direct = Vec2(msg.x, msg.y) - Rect.GetLeftTop();Vec2 Grid = Rect.size / 9.0;indexX = direct.xx / Grid.xx;indexY = direct.yy / Grid.yy;if (Auxiliary[indexY][indexX] == false)isSelected = true;else isSelected = false;}else if (msg.rbutton && Rect.isInBoundingBox(Vec2(msg.x, msg.y))){Vec2 direct = Vec2(msg.x, msg.y) - Rect.GetLeftTop();Vec2 Grid = Rect.size / 9.0;indexX = direct.xx / Grid.xx;indexY = direct.yy / Grid.yy;if (Auxiliary[indexY][indexX] == false){if (showMap[indexY][indexX] > 0){CanFillNum++;isWin = false;}showMap[indexY][indexX] = 0;}}}cleardevice();manager.DrawButton();command.DrawButton();DrawMap(&showMap, &Auxiliary, isSelected, indexX, indexY, Rect);if (isWin){settextcolor(YELLOW);Outtextxy_MO(Vec2(WIDTH / 2, 50), TEXT("恭喜你,胜利了"));}FlushBatchDraw();}cleardevice();} void CustomMode(){cleardevice();char map[9][9] = { 0 };char showMap[9][9] = { 0 };bool Auxiliary[9][9] = { false };int CanFillNum = 81; ExMessage msg;bool isExit = false;bool isSelected = false;bool isWin = false;int indexX, indexY;bool isGamePlay = false; TCW_GUI::ButtonManager command;command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(90, 50),TCW_GUI::Vec2(150, 40)), TEXT("返回"), [](void* param) {*(bool*)param = true; return 0; }, &isExit));command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(90, 100),TCW_GUI::Vec2(150, 40)), TEXT("开始解数独"), [&](void* param){// 判断是否安全忘了!!!if (!DetectedSudoku(&map)){settextcolor(LIGHTGRAY);Outtextxy_MO(Vec2(WIDTH / 2, HEIGHT / 2), TEXT("这个数独不成立"));FlushBatchDraw();Sleep(500);return 0;}else{memcpy(showMap, map, sizeof(map));if (!solver(&showMap, 0)){settextcolor(LIGHTGRAY);Outtextxy_MO(Vec2(WIDTH / 2, HEIGHT / 2), TEXT("这个数独无解"));FlushBatchDraw();Sleep(500);return 0;}}isGamePlay = true;for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){if (map[i][j] != 0)Auxiliary[i][j] = true;showMap[i][j] = map[i][j];}}return 0;}, nullptr));command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(90, 150),TCW_GUI::Vec2(150, 40)), TEXT("重置"), [&](void* param){memset(Auxiliary, 0, sizeof(Auxiliary));memset(showMap, 0, sizeof(showMap));memset(map, 0, sizeof(map));CanFillNum = 81;isWin = false;isGamePlay = false;return 0;}, nullptr));command.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(90, 200),TCW_GUI::Vec2(150, 40)), TEXT("展示其中一种答案"), [&](void* param){memcpy(showMap, map, sizeof(map));if (!solver(&showMap, 0)){settextcolor(LIGHTGRAY);Outtextxy_MO(Vec2(WIDTH / 2, HEIGHT / 2), TEXT("这个数独无解"));FlushBatchDraw();Sleep(500);}else{memset(Auxiliary, true, sizeof(Auxiliary));CanFillNum = 0;isWin = false;isGamePlay = true;}return 0;}, nullptr));TCW_GUI::ButtonManager manager;BoundingBox Rect(Vec2(300, 300), Vec2(WIDTH / 2.0, HEIGHT / 2.0));char Answer[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };for (int i = 0; i < 9; i++){TCHAR arr[16];SPRINTF(arr, TEXT("%d"), i + 1);manager.AddButton(TCW_GUI::Button(TCW_GUI::Rect(TCW_GUI::Vec2(WIDTH - HEIGHT / 10, HEIGHT / 10 * (i + 1)),TCW_GUI::Vec2(HEIGHT / 11, HEIGHT / 11)), arr, [&](void* param){if (isSelected){if (!isGamePlay){if (map[indexY][indexX] > 0)CanFillNum++;map[indexY][indexX] = 0;char tempNum = *(char*)param;if (!isvalid(&map, tempNum, indexY, indexX))tempNum = -tempNum;else CanFillNum--;map[indexY][indexX] = tempNum;isSelected = false;}else{// 检查是否正确if (showMap[indexY][indexX] > 0)CanFillNum++;showMap[indexY][indexX] = 0;char tempNum = *(char*)param;if (!isvalid(&showMap, tempNum, indexY, indexX))tempNum = -tempNum;else CanFillNum--;showMap[indexY][indexX] = tempNum;isSelected = false;if (CanFillNum == 0){isWin = true;}else isWin = false;}}return 0;}, Answer + i));} while (!isExit){if (peekmessage(&msg, EM_MOUSE)){command.ReceiveMessage(&msg);manager.ReceiveMessage(&msg);if (msg.lbutton && Rect.isInBoundingBox(Vec2(msg.x, msg.y))){Vec2 direct = Vec2(msg.x, msg.y) - Rect.GetLeftTop();Vec2 Grid = Rect.size / 9.0;indexX = direct.xx / Grid.xx;indexY = direct.yy / Grid.yy;if (Auxiliary[indexY][indexX] == false)isSelected = true;else isSelected = false;}else if (msg.rbutton && Rect.isInBoundingBox(Vec2(msg.x, msg.y))){Vec2 direct = Vec2(msg.x, msg.y) - Rect.GetLeftTop();Vec2 Grid = Rect.size / 9.0;indexX = direct.xx / Grid.xx;indexY = direct.yy / Grid.yy;if (Auxiliary[indexY][indexX] == false){if (isGamePlay){if (showMap[indexY][indexX] > 0){CanFillNum++;isWin = false;}showMap[indexY][indexX] = 0;}else{if (map[indexY][indexX] > 0)CanFillNum++;map[indexY][indexX] = 0;}}}}cleardevice();manager.DrawButton();command.DrawButton();if (!isGamePlay)DrawMap(&map, &Auxiliary, isSelected, indexX, indexY, Rect);else DrawMap(&showMap, &Auxiliary, isSelected, indexX, indexY, Rect);if (isWin){settextcolor(YELLOW);Outtextxy_MO(Vec2(WIDTH / 2, 50), TEXT("恭喜你,胜利了"));}FlushBatchDraw();}cleardevice();}

到此,关于“C语言如何实现数独程序”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: C语言如何实现数独程序

本文链接: https://www.lsjlt.com/news/350581.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C语言如何实现数独程序
    这篇文章主要介绍“C语言如何实现数独程序”,在日常操作中,相信很多人在C语言如何实现数独程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现数独程序”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-07-05
  • C语言只能实现解数独程序
    这篇文章主要介绍“C语言只能实现解数独程序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C语言只能实现解数独程序”文章能帮助大家解决问题。用C语言写的解数独的程序。在linux下测试成功运行。效果如...
    99+
    2023-06-08
  • C语言实现数独程序的示例代码
    目录程序截图简单说明代码实现程序截图 简单说明 随机生成数独的算法见力扣上对应题目的题解,我用的是递归回溯法 力扣原题 先随机放入 11 个数就能生成一个数独然后求数独的解最后...
    99+
    2023-03-03
    C语言实现数独游戏 C语言数独程序 C语言数独
  • C语言实现数独游戏
    本文实例为大家分享了C语言实现数独游戏的具体代码,供大家参考,具体内容如下 目标 写一个数独游戏,有以下功能: 1:能随机产生题目并给出答案。 2:求解输入的题目并输出答案。 实现说...
    99+
    2022-11-13
  • C语言实现数独小游戏
    本文实例为大家分享了C语言实现数独小游戏的具体代码,供大家参考,具体内容如下 输入包含9x9的已知数字,空位用0补齐,中间用空格隔开。(输入数独题目确保正确)输出为输入数独题目的解。...
    99+
    2022-11-13
  • C语言怎么实现数独游戏
    本文小编为大家详细介绍“C语言怎么实现数独游戏”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言怎么实现数独游戏”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。目标写一个数独游戏,有以下功能:能随机产生题目并给...
    99+
    2023-06-29
  • C语言实现数独辅助器(附源码)
    目录数独游戏介绍数独辅助器编写思路效果图源码数独游戏介绍 数独是源自瑞士的一种数学游戏。是一种运用纸、笔进行演算的逻辑游戏。玩家需要根据 9×9 盘面上的已知数字,推理出...
    99+
    2023-01-11
    C语言实现数独辅助器 C语言数独辅助器 C语言数独
  • 用c语言编程实现素数判断(判断素数的c语言程序函数)
    以下是一个用C语言编写的判断素数的函数:```c#include #include bool isPrime(int n) {if ...
    99+
    2023-09-22
    c语言
  • C语言如何实现烟花表白程序
    这篇文章将为大家详细讲解有关C语言如何实现烟花表白程序,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。效果图烟花爆炸效果思路不能直接把烟花图片贴到窗口中,需要把烟花的像素点保存到二维数组中,以相同的半径大小...
    99+
    2023-06-29
  • C语言如何实现一个扫雷程序
    本篇内容介绍了“C语言如何实现一个扫雷程序”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!整个游戏设计中主要的函数为:1、初始化地雷棋盘和显示...
    99+
    2023-06-17
  • C语言顺序表如何实现
    这篇文章主要讲解了“C语言顺序表如何实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言顺序表如何实现”吧!概念及结构顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般...
    99+
    2023-06-29
  • C语言如何实现数组元素排序
    这篇文章主要讲解了“C语言如何实现数组元素排序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C语言如何实现数组元素排序”吧!前言在实际开发中,有很多场景需要我们将数组元素按照从大到小(或者从...
    99+
    2023-07-05
  • C语言实现扫雷小程序
    前言 《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。 多文件形式 在实现游戏...
    99+
    2022-11-12
  • C语言实现通讯录程序
    本文实例为大家分享了C语言实现通讯录程序的具体代码,供大家参考,具体内容如下 设计要求: 可以存放1000个人的信息,每个人的信息包括姓名、年龄、性别、电话、住址 通讯录功能包括: ...
    99+
    2022-11-12
  • C语言如何实现通讯录系统程序
    本文小编为大家详细介绍“C语言如何实现通讯录系统程序”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言如何实现通讯录系统程序”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。前言利用链表增、删、改、查功能以及文件...
    99+
    2023-07-02
  • C语言中如何实现桶排序
    目录C语言实现桶排序1.原理2.桶排序不是基于比较的排序3.桶的实现形式4.桶中元素的排序4.最后就是将桶中的元素依次输出5完整代码如下7.桶排序的时间复杂度和空间复杂度【排序】图解...
    99+
    2022-11-16
    C语言桶排序 C桶排序 C语言排序
  • C语言如何实现快速排序
    今天小编给大家分享一下C语言如何实现快速排序的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。交换排序的思想基本思想:所谓交换,...
    99+
    2023-07-02
  • C语言如何实现后序遍历
    这篇文章主要介绍“C语言如何实现后序遍历”,在日常操作中,相信很多人在C语言如何实现后序遍历问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言如何实现后序遍历”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-06-16
  • c语言如何实现排序算法
    小编给大家分享一下c语言如何实现排序算法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!1.选择排序-简单选择排序选择排序是最简单的一种基于O(n2)时间复杂度的排...
    99+
    2023-06-15
  • 用C语言实现扫雷小程序
    本文实例为大家分享了C语言实现扫雷小程序的具体代码,供大家参考,具体内容如下 扫雷程序的编写需要有清晰的思路,所以我们先要清楚扫雷的实现有几个功能模块让我们编写,再用主函数将功能结合...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作