You are on page 1of 22

LI NI U

Ni n CNTT l chng ta hnh dung ngay ti mt mi trng pht trin nng ng vo bc nht trn ton th gii hin nay. N c ng dng vo mi mt ca i sng ca con ngi, t cng vic cho ti gii tr. Cng vi s pht trin vt bc ca CNTT, con ngi cho ra i rt nhiu sn phm mang tnh gii tr mnh m v hp dn, tuy nhin nhng chng trnh nh mang tnh tr tu vn chim mt th phn no trong lnh vc ny. Mt trong nhng chng trnh phi k ti l cc chng trnh chi c, i din l Tr chi c Caro. C Caro c kh nhiu ngi a thch v tnh n gin ca tr chi nhng bn thn li mang tnh tr tu kh cao. Chng trnh c vit bng ngn ng Java, l mt ngn ng c kh nng tng tc kh mnh vi nhng cng c sn c gip cho ngi lp trnh pht trin ng dng cc nhanh v thun tin Bn thn l mt sinh vin trong iu kin thc t cng vic, kinh nghim lp trnh cha c nhiu , qu trnh thc hin vic vit chng trnh cng nh k nng cn nhiu hn ch. Tuy vy c s gip tn tnh ca thy hng dn cng nh cc bn sinh vin trong lp. Em c gng thc hin xong chng trnh ny. Tuy chng trnh ch rt nh v cn nhiu li nhng n gip em c nim tin thc hin nhiu cng vic ln hn sau ny. Mc d c gng gii quyt bi ton mt cch ng n nht nhng chc chn vn khng th trnh khi nhng thiu st. Em thc hin ti ny mong mun nhn c cc kin ng gp c th hon thin hn na chng trnh. H Ni, ngy 20 thng 11 nm 2012 Nhm sinh vin thc hin

1. C s l thuyt

1.1
O.

Yu cu bi ton.

Xy dng mt bn c c k cc vung vi kch thc 20 x 20. C 2 qun c l X v Ngi chi c th nh vi my hoc 2 ngi chi vi nhau. Ngi thng l ngi i c 5 qun c cng kiu trn hng dc, hng ngang hoc ng cho. Hai ngi ho nhau khi bn c ht ch nh m vn cha phn c thng bi.

1.2

Phn tch bi ton.

1.2.1 M phng bn c Bn c (Board) bao gm cc c (Cells) c t trong mt mng 2 chiu (kch thc a x b) Trong mi Cell c th xc nh c: V tr cell ( Row, collumme) Trng thi Cell (Status) Bao gm ang trng (empty) nc i ca i th (Player) hoc nc i ca my (Com) nguy him ca c tu theo trng thi Cell v c th thay i c. 1.2.2 nh gi gi tr Cell. Ging nh trong thc t, ngi chi thng nh gi mt s nc c l nguy him, bnh thng hoc t nguy him, my tnh cng nh gi nhng c th hn bng cc con s. 1.3 Phng php gii quyt bi ton. 1.3.1 Tm kim nc i, khng gian tm kim. Trong tr chi Caro, c sau mi nc c, mi i th s chn ra t nhng trng i, do , sau 1 mi nc i th s trng cn li s gim. Nh vy, vic tm nc i tip theo cho trng thi c sn ch l vic tm kim nhng trng cn li, ng thi, khng gian tm kim s thu hp theo s nc i to.

Khng gian chn nc i t mi trng thi ban u l hu hn, nhng khng gian tm kim 1 nc i dn n chin thng l rt ln. Do ta khng th vt sch khng gian tm kim nc i ny m ta phi gii hn khng gian tm kim. Mt khng gian tm kim c th hin theo 1 cy a phn v uc gi l cy tm kim hay cy tr chi. V d:

Da vo ci cy tr chi nh ngha trn, vic tm kim nc i l chn 1 nt trn cy ( mc 1) sao cho nc l tt. Theo thng thng khi chi, mt nc i tt hay khng l ph thuc vo kh nng dnh chin thng l cao hay thp sau khi nc i ny uc i. Do , mun chn 1 nc i tt th nu ch da vo

