//Deep array copy, copies every value individually to new gameboard. private Piece[,] ArrayCopy(Piece[,] currentState) { var x = currentState.GetLength(0); var y = currentState.GetLength(1); var newState = new Piece[x, y]; for (var i = 0; i < x; i++) { for (var j = 0; j < y; j++) { newState[i, j] = currentState[i, j]; } } return newState; }
//Add piece to board based on column and player, black = AI, white = human //If picks a column then starts from the bottom and goes up each row to find the first empty spot. public void AddToBoard(int col, bool isBlack) { var row = State.GetLength(1) - 1; //Find deepest open spot for (var i = row; i >= 0; i--) { if (State[i, col] == null) { var piece = new Piece(isBlack); piece.Position.Row = i; piece.Position.Col = col; State[i, col] = piece; break; } } }
//Returns all adjacent pieces to a given piece, hardcoded the search, could have been better. public static List<Tuple<bool, Piece>> GetAdjacentPieces(Piece piece, Board board) { var row = board.State.GetLength(0) - 1; var col = board.State.GetLength(1) - 1; var listOfAdjacentPieces = new List<Tuple<bool, Piece>>(); if (piece.Position.Row > 0) //Top Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row - 1, piece.Position.Col])); } if (piece.Position.Col < col && piece.Position.Row > 0) //TopRight Boundary Test { var adjPiece = board.State[piece.Position.Row - 1, piece.Position.Col + 1]; listOfAdjacentPieces.Add(Tuple.Create(true, adjPiece)); } if (piece.Position.Col < col) //Right Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row, piece.Position.Col + 1])); } if (piece.Position.Row < row && piece.Position.Col < col) //BottomRight Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row + 1, piece.Position.Col + 1])); } if (piece.Position.Row < row)//Bottom Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row + 1, piece.Position.Col])); } if (piece.Position.Row < row && piece.Position.Col > 0) //BottomLeft Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row + 1, piece.Position.Col - 1])); } if (piece.Position.Col > 0) //Left Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(false, board.State[piece.Position.Row, piece.Position.Col - 1])); } if (piece.Position.Row > 0 && piece.Position.Col > 0)//TopLeft Boundary Test { listOfAdjacentPieces.Add(Tuple.Create(true, board.State[piece.Position.Row - 1, piece.Position.Col - 1])); } return listOfAdjacentPieces; }
public Board(Piece[,] currentState) { State = ArrayCopy(currentState); }
public Board(int width, int height) { State = new Piece[width, height]; Width = width; Height = height; }