// Kiem tra tren duong cheo xuong.... private void EvalueGomokuBoardPlayer player { int rw, cl, i; int cntHuman, cntMachine;... • // Luong gia cho duong cheo xuong... // Copy mot ban
Trang 5•
•
// Thiet lap lai ban co.
public void ResetBoard()
{
int r, c;
// Thiet lap lai gia tri bang.
for (r = 0; r < Height + 2; r++)//phai cong them 2 neu khong moi nguoi chi di duoc 1 quan
Trang 6// Ve ban co.
public void DrawBoard()
{
Graphics g = Parent.CreateGraphics();// tu khoa hinh ve do hoa
Rectangle rect = new Rectangle();//hinh chu nhat
for (int r = OffsetH; r <= Height + 1; r++)//offsetH=1
for (int c = OffsetW; c <= Width + 1; c++)//offsetW=1
//in ra so dong va so cot
{
rect = new Rectangle((c - OffsetW) * 20, (r - OffsetH) * 20, 20, 20);
if (Board[r, c] == Player.None)// la o bang
Trang 7// Kiem tra van dau co ket thuc chua ?
private Player CheckEnd(int rw, int cl)
{
bool Human, Machine;
int r = 1, c = 1;
int i;
// Kiem tra tren hang
while (c <= Width - 4)//neu khong co se di hoai tren hang ma k thang
Trang 8// Kiem tra tren cot
while (r <= Height - 4)//trong khi chua thang
Trang 9// Kiem tra tren duong cheo xuong.
Trang 10// Kiem tra duong cheo len
r = rw; c = cl;
// Di chuyen den dau duong cheo len
while (r < Height && c > 1) { r++; c ; }
while (r >= 5 && c <= Width - 4)
Trang 11• // Luong gia cho ban co - kinh nghiem cua may.
private void EvalueGomokuBoard(Player player)
{
int rw, cl, i;
int cntHuman, cntMachine;
Trang 12• // Luong gia cho hang.
for (rw = 1; rw <= Height; rw++)
for (cl = 1; cl <= Width-4; cl++)
{
cntHuman = 0; cntMachine = 0; for (i = 0; i < 5; i++)
Trang 14if (cntMachine == 0)
{
if (player == Player.Machine) EBoard.Board[rw, cl + i] += TScore[cntHuman];
else EBoard.Board[rw, cl + i] += KScore[cntHuman];
Trang 15• // Truong hop bi chan 2 dau.
Player.None || Board[rw, cl + i + 1] == Player.None))
EBoard.Board[rw, cl + i] *= 2;
}
}
Trang 16• // Luong gia cho cot.
if (Board[rw+i, cl] == Player.Machine) cntMachine++;
}
Trang 17if (cntMachine == 0)
{
if (player == Player.Machine) EBoard.Board[rw + i, cl] += TScore[cntHuman];
else EBoard.Board[rw + i, cl] += KScore[cntHuman];
Trang 18• // Truong hop bi chan 2 dau.
Trang 19• // Truong hop bi chan 2 dau.
if (Board[rw - 1, cl] == Player.Human && Board[rw + 5, cl]
Trang 20• // Luong gia cho duong cheo xuong.
if (Board[rw + i, cl + i] == Player.Machine) cntMachine++;
}
Trang 22• // Truong hop bi chan 2 dau.
• if (Board[rw - 1, cl - 1] == Player.Machine && Board[rw + 5, cl + 5] == Player.Machine)
Trang 23• // Truong hop bi chan 2 dau.
• if (Board[rw - 1, cl - 1] == Player.Human && Board[rw + 5, cl + 5] == Player.Human)
Trang 24• // Luong gia cho duong cheo len.
if (Board[rw - i, cl + i] == Player.Machine) cntMachine++;
}
Trang 26• // Truong hop bi chan 2 dau.
if (Board[rw + 1, cl - 1] ==
Player.Machine && Board[rw - 5, cl + 5] ==
Player.Machine)
EBoard.Board[rw - i, cl + i] = 0; }
Trang 27• // Truong hop bi chan 2 dau.
if (Board[rw + 1, cl - 1] == Player.Human && Board[rw - 5, cl + 5] == Player.Human)
Trang 28// Ve gia tri luong gia ra bang !
private void DrawEvalue()
{ Graphics g = Parent.CreateGraphics();
Rectangle rect = new Rectangle();
for (int r = 1; r <= Height+1; r++) for (int c = 1; c <= Width+1; c++)
{ rect = new Rectangle((c - 1) * 20, (r - 1) * 20, 20, 20);
Font fnt = new Font(FontFamily.GenericMonospace, 9);
g.DrawString(EBoard.Board[r, c].ToString(), fnt, Brushes.Black, rect);
} }
Trang 29// Copy mot ban co khac.
• public void CopyTo(ref GomokuBoard Br)
Trang 30// Sinh nuoc di - do thong minh cua
may.
• public int Depth = 0;
• static public int MaxDepth = 21;
• static public int MaxBreadth = 8;
public Node[] WinMoves = new
Trang 31// Ham de quy - Sinh nuoc di cho
Trang 32// Luong gia cho ma tran
&// Lay nuoc di ra khoi danh sach - Danh thu
nuoc di.
• EvalueGomokuBoard(Player.Machine);
•
• // Lay MaxBreadth nuoc di tot nhat.
• for (int i = 1; i <= MaxBreadth; i++)
Trang 33// Tim cac nuoc di toi uu cua doi
Trang 34// Kiem tra ket qua nuoc di.
• if (CheckEnd(MyNode.Row, MyNode.Column) == Player.Machine)
• Board[HisNode.Row, HisNode.Column] = Player.None;
• Board[MyNode.Row, MyNode.Column] = Player.None;
• return;
• }
• if (Win)
• {
Trang 35// Loai nuoc di thu.
• Board[HisNode.Row, HisNode.Column] = Player.None;
• Board[MyNode.Row, MyNode.Column] =
Player.None;
• return;
• }
• else GenerateMoves(); // tim tiep.
• // Loai nuoc di thu.
• Board[HisNode.Row, HisNode.Column] = Player.None;
Trang 36// Goi Generator - Tim duong di cho may.
• public void GetGenResult()
• {
• Win = Lose = false;
• // Xoa mang duong di.
• WinMoves = new Node[MaxDepth + 1];
• for (int i = 0; i <= MaxDepth; i++)
• WinMoves[i] = new Node();
•
• // Xoa stack.
• for (int i = 0; i < MaxBreadth; i++)
• MyMoves[i] = new Node();