Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

it get NONE direction #6

Open
d7185540 opened this issue Oct 26, 2018 · 2 comments
Open

it get NONE direction #6

d7185540 opened this issue Oct 26, 2018 · 2 comments

Comments

@d7185540
Copy link

d7185540 commented Oct 26, 2018

image

// 这种情况会返回None结果,其实也差不多快要结束了。
AI.go

func (a *AI) Search() grid.Direction {
	var (
		bestDire  = grid.NONE
		bestScore float64
	)
	// depth value depending on grid's max value.
	dept := a.deptSelect()
	for _, dire := range directions {
		newGrid := a.Grid.Clone()
		if newGrid.Move(dire) {.     **//It No Caculate  score generated by Move.**
			// Could move.
			// Active is false represent computer should fill number to grid now.

			newAI := &AI{Grid: newGrid, Active: false}
			if newScore := newAI.expectSearch(dept); newScore > bestScore {
				bestDire = dire
				bestScore = newScore
			}
		}
	}
	return bestDire
}
  1. 在第一次遍历方向时并没有计算移动的分数,会不会有影响?
  2. 如果所有方向分数都为0时,这个时候差不多要结束游戏,但返回的结果为grid.NONE,如果返回一个可以移动的反向是不是更好?
func (g *Grid) Move(d Direction) (bool, float64) {
	originData := g.Data
	data := &g.Data
	Score := float64(0)
	switch d {
	case UP:
		for y := 0; y < 4; y++ {
			for x := 0; x < 3; x++ {
				for nx := x + 1; nx <= 3; nx++ {
					if data[nx][y] > 0 {
						if data[x][y] <= 0 {
							data[x][y] = data[nx][y]
							data[nx][y] = 0
							x -= 1
						} else if data[x][y] == data[nx][y] {
							data[x][y] += data[nx][y]
							data[nx][y] = 0
							Score += float64(data[x][y] * 2)
						}
						break
					}
				}
			}
		}
	case DOWN:
		for y := 0; y < 4; y++ {
			for x := 3; x > 0; x-- {
				for nx := x - 1; nx >= 0; nx-- {
					if data[nx][y] > 0 {
						if data[x][y] <= 0 {
							data[x][y] = data[nx][y]
							data[nx][y] = 0
							x += 1
						} else if data[x][y] == data[nx][y] {
							data[x][y] += data[nx][y]
							data[nx][y] = 0
							Score += float64(data[x][y] * 2)
						}
						break
					}
				}
			}
		}
	case LEFT:
		for x := 0; x < 4; x++ {
			for y := 0; y < 3; y++ {
				for ny := y + 1; ny <= 3; ny++ {
					if data[x][ny] > 0 {
						if data[x][y] <= 0 {
							data[x][y] = data[x][ny]
							data[x][ny] = 0
							y -= 1
						} else if data[x][y] == data[x][ny] {
							data[x][y] += data[x][ny]
							data[x][ny] = 0
							Score += float64(data[x][y] * 2)
						}
						break
					}
				}
			}
		}
	case RIGHT:
		for x := 0; x < 4; x++ {
			for y := 3; y > 0; y-- {
				for ny := y - 1; ny >= 0; ny-- {
					if data[x][ny] > 0 {
						if data[x][y] <= 0 {
							data[x][y] = data[x][ny]
							data[x][ny] = 0
							y += 1
						} else if data[x][y] == data[x][ny] {
							data[x][y] += data[x][ny]
							data[x][ny] = 0
							Score += float64(data[x][y] * 2)
						}
						break
					}
				}
			}
		}
	}
	return utils.Diff(*data, originData), Score
}

 //优化后的Search函数
func (a *AI) Search() grid.Direction {
	var (
		bestDire  = grid.NONE
		bestScore float64
	)
	// depth value depending on grid's max value.
	// 根据场上最大分数来判断搜索深度
dept := a.deptSelect()
	for _, dire := range directions {
		newGrid := a.Grid.Clone()
       
		canmove, score := newGrid.Move(dire)
		if canmove {
               if bestDire == grid.NONE {
				bestDire = dire
			}
			// Could move.
			// Active is false represent computer should fill number to grid now.
			newAI := &AI{Grid: newGrid, Active: false}
			if newScore := newAI.expectSearch(dept); newScore+score > bestScore {
				bestDire = dire
				bestScore = newScore
			}

		}
	}

	return bestDire
@d7185540
Copy link
Author

跑了一晚上2048,结果发现在快输时有概率返回无效路径

@xwjdsh
Copy link
Owner

xwjdsh commented Oct 28, 2018

谢谢指出,确实是有问题。不过原因和 Move 函数的返回没有关系,这里上下文中的 score 指的是对游戏局面的评分,并非游戏中的得分。在这种情况下,Search 函数中会先移动一步,然后 expectSearch 函数中会先模拟电脑在空位中补上一个点,此时所有点都已经占满了,任何一个方向都没办法移动,所以 expectSearch 函数返回的 newScore 为 0,和初始的 bestScore 相等,所以也就不会把可行的方向赋值给 bestDire,我的改法是把 bestScore 初始为 -1,这样也就可以得到一个可行的方向。

Repository owner deleted a comment Feb 2, 2024
Repository owner deleted a comment from fede-s Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@xwjdsh @d7185540 and others