public List<Point> nextMovePlayOut(Board board) { PlayResult p = new PlayResult(board); Attack a = new Attack(board); int t = board.turn; PointSet ps; for(int i=2;i>0;--i) { ps = board.stones[1 - t] & a.attack[t, i]; if (ps.size() != 0) { List<Point> l = ps.toList(); PointSet res = new PointSet(); foreach (Point n in l) { res = res | PointSet.around[n.x, n.y]; } res = res & p.valid; if (res.size() != 0) { return res.toList(); } } } ps = a.attack[0, 0] & a.attack[1, 0]; ps = ps & p.valid; if (ps.size() != 0) { return ps.toList(); } return p.valid.toList(); }
public PlayResult(Board board) { valid = new PointSet(); invalid = new PointSet(); Point p = new Point(); for(int x=0;x<Info.N;++x) { for (int y = 0; y < Info.N; ++y) { p.x = x; p.y = y; Info.GameState s = board.test_play(p); switch(s) { case Info.GameState.VALID_MOVE: valid.set(p); break; case Info.GameState.INVALID_MOVE: invalid.set(p); break; default: break; } } } }
public Attack(Board board) { attack = new PointSet[2, 3]; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { attack[i, j] = new PointSet(); } } for (int x = 0; x < Info.N; ++x) { for (int y = 0; y < Info.N; ++y) { Point p = new Point(x, y); PointSet ps = PointSet.around[x, y] & board.stones[0]; int s = ps.size(); if (s<=2) { attack[0,s].set(p); } ps = PointSet.around[x, y] & board.stones[1]; s = ps.size(); if (s <= 2) { attack[1, s].set(p); } } } }
public Board(Board b) { points = new List<Point>(b.points); stones = new PointSet[2]; stones[0] = new PointSet(b.stones[0]); stones[1] = new PointSet(b.stones[1]); turn = b.turn; }
private void button1_Click(object sender, EventArgs e) { black = comboBox1.Text; white = comboBox2.Text; board = new Board(); if (black != "Human" && white != "Human") { if (black == "RandomAI") { ai = new RandomAI(); } else if (black == "MonteCarloAI") { ai = new MCTAI(); } if (white == "RandomAI") { ai2 = new RandomAI(); } else if (white == "MonteCarloAI") { ai2 = new MCTAI(); } Info.GameState st; while (true) { st = board.play(ai.play(board)); Refresh(); if (st == Info.GameState.END_GAME) { break; } st = board.play(ai2.play(board)); Refresh(); if (st == Info.GameState.END_GAME) { break; } } double black_point = board.stones[0].size(); double white_point = board.stones[1].size() + 6.5; String msg = "Black " + black_point + " White " + white_point; MessageBox.Show(msg); return; } if (black == "RandomAI") { ai = new RandomAI(); } else if (black == "MonteCarloAI") { ai = new MCTAI(); } if (white == "RandomAI") { ai = new RandomAI(); } else if (white == "MonteCarloAI") { ai = new MCTAI(); } if (black != "Human") { ai_vs_human = true; board.play(ai.play(board)); } if (white != "Human") { ai_vs_human = true; } Refresh(); on_game = true; }
private void Form1_Load(object sender, EventArgs e) { on_game = false; ai_vs_human = false; Init.init(); board = new Board(); comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 0; square = new Square[Info.N, Info.N]; for(int x=0;x<Info.N;++x) { for (int y = 0; y < Info.N; ++y) { square[x, y] = new Square(x, y); square[x, y].Size = new Size(30, 30); square[x, y].Location = new System.Drawing.Point((x + 1) * 30, (y + 1) * 30); square[x, y].Click += new EventHandler(square_click); this.Controls.Add(square[x, y]); } } }
private void button3_Click(object sender, EventArgs e) { if(!on_game) { return; } if(board.points.Count==0) { return; } Board n = new Board(); if (ai_vs_human) { if(board.points.Count==1) { return; } for(int i=0;i<board.points.Count-2;++i) { n.play(board.points[i]); } } else { for (int i = 0; i < board.points.Count - 1; ++i) { n.play(board.points[i]); } } board = n; Refresh(); }
public bool onePlayOut(Board board) { Board tmp = new Board(board); Info.GameState s; while(true) { List<Point> l = nextMovePlayOut(tmp); if (l.Count == 0) { s = tmp.play(Point.Pass); if (s == Info.GameState.END_GAME) { return (double)tmp.stones[0].size() > (double)tmp.stones[1].size() + 6.5; } } else { Point p = l[rnd.Next(l.Count)]; tmp.play(p); } } }
public List<Point> nextMove(Board board) { return nextMovePlayOut(board); }
public Point play(Board board) { List<Point> kouho = nextMove(board); if(kouho.Count==0) { return Point.Pass; }else if(kouho.Count==1) { return kouho[0]; } root = new UCBNode(); root.board = new Board(board); expandNode(root); for(int i=0;i< iteration;++i) { List<UCBNode> list = new List<UCBNode>(); UCBNode n = root; while(n!= null) { list.Add(n); n = n.maxUCB(root.iteration); } Board tmp = list.Last().board; bool black_win = onePlayOut(tmp); foreach (UCBNode j in list) { if(j.board.turn==1 && black_win) { j.win++; } if(j.board.turn==0 && (!black_win)) { j.win++; } j.iteration++; } if(list.Last().iteration >= 20) { expandNode(list.Last()); } } UCBNode m=root.children[0]; int win = -1; foreach(UCBNode i in root.children) { if(i.win > win) { m = i; win = i.win; } } return m.board.points.Last(); }