Val Variable Keeps Changing Without Any Reassignment

I’ve been working on a basic snake game wirh 3 level. First 2 of them are working completely fine, but the 3rd one is causing trouble. Even though all of the code except for one extra feature (viewModel’s extraWalls variable) is same, idk why this error is occuring only in this one.

Full project is at: https://github.com/tauqirnizami/SnakeGame

My viewModel for 3rd (difficult) level is:

class DifficultSnakeViewModel : ViewModel() {

    /*Pair(length/y-coordinate, width/x-coordinate)*/

    var coordinates = mutableStateListOf(Pair(14, 14), Pair(15, 14), Pair(16, 14))

        private set



    private var gameGoing by mutableStateOf(true)



    private val eachExtraWallWidthPlusOne = 4

    val extraWalls: List<Pair<Int, Int>> = listOf(Pair(2,3), Pair(3,4), Pair(4,5)) //xtra walls added to snake game's grid to increase difficulty. This is the one causing problem.



    init {

        foodCoordinates = Pair(10, 9)

        score = 0L

        directions =

            mutableStateListOf(0) //Using a list instead of a single int to keep track when user changes directions too quickly while the viewModel is on delay


        giantFoodCoordinates = null

        giantFoodCounter = 1



        viewModelScope.launch(Dispatchers.Main) {

            while (gameGoing) {

                delay(if (score < 333) 500 - (score * 1.5).toLong() else 0) //This controls the snake speed

                coordinatesUpdation()

            }

        }

    }



    private suspend fun coordinatesUpdation() {



        // This code was working fine with other 2 levels

    }



    private fun food(otherFood: Pair<Int, Int>?): Pair<Int, Int> {

        //code to generate food coordinates. Working fine (at least in other 2 levels)

    }





private fun walls(): List<Pair<Int, Int>> {

        val walls = mutableListOf<Pair<Int, Int>>()

        val uniquePositions = mutableSetOf<Pair<Int, Int>>()



        while (uniquePositions.size < 9) {

            var position: Pair<Int, Int>

            do {

                position = Pair((2 until gridLength).random(), (2 until gridWidth - eachExtraWallWidthPlusOne).random())

            } while (coordinates.contains(position) || position == foodCoordinates || position == Pair(coordinates[0].first + 1, coordinates[0].second))



            walls.add(position)

            uniquePositions.add(position)

            repeat(2) {

                position = Pair(position.first, position.second + 1)

                walls.add(position)

                uniquePositions.add(position)

            }

        }



        return uniquePositions.toList()

    }

}

And my composable file is:

@Composable

fun DifficultDisplayGrid(

    modifier: Modifier,

    snakeViewModel: DifficultSnakeViewModel

) {

    val colors = difficultGenerateColorGrid(coordinates = snakeViewModel.coordinates, extraWalls = snakeViewModel.extraWalls)

    Column {

        ColorGrid(colors = colors, modifier = modifier)

        Text(text = "Score = $score")

    }



}



@Composable

fun DifficultScreen(

    modifier: Modifier = Modifier,

    snakeViewModel: DifficultSnakeViewModel = DifficultSnakeViewModel()

) {

    Column(

        verticalArrangement = Arrangement.Center,

        horizontalAlignment = Alignment.CenterHorizontally,

        modifier = modifier

    ) {

        DifficultDisplayGrid(modifier, snakeViewModel = snakeViewModel)



        Row(

            modifier = Modifier,

            verticalAlignment = Alignment.CenterVertically,

            horizontalArrangement = Arrangement.Center

        ) {

            Button(onClick = { directions.add(2) }) {

                Icon(imageVector = Icons.Filled.KeyboardArrowLeft, contentDescription = "Left")

            }
//other buttons
}
//other code

The problem which is occuring is that, in this (difficult) level, when I run the program, the walls (extraWalls variable) are the ones who keeps changing, whereas the snake’s coordinates remain the same. The coordinates updation function is exactly the same as in medium and easy level, which are both working completely fine.

Ps: I know the walls function isn’t perfect, and the walls might sometomes overlap the snake or food, but that’s a secondary concern.

I thiught the error might be because maybe the walls() is being called everytine when the variable is accessed, so i tried to store the value in a separate data class, assign the data class’s variable’s value to another variable and use that variable’s value to be retrieved by the composable. As expected, this too didn’t worked at all!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật