xwjdsh / 2048-ai Goto Github PK
View Code? Open in Web Editor NEWAn simple AI for the 2048 game.
License: MIT License
An simple AI for the 2048 game.
License: MIT License
[root@host ~]# docker run -p 8080:8080 wendellsun/2048-ai
Unable to find image 'wendellsun/2048-ai:latest' locally
latest: Pulling from wendellsun/2048-ai
a52c7d714e5f: Pull complete
15e0dc04655d: Pull complete
f2435a32f659: Pull complete
bdf9109f7f82: Pull complete
9d85d078285a: Pull complete
9cc6c519d7ab: Pull complete
7537db3001ad: Pull complete
0a11135497c2: Pull complete
8678b49e634e: Pull complete
Digest: sha256:82bff73de628bf2fd14f345636937e374e3afdf0f8773dec38776df50fd7b1e2
Status: Downloaded newer image for wendellsun/2048-ai:latest
Error response from daemon: Cannot start container c26c446a7e8ce4a970e976fd0f2c7ca4e165a4534af350d143a0a5603caa116e: iptables failed: iptables -t nat -A DOCKER -p tcp -d 0/0 --dport 8080 -j DNAT --to-destination 192.168.42.2:8080 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)
理论只需要遍历每个空点的上下左右四种情况就可以了,这样的dept不需要设置了
// 这种情况会返回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
}
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
docker build -t 2048-ai .
Sending build context to Docker daemon 1.698 MB
Step 1/12 : FROM golang:1.9 as builder
Error parsing reference: "golang:1.9 as builder" is not a valid repository/tag: invalid reference format
My golang version is go version go1.9.2 linux/amd64
OS is Ubuntu 16.04
cute
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.