/** * Tests a move and returns a list containing the move. */ func testMove(state *util.BoardState, move util.Move) *list.List { l := list.New() lR := util.GetRow(move.NewPosition) if lR < 0 || lR >= util.GetNumberOfRows() { return l } lD := util.GetDisplacement(move.NewPosition) if 0 <= lR && lR <= util.GetNumberOfRows() && 0 <= lD && lD <= lR { b := state.Board[move.OriginalPosition] && !state.Board[move.NewPosition] && state.Board[move.RemovePiece] if b { l.PushFront(move) } } return l }
/** * Gets the list of all available moves of the given peg. */ func getValidMovesForPosition(state *util.BoardState, peg int) *list.List { validMoves := list.New() r := util.GetRow(peg) d := util.GetDisplacement(peg) /** * There are only 6 possible directions the peg can jump based on the diagram below. * * 6 1 * 5 0 2 * 4 3 */ // Direction 1 land := util.GetPegNumber(r-2, d) jump := util.GetPegNumber(r-1, d) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) // Direction 2 land = util.GetPegNumber(r, d+2) jump = util.GetPegNumber(r, d+1) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) // Direction 3 land = util.GetPegNumber(r+2, d+2) jump = util.GetPegNumber(r+1, d+1) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) // Direction 4 land = util.GetPegNumber(r+2, d) jump = util.GetPegNumber(r+1, d) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) // Direction 5 land = util.GetPegNumber(r, d-2) jump = util.GetPegNumber(r, d-1) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) // Direction 6 land = util.GetPegNumber(r-2, d-2) jump = util.GetPegNumber(r-1, d-1) validMoves.PushBackList(testMove(state, util.Move{peg, land, jump})) return validMoves }