You are on page 1of 8

Anh Tun - Tin hc 5A

CARO VI MINIMAX
1. Yu cu bi ton:
Khng gian trng thi c m t l tr chi caro (kch thc ty ). Xy dng
chng trnh gii quyt bi ton theo phng php minimax.
2. Gii thiu tr chi:
Caro l tr chi i khng hai ngi nh du vo m
t bng hnh vung gm
nhiu vung. Ngi thng cuc l ngi c hng ngang, dc, cho 3 trc.
Hoc c th ha. V d:

Khng gian trng thi ca tr chi:

3. Gii thiu Minimax


Minimax (cn gi l minmax) l mt phng php trong l thuyt quyt nh c
mc ch l ti thiu ha (minimize) tn tht vn c d tnh c th l "ti a"
(maximize). C th hiu ngc li l, n nhm ti a ha li ch vn c d tnh l
ti thiu (maximin). N c m rng cho nhiu tr chi phc tp v gip a ra cc
quyt nh chung khi c s hin din ca s khng chc chn.
Ta s nm r, th no l mt nc i "tt nht". Gii thut Minimax gip tm ra
nc i tt nht, bng cch i ngc t cui tr chi tr v u. Ti mi bc, n
s c nh rng ngi A ang c gng ti a ha c hi thng ca A khi n phin
anh ta, cn nc i k tip th ngi chi B c gng ti thiu ha c hi thng
ca ngi A (ngha l ti a ha c hi thng ca B).
Mt thut ton minimax l mt thut ton quy cho vic la chn bc i k tip
trong mt tr chi c hai ngi chi. Mt gi tr c gn cho mi v tr hay mt
trng thi ca tr chi. Gi tr ny c tnh ton bng mt hm tnh gi tr v tr v
n cho bit tt nu nh mt ngi chi t c n . Ngi chi sau i
mt bc lm ti a gi tr ti thiu ca v tr l kt qu t tp hp nhng bc i
c th ca i th. Nu l phin A s i, A s cho mt gi tr cho mi bc i
hp php ca anh ta.
Mt phng php b tr l gn cho mt s v tr thng cho A nh l +1 v cho B l
1. iu ny s dn n l thuyt tr chi t hp c pht trin bi John Horton
Conway.
Mt cch khc l s dng mt quy nh rng nu nh kt qu ca mt bc i l
mt chin thng lp tc cho A n c gn dng v hn v, nu nh l mt chin
thng lp tc cho B, m v hn. Gi tr cho A ca bt k nc i no khc l gi tr
minimum ca cc gi tr kt qu t mi bc tr li c th ca B. (A c gi l
ngi chi l cc i v B gi l ngi chi lm cc tiu), do vy c gi l thut
ton minimax. Thut ton trn s gn mt gi tr dng hay m v hn cho mi v
tr bi v gi tr ca mi v tr s l gi tr ca mt s v tr thng hay thua no .
Thng thng nhn chung iu ny ch c th xy ra ti im cui ca nhng tr

chi phc tp nh c vua hay c vy, bi v v mt tnh ton ta khng c kh nng


tnh xa n mc kt thc tr chi, tr khi l tr chi sp kt thc, v cc v tr
khng i khc nhau c cho cc gi tr hu hn nh l cc nh gi v mc tin
tng l chng s dn n chin thng cho ngi ny hay ngi khc.
iu ny c th c m rng nu nh chng ta cung cp mt hm nh gi
heuristic a ra cc gi tr cho cc v tr tr chi cha phi l cui cng m khng
xt tt c mi trng hp theo sau mt chui y . Chng ta sau c th gii
hn thut ton minimax ch xt mt s no cc nc i k tip. S ny c
gi l "s bc k tip", o bng "ply". V d, "Deep Blue" nhn trc 12 ply.
Thut ton ny c th c ngh nh l khm ph cc node ca mt cy tr chi.
S ct xn hiu qu ca mt cy l trung bnh ca s cc con ca mi nt (i.e.,
trung bnh ca cc nc i hp php trong mt v tr). S lng cc nodes c
khm ph thng l tng theo hm m vi s lng ply (n s nh hn hm m
nu nh gi cc nc i bt buc hay l cc bc lp li). S lng cc nodes cn
khm ph cho vic phn tch mt tr chi do gn bng s ct xt nng ln lu
tha s ply. Do vy l khng th phn tch tr chi v d nh c vua mt cch hon
ton ch bng thut ton minimax.
S trnh din ca thut ton minimax c th c ci tin ng k, m khng nh
hng n kt qu, bng cch s dng ct xn alpha-beta. Cc phng php ct
xn heuristic khc cng c th c s dng, nhng khng phi tt c chng bo
m s cho kt qu ging nhau nh l tm kim khng ct xn.

