You are on page 1of 27

1

MC LC

LI NI U ......................................................................................... 2
I.YU CU BI TON.......................................................................... 3
II.PHN TCH GII QUYT BI TON .......................................... 3
1. Phn tch yu cu .......................................................................... 3
2. Phng php gii quyt ............................................................... 3
2.1 Tm kim nc i ...................................................................... 3
2.2 K thut lng gi ..................................................................... 8
3. Xy dng cc lp .......................................................................... 9
4. Giao din v cch chi ............................................................... 25
KT LUN ............................................................................................ 27










2

LI NI U


Game Caro (hay cn gi l game Gomoku) l mt tr chi quen thuc i
vi nhiu i tng, d chi, gim cng thng,...C Caro l mt trong nhng tr
chi rt ph bin, c bit l trong gii hc sinh, sinh vin. y cng l mt tr
chi chng em rt thch, chnh v vychng em chn ti Lm game c caro
cho mn Tr tu nhn to.
Trong qu trnh hon thnh ti ny, chng em tm hiu c cc thut
ton c hc trong mn Tr tu nhn to nh thut ton tm kim nc i
Minimax, gii thut Alpha-Beta cng nh k nng lp trnh ngn ng Java.
Chng em cng xin cm n s hng dn tn tnh ca thy Phm Vn Hi,
c v chuyn mn cng nh nh hng. V kin thc cn hn hp nn trong qu
trnh thc hin ti khng th trnh khi thiu st. V vy rt mong nhn c
nhn s gp ca thy ti c th hon thin hn na.
3


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

II. PHN TCH GII QUYT BI TON
1. Phn tch yu cu
M phng bn c
Bn c (Board) bao gm cc c ( Pos) c t trong mt mng 2 chiu
(kch thc a x b)
Trong mi Pos c th xc nh c:
V tr pos ( Row, collumme)
Trng thi pos (Status) Bao gm ang trng (0) nc i ca i th (2) hoc nc
i ca my (1)
nguy him ca c tu theo trng thi pos v c th thay i c.
nh gi gi tr cc pos
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.

2. Phng php gii quyt
2.1 Tm kim nc i
Gii thiu v 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.




4


V d :

Cy tr chi

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.

Chin lc minimax tm kim nc i
Chin lc ny c xc nh bng cch xt 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 cgitr cc
ica 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.


5


V d:


Gii thut minimax




6



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

Phng php ct ta - (Alpha-beta prunning)
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 :











7



Gii thut alpha beta







8



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

{0, 1, 1, 1, 1}, {1, 0, 1, 1, 1}, {1, 1, 0, 1, 1},
9




* TH2: Trng hp thun li (+585 im)

{0, 0, 1, 1, 1, 0}, {0, 1, 0, 1, 1, 0}, {1, 0, 1, 0, 1, 0, 1}

*TH3: ( +73 )
{0, 1, 1, 1, 0}, {0, 0, 1, 1, 1}, {0, 1, 0, 1, 1},
{0, 1, 1, 0, 1}, {0, 0, 1, 1, 1}, {1, 0, 1, 1, 0},
{1, 0, 0, 1, 1}, {1, 0, 1, 0, 1}, {1, 0, 0, 1, 1},
{0, 1, 0, 1, 1}

*TH3: Trng hp bnh thng (+9 im)
{0, 0, 1, 1, 0, 0}, {0, 1, 0, 1, 0, 0},
{0, 1, 1, 0, 0, 0}, {0, 1, 0, 1, 0, 0}, {0, 1, 1, 0, 0, 0},
{0, 1, 0, 0, 1, 0}

3. Xy dng cc lp
Lp CaroBoard
Biu din cc trng thi ca bn c Caro, danh sch cc nc nh. Cp nht
tm im nh cho ngi chi "class Player". C th coi nh thnh phn lu tr
thng tin ca class Player