th c hin ti l cha , m phi bit thng tin ca nhng th c sau khi chn nc ny i. 1.3.2 Gii thut Minimax. 1.3.2.1 Chin lc Minimax tm kim nc i Chin lc ny c xc nh bng cch xet gi tr MINIMAX i vi mi nt trong cy biu din tr chi. MAX chn nc i ng vi gi tr MINIMAX cc i ( t c gitr cc i ca hm mc tiu) t c gi tr cc i ca hm mc tiu) Ngc li, MIN chn nc i ng vi gi tr MINIMAX cc tiu. V d:

1.3.2.2

Gii thut Minimax:

Gii thut tm kim MINIMAX vp phi vn bng n (mc hm m) cc kh nng nc i cn phi xt khng ph hp vi nhiu bi ton tr chi thc t. Chng ta c th ct ta (b i khng xt n) mt s nhnh tm kim trong cy biu din tr chi. 1.3.3 Phng php ctta - (Alpha-beta prunning). 1.3.3.1 tng. Nu mt nhnh tm kim no khng th ci thin i vi gi tr (hm tin ch) m chng ta c, th khng cn xt n nhnh tm kim na. Vic ct ta cc nhnh tm kim (ti) khng nh hng n kt qu cui cng

l gi tr ca nc i tt nht i vi MAX (gi tr ti a) tnh n hin ti i vi nhnh tm kim. Nu v l gi tr ti hn , MAX s b qua nc i ng vi v -> Ct ta nhnh ng vi v c nh ngha tng t i vi MIN. V d :

1.3.3.2

Gii thut phng php ct ta - (Alpha-beta prunning).

So snh s nt phi xt gia 2 thut ton Minimax v - :

i vi cc tr chi c khng gian trng thi ln, th phng php ct ta - vn khng ph hp. Khng gian tm kim (kt hp ct ta) vn ln C th hn ch khng gian tm kim bng cch s dng cc tri thc c th ca bi ton + Tri thc cho php nh gi mi trng thi ca tr chi. + Tri thc b sung (heuristic) ny ng vai tr tng t nh l hm c lng h(n) trong gii thut tm kim A* 2. Xy dng chng trnh: 2.1 Gii thiu tng quan chng trnh: y l mt Game m t tr chi c Caro c th chi online trn mng Internet hoc offline ti my tnh c nhn, tr chi c lp trnh bng ngn ng Java theo phng php hng i tng. Tuy l mt Game xut hin t lu trn th gii, nhng u im ca Game Nhm chng Ti thit k ha kh p, tc Game nhanh, khng yu cu cu hnh mnh.

2.2

K thut lng gi. K thut lng gi l mt k thut quan trng trong vic xy dng tr chi c caro. K thut ny gip cho im trng thi ca bn c t xy dng cy tr chi. Vic xy dng hm lng gi hp l, chnh xc s gip cho h thng c nh gi chnh xc v trng thi bn c a ra nc i thng minh hn.

i vi bi ton c caro, ta c th dng 1 hm lng gi nh gi tnh "tt, xu" ti 1 thi im. Nhng no gn cc qun nh trc s c im cao hn. Nhng cng xa th c cng t im. Tuy nhin y ch l Heuristic nn ta phi b sung thm cc Heuristic khc na, v d vng c 2, 3, 4 ... qun lin tip th s c cng thm 1 s im thng no cho vng da vo trng s qun (tc l nhiu qun lin tip th c cng nhiu im thng hn). Sau mi nc i, h thng s kim tra bn c tm cc th c ri ty vo li th nh trc tnh ra im. C th l: *TH1: Trng hp chc thng (+5000 im)

* TH2: Trng hp rt thun li (+600 im)

* TH3: Trng hp thun li (+500 im)

*TH4: Trng hp bnh thng (+50 im)

2.3 Xy dng chng trnh. 2.3.1 CComputer.java


