2011年4月27日 星期三

[程試分享]雲.蝦米 (雲端無蝦米輸入法)

雲.蝦米
雲端無蝦米輸入法 for Chrome Extensions

目前版本:0.0.3.r1
狀態:尚在開發中,但已經可以使用了^^
因為想要嘗試寫寫看chrome的擴充功能程式,加上本身也是無蝦米輸入法的使用者,所以就以此為題材。雲端的字碼對照表格是建立在Google App Engine的伺服器中,字碼對照表沒有使用無蝦米官方的表格,而是使用我自己修改後的表格並以資料庫的形式來分析、讀取,所以目前僅有基本的中文輸入,並不支援外字。
暫時只能在Chrome瀏覽器的環境下執行
安裝擴充功能

使用說明
Google Code頁面



執行畫面
執行畫面

測試報告:
Google: ok!
GMail: ok!
Facebook: ok!
imo: ok!
Blogger: ok!
Notepad.cc: ok!
Google文件(試算表): ok! 但很lag...
Google文件(文件): 無法使用! 囧!

2011年4月21日 星期四

[程式分享] 數獨 Part. 3:遊戲操作界面 (終端機)

SudokuGen.h與SudokuGen.cpp的程式碼
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]);}

        }
}

2011年4月2日 星期六

[程式分享] 數獨 Part. 2:解題程式 (C++ Class)

SudokuGen.h與SudokuGen.cpp程式內容請參考:[程式分享] 數獨 Part. 1:自動題目產生器 (C++ Class)


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

#include <iostream>
#include <vector>
#include "SudokuGen.h"
#include "DigSudoku.h"
#include "SolveSudoku.h"

using namespace std;

void print_board(vector <int >);

int main()
{
    int Ans = 0; // 數獨解的個數
    vector <int > digboard;
    SudokuGen NewBoard; // 產生棋盤物件
    vector <int > board = NewBoard.get_board(); //取得棋盤
    print_board(board); // 將棋盤印出

    while(Ans!=1) // 檢查題目是否有唯一解,且解答與題目一致
    {
        DigSudoku Board_Dig(board,35); // 挖洞出題
        digboard = Board_Dig.get_board(); // 取得挖洞後的棋盤
        SolveSudoku Board_Solve(digboard,0); // 解題
        Ans = Board_Solve.getAns(); // 取得題目總共有多少個解
        if (Ans==1 && board!=Board_Solve.get_board())
        {
            Ans = 0;
        }
    }
    print_board(digboard); // 印出題目
    return 0;
}

void print_board(vector <int > board) //將棋盤印出
{
    for (int i=0;i<board.size();i++)
    {
        if(board[i]==0){cout << "  ";}
        else{cout << board[i] << ' ';}
        if ((i+1) % 9 == 0)
        {
            cout << endl;
        }
    }
    cout << endl;
}


DigSudoku.h (數獨題目挖洞程式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#define SIZE 9
#include<vector>
using namespace std;

class DigSudoku
{
    public:
        DigSudoku(vector < int >, int); //建構子
        void board_dig(int); //挖洞函式
        vector < int > get_board(); //輸出棋盤至vector變數
        void copy_board(); //複製棋盤函式
    private:
        int i,j; //迴圈用變數
        int in_board[SIZE][SIZE]; //輸入棋盤
        int dig_board[SIZE][SIZE]; //輸出棋盤(挖完洞)
        int number_seres[SIZE*SIZE]; //數字序列
};


DigSudoku.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

#include "DigSudoku.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

DigSudoku::DigSudoku(vector < int > board, int n_hole)
{
    srand(time(NULL));//以物件初始化時間設定為亂數種子
    for (i=0;i<SIZE*SIZE;i++) //建立0到80的數字序列
    {
        number_seres[i] = i;
    }
    for (i=0;i<SIZE;i++)//將輸入的數獨題目寫入至in_board變數
        for (j=0;j<SIZE;j++)
        {
            in_board[i][j] = board[i*SIZE+j];

        }
    board_dig(n_hole);//呼叫挖洞函式
}

void DigSudoku::board_dig(int n_hole)
{
    int temp; //置換用暫存變數
    copy_board(); //呼叫複製函數
    for (i=0;i<81;i++) //將數字序列打亂
    {
        j = rand()%81;
        temp = number_seres[i];
        number_seres[i] = number_seres[j];
        number_seres[j] = temp;
    }
    for (i=0;i<n_hole;i++)
    {
        dig_board[number_seres[i]/SIZE][number_seres[i]%SIZE] = 0;
    }
}

vector < int > DigSudoku::get_board()
/*輸出挖完洞的題目至vector變數*/
{
    vector < int > out_board(SIZE*SIZE);
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            out_board[i*SIZE+j] = dig_board[i][j];
        }
    return out_board;
}

void DigSudoku::copy_board()
/*
複製棋盤函式
copy in_board into dig_board
*/
{
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            dig_board[i][j] = in_board[i][j];
        }
}