-public void updateStateOfBoard(Point point): cp nht trng thi bn c. B im
nh ra khi cc nh cha nh, cp nht cc im cn li:
public void updateStateOfBoard(Point point) {
// Bo diem da danh ra khoi danh sach cac dinh chua danh
MarkNonTickedOfOnePlayer[point.x][point.y] = 0;
MarkNonTickedOfTwoPlayer[point.x][point.y] = 0;

// Cap nhat lai danh sach cac o chua danh
// Phan cap nhat nay can sua sau cho nhanh hon
numberNonTicked = 0;
for (int i = 0; i < number_step; i++) {
for (int j = 0; j < 8; j++) {
if (board[listAreaTicked[i].x + dx[j]][listAreaTicked[i].y + dy[j]] == 0) {
Point p = new Point(listAreaTicked[i].x + dx[j], listAreaTicked[i].y +
dy[j]);
10

int k;
for (k = 0; k < numberNonTicked; k++) {
Point p2 = listAreaNonTicked[k];
if (p.x == p2.x && p.y == p2.y) {
break;
}
}
if (k >= numberNonTicked) {
listAreaNonTicked[numberNonTicked++] = p;
}
}
}
}

// Cap nhat lai diem cua 32 o xung quanh o vua danh
// Tim cac o duong doc
for (int j = 0; j < 11; j++) {
if ((point.x + stepValue[j] < rows) && (point.x + stepValue[j] >= 0) &&
board[point.x + stepValue[j]][point.y] == 0) {
updateMarkOfPoint(new Point(point.x + stepValue[j], point.y));
}
}

// Tim cac o duong ngang
for (int j = 0; j < 11; j++) {
if ((point.y + stepValue[j] < cols) && (point.y + stepValue[j] >= 0) &&
board[point.x][point.y + stepValue[j]] == 0) {
updateMarkOfPoint(new Point(point.x, point.y + stepValue[j]));
}
}

// Tim cac o duong cheo chinh
for (int j = 0; j < 11; j++) {
if (point.x + stepValue[j] < rows && point.x + stepValue[j] >= 0
&& point.y + stepValue[j] < cols && point.y + stepValue[j] >= 0
&& board[point.x + stepValue[j]][point.y + stepValue[j]] == 0) {
updateMarkOfPoint(new Point(point.x + stepValue[j], point.y +
stepValue[j]));
}
}
11


// Tim cac o duong cheo phu
for (int j = 0; j < 11; j++) {
if (point.x + stepValue[10 - j] < rows && point.x + stepValue[10 - j] >= 0
&& point.y + stepValue[j] < cols && point.y + stepValue[j] >= 0
&& board[point.x + stepValue[10 - j]][point.y + stepValue[j]] == 0) {
updateMarkOfPoint(new Point(point.x + stepValue[10 - j], point.y +
stepValue[j]));
}
}

}
-public int getMarkOfPointByPlayer(Point p, int player): tnhim cho mt pos (
mt trn bn c ):

public int getMarkOfPointByPlayer(Point p, int player) {
int mark = 0;
// Tim cac o duong doc
for (int j = 0; j < 11; j++) {
StepChess[j] = ((p.x + stepValue[j] < rows) && (p.x + stepValue[j] >= 0) ?
board[p.x + stepValue[j]][p.y] : -1);
}
mark += ChessMark.MarkOfChessArea(StepChess, player);

// Tim cac o duong ngang
for (int j = 0; j < 11; j++) {
StepChess[j] = ((p.y + stepValue[j] < cols) && (p.y + stepValue[j] >= 0) ?
board[p.x][p.y + stepValue[j]] : -1);
}
mark += ChessMark.MarkOfChessArea(StepChess, player);


// Tim cac o duong cheo chinh
for (int j = 0; j < 11; j++) {
StepChess[j] = ((p.x + stepValue[j] < rows && p.x + stepValue[j] >= 0)
&& (p.y + stepValue[j] < cols && p.y + stepValue[j] >= 0) ? board[p.x +
stepValue[j]][p.y + stepValue[j]] : -1);
}
mark += ChessMark.MarkOfChessArea(StepChess, player);

12

// Tim cac o duong cheo phu
for (int j = 0; j < 11; j++) {
StepChess[j] = ((p.x + stepValue[10 - j] < rows && p.x + stepValue[10 - j]
>= 0) && (p.y + stepValue[j] < cols && p.y + stepValue[j] >= 0) ? board[p.x +
stepValue[10 - j]][p.y + stepValue[j]] : -1);
}
mark += ChessMark.MarkOfChessArea(StepChess, player);

// Tinh toan nuoc doi
if (mark >= 2 * ChessMark.MARK_2 && mark < ChessMark.MARK_3) {
mark = ChessMark.MARK_3 + 4 * ChessMark.MARK_1;
} else if (mark > (ChessMark.MARK_3 + ChessMark.MARK_2) && mark <
ChessMark.WIN_MARK) {
mark = ChessMark.WIN_MARK;
}
return mark;
}

