static int CheckAdjacentMove(GameBoard board, GameBoard.MinMax child_type, Colour player_colour, int x, int y, int a, int b, int moves) { //check boundary of array if (a >= 0 && a <= 3 && b >= 0 && b <= 3) { //check adjacent square is empty if (board.ReturnPosition(a, b).stones == 0 || board.ReturnPosition(a, b).colour == player_colour) { //new child GameBoard child = new GameBoard(child_type, board); int stones = child.ReturnPosition(x, y).stones; child.SetStones(x, y, 0, player_colour); int original_stones = child.ReturnPosition(a, b).stones; //adjacent square child.SetStones(a, b, stones+original_stones, player_colour); //add child to parent board.AddChildren(child); //displayBoard(child); moves++; } } return moves; }
static int CheckTwoSquares(GameBoard board, GameBoard.MinMax child_type, Colour player_colour, int x, int y, int a, int b, int c, int d, int moves) { //check boundary of array if (a >= 0 && a <= 3 && b >= 0 && b <= 3) { //check adjacent square is empty if (board.ReturnPosition(a, b).stones == 0 || board.ReturnPosition(a, b).colour == player_colour) { if (c >= 0 && c <= 3 && d >= 0 && d <= 3) { if (board.ReturnPosition(c, d).stones == 0 || board.ReturnPosition(c, d).colour == player_colour) { //new child GameBoard child = new GameBoard(child_type, board); int stones = child.ReturnPosition(x, y).stones; child.SetStones(x, y, 0, player_colour); int original_stones = child.ReturnPosition(a, b).stones; child.SetStones(a, b, original_stones + 1, player_colour); //first square stones--; //one stone moved into a,b original_stones = child.ReturnPosition(c, d).stones; //rest of stones moved into c,d child.SetStones(c, d, stones + original_stones, player_colour); //second square //add child to parent board.AddChildren(child); //displayBoard(child); moves++; }else { moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); } } else { moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); } }else { moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); } } return moves; }
static int CalculateMoves(GameBoard board) { /* goes through all the possible moves * on a gameboard * returns number of moves */ int moves = 0; Colour player_colour = Colour.NONE; GameBoard.MinMax child_type = GameBoard.MinMax.Null; if (board.GetNodeType() == GameBoard.MinMax.Max) { player_colour = Colour.BLACK; child_type = GameBoard.MinMax.Min; }else if (board.GetNodeType() == GameBoard.MinMax.Min) { player_colour = Colour.WHITE; child_type = GameBoard.MinMax.Max; } for (int x = 0; x <= 3; x++) { for (int y = 0; y <= 3; y++) { if (board.ReturnPosition(x,y).stones > 0 && board.ReturnPosition(x,y).colour == player_colour) { //if statement to sort number of stones //1-2 stones = 1 square if (board.ReturnPosition(x, y).stones == 1 || board.ReturnPosition(x, y).stones == 2) { //north int a = x; int b = y + 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //south a = x; b = y - 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //east a = x - 1; b = y; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //west a = x + 1; b = y; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //ne a = x + 1; b = y - 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //nw a = x - 1; b = y - 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //se a = x + 1; b = y + 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); //sw a = x - 1; b = y + 1; moves = CheckAdjacentMove(board, child_type, player_colour, x, y, a, b, moves); } else if (board.ReturnPosition(x, y).stones == 3)//3 stones = 2 squares { //north x2 int a = x; int b = y + 1; int c = x; int d = y + 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //south x2 a = x; b = y - 1; c = x; d = y - 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //eastx2 a = x - 1; b = y; c = x - 2; d = y; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //westx2 a = x + 1; b = y; c = x + 2; d = y; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //nex2 a = x + 1; b = y - 1; c = x + 2; d = y - 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //nwx2 a = x - 1; b = y - 1; c = x - 2; d = y - 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //se x2 a = x + 1; b = y + 1; c = x + 2; d = y + 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); //swx2 a = x - 1; b = y + 1; c = x - 2; d = y + 2; moves = CheckTwoSquares(board, child_type, player_colour, x, y, a, b, c, d, moves); } else // 4+ stones = 3 squares { //north x3 int a = x; int b = y + 1; int c = x; int d = y + 2; int e = x; int f = y + 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //south x3 a = x; b = y - 1; c = x; d = y - 2; e = x; f = y - 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //westx3 a = x - 1; b = y; c = x - 2; d = y; e = x - 3; f = y; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //eastx3 a = x + 1; b = y; c = x + 2; d = y; e = x + 3; f = y; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //nex3 a = x + 1; b = y + 1; c = x + 2; d = y + 2; e = x + 3; f = y + 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //nwx3 //---------???? a = x - 1; b = y + 1; c = x - 2; d = y + 2; e = x - 3; f = y + 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //se x3 a = x + 1; b = y - 1; c = x + 2; d = y - 2; e = x + 3; f = y - 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); //swx3 a = x - 1; b = y - 1; c = x - 2; d = y - 2; e = x - 3; f = y - 3; moves = CheckThreeSquares(board, child_type, player_colour, x, y, a, b, c, d, e, f, moves); } } } } //store moves in parent - number of squares covereed - failed eval fxn /* int squares = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (board.ReturnPosition(x, y).colour == player_colour) { squares++; } } } board.SetAlphaBetaValue(squares); return squares; */ board.SetAlphaBetaValue(moves); return moves; }