SolveSudoku.h (數獨解題程式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

/* 本Class主要針對唯一解的數獨題目來設計
    若題目有多重解,則不適用 */
#define SIZE 9
#include <vector>
using namespace std;

class SolveSudoku
{
    public:
        static int Ans; //數獨解個數計數器 (這是個Global的變數)
        SolveSudoku(vector<int>,int); //建構子
        void sol(); //解題函式
        vector < int > get_board(); //取得數獨解
        int getAns(); //取得數獨解的個數

    private:
        int i,j,k; //迴圈用變數
        int in_board[SIZE*SIZE]; //數獨題目
        int count[SIZE*SIZE]; //每個空格排除數字個數
        int Found; //已知空格數字的個數
        bool tag[SIZE*SIZE][SIZE]; //每個空格排除的數字
        void setAns(int A); //設定Ans變數
};


SolveSudoku.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

#include "SolveSudoku.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;

int SolveSudoku::Ans = 0; //數獨解個數計數器 (Global變數)
SolveSudoku::SolveSudoku(vector<int> board,int A)
{
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            in_board[i*SIZE+j] = board[i*SIZE+j];
        }
    //初始化 count, tag, Found變數
    for (i=0;i<SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            count[i*SIZE+j] = 0;
        }
    for (i=0;i<SIZE*SIZE;i++)
        for (j=0;j<SIZE;j++)
        {
            tag[i][j]=0;
        }
    Found = 0;
    setAns(A);
    sol();
}

void SolveSudoku::sol()
{
    int check, Max, iMax;
    /* 一般解法
    Found: 計算已知數字的個數
    count, tag: 掃描直、橫排又九宮格找出空格裡排除的數字
    */
    for (i=0;i<SIZE*SIZE;i++)
    {
        if (in_board[i])
        {
            Found++;
        }
        else
        {
            for (j=0;j<SIZE;j++)
            {
                if (in_board[(i/SIZE)*SIZE+j] && !tag[i][in_board[(i/SIZE)*SIZE+j]-1])
                {
                    count[i]++;
                    tag[i][in_board[(i/SIZE)*SIZE+j]-1] = true;
                }
                if (in_board[j*SIZE+i%SIZE] && !tag[i][in_board[j*SIZE+i%SIZE]-1])
                {
                    count[i]++;
                    tag[i][in_board[j*SIZE+i%SIZE]-1] = true;
                }
                check = in_board[((i/(SIZE*3))*3+j/3)*SIZE + ((i%SIZE)/3)*3+j%3] - 1;
                if (in_board[((i/(SIZE*3))*3+j/3)*SIZE + ((i%SIZE)/3)*3+j%3] && !tag[i][check])
                {
                    count[i]++;
                    tag[i][check] = true;

                }
            }
        }
    }
    /* 將排除數字有8個的空格填入正確的數字&增加Found個數
    並更新其直、橫排、九宮格內之空格的count, tag
    */
    for (i=0;i<SIZE*SIZE;i++)
    {
        if (!in_board[i] && count[i]==8)
        {
            count[i] = 0;
            Found++;
            for (j=0;j<SIZE;j++)
            {
                if(!tag[i][j])
                {
                    in_board[i] = j+1;
                    for (k=0;k<SIZE;k++)
                    {
                        if (!in_board[(i/SIZE)*SIZE+k] && !tag[(i/SIZE)*SIZE+k][j])
                        {
                            count[(i/SIZE)*SIZE+k]++;
                            tag[(i/SIZE)*SIZE+k][j] = true;
                        }
                        if (!in_board[k*SIZE+i%SIZE] && !tag[k*SIZE+i%SIZE][j])
                        {
                            count[k*SIZE+i%SIZE]++;
                            tag[k*SIZE+i%SIZE][j] = true;
                        }
                        check = ((i/(SIZE*3))*3+k/3)*SIZE + ((i%SIZE)/3)*3+k%3;
                        if (!in_board[check] && !tag[check][j])
                        {
                            count[check]++;
                            tag[check][j] = true;
                        }

                    }
                }
            }
            i = -1; //新填入空格數字就重新開始迴圈
        }

    }
    if (Found == 81) //全部空格數字都找到
    {
        setAns(Ans+1); //數獨解個數加1
    }
    else //一般解法不行,只好用暴力解了XD
    {
        //找出排除數字最多的空格
        Max = 0;
        iMax = -1;
        for (i=0;i<SIZE*SIZE;i++)
        {
            if (count[i] > Max)
            {
                Max = count[i];
                iMax = i;
            }
        }
        //暴力解開始!!
        //從排除數字最多的空格開始猜答案,把每個有可能的解法都跑一次
        for (j=0;j<SIZE;j++)
        {
            if(!tag[iMax][j])
            {
                in_board[iMax] = j+1;
                /*
                建立一個新的解題物件,並把目前數獨解個數代入
                若有新的解,將會更新數獨解(Ans)的個數,in_board則為其解
                若題目有多重解(Ans>1),in_board只會紀錄下最後一組的數獨解
                */
                SolveSudoku *tmp = new SolveSudoku(get_board(),getAns());
                //刪除此物件,並開始下一個可能解(節省記憶體的用量)
                delete tmp;
            }
        }
    }
}

vector < int > SolveSudoku::get_board() //取得數獨解答
{
    vector <int> out_board(SIZE*SIZE);
    for (i=0;i<SIZE*SIZE;i++)
    {
        out_board[i] = in_board[i];
    }
    return out_board;
}

int SolveSudoku::getAns() //取得數獨解答的個數
{
    return Ans;
}

void SolveSudoku::setAns(int A)//設定數獨解答的個數
{
    Ans = A;
}