-public Point getPointMaxMark(int diff, int defendOrAttack): Chn c im ln
nht

public Point getPointMaxMark(int diff, int defendOrAttack) {
if (number_step == 0) {
return new Point(cols / 2, rows / 2);
}
int pos = selectPoint(diff, defendOrAttack);
return listAreaNonTicked[pos];
//return null;
}
-public float getAttackDefend(Point point, int player): Tnh t l im ca my v
t l im ca ngi chi nunh th vo point mt qun player.

public float getAttackDefend(Point point, int player) {
int decreaseMark = 0;
int increaseMark = 0;
board[point.x][point.y] = player;
// Tim cac o duong doc
for (int j = 0; j < 11; j++) {
if ((point.x + stepValue[j] < rows) && (point.x + stepValue[j] >= 0) &&
board[point.x + stepValue[j]][point.y] == 0) {
13

Point p = new Point(point.x + stepValue[j], point.y);
increaseMark = increaseMark + getMarkOfPointByPlayer(p, 1) -
MarkNonTickedOfOnePlayer[p.x][p.y];
decreaseMark = decreaseMark + MarkNonTickedOfTwoPlayer[p.x][p.y]
- getMarkOfPointByPlayer(p, 2);
}
}

// Tim cac o duong ngang
for (int j = 0; j < 11; j++) {
if ((point.y + stepValue[j] < cols) && (point.y + stepValue[j] >= 0) &&
board[point.x][point.y + stepValue[j]] == 0) {
Point p = new Point(point.x, point.y + stepValue[j]);
increaseMark = increaseMark + getMarkOfPointByPlayer(p, 1) -
MarkNonTickedOfOnePlayer[p.x][p.y];
decreaseMark = decreaseMark + MarkNonTickedOfTwoPlayer[p.x][p.y]
- getMarkOfPointByPlayer(p, 2);
}
}

// Tim cac o duong cheo chinh
for (int j = 0; j < 11; j++) {
if (point.x + stepValue[j] < rows && point.x + stepValue[j] >= 0
&& point.y + stepValue[j] < cols && point.y + stepValue[j] >= 0
&& board[point.x + stepValue[j]][point.y + stepValue[j]] == 0) {
Point p = new Point(point.x + stepValue[j], point.y + stepValue[j]);
increaseMark = increaseMark + getMarkOfPointByPlayer(p, 1) -
MarkNonTickedOfOnePlayer[p.x][p.y];
decreaseMark = decreaseMark + MarkNonTickedOfTwoPlayer[p.x][p.y]
- getMarkOfPointByPlayer(p, 2);
}
}

// Tim cac o duong cheo phu
for (int j = 0; j < 11; j++) {
if (point.x + stepValue[10 - j] < rows && point.x + stepValue[10 - j] >= 0
&& point.y + stepValue[j] < cols && point.y + stepValue[j] >= 0
&& board[point.x + stepValue[10 - j]][point.y + stepValue[j]] == 0) {
Point p = new Point(point.x + stepValue[10 - j], point.y + stepValue[j]);
14

increaseMark = increaseMark + getMarkOfPointByPlayer(p, 1) -
MarkNonTickedOfOnePlayer[p.x][p.y];
decreaseMark = decreaseMark + MarkNonTickedOfTwoPlayer[p.x][p.y]
- getMarkOfPointByPlayer(p, 2);
}
}
board[point.x][point.y] = 0;
return (increaseMark + 1) / (decreaseMark + 1);
}

