//Ham tinh gia tri cho bang luong gia private void EvalChessBoard(int player, ref EvalBoard eBoard) { int rw, cl, ePC, eHuman; eBoard.ResetBoard(); //Danh gia theo hang for (rw = 0; rw < graph.Row; rw++) { for (cl = 0; cl < graph.Col - 4; cl++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw, cl + i] == 1) { eHuman++; } if (BoardArr[rw, cl + i] == 2) { ePC++; } } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) { if (player == 1) { eBoard.EBoard[rw, cl + i] += DScore[ePC]; } else { eBoard.EBoard[rw, cl + i] += AScore[ePC]; } } if (ePC == 0) { if (player == 2) { eBoard.EBoard[rw, cl + i] += DScore[eHuman]; } else { eBoard.EBoard[rw, cl + i] += AScore[eHuman]; } } if (eHuman == 4 || ePC == 4) { eBoard.EBoard[rw, cl + i] *= 2; } } } } } } //Danh gia theo cot for (cl = 0; cl < graph.Col; cl++) { for (rw = 0; rw < graph.Row - 4; rw++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl] == 1) { eHuman++; } if (BoardArr[rw + i, cl] == 2) { ePC++; } } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl] == 0) // Neu o chua duoc danh { if (eHuman == 0) { if (player == 1) { eBoard.EBoard[rw + i, cl] += DScore[ePC]; } else { eBoard.EBoard[rw + i, cl] += AScore[ePC]; } } if (ePC == 0) { if (player == 2) { eBoard.EBoard[rw + i, cl] += DScore[eHuman]; } else { eBoard.EBoard[rw + i, cl] += AScore[eHuman]; } } if (eHuman == 4 || ePC == 4) { eBoard.EBoard[rw + i, cl] *= 2; } } } } } } //Danh gia duong cheo xuong for (cl = 0; cl < graph.Col - 4; cl++) { for (rw = 0; rw < graph.Row - 4; rw++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl + i] == 1) { eHuman++; } if (BoardArr[rw + i, cl + i] == 2) { ePC++; } } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) { if (player == 1) { eBoard.EBoard[rw + i, cl + i] += DScore[ePC]; } else { eBoard.EBoard[rw + i, cl + i] += AScore[ePC]; } } if (ePC == 0) { if (player == 2) { eBoard.EBoard[rw + i, cl + i] += DScore[eHuman]; } else { eBoard.EBoard[rw + i, cl + i] += AScore[eHuman]; } } if (eHuman == 4 || ePC == 4) { eBoard.EBoard[rw + i, cl + i] *= 2; } } } } } } //Danh gia duong cheo len for (rw = 4; rw < graph.Row; rw++) { for (cl = 0; cl < graph.Col - 4; cl++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw - i, cl + i] == 1) { eHuman++; } if (BoardArr[rw - i, cl + i] == 2) { ePC++; } } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw - i, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) { if (player == 1) { eBoard.EBoard[rw - i, cl + i] += DScore[ePC]; } else { eBoard.EBoard[rw - i, cl + i] += AScore[ePC]; } } if (ePC == 0) { if (player == 2) { eBoard.EBoard[rw - i, cl + i] += DScore[eHuman]; } else { eBoard.EBoard[rw - i, cl + i] += AScore[eHuman]; } } if (eHuman == 4 || ePC == 4) { eBoard.EBoard[rw - i, cl + i] *= 2; } } } } } } }
//Ham tim nuoc di cho may private void FindMove() { if (depth > maxDepth) { return; } depth++; fWin = false; bool fLose = false; Point pcMove = new Point(); Point humanMove = new Point(); int countMove = 0; EvalChessBoard(2, ref eBoard); //Lay ra MaxMove buoc di co diem cao nhat Point temp = new Point(); for (int i = 0; i < maxMove; i++) { temp = eBoard.MaxPos(); PCMove[i] = temp; eBoard.EBoard[temp.X, temp.Y] = 0; } //Lay nuoc di trong PCMove[] ra danh thu countMove = 0; while (countMove < maxMove) { pcMove = PCMove[countMove++]; BoardArr[pcMove.X, pcMove.Y] = 2; WinMove.SetValue(pcMove, depth - 1); //Tim cac nuoc di toi uu cua nguoi eBoard.ResetBoard(); EvalChessBoard(1, ref eBoard); //Lay ra maxMove nuoc di co diem cao nhat cua nguoi for (int i = 0; i < maxMove; i++) { temp = eBoard.MaxPos(); HumanMove[i] = temp; eBoard.EBoard[temp.X, temp.Y] = 0; } //Danh thu cac nuoc di for (int i = 0; i < maxMove; i++) { humanMove = HumanMove[i]; BoardArr[humanMove.X, humanMove.Y] = 1; if (CheckEnd(humanMove.X, humanMove.Y) == 2) { fWin = true; //MessageBox.Show("fwin" + fWin.ToString()); } if (CheckEnd(humanMove.X, humanMove.Y) == 1) { fLose = true; //MessageBox.Show("flose" + fLose.ToString()); } if (fLose) { BoardArr[pcMove.X, pcMove.Y] = 0; BoardArr[humanMove.X, humanMove.Y] = 0; break; } if (fWin) { BoardArr[pcMove.X, pcMove.Y] = 0; BoardArr[humanMove.X, humanMove.Y] = 0; return; } FindMove(); BoardArr[humanMove.X, humanMove.Y] = 0; } BoardArr[pcMove.X, pcMove.Y] = 0; } }
//Ham tinh gia tri cho bang luong gia private void EvalChessBoard(int player,ref EvalBoard eBoard) { int rw, cl, ePC, eHuman; eBoard.ResetBoard(); //Danh gia theo hang for (rw = 0; rw < graph.Row; rw++) for (cl = 0; cl < graph.Col - 4; cl++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw, cl + i] == 1) eHuman++; if (BoardArr[rw, cl + i] == 2) ePC++; } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) if (player == 1) eBoard.EBoard[rw, cl + i] += DScore[ePC]; else eBoard.EBoard[rw, cl + i] += AScore[ePC]; if (ePC == 0) if (player == 2) eBoard.EBoard[rw, cl + i] += DScore[eHuman]; else eBoard.EBoard[rw, cl + i] += AScore[eHuman]; if (eHuman == 4 || ePC == 4) eBoard.EBoard[rw, cl + i] *= 2; } } } } //Danh gia theo cot for (cl = 0; cl < graph.Col; cl++) for (rw = 0; rw < graph.Row - 4; rw++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl] == 1) eHuman++; if (BoardArr[rw + i, cl] == 2) ePC++; } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl] == 0) // Neu o chua duoc danh { if (eHuman == 0) if (player == 1) eBoard.EBoard[rw + i, cl] += DScore[ePC]; else eBoard.EBoard[rw + i, cl] += AScore[ePC]; if (ePC == 0) if (player == 2) eBoard.EBoard[rw + i, cl] += DScore[eHuman]; else eBoard.EBoard[rw + i, cl] += AScore[eHuman]; if (eHuman == 4 || ePC == 4) eBoard.EBoard[rw + i, cl] *= 2; } } } } //Danh gia duong cheo xuong for (cl = 0; cl < graph.Col - 4; cl++) for (rw = 0; rw < graph.Row - 4; rw++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl + i] == 1) eHuman++; if (BoardArr[rw + i, cl + i] == 2) ePC++; } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw + i, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) if (player == 1) eBoard.EBoard[rw + i, cl + i] += DScore[ePC]; else eBoard.EBoard[rw + i, cl + i] += AScore[ePC]; if (ePC == 0) if (player == 2) eBoard.EBoard[rw + i, cl + i] += DScore[eHuman]; else eBoard.EBoard[rw + i, cl + i] += AScore[eHuman]; if (eHuman == 4 || ePC == 4) eBoard.EBoard[rw + i, cl + i] *= 2; } } } } //Danh gia duong cheo len for (rw = 4; rw < graph.Row; rw++) for (cl = 0; cl < graph.Col - 4; cl++) { ePC = 0; eHuman = 0; for (int i = 0; i < 5; i++) { if (BoardArr[rw - i, cl + i] == 1) eHuman++; if (BoardArr[rw - i, cl + i] == 2) ePC++; } if (eHuman * ePC == 0 && eHuman != ePC) { for (int i = 0; i < 5; i++) { if (BoardArr[rw - i, cl + i] == 0) // Neu o chua duoc danh { if (eHuman == 0) if (player == 1) eBoard.EBoard[rw - i, cl + i] += DScore[ePC]; else eBoard.EBoard[rw - i, cl + i] += AScore[ePC]; if (ePC == 0) if (player == 2) eBoard.EBoard[rw - i, cl + i] += DScore[eHuman]; else eBoard.EBoard[rw - i, cl + i] += AScore[eHuman]; if (eHuman == 4 || ePC == 4) eBoard.EBoard[rw - i, cl + i] *= 2; } } } } }