DigSudoku.h與DigSudoku.cpp的程式碼
SolveSudoku.h與SolveSudoku.cpp的程式碼
開發環境: Windows XP 32bit
編輯器: Code Blocks
Compiler: GNU GCC compiler (MinGW)
遊戲界面所使用的Library: pdcurses
若是在Linux下compile的話,請安裝ncurses,並將 line 4 改成
#include "ncurses.h"
若是在Cygwin下compile的話,請安裝ncurses,並將 line 4改成
#include "ncurses/ncurses.h"
main.cpp
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
#include "SudokuGen.h" #include "DigSudoku.h" #include "SolveSudoku.h" #include "curses.h" // pdcurses library: 用來產生遊戲界面 (需另外安裝) #include <vector> using namespace std; void print_edge(); //印出棋盤框 void print_result(vector<int >,bool [][9]); //印出答案 int main() { int Ans=0; int i,j; //迴圈用變數 int ch; //紀錄按鍵 int startx = 0, starty = 0; //游標起始位置 int pos[9][9][2]; //棋盤的loacl座標所對映的global座標 bool check[9][9]; //紀錄每個棋盤格是屬於已知(1)或是未知(0) bool game_term = false; //遊戲執行狀態(true:結束) int count[9]; // 紀錄1-9所有數字的個數 for (i=0;i<9;i++) { count[i] = 0; } vector <int> Ans_board(81); //答案 vector <int> board(81); //題目 //====================初始化視窗============================ initscr(); start_color(); init_pair(1,COLOR_GREEN,COLOR_BLACK); init_pair(2,COLOR_YELLOW,COLOR_BLACK); cbreak(); noecho(); keypad(stdscr,TRUE); //========================產生題目=========================== printw("Initialzing..."); refresh(); SudokuGen Exam1; while(Ans!=1) { Exam1.generator(); //產生題目 DigSudoku DigExam1(Exam1.get_board(),35); // 挖洞出題 SolveSudoku SolExam1(DigExam1.get_board(),0); // 解題 Ans = SolExam1.getAns(); // 取得題目總共有多少個解 if (Ans==1 && Exam1.get_board()==SolExam1.get_board()) { //出題條件成立 board = DigExam1.get_board(); //題目 Ans_board = Exam1.get_board(); //解答 break; } } printw(" >> Complete!"); //====================顯示作者資訊============================ attron(A_BOLD); attron(COLOR_PAIR(2)); mvprintw(0,45,"Author: Herman Liang (2011-03)"); attroff(COLOR_PAIR(2)); attroff(A_BOLD); refresh(); //====================產生棋盤框架============================= print_edge(); //====================將題目寫入棋盤=========================== for (i=0;i<9;i++) for (j=0;j<9;j++) { if (board[i*9+j]!=0) { attron(A_BOLD); attron(COLOR_PAIR(1)); mvprintw(i+5+i/3,(j+j/3)*2+5,"%d",board[i*9+j]); attroff(COLOR_PAIR(1)); attroff(A_BOLD); check[i][j] = true; ++count[board[i*9+j]-1]; } else{check[i][j] = false;} //------------紀錄棋盤的loacl座標所對映的global座標-------------- pos[i][j][0] = i+5+i/3; pos[i][j][1] = (j+j/3)*2+5; } //====================產生剩餘數字個數計數器========================= for (i=5;i<14;i++) { mvprintw(i,35,"%d: ",i-4); for (j=0;j<9-count[i-5];j++) { mvprintw(i,j+38,"*"); } } refresh(); //====================移動指標至起始位置========================== move(pos[starty][startx][0],pos[starty][startx][1]); //====================開始監視鍵盤動作============================ //--------------------迴圈結束條件:q鍵--------------------------- while((ch = getch()) != 'q' && !game_term) { switch(ch) { case KEY_LEFT: mvaddstr(1,10,"Left "); mvprintw(22,0," "); --startx; break; case KEY_RIGHT: mvaddstr(1,10,"Right"); mvprintw(22,0," "); ++startx; break; case KEY_UP: mvaddstr(1,10,"Up "); mvprintw(22,0," "); --starty; break; case KEY_DOWN: mvaddstr(1,10,"Down "); mvprintw(22,0," "); ++starty; break; case 's': //檢查答案是否正確 if (board == Ans_board) { mvprintw(22,2,"Success!"); print_result(Ans_board,check); game_term = true; break; } else { mvprintw(22,2,"Fail!"); break; } case 'r': //顯示結果 print_result(Ans_board,check); default: mvprintw(1,10,"%c ",ch); mvprintw(22,2," "); //------------------該格子為空白且輸入數字時產生的動作----------------- if (check[starty][startx]==0) { if (ch>=48 && ch<=57) //數字0-9區間(48-57) { if (ch==48) //輸入數字為0 { if (board[starty*9+startx]!=0) { --count[board[starty*9+startx]-1]; } board[starty*9+startx] = 0; mvprintw(pos[starty][startx][0],pos[starty][startx][1]," "); } else //輸入數字為1-9 { int ch_int = ch-48; if (board[starty*9+startx]!=0) { --count[board[starty*9+startx]-1]; } ++count[ch_int-1]; board[starty*9+startx] = ch_int; mvprintw(pos[starty][startx][0],pos[starty][startx][1],"%c",ch); } for (i=5;i<14;i++) //重新整理剩餘數字個數計數器 { mvprintw(i,38," "); for (j=0;j<9-count[i-5];j++) { mvprintw(i,j+38,"*"); } if (count[i-5]>9) { mvprintw(i,38,"%d",9-count[i-5]); } } } } } //-----------------------游標超出邊界的處理---------------------------- while(startx < 0) { startx += 9; } while(startx >= 9) { startx -= 9; } while(starty < 0) { starty += 9; } while(starty >= 9) { starty -= 9; } // ----------------------顯示目前座標位置------------------------------------ mvprintw(1,16,"(%d,%d) %d",starty,startx,check[starty][startx]); // -------------------更新座標座置(若有按「上下左右」)----------------------- move(pos[starty][startx][0],pos[starty][startx][1]); refresh(); } // -------------------------結束視窗----------------------------------------- endwin(); return 0; } // 印出棋盤框 void print_edge() { int i,j; for (i=0;i<4;i++) for (j=0;j<9;j++) { mvprintw(i*4+4,(j+j/3)*2+4,"--"); } for (i=0;i<9;i++) for (j=0;j<4;j++) { mvprintw(i+5+i/3,j*8+3,"|"); } mvprintw(4,3,"/"); mvprintw(4,26,"\\"); mvprintw(16,3,"\\"); mvprintw(16,26,"/"); for (i=0;i<4;i++) for (j=0;j<9;j++) { mvprintw(i*4+4,(j+j/3)*2+54,"--"); } for (i=0;i<9;i++) for (j=0;j<4;j++) { mvprintw(i+5+i/3,j*8+53,"|"); } mvprintw(4,53,"/"); mvprintw(4,76,"\\"); mvprintw(16,53,"\\"); mvprintw(16,76,"/"); mvprintw(20,2,"s:Solve 0:Clear Bolck r:See Result q:Quit"); } // 印出答案 void print_result(vector<int > board,bool check[][9]) { int i,j; for (i=0;i<9;i++) for (j=0;j<9;j++) { if (check[i][j]==true) { attron(A_BOLD); attron(COLOR_PAIR(1)); mvprintw(i+5+i/3,(j+j/3)*2+55,"%d",board[i*9+j]); attroff(COLOR_PAIR(1)); attroff(A_BOLD); } else{mvprintw(i+5+i/3,(j+j/3)*2+55,"%d",board[i*9+j]);} } } |
沒有留言:
張貼留言