Archive for the ‘Chess’ Category

明報的水準真是不太行

Tuesday, July 31st, 2007

明報的記者原來連甚麼是 chess,甚麼是 checker 也不知道。初時看一看這篇明報的報導

加拿大科學家用了18年時間,發展出一套完美西洋棋的電腦程式,……

這時已有點驚奇,因為在現代來說應該還是不可能的,但它還拿深藍來比較啊。再去 Slashdot 看一看

My story on the Nature site announced that a team of computer scientists at the University of Alberta has solved checkers.

兩者必有一者錯了。就算 Slashdot 的品質如何差,鬼佬也不至於會將 chess 和 checker 搞錯吧。於是上 Chinook 的 site 看一看,果然是 checker。

棋賽也出千

Friday, January 19th, 2007

平常只有賭場才會常有高科技出千,現在這種風氣竟然曼延到棋賽了。現在還是國際象棋,何時輪到中國象棋?雖然中國象棋的棋手現在大多都沒錢買這種玩意,但難保將來沒有。

SVG 好還是 Bitmap 好?

Friday, November 10th, 2006

我對美術真是很沒天份,不信的話,看看我第一次製作的棋子,慘不忍睹:

用 ukai 製作的象棋字型,唉……

固然用了 ukai 來產生字型是有影響,因為總覺得 ukai 的楷書很不平衡、不協調;但自己的手法拙劣是絕對推不掉的。於是第二次嘗試,使用了王漢宗的隸書下載),而且將圓圈和字型大小都調整過,終於像樣了。除了「將」字還覺得有點偏之外,似乎沒甚麼大問題,另外加上了一點半透明的效果:

用王漢宗的隸書製作象棋字型

第一次製作時不知輕重,將大小設定為 400×400 px,結果淨是載入十數個 SVG 棋子所需的時間就令程式啟動時間加長幾倍了!現在設定為 64×64 px 是好一點;不過啟動時間始終要增加一倍。當然用小的 Bitmap 是快得多了,但這樣的話棋盤又不能放大縮小,真是魚與熊掌,只能任擇其一。Quarry 就好得多了,因為只有黑白兩子,頂多加上 25% 和 50% 透明效果。連 light diffusion 和 ambience 都是計算出來的,令兩種棋盤的棋子看來完全不同,我真是服了它的作者。

My chess project

Thursday, November 2nd, 2006

This thing starts to be (barely) playable:

XQboard

XQboard

Really glad that I managed to do something, despite my health deteriorating sharply because of it. (Cough, cough, …)

However the whole project is far more complex than I have thought. Only after starting coding for a while that I found existing protocol definition between chess engines and boards, namely “cxboard” and “UCCI” protocol, akin to xboard/WinBoard and UCI protocols. What it means to me is, one single project becomes two!

There are only very few existing open source implementations of Chinese Chess (XiangQi):

Name Type Comment
MRSD+ (夢入神蛋) engine + board Strong AI, dead project (since 2003), UI and engine not separable (i.e. no communication protocol)
ElephantEye (象眼) engine Strong AI, actively developed, supposed to be UCCI reference implementation
CXBoard board Feature rich, uses GTK+ 1.2 (old), doesn’t support newer cxboard protocol in spite of its name
JunFa engine Supposed to be the reference implementation of cxboard protocol. Guess what — one can capture all of the opponent’s 16 pieces in the first 16 moves with one single pawn. Only supports a few commands in cxboard protocol.
QiPan board Uses Qt 2.x (not available on most Linux now), better support of cxboard protocol, can’t test because I can’t compile it
tsito engine Only supports old cxboard protocol, very weak AI
VSCCP engine Only intended as a coding sample, very weak AI

Until today, the route chosen is: (1) separate the engine part of MRSD+ into a command line interface, which is not so hard, thanks to Wei Tzer Jen implementing the AI as a C++ class; (2) write a GTK+2 frontend, as I have (psychological?) problem learning any OO language.

However when I found Elephany Eye to be compilable on Linux without problem, seems it’s time to switch strategy: using existing, working engine is far better than modifying one not working well. So most likely I’ll make sure it is playable with Elephant Eye first, only work on MRSD+ if there is any spare time.

Anyway, this stuff is still far from release quality — lots of variables and memory leaked globally, even more variables hard-coded, etc. Much more time is needed to clean up all of these, not to mention all of the TODOs below:

  • Make preferences work
  • Allows taking back moves
  • Allows engine playing either side (right now hard-coded to black/blue side)
  • More complete cxboard implementation
  • Implement UCCI protocol support
  • Compile with autotools (though I know this toolchain sucks)
  • Some artwork (EEEK, I never mean doing it myself)

And the long term plans:

  • Two engine mode
  • Implement chess clock
  • Implement proper game history navigation
  • Turn the board into a full featured widget
  • Implement import and export of PGN or XGF or whatever format invented in universe
  • ……
  • …………

In the process I found Quarry to be very helpful as a reference, IMHO it is one of the best implementation of game boards, fully widgetized and controllable in great detail, not to mention its capability in handling various kinds of game board (Go, Amazons and Reversi) in a single program.

About releasing, my best guess is that it can at least compile and run properly on, say, 2-3 weeks later, since I need to go to Beijing for a job interview recently, thus unable to work on it for a few days. Please forgive me if anybody thinks my code is as ugly as food dipped in shit — I’m just an absolute beginner in programming, especially on working at larger scale program. Yes, that’s not a joke, perhaps I can read a bit of code, but have almost never been a coder myself before.