4. Gii thiu chng trnh

Hnh minh ho tr chi hon thnh v phn thng thuc v my tnh


CCH CHI:
Game c thc hin trn bng li 3x3 .
C 2 ngi tham gia chi: 1 bn l my tnh, bn cn li l ngi
tham gia game.
Ngi chi c th c nhng chn la sau:
o

To game mi.

Th t ngi i trc hay my i trc.

Mnh l qun X hay O.

Nu bn no to c 1 ng thng trn 3 lin tc s l ngi


chin thng. Lc trn title mn hnh s hin dng ch Computer
Wins! (hay You Wins!). Cn nu ho nhau s l ch Draw.

5. Cu trc chng trnh:


Tr chi tic-tac-toe c vit bng ngn ng C# trn nn Visual Studio.NET 2005.
Chng trnh c chia thnh 3 class: Board, SquareControl, Caro.
Board(bng li lm nn ca game) class:

Bng li c th hin bng mng 2 chiu


//Size of board in one dimension
public int iBoardSize;

//Create a new Board object from a size parameter


public Board(int iSize)
{
this.iBoardSize

= iSize;

this.iEmptySquares = iSize * iSize;


this.aiBoard

= new int[iSize, iSize];

this.BoardState

= GameState.InProgress;

Bin BoardState biu th trng thi ca game. Ngha l liu game ang
c chi (InProgress) hay kt qu trc l 1 ci g . Nhng gi
tr c th c ca BoardState c nh bi GameState enumeration.
public enum GameState
{
InProgress,

ComputerWins,
HumanWins,
Draw
}
public GameState BoardState;

Bin iEmptySquares lu gi s trng trn bng li lm cho vic


tm ng i c d dng.
public int iEmptySquares;
Board class c 2 public constructors. Mt constructor to 1 bng mi
rng, mt constructor khc to 1 bng mi bng cch copy bng
c. Constructor th 2 c s dng trong sut qu trnh chi game (c
p dng AI).

//Create a new Board object from a size parameter


public Board(int iSize)
{
this.iBoardSize

= iSize;

this.iEmptySquares = iSize * iSize;


this.aiBoard
this.BoardState

= new int[iSize, iSize];


= GameState.InProgress;

}
//Create a new Board object by copying an existing one
public Board(Board board)
{

this.iEmptySquares = board.iEmptySquares;
this.iBoardSize
this.BoardState
this.aiBoard

= board.iBoardSize;
= board.BoardState;
= new int[iBoardSize, iBoardSize];

//Copy aiBoard
int i, j;
for (i = 0; i < this.iBoardSize; i++)
{
for (j = 0; j < this.iBoardSize; j++)
{
this.aiBoard[i, j] = board.aiBoard[i, j];
}
}
}

Phng thc CheckBoard() kim tra xem bn no thng sau mi ln


thc hin phng thc MakeMove().

Caro class:
Class Caro chnh l form main ca chng trnh. Caro cha 1 vi phng thc
iu khin game. chng hn nh:
private void MakeMove(Move move): phng thc chu trch nhim v
vic a bin move cho vo i tng Board v i tng SquareControl thch
hp.
private void Reset(): phng thc dng thit lp li trng thi ban u
chun b cho game mi.

S dng AI trong program:


Phng thc GetBestMove () trin khai thut ton Minimax look-ahead
xc nh bc i tt nht cho Computer Player.
Look-ahead c thc hin trn worker thread trnh vic ch i trn UI
trong qu trnh tm kim. Khi tm kim xong, worker thread s gi callback ti main
thread ri a vo move mi. Trong phng thc CalculateComputerMove() v
MakeComputerMove() th hin r iu ny.
iu hn ch ca chng trnh:
- Cha thc hin c chng trnh vi khng gian trng thi ln hn.
- Cha kt hp c vi phng php ct ta Alpha Beta thu hp phm vi duyt
cc nt v ti u thut gii