/// <summary> /// Bắt đầu game mới /// </summary> /// <param name="playerFirst">Người đi trước</param> /// <param name="playerSymbol">Quân người chơi (x-o)</param> /// <param name="conputerAI">Độ sâu khi máy tính toán</param> /// public void NewGame1(bool playerFirst, char player2, bool human) { if (!GameOver) { if (MessageBox.Show("You lose this round?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { return; } ComputerScore += 1; UpdateMessage(); } step = new List <Step>(); PlayerSymbol = player2 == 'x' ? 'x' : 'o'; _board = new CaroBoard(19, zingLaw, human); _board.PrevMove.Set(-1, -1); win = new int[5]; CurrIndex = -1; t = 0; timer2.Start(); GameOver = false; Invalidate(); if (playerFirst) { _board.XPlaying = player2 == 'x' ? true : false; _board.CurrMove.Set(-1, -1); } else { _board.XPlaying = player2 == 'x' ? false : true; _board.CurrMove.Set(_board.size / 2, _board.size / 2); _board.cells[_board.size / 2, _board.size / 2] = player2 == 'x' ? 'o' : 'x'; SwithchPlayer1(); } }
public void LoadGame(string FileName) { GameOver = false; GameData.Rows.Clear(); GameData.ReadXml(FileName); step = (List <Step>)GameData.Rows[0]["Step"]; t = (int)GameData.Rows[0]["PlayedTime"]; PlayerScore = (int)GameData.Rows[0]["PlayerScore"]; ComputerScore = (int)GameData.Rows[0]["ComputerScore"]; char playerSymbol = (char)GameData.Rows[0]["PlayerSymbol"]; CurrIndex = step.Count - 1; _board = new CaroBoard(19, zingLaw, false); for (int i = 0; i < step.Count; i++) { _board.cells[step[i].p.x, step[i].p.y] = step[i].CurrentPlayer == playerSymbol ? PlayerSymbol : ComputerSymbol; } _board.XPlaying = (_board.cells[step[CurrIndex].p.x, step[CurrIndex].p.y] == 'o'); _board.CurrMove.Set(step[CurrIndex].p); CaroScore.Text = PlayerScore + ":" + ComputerScore; timer2.Start(); UpdateMessage(); Invalidate(); }
private int MinVal(ref CaroBoard b, State s, int alpha, int beta, int depth) { int val = Eval(ref b); if (depth >= maxdepth || Math.Abs(val) > 3000) { return(val); } EvalueCaroBoard(ref b, _player); List <State> list = new List <State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw && list[i].val > 1538) { break; } } for (int i = 0; i < list.Count; i++) { b.cells[list[i].p.x, list[i].p.y] = _player; beta = Math.Min(beta, MaxVal(ref b, list[i], alpha, beta, depth + 1)); b.cells[list[i].p.x, list[i].p.y] = ' '; if (alpha >= beta) { break; } } return(beta); }
public Position Solve(ref CaroBoard bb, char Player) { currp.Set(-1, -1); prevp.Set(-1, -1); bool F = false; CaroBoard b = new CaroBoard(bb.size, zingLaw, F); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { b.cells[i, j] = bb.cells[i, j]; } } //Console.WriteLine("Current P={0}", Eval(ref b)); computer = Player; EvalueCaroBoard(ref b, _computer); //EchoVal(); List <State> list = new List <State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw && list[i].val > 1538) { break; } } int maxp = -INT_MAX; List <State> ListChoose = new List <State>(); for (int i = 0; i < list.Count; i++) { currp.Set(list[i].p); b.cells[list[i].p.x, list[i].p.y] = _computer; int t = MinVal(ref b, list[i], -INT_MAX, INT_MAX, 0); //Console.WriteLine("{0}-{1}: {2}", list[i].p.x, list[i].p.y, t);//list[i].val); if (maxp < t) { maxp = t; ListChoose.Clear(); ListChoose.Add(list[i]); } else if (maxp == t) { ListChoose.Add(list[i]); } b.cells[list[i].p.x, list[i].p.y] = ' '; } int x = rand.Next(0, ListChoose.Count); //Console.Write("i={0};",x); return(ListChoose[x].p); }
private int NegaScout(ref CaroBoard bb, State s, int alpha, int beta, int depth) { int val = Eval(ref bb); if (depth >= maxdepth || Math.Abs(val) > 3000) { return(val); } int b = beta, a; if (depth % 2 == 0) { EvalueCaroBoard(ref bb, _player); } else { EvalueCaroBoard(ref bb, _computer); } List <State> list = new List <State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw && list[i].val > 1538) { break; } } for (int i = 0; i < list.Count; i++) { if (depth % 2 == 0) { bb.cells[list[i].p.x, list[i].p.y] = _computer; } else { bb.cells[list[i].p.x, list[i].p.y] = _player; } a = -NegaScout(ref bb, list[i], -b, -alpha, depth + 1); if (a > alpha && a < beta && i != 0) { a = -NegaScout(ref bb, list[i], -beta, -alpha, depth + 1); } bb.cells[list[i].p.x, list[i].p.y] = ' '; alpha = Math.Max(a, alpha); if (alpha >= beta) { break; } b = alpha + 1; } return(alpha); }
public CaroBoardUI() { InitializeComponent(); _ImgX = new Bitmap(Properties.Resources.x, CELL_SIZE, CELL_SIZE); _ImgO = new Bitmap(Properties.Resources.o, CELL_SIZE, CELL_SIZE); _ImgThink = new Bitmap(Properties.Resources.think, CELL_SIZE, CELL_SIZE); _board = new CaroBoard(); GameOver = true; this.NewGame(true,'x',3); t = -1; ResetScores(); GameOver = true; GameData = new DataTable("Caro"); GameData.Columns.Add("Step", typeof(List<Step>)); GameData.Columns.Add("PlayerScore", typeof(int)); GameData.Columns.Add("ComputerScore", typeof(int)); GameData.Columns.Add("PlayedTime", typeof(int)); GameData.Columns.Add("PlayerSymbol", typeof(char)); UpdateMessage(); }
public CaroBoardUI() { InitializeComponent(); _ImgX = new Bitmap(Properties.Resources.x, CELL_SIZE, CELL_SIZE); _ImgO = new Bitmap(Properties.Resources.o, CELL_SIZE, CELL_SIZE); _ImgThink = new Bitmap(Properties.Resources.think, CELL_SIZE, CELL_SIZE); _board = new CaroBoard(); GameOver = true; this.NewGame(true, 'x', 3); t = -1; ResetScores(); GameOver = true; GameData = new DataTable("Caro"); GameData.Columns.Add("Step", typeof(List <Step>)); GameData.Columns.Add("PlayerScore", typeof(int)); GameData.Columns.Add("ComputerScore", typeof(int)); GameData.Columns.Add("PlayedTime", typeof(int)); GameData.Columns.Add("PlayerSymbol", typeof(char)); UpdateMessage(); }
public void NewGame(bool playerFirst, char playerSymbol, int computerAI) { zingLaw = false; if (!GameOver) { if (MessageBox.Show("You lose this round?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) { return; } ComputerScore += 1; UpdateMessage(); } ComputerAI = computerAI; step = new List <Step>(); PlayerSymbol = playerSymbol == 'x'?'x':'o'; _board = new CaroBoard(19, zingLaw, false); _board.PrevMove.Set(-1, -1); win = new int[5]; // this.MaximumSize = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); // this.MinimumSize = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); // this.Size = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); CurrIndex = -1; t = 0; timer2.Start(); GameOver = false; Invalidate(); if (playerFirst) { _board.XPlaying = playerSymbol == 'x' ? true : false; _board.CurrMove.Set(-1, -1); } else { _board.XPlaying = playerSymbol == 'x' ? false : true; _board.CurrMove.Set(_board.size / 2, _board.size / 2); _board.cells[_board.size / 2, _board.size / 2] = playerSymbol == 'x'?'o':'x'; SwithchPlayer(); } }
/// <summary> /// Bắt đầu game mới /// </summary> /// <param name="playerFirst">Người đi trước</param> /// <param name="playerSymbol">Quân người chơi (x-o)</param> /// <param name="conputerAI">Độ sâu khi máy tính toán</param> public void NewGame(bool playerFirst, char playerSymbol, int computerAI) { if (!GameOver) { if (MessageBox.Show("You lose this round?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return; ComputerScore += 1; UpdateMessage(); } ComputerAI = computerAI; step = new List<Step>(); PlayerSymbol = playerSymbol=='x'?'x':'o'; _board = new CaroBoard(19); _board.PrevMove.Set(-1, -1); kq = new int[5]; this.MaximumSize = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); this.MinimumSize = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); this.Size = new Size(_board.size * CELL_SIZE + 1, (_board.size + 1) * CELL_SIZE + 1); CurrIndex = -1; t = 0; timer2.Start(); GameOver = false; Invalidate(); if (playerFirst) { _board.XPlaying = playerSymbol == 'x' ? true : false; _board.CurrMove.Set(-1, -1); } else { _board.XPlaying = playerSymbol == 'x' ? false : true; _board.CurrMove.Set(_board.size / 2, _board.size / 2); _board.cells[_board.size / 2, _board.size / 2] = playerSymbol=='x'?'o':'x'; SwithchPlayer(); } }
public void LoadGame(string FileName) { GameOver = false; GameData.Rows.Clear(); GameData.ReadXml(FileName); step = (List<Step>)GameData.Rows[0]["Step"]; t = (int)GameData.Rows[0]["PlayedTime"]; PlayerScore = (int)GameData.Rows[0]["PlayerScore"]; ComputerScore = (int)GameData.Rows[0]["ComputerScore"]; char playerSymbol = (char)GameData.Rows[0]["PlayerSymbol"]; CurrIndex = step.Count - 1; _board = new CaroBoard(19); for (int i = 0; i < step.Count; i++) _board.cells[step[i].p.x, step[i].p.y] = step[i].CurrentPlayer == playerSymbol ? PlayerSymbol : ComputerSymbol; _board.XPlaying = (_board.cells[step[CurrIndex].p.x, step[CurrIndex].p.y] == 'o'); _board.CurrMove.Set(step[CurrIndex].p); CaroScore.Text = PlayerScore + ":" + ComputerScore; timer2.Start(); UpdateMessage(); Invalidate(); }
public void NewGame() { _board = new CaroBoard(15); }
public void EvalueCaroBoard(ref CaroBoard b, char Player) { n = b.size; ResetVal(); int rw, cl, i; int cComputer, cPlayer; //kiem tra hang for (rw = 0; rw < n; rw++) for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw, cl + i] == _computer) cComputer++; if (b.cells[rw, cl + i] == _player) cPlayer++; } if (cComputer * cPlayer == 0 && cComputer != cPlayer) for (i = 0; i < 5; i++) if (b.cells[rw, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) Val[rw, cl + i] += TScore[cPlayer]; else Val[rw, cl + i] += KScore[cPlayer]; if (zingLaw && b.CheckPosition(rw, cl - 1) && b.CheckPosition(rw, cl + 5) && b.cells[rw, cl - 1] == _computer && b.cells[rw, cl + 5] == _computer) Val[rw, cl + i] = 0; } if (cPlayer == 0) { if (Player == _player) Val[rw, cl + i] += TScore[cComputer]; else Val[rw, cl + i] += KScore[cComputer]; if (zingLaw && b.CheckPosition(rw, cl - 1) && b.CheckPosition(rw, cl + 5) && b.cells[rw, cl - 1] == _player && b.cells[rw, cl + 5] == _player) Val[rw, cl + i] = 0; } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw, cl + i - 1) && b.cells[rw, cl + i - 1] == ' ') || (b.CheckPosition(rw, cl + i + 1) && b.cells[rw, cl + i + 1] == ' '))) Val[rw, cl + i] *= 2; else if (cComputer == 4 || cPlayer == 4) Val[rw, cl + i] *= 2; } } //Cot for (rw = 0; rw < n - 4; rw++) for (cl = 0; cl < n; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl] == _computer) cComputer++; if (b.cells[rw + i, cl] == _player) cPlayer++; } if (cComputer * cPlayer == 0 && cComputer != cPlayer) for (i = 0; i < 5; i++) if (b.cells[rw + i, cl] == ' ') { if (cComputer == 0) { if (Player == _computer) Val[rw + i, cl] += TScore[cPlayer]; else Val[rw + i, cl] += KScore[cPlayer]; if (zingLaw && b.CheckPosition(rw - 1, cl) && b.CheckPosition(rw + 5, cl) && b.cells[rw - 1, cl] == _computer && b.cells[rw + 5, cl] == _computer) Val[rw + i, cl] = 0; } if (cPlayer == 0) { if (Player == _player) Val[rw + i, cl] += TScore[cComputer]; else Val[rw + i, cl] += KScore[cComputer]; if (zingLaw && b.CheckPosition(rw - 1, cl) && b.CheckPosition(rw + 5, cl) && b.cells[rw - 1, cl] == _player && b.cells[rw + 5, cl] == _player) Val[rw + i, cl] = 0; } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw + i - 1, cl) && b.cells[rw + i - 1, cl] == ' ') || (b.CheckPosition(rw + i + 1, cl) && b.cells[rw + i + 1, cl] == ' '))) Val[rw + i, cl] *= 2; else if (cComputer == 4 || cPlayer == 4) Val[rw + i, cl] *= 2; } } //Duong cheo xuong for (rw = 0; rw < n - 4; rw++) for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl + i] == _computer) cComputer++; if (b.cells[rw + i, cl + i] == _player) cPlayer++; } //Luong gia.. if (cComputer * cPlayer == 0 && cComputer != cPlayer) for (i = 0; i < 5; i++) if (b.cells[rw + i, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) Val[rw + i, cl + i] += TScore[cPlayer]; else Val[rw + i, cl + i] += KScore[cPlayer]; if (zingLaw && b.CheckPosition(rw - 1, cl - 1) && b.CheckPosition(rw + 5, cl + 5) && b.cells[rw - 1, cl - 1] == _computer && b.cells[rw + 5, cl + 5] == _computer) Val[rw + i, cl + i] = 0; } if (cPlayer == 0) { if (Player == _player) Val[rw + i, cl + i] += TScore[cComputer]; else Val[rw + i, cl + i] += KScore[cComputer]; if (zingLaw && b.CheckPosition(rw - 1, cl - 1) && b.CheckPosition(rw + 5, cl + 5) && b.cells[rw - 1, cl - 1] == _player && b.cells[rw + 5, cl + 5] == _player) Val[rw + i, cl + i] = 0; } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw + i - 1, cl + i - 1) && b.cells[rw + i - 1, cl + i - 1] == ' ') || (b.CheckPosition(rw + i + 1, cl + i + 1) && b.cells[rw + i + 1, cl + i + 1] == ' '))) Val[rw + i, cl + i] *= 2; else if (cComputer == 4 || cPlayer == 4) Val[rw + i, cl + i] *= 2; } } //Duong cheo len for (rw = 4; rw < n; rw++) for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw - i, cl + i] == _computer) cComputer++; if (b.cells[rw - i, cl + i] == _player) cPlayer++; } //Luong gia.. if (cComputer * cPlayer == 0 && cComputer != cPlayer) for (i = 0; i < 5; i++) if (b.cells[rw - i, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) Val[rw - i, cl + i] += TScore[cPlayer]; else Val[rw - i, cl + i] += KScore[cPlayer]; if (zingLaw && b.CheckPosition(rw + 1, cl - 1) && b.CheckPosition(rw - 5, cl + 5) && b.cells[rw + 1, cl - 1] == _computer && b.cells[rw - 5, cl + 5] == _computer) Val[rw - i, cl + i] = 0; } if (cPlayer == 0) { if (Player == _player) Val[rw - i, cl + i] += TScore[cComputer]; else Val[rw - i, cl + i] += KScore[cComputer]; if (zingLaw && b.CheckPosition(rw + 1, cl - 1) && b.CheckPosition(rw - 5, cl + 5) && b.cells[rw + 1, cl - 1] == _player && b.cells[rw - 5, cl + 5] == _player) Val[rw + i, cl + i] = 0; } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw - i + 1, cl + i - 1) && b.cells[rw - i + 1, cl + i - 1] == ' ') || (b.CheckPosition(rw - i - 1, cl + i + 1) && b.cells[rw - i - 1, cl + i + 1] == ' '))) Val[rw - i, cl + i] *= 2; else if (cComputer == 4 || cPlayer == 4) Val[rw - i, cl + i] *= 2; } } //EchoVal(); }
public Position Solve(ref CaroBoard bb, char Player) { currp.Set(-1, -1); prevp.Set(-1, -1); bool F = false; CaroBoard b = new CaroBoard(bb.size,zingLaw,F); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) b.cells[i, j] = bb.cells[i, j]; //Console.WriteLine("Current P={0}", Eval(ref b)); computer = Player; EvalueCaroBoard(ref b, _computer); //EchoVal(); List<State> list = new List<State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw&&list[i].val > 1538) break; } int maxp = -INT_MAX; List<State> ListChoose = new List<State>(); for (int i = 0; i < list.Count; i++) { currp.Set(list[i].p); b.cells[list[i].p.x, list[i].p.y] = _computer; int t = MinVal(ref b, list[i], -INT_MAX, INT_MAX, 0); //Console.WriteLine("{0}-{1}: {2}", list[i].p.x, list[i].p.y, t);//list[i].val); if (maxp < t) { maxp = t; ListChoose.Clear(); ListChoose.Add(list[i]); } else if (maxp == t) { ListChoose.Add(list[i]); } b.cells[list[i].p.x, list[i].p.y] = ' '; } int x = rand.Next(0, ListChoose.Count); //Console.Write("i={0};",x); return ListChoose[x].p; }
private int Eval(ref CaroBoard b) { string s = ""; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) s += b.cells[i, j]; s += ";"; for (int j = 0; j < n; j++) s += b.cells[j, i]; s += ";"; } for (int i = 0; i < n - 4; i++) { for (int j = 0; j < n - i; j++) s += b.cells[j, i + j]; s += ";"; } for (int i = n - 5; i > 0; i--) { for (int j = 0; j < n - i; j++) s += b.cells[i + j, j]; s += ";"; } for (int i = 4; i < n; i++) { for (int j = 0; j <= i; j++) s += b.cells[i - j, j]; s += ";"; } for (int i = n - 5; i > 0; i--) { for (int j = n - 1; j >= i; j--) s += b.cells[j, i + n - j - 1]; s += ";\n"; } //Console.WriteLine(s); Regex regex1, regex2; int diem = 0; for (int i = 0; i < Truonghopx.Length; i++) { regex1 = new Regex(Truonghopx[i]); regex2 = new Regex(Truonghopo[i]); if (_computer == 'o') { diem += point[i] * regex2.Matches(s).Count; diem -= point[i] * regex1.Matches(s).Count; } else { diem -= point[i] * regex2.Matches(s).Count; diem += point[i] * regex1.Matches(s).Count; } } return diem; }
/// <summary> /// Bắt đầu game mới /// </summary> /// <param name="playerFirst">Người đi trước</param> /// <param name="playerSymbol">Quân người chơi (x-o)</param> /// <param name="conputerAI">Độ sâu khi máy tính toán</param> /// public void NewGame1(bool playerFirst,char player2, bool human) { if (!GameOver) { if (MessageBox.Show("You lose this round?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return; ComputerScore += 1; UpdateMessage(); } step = new List<Step>(); PlayerSymbol = player2 == 'x' ? 'x' : 'o'; _board = new CaroBoard(19, zingLaw,human); _board.PrevMove.Set(-1, -1); win = new int[5]; CurrIndex = -1; t = 0; timer2.Start(); GameOver = false; Invalidate(); if (playerFirst) { _board.XPlaying = player2 == 'x' ? true : false; _board.CurrMove.Set(-1, -1); } else { _board.XPlaying = player2 == 'x' ? false : true; _board.CurrMove.Set(_board.size / 2, _board.size / 2); _board.cells[_board.size / 2, _board.size / 2] = player2 == 'x' ? 'o' : 'x'; SwithchPlayer1(); } }
private int Eval(ref CaroBoard b) { string s = ""; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { s += b.cells[i, j]; } s += ";"; for (int j = 0; j < n; j++) { s += b.cells[j, i]; } s += ";"; } for (int i = 0; i < n - 4; i++) { for (int j = 0; j < n - i; j++) { s += b.cells[j, i + j]; } s += ";"; } for (int i = n - 5; i > 0; i--) { for (int j = 0; j < n - i; j++) { s += b.cells[i + j, j]; } s += ";"; } for (int i = 4; i < n; i++) { for (int j = 0; j <= i; j++) { s += b.cells[i - j, j]; } s += ";"; } for (int i = n - 5; i > 0; i--) { for (int j = n - 1; j >= i; j--) { s += b.cells[j, i + n - j - 1]; } s += ";\n"; } //Console.WriteLine(s); Regex regex1, regex2; int diem = 0; for (int i = 0; i < Truonghopx.Length; i++) { regex1 = new Regex(Truonghopx[i]); regex2 = new Regex(Truonghopo[i]); if (_computer == 'o') { diem += point[i] * regex2.Matches(s).Count; diem -= point[i] * regex1.Matches(s).Count; } else { diem -= point[i] * regex2.Matches(s).Count; diem += point[i] * regex1.Matches(s).Count; } } return(diem); }
private int MinVal(ref CaroBoard b, State s, int alpha, int beta, int depth) { int val = Eval(ref b); if (depth >= maxdepth || Math.Abs(val) > 3000) return val; EvalueCaroBoard(ref b, _player); List<State> list = new List<State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw&&list[i].val > 1538) break; } for (int i = 0; i < list.Count; i++) { b.cells[list[i].p.x, list[i].p.y] = _player; beta = Math.Min(beta, MaxVal(ref b, list[i], alpha, beta, depth + 1)); b.cells[list[i].p.x, list[i].p.y] = ' '; if (alpha >= beta) break; } return beta; }
private int NegaScout(ref CaroBoard bb, State s, int alpha, int beta, int depth) { int val = Eval(ref bb); if (depth >= maxdepth || Math.Abs(val) > 3000) return val; int b = beta, a; if (depth % 2 == 0) EvalueCaroBoard(ref bb, _player); else EvalueCaroBoard(ref bb, _computer); List<State> list = new List<State>(); for (int i = 0; i < _branch; i++) { list.Add(GetMaxNode()); if (!zingLaw&&list[i].val > 1538) break; } for (int i = 0; i < list.Count; i++) { if (depth % 2 == 0) bb.cells[list[i].p.x, list[i].p.y] = _computer; else bb.cells[list[i].p.x, list[i].p.y] = _player; a = -NegaScout(ref bb, list[i], -b, -alpha, depth + 1); if (a > alpha && a < beta && i != 0) a = -NegaScout(ref bb, list[i], -beta, -alpha, depth + 1); bb.cells[list[i].p.x, list[i].p.y] = ' '; alpha = Math.Max(a, alpha); if (alpha >= beta) break; b = alpha + 1; } return alpha; }
public void EvalueCaroBoard(ref CaroBoard b, char Player) { n = b.size; ResetVal(); int rw, cl, i; int cComputer, cPlayer; //kiem tra hang for (rw = 0; rw < n; rw++) { for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw, cl + i] == _computer) { cComputer++; } if (b.cells[rw, cl + i] == _player) { cPlayer++; } } if (cComputer * cPlayer == 0 && cComputer != cPlayer) { for (i = 0; i < 5; i++) { if (b.cells[rw, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) { Val[rw, cl + i] += TScore[cPlayer]; } else { Val[rw, cl + i] += KScore[cPlayer]; } if (zingLaw && b.CheckPosition(rw, cl - 1) && b.CheckPosition(rw, cl + 5) && b.cells[rw, cl - 1] == _computer && b.cells[rw, cl + 5] == _computer) { Val[rw, cl + i] = 0; } } if (cPlayer == 0) { if (Player == _player) { Val[rw, cl + i] += TScore[cComputer]; } else { Val[rw, cl + i] += KScore[cComputer]; } if (zingLaw && b.CheckPosition(rw, cl - 1) && b.CheckPosition(rw, cl + 5) && b.cells[rw, cl - 1] == _player && b.cells[rw, cl + 5] == _player) { Val[rw, cl + i] = 0; } } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw, cl + i - 1) && b.cells[rw, cl + i - 1] == ' ') || (b.CheckPosition(rw, cl + i + 1) && b.cells[rw, cl + i + 1] == ' '))) { Val[rw, cl + i] *= 2; } else if (cComputer == 4 || cPlayer == 4) { Val[rw, cl + i] *= 2; } } } } } } //Cot for (rw = 0; rw < n - 4; rw++) { for (cl = 0; cl < n; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl] == _computer) { cComputer++; } if (b.cells[rw + i, cl] == _player) { cPlayer++; } } if (cComputer * cPlayer == 0 && cComputer != cPlayer) { for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl] == ' ') { if (cComputer == 0) { if (Player == _computer) { Val[rw + i, cl] += TScore[cPlayer]; } else { Val[rw + i, cl] += KScore[cPlayer]; } if (zingLaw && b.CheckPosition(rw - 1, cl) && b.CheckPosition(rw + 5, cl) && b.cells[rw - 1, cl] == _computer && b.cells[rw + 5, cl] == _computer) { Val[rw + i, cl] = 0; } } if (cPlayer == 0) { if (Player == _player) { Val[rw + i, cl] += TScore[cComputer]; } else { Val[rw + i, cl] += KScore[cComputer]; } if (zingLaw && b.CheckPosition(rw - 1, cl) && b.CheckPosition(rw + 5, cl) && b.cells[rw - 1, cl] == _player && b.cells[rw + 5, cl] == _player) { Val[rw + i, cl] = 0; } } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw + i - 1, cl) && b.cells[rw + i - 1, cl] == ' ') || (b.CheckPosition(rw + i + 1, cl) && b.cells[rw + i + 1, cl] == ' '))) { Val[rw + i, cl] *= 2; } else if (cComputer == 4 || cPlayer == 4) { Val[rw + i, cl] *= 2; } } } } } } //Duong cheo xuong for (rw = 0; rw < n - 4; rw++) { for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl + i] == _computer) { cComputer++; } if (b.cells[rw + i, cl + i] == _player) { cPlayer++; } } //Luong gia.. if (cComputer * cPlayer == 0 && cComputer != cPlayer) { for (i = 0; i < 5; i++) { if (b.cells[rw + i, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) { Val[rw + i, cl + i] += TScore[cPlayer]; } else { Val[rw + i, cl + i] += KScore[cPlayer]; } if (zingLaw && b.CheckPosition(rw - 1, cl - 1) && b.CheckPosition(rw + 5, cl + 5) && b.cells[rw - 1, cl - 1] == _computer && b.cells[rw + 5, cl + 5] == _computer) { Val[rw + i, cl + i] = 0; } } if (cPlayer == 0) { if (Player == _player) { Val[rw + i, cl + i] += TScore[cComputer]; } else { Val[rw + i, cl + i] += KScore[cComputer]; } if (zingLaw && b.CheckPosition(rw - 1, cl - 1) && b.CheckPosition(rw + 5, cl + 5) && b.cells[rw - 1, cl - 1] == _player && b.cells[rw + 5, cl + 5] == _player) { Val[rw + i, cl + i] = 0; } } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw + i - 1, cl + i - 1) && b.cells[rw + i - 1, cl + i - 1] == ' ') || (b.CheckPosition(rw + i + 1, cl + i + 1) && b.cells[rw + i + 1, cl + i + 1] == ' '))) { Val[rw + i, cl + i] *= 2; } else if (cComputer == 4 || cPlayer == 4) { Val[rw + i, cl + i] *= 2; } } } } } } //Duong cheo len for (rw = 4; rw < n; rw++) { for (cl = 0; cl < n - 4; cl++) { cComputer = 0; cPlayer = 0; for (i = 0; i < 5; i++) { if (b.cells[rw - i, cl + i] == _computer) { cComputer++; } if (b.cells[rw - i, cl + i] == _player) { cPlayer++; } } //Luong gia.. if (cComputer * cPlayer == 0 && cComputer != cPlayer) { for (i = 0; i < 5; i++) { if (b.cells[rw - i, cl + i] == ' ') { if (cComputer == 0) { if (Player == _computer) { Val[rw - i, cl + i] += TScore[cPlayer]; } else { Val[rw - i, cl + i] += KScore[cPlayer]; } if (zingLaw && b.CheckPosition(rw + 1, cl - 1) && b.CheckPosition(rw - 5, cl + 5) && b.cells[rw + 1, cl - 1] == _computer && b.cells[rw - 5, cl + 5] == _computer) { Val[rw - i, cl + i] = 0; } } if (cPlayer == 0) { if (Player == _player) { Val[rw - i, cl + i] += TScore[cComputer]; } else { Val[rw - i, cl + i] += KScore[cComputer]; } if (zingLaw && b.CheckPosition(rw + 1, cl - 1) && b.CheckPosition(rw - 5, cl + 5) && b.cells[rw + 1, cl - 1] == _player && b.cells[rw - 5, cl + 5] == _player) { Val[rw + i, cl + i] = 0; } } if (zingLaw && (cComputer == 4 || cPlayer == 4) && ((b.CheckPosition(rw - i + 1, cl + i - 1) && b.cells[rw - i + 1, cl + i - 1] == ' ') || (b.CheckPosition(rw - i - 1, cl + i + 1) && b.cells[rw - i - 1, cl + i + 1] == ' '))) { Val[rw - i, cl + i] *= 2; } else if (cComputer == 4 || cPlayer == 4) { Val[rw - i, cl + i] *= 2; } } } } } } //EchoVal(); }