Currently there are a few areas where help is needed:

  1. Public source code repository. Is sourceforge a good idea? Or should I set up my own subversion/cvs?
  2. Somebody care to teach me on stuff about widget and signals? :-D
  3. Artwork (right now most of the things are shamelessly ripped off from Quarry and MRSD+)

近來搞的小 project

Tuesday, October 24th, 2006

在可見的將來,我都要面對類似以下的程式碼:


int XQ::pidS(int t,char c)
{
int n,s,e,i,j,j0,j1,j2,cmp=-1;;
s=24+Turn();
e=32+Turn();
if(c<='9'  && c>='1')
{
         if(Turn()) n=c-'1';     else n='9'-c;
for(i=s;i<=e;i+=2)
if(YX_X(cch_p[i])==n) {return i;}
return s;
}
if(c>='A' && c<='E')
{
n=c-'A';
for(i=8;i>=0;i--)
for(j=9;j>=0;j--)
if(bd(j,i)>=s)
 if(bd(j,i)<=e)
 if((bd(j,i)&1)==Turn())
{
if(n==0) return bd(j,i);
n--;
}
return s;
}/*A-E*/
#define CMP_SWAP(a,b) if(YX_Y(cch_p[a])<YX_Y(cch_p[b])) {j=a;a=b;b=j;}
j0=-1;
j1=-1;
j2=-1;
for(i=s;i<=e &&j0==-1;i+=2)
  for(j=i+2;j<=e;j+=2)
  if(YX_X(cch_p[i])==YX_X(cch_p[j]))
  {   if(j1==-1){  j0=i;        j1=j;}
  else {j2=j;跳出;}     }
if(j2==-1) j2=j1;
CMP_SWAP(j1,j2);
CMP_SWAP(j0,j1);
CMP_SWAP(j1,j2);
cmp=-1;
if(c=='+' &&!Turn()) cmp=1;
 if(c=='-' &&Turn()) cmp=1;
 if(cmp==1) return j0;
  return j2;
}
static char translate_c2e(char *c)
{
int i;

感覺就像要看懂 Google 的 Javascript 一樣:

  1. 大堆像 i, p, k, n, syx, dyx, cch_p 等等不知是甚麼東西、甚麼用途的變數名稱,就好像是曾經用過 code obfuscator 處理過,為了保存甚麼商業秘密似的。
  2. 每行都可以隨機地出現由一至四個不等的空格、tab 組成的縮排,甚至每行都可能會反向縮排,例如 if/else 中間的內容可以隨時在 if 左面、右面,或者在同一行。個個檔案都可以不一樣,甚至同一個檔案內各處也不一樣。
  3. 隨了縮排不計算,其它正常需要的空格全部不見了,就好像為了要慳位置似的,我說像 Google 的 Javascript 就是這個原因。

真的,不是 wordpress 排版錯誤,我已經努力確保所有空格、縮排位置和原來的程式一模一樣。唯一有改動的,就是程式本來有 Big5 碼,我將那兩個字改為用 UTF-8 碼而已,否則在 wordpress 就看不到了。會使用中文來當程式碼來寫,還是 C++,沒錯,這些中文程式碼就是丙正正遺留下來的餘孽。別的中文化程式語言也落得一樣下場,即使是當年「風頭一時無兩」的中蠎(中文 python)也不例外。這種中文化根本就是為了起長城,將中國人的腦袋和外面的世界隔絕,推廣方面也完全沒有半點好處,更不見得這種東西比英文易理解。

面對這些程式,我只能喊:

救命。

可能是因為當初魏澤人想也沒想過會有人想看、想改他的東西,所以就不需要用別人也能理解的方式去寫吧。但對於我這個入門的新手來說,真是頭痛得不得了。

另一樣更麻煩的,就是夢入神蛋是用 C++ 寫的,但我暫時又只懂用 C(以前學過的 C++ 都原封不動還了給教授),所以或許我只能將 engine 部份加個 command line frontend,然後和 GUI 之間用 socket 溝通也好,別的甚麼方法也好,總之是不能混在一起。其實這種做法和別的國際象棋做法一樣,如果能支援 GTP protocol 就最好,但短期目標 — 起碼要能夠用吧。對於我這個沒怎麼接觸過稍大一點的程式的人來說,距離這個目標還差得太遠了。

等能夠見人的時候才拿出來吧。現在以我的功力,寫出來的東西真是笑壞人肚皮……

Sjeng — You did give me some pleasure

Wednesday, July 6th, 2005

After packaging and running tournament among various chess engines (Crafty, Phalanx, Gnuchess), Sjeng is the most interesting out of all (of course I mean the old Sjeng Free, not Deep Sjeng).

It invariably loses every game, and suffers humiliatingly. Yes, I did add opening book for sjeng, and that’s still so. Seems it blunders like less powerful chess engines do:

  • Exchanges most powerful (queen here) piece with 2 less powerful ones, but with no gain of positional or material advantage at all. And just in the middle of the game! At later stage this proves to be fatal…
  • When there are no obvious good move, it just plays dumb moves instead of attempting to break through.
  • Don’t know how to resign, like gnuchess do.

But watching sjeng playing these silly moves did give me some joyful time.

Link to partial analysis of gm2600.pgn

Monday, July 4th, 2005

When searching for mirror of gm2600.pgn (which should be available from Crafty’s main site, but right now it’s down), noticed this link which contains an analysis of gm2600.pgn with 5-pieces endgame.