import java.applet.*; import java.awt.*; public class Gomoku extends Applet { Panel ButtonArea; Button Buttons[]; Label MessageLabel; Checkbox Check1,Check2; CGoban Goban; CComputer Computer; int Player[]; final int HUMAN = 1;

final int COMPUTER = 2; int CurrentPlayer; final int WHITE = 1; final int BLACK = 2; int GameState; final int GS_END = 0; final int GS_PLAYUSER = 1; final int GS_PLAYCOM = 2; final int GS_READY = 3; public void init() { ButtonArea = new Panel(); setLayout(new BorderLayout()); add("South",ButtonArea); MessageLabel = new Label("Welcome to Gomoku-Narabe Game !"); add("Center",MessageLabel); Goban = new CGoban(); add("North",Goban); Buttons = new Button[4]; Buttons[0] = new Button("First"); Buttons[1] = new Button("Second"); Buttons[2] = new Button("Clear"); ;Buttons[3] = new Button("Check"); ButtonArea.setLayout(new FlowLayout()); for(int i=0;i<3;i++) ButtonArea.add(Buttons[i]); Check1 = new Checkbox("Sakiyomi"); Check2 = new Checkbox("Kinjite"); ButtonArea.add(Check1); ButtonArea.add(Check2); Check1.setState(true); Check2.setState(true); Goban.Sakiyomi = true; Goban.Kinjite=true; Computer = new CComputer(Goban); Player = new int[3]; } public void start() { InitGame(); } public boolean action(Event e,Object o) { String label = o.toString(); if(e.target instanceof Button){ if(label.equals("First")){ StartGame(HUMAN); } else if (label.equals("Second")) { StartGame(COMPUTER);

} else if (label.equals("Clear")) { InitGame(); } } else if(e.target instanceof Checkbox) { Goban.Sakiyomi = Check1.getState(); Goban.Kinjite = Check2.getState(); } return true; } void InitGame() { Goban.Initialize(Computer); Goban.Draw(); repaint(); GameState = GS_READY; for(int i=0;i<2;i++) Buttons[i].enable(); PutMessage("Click 'First' or 'Second' to start game."); } void StartGame(int FirstPlayer) { if(GameState!=GS_READY) return; Player[BLACK] = FirstPlayer; Player[WHITE] = (FirstPlayer==COMPUTER) ? HUMAN : COMPUTER; CurrentPlayer = BLACK; for(int i=0;i<2;i++) Buttons[i].disable(); if(FirstPlayer==COMPUTER){ Goban.Area[7][7] = 1; PlayComputer(); } else { GameState = GS_PLAYUSER; } } public boolean mouseDown(Event evt,int x,int y) { if(GameState==GS_PLAYUSER && Player[CurrentPlayer]==HUMAN) { GPos pos = new GPos(); if( !Goban.GetPos(x,y,pos) ) return true; switch(Goban.Check(CurrentPlayer, pos.x, pos.y)) { case CGoban.GC_OK:
CurrentPlayer = (CurrentPlayer == WHITE) ? BLACK : WHITE;

if(Player[CurrentPlayer] == COMPUTER){ GameState = GS_PLAYCOM; } break; case CGoban.GC_ILLEGAL:

PutMessage("Illegal postion ! Can't put"); break; case CGoban.GC_CANNOT: PutMessage("Aleady exist! Can't put"); break; case CGoban.GC_FILLED: PutMessage("Board is filled ! Draw game."); GameState = GS_END; break; case CGoban.GC_WIN: PutMessage("You Win !!!"); GameState = GS_END; break; } } return true; } public boolean mouseUp(Event evt,int x,int y) { if(GameState == GS_PLAYCOM){ PlayComputer(); } return true; } void PlayComputer() { GPos pos = new GPos(); PutMessage("Computer now thinking..."); switch(Computer.Think(CurrentPlayer,pos)) { case CComputer.THINK_OK: PutMessage("It's now your turn."); CurrentPlayer = (CurrentPlayer == WHITE) ? BLACK : WHITE; if(Player[CurrentPlayer] == COMPUTER){ PlayComputer(); } else { GameState = GS_PLAYUSER; } break; case CComputer.THINK_FILLED: PutMessage("Board is filled ! Draw game."); GameState = GS_END; break; case CComputer.THINK_WIN: PutMessage("Computer Win !!!"); GameState = GS_END; break; } }

public void PutMessage(String s) { MessageLabel.setText(s); } public void paint(Graphics g) { Goban.Draw(); } }

