package com.dong.maxhap.demos.game

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class Game2048LogicTest {
    
    private val logic = Game2048Logic()
    
    @Test
    fun testInitGrid() {
        val grid = logic.doInitGrid()
        assertEquals(Game2048Logic.GRID_SIZE, grid.size)
        assertEquals(Game2048Logic.GRID_SIZE, grid[0].size)
        
        // 检查初始有两个非零数字
        val nonZeroCount = grid.sumOf { row -> row.count { it != 0 } }
        assertEquals(2, nonZeroCount)
    }
    
    @Test
    fun testHasEmptyCell() {
        val gridWithEmpty = listOf(
            listOf(2, 0, 4, 0),
            listOf(0, 0, 0, 0),
            listOf(8, 2, 0, 4),
            listOf(0, 0, 0, 0)
        )
        
        val gridFull = listOf(
            listOf(2, 4, 8, 16),
            listOf(32, 64, 128, 256),
            listOf(512, 1024, 2, 4),
            listOf(8, 16, 32, 64)
        )
        
        assertTrue(logic.hasEmptyCell(gridWithEmpty))
        assertFalse(logic.hasEmptyCell(gridFull))
    }
    
    @Test
    fun testCheckWin() {
        val gridWithoutWin = listOf(
            listOf(2, 4, 8, 16),
            listOf(32, 64, 128, 256),
            listOf(512, 1024, 2, 4),
            listOf(8, 16, 32, 64)
        )
        
        val gridWithWin = listOf(
            listOf(2, 4, 8, 16),
            listOf(32, 64, 128, 256),
            listOf(512, 1024, 2048, 4),
            listOf(8, 16, 32, 64)
        )
        
        assertFalse(logic.checkWin(gridWithoutWin))
        assertTrue(logic.checkWin(gridWithWin))
    }
}