-public int selectPoint(int diff, int defendOrAttack): Chn mt (pos) trong s cc
trng nh, ny tha mn gii thut Minimax.
public int selectPoint(int diff, int defendOrAttack) {
int max1 = -1;
int max2 = -1;
for (int i = 0; i < numberNonTicked; i++) {
Point p = listAreaNonTicked[i];

if (max1 == -1 || MarkNonTickedOfOnePlayer[p.x][p.y] >
MarkNonTickedOfOnePlayer[listAreaNonTicked[max1].x][listAreaNonTicked[m
ax1].y]) {
max1 = i;
}

if (max2 == -1 || MarkNonTickedOfTwoPlayer[p.x][p.y] >
MarkNonTickedOfTwoPlayer[listAreaNonTicked[max2].x][listAreaNonTicked[m
ax2].y]) {
max2 = i;
}
}
Point p1 = listAreaNonTicked[max1];
Point p2 = listAreaNonTicked[max2];
if (MarkNonTickedOfOnePlayer[p1.x][p1.y] >=
MarkNonTickedOfTwoPlayer[p2.x][p2.y]
&& MarkNonTickedOfOnePlayer[p1.x][p1.y] >=
ChessMark.WIN_MARK) {
return max1;
}
if (MarkNonTickedOfOnePlayer[p1.x][p1.y] <
MarkNonTickedOfTwoPlayer[p2.x][p2.y]
15

&& MarkNonTickedOfTwoPlayer[p2.x][p2.y] >=
ChessMark.WIN_MARK) {
// Tim nuoc ma co kha nang chan duoc nuoc tan cong cua doi phuong
int max = MarkNonTickedOfTwoPlayer[p2.x][p2.y];

return getPosition(max);
}
return getNormalPos(diff, defendOrAttack);
}


-public int getNormalPos(int diff, int DefendOrAttack): nh nci nuim cc
pos bnh thng
public int getNormalPos(int diff, int DefendOrAttack) {
int[] candidates = new int[numberNonTicked];
int i, j;
int temp;
Point p1, p2;
boolean isAttack = true;
for (i = 0; i < numberNonTicked; i++) {
candidates[i] = i;
}
if (DefendOrAttack > 2) {
// Lay phong thu
isAttack = false;
for (i = 0; i < numberNonTicked - 1; i++) {
p1 = listAreaNonTicked[i];
for (j = i + 1; j < numberNonTicked; j++) {
p2 = listAreaNonTicked[j];
if (MarkNonTickedOfTwoPlayer[p1.x][p1.y] <
MarkNonTickedOfTwoPlayer[p2.x][p2.y]) {
temp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = temp;
}
}
}
} else {
// Lay tan cong
isAttack = true;
16

for (i = 0; i < numberNonTicked - 1; i++) {
p1 = listAreaNonTicked[i];
for (j = i + 1; j < numberNonTicked; j++) {
p2 = listAreaNonTicked[j];
if (MarkNonTickedOfOnePlayer[p1.x][p1.y] <
MarkNonTickedOfOnePlayer[p2.x][p2.y]) {
temp = candidates[i];
candidates[i] = candidates[j];
candidates[j] = temp;
}
}
}
}

// Lay do kho theo khoang
int level;
switch (diff) {
case 0:
case 1:
case 2:
level = 0;
break;
case 3:
case 4:
level = 1;
break;
case 5:
case 6:
level = 2;
break;
default:
level = 2;
break;
}
// Tim muc diem tuong ung voi level
int max = -1;

for (i = 0; i < numberNonTicked; i++) {
p1 = listAreaNonTicked[candidates[i]];
if (isAttack && max < MarkNonTickedOfOnePlayer[p1.x][p1.y]) {
17

max = MarkNonTickedOfOnePlayer[p1.x][p1.y];
level--;
}
if (!isAttack && max < MarkNonTickedOfTwoPlayer[p1.x][p1.y]) {
max = MarkNonTickedOfTwoPlayer[p1.x][p1.y];
level--;
}
if (level < 0) {
break;
}
}
if (isAttack) {
return getPostOnePlayer(max, diff, DefendOrAttack);
} else {
return getPostOfTwoPlayer(max, diff, DefendOrAttack);
}
}