2.4.2 Cgoban.java
import java.applet.*; import java.awt.*; public class CGoban extends Canvas { static final int TOP = 10; static final int LEFT = 10; static final int BOARDSIZE = 15; static final int PIECESIZE = 20; static final int MAXPIECENUM = BOARDSIZE * BOARDSIZE; static final int GC_OK = 0; static final int GC_ILLEGAL = 1; static final int GC_CANNOT= 2; static final int GC_FILLED = 3; static final int GC_WIN = 4; public boolean Sakiyomi; public boolean Kinjite; CPiece Pieces[][]; public int Area[][]; static final int AREASIZE = 2; public int numPiece; CComputer Computer; public CGoban() { resize(310,310); Pieces = new CPiece[BOARDSIZE][BOARDSIZE]; for(int i=0; i < BOARDSIZE; i++){ for(int j=0; j < BOARDSIZE; j++){ Pieces[i][j] = new CPiece(); } } Area = new int[BOARDSIZE][BOARDSIZE]; } public void Initialize(CComputer com) { Computer = com; for(int x=0; x < BOARDSIZE; x++){ for(int y=0; y < BOARDSIZE; y++){ Pieces[x][y].State = CPiece.EMPTY; Area[x][y] = 0; }

} numPiece = 0; } public void Put(int color,int x,int y) { if(Pieces[x][y].State == CPiece.EMPTY) { Pieces[x][y].State = color; numPiece++; int x1,x2,y1,y2; x1 = (x-AREASIZE < 0) ? 0 : x-AREASIZE ; x2 = (x+AREASIZE >= BOARDSIZE) ? BOARDSIZE-1 : x+AREASIZE; y1 = (y-AREASIZE < 0) ? 0 : y-AREASIZE ; y2 = (y+AREASIZE >= BOARDSIZE) ? BOARDSIZE-1 : y+AREASIZE; for( ; x1 <= x2; x1++){ for(y = y1 ; y <= y2; y++) { Area[x1][y]++; } } } } public void Remove(int x,int y) { if(Pieces[x][y].State != CPiece.EMPTY) { Pieces[x][y].State = CPiece.EMPTY; numPiece--; int x1,x2,y1,y2; x1 = (x-AREASIZE < 0) ? 0 : x-AREASIZE ; x2 = (x+AREASIZE >= BOARDSIZE) ? BOARDSIZE-1 : x+AREASIZE; y1 = (y-AREASIZE < 0) ? 0 : y-AREASIZE ; y2 = (y+AREASIZE >= BOARDSIZE) ? BOARDSIZE-1 : y+AREASIZE; for( ; x1 <= x2; x1++){ for( y = y1; y <= y2; y++) { Area[x1][y]--; } } } } public int Check(int color,int x,int y) { int ret; if(Pieces[x][y].State != CPiece.EMPTY) return GC_CANNOT; Put(color,x,y); ret = Computer.CheckIllegal(color,x,y); if(ret != Computer.ILL_NOT){ Remove(x,y); return GC_ILLEGAL; } ret = Computer.Find5Block(color,x,y); Draw();

if(ret == Computer.OK5) return GC_WIN; if(numPiece == MAXPIECENUM) return GC_FILLED; return GC_OK; } public boolean GetPos(int x,int y,GPos pos) {
if(x < LEFT-(PIECESIZE/2) || x > LEFT+(PIECESIZE*(BOARDSIZE-1))+(PIECESIZE/2) )

return false;
if(y < TOP-(PIECESIZE/2) || y > TOP+(PIECESIZE*(BOARDSIZE-1))+(PIECESIZE/2) )

return false; pos.x = ( x-(LEFT-(PIECESIZE/2)) ) / PIECESIZE; pos.y = ( y-(TOP -(PIECESIZE/2)) ) / PIECESIZE; return true; } public void Draw() { repaint(); } public void paint(Graphics g) { int x,y; g.setColor(Color.black); for(x = 0; x < BOARDSIZE; x++) { g.drawLine(x*PIECESIZE+LEFT,TOP,x*PIECESIZE+LEFT,TOP+(BOARDSIZE1)*PIECESIZE); } for(y = 0; y < BOARDSIZE; y++) { g.drawLine(LEFT,y*PIECESIZE+TOP,LEFT+(BOARDSIZE1)*PIECESIZE,y*PIECESIZE+TOP); } for(x = 0; x < BOARDSIZE; x++) { for(y = 0; y < BOARDSIZE; y++) { switch(Pieces[x][y].State) { case CPiece.WHITE: g.setColor(Color.white); g.fillOval(x*PIECESIZE,y*PIECESIZE,PIECESIZE-2,PIECESIZE-2); g.setColor(Color.black); g.drawOval(x*PIECESIZE,y*PIECESIZE,PIECESIZE-2,PIECESIZE-2); break; case CPiece.BLACK: g.setColor(Color.black); g.fillOval(x*PIECESIZE,y*PIECESIZE,PIECESIZE-2,PIECESIZE-2); break;

} } } }

2.4.3 Cpiece.java public class CPiece { public int State; static final int EMPTY = 0; static final int WHITE = 1; static final int BLACK = 2; } 2.4.4 Gpos.java public class GPos { int x; int y; }

2.4.5 Gomoku.java File giao din chnh ca chng trnh: import java.applet.*; import java.awt.*; public class Gomoku extends Applet { Panel ButtonArea; Button Buttons[]; Label MessageLabel; Checkbox Check1,Check2; CGoban Goban; CComputer Computer; int Player[]; final int HUMAN = 1; final int COMPUTER = 2; int CurrentPlayer; final int WHITE = 1; final int BLACK = 2; int GameState;

final int GS_END = 0; final int GS_PLAYUSER = 1; final int GS_PLAYCOM = 2; final int GS_READY = 3; public void init() { ButtonArea = new Panel(); setLayout(new BorderLayout()); add("South",ButtonArea); MessageLabel = new Label("Welcome to Gomoku-Narabe Game !"); add("Center",MessageLabel); Goban = new CGoban(); add("North",Goban); Buttons = new Button[4]; Buttons[0] = new Button("First"); Buttons[1] = new Button("Second"); Buttons[2] = new Button("Clear"); Buttons[3] = new Button("Check"); ButtonArea.setLayout(new FlowLayout()); for(int i=0;i<3;i++) ButtonArea.add(Buttons[i]); Check1 = new Checkbox("Sakiyomi"); Check2 = new Checkbox("Kinjite"); ButtonArea.add(Check1); ButtonArea.add(Check2); Check1.setState(true); Check2.setState(true); Goban.Sakiyomi = true; Goban.Kinjite=true; Computer = new CComputer(Goban); Player = new int[3]; } public void start() { InitGame(); } public boolean action(Event e,Object o) { String label = o.toString(); if(e.target instanceof Button){ if(label.equals("First")){ StartGame(HUMAN); } else if (label.equals("Second")) { StartGame(COMPUTER); } else if (label.equals("Clear")) { InitGame();

} } else if(e.target instanceof Checkbox) { Goban.Sakiyomi = Check1.getState(); Goban.Kinjite = Check2.getState(); } return true; } void InitGame() { Goban.Initialize(Computer); Goban.Draw(); repaint(); GameState = GS_READY; for(int i=0;i<2;i++) Buttons[i].enable(); PutMessage("Click 'First' or 'Second' to start game."); } void StartGame(int FirstPlayer) { if(GameState!=GS_READY) return; Player[BLACK] = FirstPlayer; Player[WHITE] = (FirstPlayer==COMPUTER) ? HUMAN : COMPUTER; CurrentPlayer = BLACK; for(int i=0;i<2;i++) Buttons[i].disable(); if(FirstPlayer==COMPUTER){ Goban.Area[7][7] = 1; PlayComputer(); } else { GameState = GS_PLAYUSER; } } public boolean mouseDown(Event evt,int x,int y) { if(GameState==GS_PLAYUSER && Player[CurrentPlayer]==HUMAN) { GPos pos = new GPos(); if( !Goban.GetPos(x,y,pos) ) return true; switch(Goban.Check(CurrentPlayer, pos.x, pos.y)) { case CGoban.GC_OK: CurrentPlayer = (CurrentPlayer == WHITE) ? BLACK : WHITE; if(Player[CurrentPlayer] == COMPUTER){ GameState = GS_PLAYCOM;

} break; case CGoban.GC_ILLEGAL: PutMessage("Illegal postion ! Can't put"); break; case CGoban.GC_CANNOT: PutMessage("Aleady exist! Can't put"); break; case CGoban.GC_FILLED: PutMessage("Board is filled ! Draw game."); GameState = GS_END; break; case CGoban.GC_WIN: PutMessage("You Win !!!"); GameState = GS_END; break; } } return true; } public boolean mouseUp(Event evt,int x,int y) { if(GameState == GS_PLAYCOM){ PlayComputer(); } return true; } void PlayComputer() { GPos pos = new GPos(); PutMessage("Computer now thinking..."); switch(Computer.Think(CurrentPlayer,pos)) { case CComputer.THINK_OK: PutMessage("It's now your turn.");
CurrentPlayer = (CurrentPlayer == WHITE) ? BLACK : WHITE;

if(Player[CurrentPlayer] == COMPUTER){ PlayComputer(); } else { GameState = GS_PLAYUSER; } break; case CComputer.THINK_FILLED: PutMessage("Board is filled ! Draw game."); GameState = GS_END; break;

case CComputer.THINK_WIN: PutMessage("Computer Win !!!"); GameState = GS_END; break; } } public void PutMessage(String s) { MessageLabel.setText(s); } public void paint(Graphics g) { Goban.Draw(); } }

3. Ci t chng trnh. 3.1 Giao din chng trnh chnh:

3.2 Cch s dng chng trnh:

Nu Click vo First th ngi nh trc. Nu chn vo Second th my nh trc.

KT LUN

Vi mc tiu ra ca ti l xy dng chng trnh Game c caro v nghin cu v thut ton tm kim i khng Minimax, cc ci tin ca n v ng dng trong tr chi c tng bng khng, cc kt lun chnh t c ca ti c th tm tt nh sau: nhc li mt cch tng quan v vn tm kim trong c pht biu bi ton tm kim v gii thiu cc k thut tm kim c bn nh tm kim khng c thng tin, tm kim c thng tin v tm kim i khng.

Tm hiu v c im ca tr chi c tng bng khng trong lnh vc L


thuyt tr chi. ng thi a ra m hnh ton hc ca tr chi c tng bng khng v pht biu nh l Minimax p dng cho cc tr chi c tng bng khng. nghin cu gii thut tm kim Minimax v gii thut ci tin ca n l gii thut Alpha-beta cho cc tr chi c tng bng khng.

Ci t c gii thut cho tr chi c tng bng khng vi hai ngi chi
trong bi ton c caro. Kt qu ca vic ci t l chng trnh tr chi Game c caro gia ngi v my tnh. Trong thut ton c ci t cho vic suy ngh ca My tnh. Mc d c nhiu c gng nhng chc chn rng cc kt qu ci t c khng trnh khi nhng thiu st v hn ch, hy vng rng trong tng lai vn ny s c nghin cu su hn v pht trin vi thut ton c ci tin tt hn, chng hn chng ta c th i vo nghin cu vn song song ha thut ton trn. Trn c s cc kt qu t c, chng ta c th pht trin nhng nghin cu tip v thut ton Alpha- Beta song song. Hn na, chng ta c th ngh n vic trin khai nhng nghin cu v ng dng ca nhng kt qu ny trong cc lnh vc khc ca x hi, c bit l kinh t.