public GomokuView(GomokuBoard myboard, int centerx = 300, int centery = 300, int targetwidth = 480) { _myboard = myboard; // prepare graphics: cell = new AnimationSprite[myboard._height, myboard._width]; for (int row = 0; row < _myboard._height; row++) { for (int col = 0; col < _myboard._width; col++) { AnimationSprite newcell = new AnimationSprite("../../assets/tileset.png", 3, 1); newcell.x = col * newcell.width; newcell.y = row * newcell.width; AddChild(newcell); cell [row, col] = newcell; } } float realwidth = cell [0, 0].width * _myboard._width; scaleX = targetwidth / realwidth; scaleY = targetwidth / realwidth; x = centerx - targetwidth / 2; y = centery - targetwidth / 2; // assuming that we have a square board // set callbacks: _myboard.OnCellChange += CellChangeHandler; _myboard.OnWin += WinHandler; wincells = new List <AnimationSprite> (); }
public GomokuBoard(GomokuBoard orig) : base(orig._width, orig._height) { _winlength = orig._winlength; _maxlength = orig._maxlength; for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { board [i, j] = orig.board[i, j]; } } activeplayer = orig.activeplayer; movesmade = orig.movesmade; winchecked = orig.winchecked; terminal = orig.terminal; }
// Heuristic function to valuate the Board private int Valuate(GomokuBoard b, Selected_cell cell, char flag ) { int result = 0; Point pos; Point[] direction = { new Point(0, -1), new Point(0, 1), new Point(1, 0), new Point(-1, 0), new Point(1, 1), new Point (-1, -1), new Point (1, -1), new Point(-1, 1)}; foreach (Point P in direction) { int countSimilar = 0; pos = cell.P; pos.X += P.X; pos.Y += P.Y; while (b.Board[pos.X, pos.Y].IsSelected == true && b.Board[pos.X, pos.Y].Flag == flag) { result++; countSimilar++; if (countSimilar == 5) { return 1000; } } } return result; }
private Selected_cell Minimax(GomokuBoard b, int depth) { return Max (b, depth); }
private Selected_cell Min(GomokuBoard b, int depth) { Selected_cell result; List<Selected_cell> a = Available_cell(b); if (depth == max_depth) { String output = "Max_depth: Min: \n"; for (int i = 0; i < a.Count; i++) { /* * Not right Selected_cell cell = new Selected_cell(); cell.P = get_opponent_turn(); cell.V = Valuate(b, cell, getOpponentFlag()); */ Selected_cell cell = a.ElementAt(i); cell.V = -Valuate(b, cell, getOpponentFlag()); output = output + "["+ cell.P.X + ", " + cell.P.Y + "]: " + cell.V + "; "; } result = MaxValue(a); output = output + "\nMax value: " + result + "\n\n"; logFile.WriteLine(output); return result; } List<Selected_cell> list_cell = new List<Selected_cell>(); foreach (Selected_cell x in a) { GomokuBoard tmp = Clone(b); tmp.select(x.P, this.team.Flag); list_cell.Add(Max(tmp, depth + 1)); result = Max(tmp, depth + 1); } result = MaxValue(list_cell); return result; }
// Clone a new Gomoku Board private GomokuBoard Clone(GomokuBoard b) { GomokuBoard result = new GomokuBoard (); result.Board = b.Board; result.Last_Selected_Position = b.Last_Selected_Position; return result; }
// Return the list of available cell private List<Selected_cell> Available_cell(GomokuBoard b) { List<Selected_cell> result = new List<Selected_cell>(); // TODO: Change into get cells in the radius of 3 cells from lastPos Point lastPos = this.board.Last_Selected_Position; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (b.Board [i, j].IsSelected == false) { Selected_cell cell = new Selected_cell (); Point p = new Point (); p.X = j; p.Y = i; cell.P = p; result.Add (cell); } } } return result; }