-public int getPostOnePlayer(int max, int diff, int defendOrAttack): chn nci
th c tn cng
public int getPostOnePlayer(int max, int diff, int defendOrAttack) {
float min = 1000000;
int pos = 0;
int[] postions = new int[numberNonTicked];
int counter = 0;
int playercounter;
int spaces = -1;
for (int i = 0; i < numberNonTicked; i++) {
Point p = listAreaNonTicked[i];
if (MarkNonTickedOfOnePlayer[p.x][p.y] == max) {
float temp = getAttackDefend(p, 1);
if (temp < min) {
min = temp;
pos = i;
counter = 0;
postions[counter++] = i;
} else if (temp == min) {
postions[counter++] = i;
}
}
18

}
// Xet ve khong gian 0 + 2 - 1
// Lay max ve khong gian
if (diff % 2 == 0) {
return postions[0];
}
for (int i = 0; i < counter; i++) {
Point p = listAreaNonTicked[postions[i]];
// Xet khong gian
playercounter = 0;
for (int j = 0; j < 8; j++) {
if (board[p.x + dx[j]][p.y + dy[j]] == 2) {
playercounter++;
}
}
if (spaces < (8 - playercounter)) {
spaces = 8 - playercounter;
pos = postions[i];
}
}
return pos;
}

-public int getPostOfTwoPlayer(int max, int diff, int defendOrAttack):Chn nci
th c phng th
public int getPostOfTwoPlayer(int max, int diff, int defendOrAttack) {
float min = 1000000;
int pos = 0;
int[] postions = new int[numberNonTicked];
int counter = 0;
int playercounter;
int spaces = -1;
for (int i = 0; i < numberNonTicked; i++) {
Point p = listAreaNonTicked[i];
if (MarkNonTickedOfTwoPlayer[p.x][p.y] == max) {
float temp = getAttackDefend(p, 1);
if (temp < min) {
min = temp;
pos = i;
counter = 0;
19

postions[counter++] = i;
} else if (temp == min) {
postions[counter++] = i;
}
}
}
// Xet ve khong gian 0 + 2 - 1
// Lay max ve khong gian
if (diff % 2 == 0) {
return postions[0];
}
for (int i = 0; i < counter; i++) {
Point p = listAreaNonTicked[postions[i]];
// Xet khong gian
playercounter = 0;
for (int j = 0; j < 8; j++) {
if (board[p.x + dx[j]][p.y + dy[j]] == 1) {
playercounter++;
}
}
if (spaces < (8 - playercounter)) {
spaces = 8 - playercounter;
pos = postions[i];
}
}
return pos;
}

