文章目录
- 2048逻辑分析
- 完整代码
2048逻辑分析
- 2048小游戏的基本操作是上下左右,每个操作的逻辑都不太一样!
- 这个时候,通过数组旋转的方式。将所有操作转换为向上的操作
- git地址
https://gitee.com/hodgekou/golang.git
package mainimport ("fmt"
)type arr [4][4]intfunc (t *arr) Print() {for _, line := range t {for _, number := range line {fmt.Printf("%2d ", number)}fmt.Println()}fmt.Println()
}func (t *arr) Left90() {temp := new(arr)for i, line := range t {for j, number := range line {temp[len(line)-j-1][i] = number}}*t = *temp
}func (t *arr) Right90() {temp := new(arr)for i, line := range t {for j, number := range line {// fmt.Println(i,j,j,len(line)-1-i)temp[j][len(line)-i-1] = number}}*t = *temp
}func (t *arr) Up() {for m := 0; m < 3; m++ {for j := 0; j < 4; j++ {for i := 0; i < 3; i++ {if t[i][j] == 0 {t[i][j] = t[i+1][j]t[i+1][j] = 0} else if t[i][j] == t[i+1][j] {t[i][j] = t[i][j] + t[i+1][j]t[i+1][j] = 0}}}}
}func main() {// t := arr{{0,2,2,4},{0},{},{0,2,0,4}}t := arr{{2, 2, 2, 2}, {2, 2, 4, 2}, {}, {0, 2, 0, 4}}t.Print()//右fmt.Println("右")t.Left90()t.Up()t.Right90()t.Print()//左fmt.Println("左")t.Right90()t.Up()t.Left90()t.Print()//下fmt.Println("下")t.Right90()t.Right90()t.Up()t.Left90()t.Left90()t.Print()//上fmt.Println("上")t.Up()t.Print()
}
完整代码
https://gitee.com/hodgekou/golang.git