public void MangOCo() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { mang[i, j] = new Oco(i, j, new Point(j * 100, i * 100), 0); } } }
public void MayDanh(Graphics g) { Random x = new Random(); int a = x.Next(1, 4); if (DanhsachCoDaDanh.Count() == 0) { if (a == 1) { VeQuanCoO(g, mang[0, 0].Vitri, mauxanh); mang[0, 0].Sohuu = 1; DanhsachCoDaDanh.Add(mang[0, 0]); } else if (a == 2) { VeQuanCoO(g, mang[1, 2].Vitri, mauxanh); mang[1, 2].Sohuu = 1; DanhsachCoDaDanh.Add(mang[1, 2]); } else { VeQuanCoO(g, mang[1, 1].Vitri, mauxanh); mang[1, 1].Sohuu = 1; DanhsachCoDaDanh.Add(mang[1, 1]); } } else { if (DanhsachCoDaDanh.Count() % 2 != 0) { Oco oco = TimNuocDi(); VeQuanCoO(g, mang[oco.Dong, oco.Cot].Vitri, mauxanh); mang[oco.Dong, oco.Cot].Sohuu = 1; DanhsachCoDaDanh.Add(mang[oco.Dong, oco.Cot]); } else { Oco oco = TimNuocDiMayDiTruoc(); VeQuanCoO(g, mang[oco.Dong, oco.Cot].Vitri, mauxanh); mang[oco.Dong, oco.Cot].Sohuu = 1; DanhsachCoDaDanh.Add(mang[oco.Dong, oco.Cot]); } } }
public Oco TimNuocDi() { int n = DanhsachCoDaDanh.Count(); int[] MangDiemF = new int[9 - n]; Oco[] MangOcoF = new Oco[9 - n]; int HamF = 0; int k = 0; bool BatBuoc = false; Oco OcoToiUu = new Oco(); Oco OcoBatBuoc = new Oco(); if (mang[0, 0].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[2, 2].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(0, 1, mang[0, 1].Vitri, mang[0, 1].Sohuu)); } if (mang[0, 2].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[2, 0].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(0, 1, mang[0, 1].Vitri, mang[0, 1].Sohuu)); } if (mang[0, 2].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[2, 1].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(2, 0, mang[2, 0].Vitri, mang[2, 0].Sohuu)); } if (mang[0, 0].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[2, 1].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(2, 0, mang[2, 0].Vitri, mang[2, 0].Sohuu)); } if (mang[2, 0].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[2, 1].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(2, 0, mang[2, 0].Vitri, mang[2, 0].Sohuu)); } if (mang[2, 0].Sohuu == 2 && mang[1, 1].Sohuu == 1 && mang[1, 2].Sohuu == 2 & DanhsachCoDaDanh.Count() == 3) { return(new Oco(0, 2, mang[0, 2].Vitri, mang[0, 2].Sohuu)); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (mang[i, j].Sohuu != 1 && mang[i, j].Sohuu != 2) { mang[i, j].Sohuu = 1; //Bắt Buộc Phải đi ô cờ thắng if (mang[i, 0].Sohuu == 1) { if (DuyetNgang(i, 0, 1)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, j].Sohuu == 1) { if (DuyetDoc(0, j, 1)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, 0].Sohuu == 1) { if (DuyetDuongCheoChinh(0, 0, 1)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, 2].Sohuu == 1) { if (DuyetDuongCheoPhu(0, 2, 1)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } mang[i, j].Sohuu = 2; if (mang[i, 0].Sohuu == 2) { if (DuyetNgang(i, 0, 2)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, j].Sohuu == 2) { if (DuyetDoc(0, j, 2)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, 0].Sohuu == 2) { if (DuyetDuongCheoChinh(0, 0, 2)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } if (mang[0, 2].Sohuu == 2) { if (DuyetDuongCheoPhu(0, 2, 2)) { OcoBatBuoc = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); BatBuoc = true; break; } } mang[i, j].Sohuu = 0; // thủ tục min max int diemmax = OcoMoNgang1() + OcoMoDoc1() + OcoCheoChinh1() + OcoCheoPhu1(); mang[i, j].Sohuu = 1; DanhsachCoDaDanh.Add(mang[i, j]); int diemmin = OcoMoNgang2() + OcoMoDoc2() + OcoCheoChinh2() + OcoCheoPhu2(); HamF = diemmax - diemmin; mang[i, j].Sohuu = 0; DanhsachCoDaDanh.Remove(mang[i, j]); MangDiemF[k] = HamF; MangOcoF[k] = new Oco(mang[i, j].Dong, mang[i, j].Cot, mang[i, j].Vitri, mang[i, j].Sohuu); // k++; } } if (BatBuoc) { break; } } if (BatBuoc) { OcoToiUu = OcoBatBuoc; } else { int vitri = TimMax(MangDiemF); OcoToiUu = MangOcoF[vitri]; } return(OcoToiUu); }