-public int getPosition(int max): chn nci chn ngi chi khng thng
c
public int getPosition(int max) {
float min = 1000000;
int pos = 0;
int[] postions = new int[numberNonTicked];
int counter = 0;
int playercounter;
int spaces = -1;
for (int i = 0; i < numberNonTicked; i++) {
Point p = listAreaNonTicked[i];
if (MarkNonTickedOfTwoPlayer[p.x][p.y] == max) {
20

float temp = getAttackDefend(p, 1);
if (temp < min) {
min = temp;
pos = i;
counter = 0;
postions[counter++] = i;
} else if (temp == min) {
postions[counter++] = i;
}
}
}
// Xet ve khong gian 0 + 2 - 1
// Lay max ve khong gian
for (int i = 0; i < counter; i++) {
Point p = listAreaNonTicked[postions[i]];
// Xet khong gian
playercounter = 0;
for (int j = 0; j < 8; j++) {
if (board[p.x + dx[j]][p.y + dy[j]] == 1) {
playercounter++;
}
}
if (spaces < (8 - playercounter)) {
spaces = 8 - playercounter;
pos = postions[i];
}
}
return pos;
}

-public int checkHasWiner(Point p): kim tra xem ai ginh phn thng
public int checkHasWiner(Point p) {
if (number_step < 9) {
return 0;
}
int player = board[p.x][p.y];

// Check hang ngang
if (board[p.x + 1][p.y] == player && board[p.x + 2][p.y] == player &&
board[p.x + 3][p.y] == player
21

&& board[p.x + 4][p.y] == player) {
return player;
}
if (board[p.x + 1][p.y] == player && board[p.x + 2][p.y] == player &&
board[p.x + 3][p.y] == player
&& board[p.x - 1][p.y] == player) {
return player;
}
if (board[p.x + 1][p.y] == player && board[p.x + 2][p.y] == player &&
board[p.x - 1][p.y] == player
&& board[p.x - 2][p.y] == player) {
return player;
}
if (board[p.x + 1][p.y] == player && board[p.x - 1][p.y] == player &&
board[p.x - 2][p.y] == player
&& board[p.x - 3][p.y] == player) {
return player;
}
if (board[p.x - 1][p.y] == player && board[p.x - 2][p.y] == player &&
board[p.x - 3][p.y] == player
&& board[p.x - 4][p.y] == player) {
return player;
}

// Check hang doc
if (board[p.x][p.y + 1] == player && board[p.x][p.y + 2] == player &&
board[p.x][p.y + 3] == player
&& board[p.x][p.y + 4] == player) {
return player;
}
if (board[p.x][p.y + 1] == player && board[p.x][p.y + 2] == player &&
board[p.x][p.y + 3] == player
&& board[p.x][p.y - 1] == player) {
return player;
}
if (board[p.x][p.y + 1] == player && board[p.x][p.y + 2] == player &&
board[p.x][p.y - 1] == player
&& board[p.x][p.y - 2] == player) {
return player;
}
22

if (board[p.x][p.y + 1] == player && board[p.x][p.y - 1] == player &&
board[p.x][p.y - 2] == player
&& board[p.x][p.y - 3] == player) {
return player;
}
if (board[p.x][p.y - 1] == player && board[p.x][p.y - 2] == player &&
board[p.x][p.y - 3] == player
&& board[p.x][p.y - 4] == player) {
return player;
}
// Check duong cheo
if (board[p.x + 1][p.y + 1] == player && board[p.x + 2][p.y + 2] == player
&& board[p.x + 3][p.y + 3] == player
&& board[p.x + 4][p.y + 4] == player) {
return player;
}
if (board[p.x + 1][p.y + 1] == player && board[p.x + 2][p.y + 2] == player
&& board[p.x + 3][p.y + 3] == player
&& board[p.x - 1][p.y - 1] == player) {
return player;
}
if (board[p.x + 1][p.y + 1] == player && board[p.x + 2][p.y + 2] == player
&& board[p.x - 1][p.y - 1] == player
&& board[p.x - 2][p.y - 2] == player) {
return player;
}
if (board[p.x + 1][p.y + 1] == player && board[p.x - 1][p.y - 1] == player &&
board[p.x - 2][p.y - 2] == player
&& board[p.x - 3][p.y - 3] == player) {
return player;
}
if (board[p.x - 1][p.y - 1] == player && board[p.x - 2][p.y - 2] == player &&
board[p.x - 3][p.y - 3] == player
&& board[p.x - 4][p.y - 4] == player) {
return player;
}

// Check duong cheo phu

23

if (board[p.x + 1][p.y - 1] == player && board[p.x + 2][p.y - 2] == player &&
board[p.x + 3][p.y - 3] == player
&& board[p.x + 4][p.y - 4] == player) {
return player;
}
if (board[p.x + 1][p.y - 1] == player && board[p.x + 2][p.y - 2] == player &&
board[p.x + 3][p.y - 3] == player
&& board[p.x - 1][p.y + 1] == player) {
return player;
}
if (board[p.x + 1][p.y - 1] == player && board[p.x + 2][p.y - 2] == player &&
board[p.x - 1][p.y + 1] == player
&& board[p.x - 2][p.y + 2] == player) {
return player;
}
if (board[p.x + 1][p.y - 1] == player && board[p.x - 1][p.y + 1] == player &&
board[p.x - 2][p.y + 2] == player
&& board[p.x - 3][p.y + 3] == player) {
return player;
}
if (board[p.x - 1][p.y + 1] == player && board[p.x - 2][p.y + 2] == player &&
board[p.x - 3][p.y + 3] == player
&& board[p.x - 4][p.y + 4] == player) {
return player;
}

return 0;
}
}

Lp Chessmark

Danh sch cch th c dng tnh im c goi trong mt
s th tc ca class CaroBoard
-public static int MarkOfChessArea(int[] arr, int player): tnhim ca, tng
ng vi th c
public static int MarkOfChessArea(int[] arr, int player) {
int j;
int i;
for (i = 0; i < thecos.length; i++) {
24

int[] theco = thecos[i];
int pos = 5 - circle[i];
int lengOfTheco = theco.length;
for (j = 0; j < lengOfTheco; j++) {
if (arr[j + pos] != player * theco[j]) {
break;
}
}
if (j >= lengOfTheco) {
return mark[i];
}

//
pos = 5 + circle[i];
for (j = 0; j < lengOfTheco; j++) {
if (arr[pos - j] != player * theco[j]) {
break;
}
}
if (j >= lengOfTheco) {
return mark[i];
}
}
return 0;
}
}

Lp player
Class Player : tng trng cho mt my tnh vi cc thng s
nhm thc hin nc i, thao tc vi ngi s dng.
-public Player(int pDiff, int pAttackOrDepend, int width, int heigth)
public Player(int pDiff, int pAttackOrDepend, int width, int heigth)
{
this.difficult = pDiff;
this.attackOrDepend = pAttackOrDepend;
caroTable = new CaroBoard(width, heigth);
}
Lp point

Biu din 1 im nh trong bn c caro
25


4. Giao din v cch chi








Trc khi bt u chi ta thit lp cc thng s v:
kh: Chn kh ca my
Cng th: Bn mun my nh th cng hay th th? Nhp 0 my nh
th 0 v 1 my nh th th

Sau nu bn mun nh trc th bn chn To ngi chi v bt u chi,
nu mun my nh trc th sau khi chn To ngi chi bn chn My i trc.




26



Cc hp thoi hin ln khi chi:
Khi bn thua


Khi bn nh vo nc nh ri

Khi bn thng:





Khi bn thng hoc thua, bn chn To ngi chi chi li hoc Exit thot
game!




27

KT LUN
Qua mn hc v trong qu trnh tm hiu thc hin ti ny, nhm em
c ci nhn ton din hn trong vic ng dng tr tu nhn to vo gii quyt
vn trong thc t. C caro l mt tr chi ng dng tt thut ton minimax v
gii thut alpha-beta. Tuy nhin trong qu trinh thc thi chng trnh khng th
trnh khi nhng sai st v cha thc s ti u. Chng em mong c s gp
ca thy c th hon thin hn trong tng lai!
Chng em xin chn thnh cm n!

You might also like