Skip to content

Commit c5bec64

Browse files
first commit
0 parents  commit c5bec64

File tree

5 files changed

+497
-0
lines changed

5 files changed

+497
-0
lines changed

.goreleaser.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
builds:
2+
- binary: 4inarowgo
3+
goos:
4+
- windows
5+
- darwin
6+
- linux
7+
goarch:
8+
- amd64
9+
- 386
10+
archive:
11+
format: zip
12+
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
13+
replacements:
14+
amd64: 64-bit
15+
386: 32-bit
16+
darwin: macOS
17+
release:
18+
github:
19+
owner: y-hatano-github
20+
name: 4-in-a-row-go

cpu/cpu.go

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package cpu
2+
3+
import (
4+
"math/rand"
5+
6+
"github.com/y-hatano-github/4inarow/game"
7+
)
8+
9+
// ExecCPUTurn CPUのターン実行
10+
func ExecCPUTurn(b *game.Board) {
11+
x, y, sx := 0, 0, 0
12+
// 左右どちらから走査するか
13+
if rand.Intn(2) == 1 {
14+
sx = 6
15+
}
16+
17+
// 勝てる場所を探す
18+
for x = 0; x < 7; x++ {
19+
if b.Height[x] > 9 {
20+
continue
21+
}
22+
y = 9 - b.Height[x]
23+
if checkCPUCell(x, y, 4, game.CPU, b) {
24+
b.Put(x, game.CPU)
25+
return
26+
}
27+
}
28+
29+
// 負ける場所を探す
30+
for x = 0; x < 7; x++ {
31+
if b.Height[x] > 9 {
32+
continue
33+
}
34+
y = 9 - b.Height[x]
35+
if checkCPUCell(x, y, 4, game.Player, b) {
36+
b.Put(x, game.CPU)
37+
return
38+
}
39+
}
40+
41+
// Playerが三つそろう箇所を抑える
42+
for x = 0; x < 7; x++ {
43+
x2 := Abs(sx - x)
44+
if b.Height[x2] > 9 {
45+
continue
46+
}
47+
y = 9 - b.Height[x2]
48+
if checkCPUCell(x2, y, 3, game.Player, b) {
49+
// 次の手で負ける場合は抑えない
50+
if IsCPULostNextTurn(x2, y, b) {
51+
continue
52+
}
53+
b.Put(x2, game.CPU)
54+
return
55+
}
56+
}
57+
58+
// CPUが三つそろう箇所を抑える
59+
for x = 0; x < 7; x++ {
60+
x2 := Abs(sx - x)
61+
if b.Height[x2] > 9 {
62+
continue
63+
}
64+
y = 9 - b.Height[x2]
65+
if checkCPUCell(x2, y, 3, game.CPU, b) {
66+
// 次の手で負ける場合は抑えない
67+
if IsCPULostNextTurn(x2, y, b) {
68+
continue
69+
}
70+
b.Put(x2, game.CPU)
71+
return
72+
}
73+
}
74+
75+
// Playerが二つそろう箇所を抑える
76+
for x = 0; x < 7; x++ {
77+
x2 := Abs(sx - x)
78+
if b.Height[x2] > 9 {
79+
continue
80+
}
81+
y = 9 - b.Height[x2]
82+
if checkCPUCell(x2, y, 2, game.Player, b) {
83+
// 次の手で負ける場合は抑えない
84+
if IsCPULostNextTurn(x2, y, b) {
85+
continue
86+
}
87+
b.Put(x2, game.CPU)
88+
return
89+
}
90+
}
91+
92+
// CPUが二つそろう箇所を抑える
93+
/*
94+
for x = 0; x < 7; x++ {
95+
x2 := Abs(sx - x)
96+
if b.Height[x2] > 9 {
97+
continue
98+
}
99+
y = 9 - b.Height[x2]
100+
if checkCPUCell(x2, y, 2, game.CPU, b) {
101+
// 次の手で負ける場合は抑えない
102+
if IsCPULostNextTurn(x2, y, b) {
103+
continue
104+
}
105+
b.Put(x2, game.CPU)
106+
return
107+
}
108+
}*/
109+
110+
// ランダムに手を置く
111+
for x = 0; x < 7; x++ {
112+
rx := rand.Intn(7)
113+
if b.Height[rx] > 9 {
114+
continue
115+
}
116+
y = 9 - b.Height[rx]
117+
// 負ける場所にはおかない
118+
if IsCPULostNextTurn(rx, y, b) {
119+
continue
120+
}
121+
b.Put(rx, game.CPU)
122+
return
123+
}
124+
125+
// 期待した場所がなければ空いている場所に置く
126+
for x = 0; x < 7; x++ {
127+
x2 := Abs(sx - x)
128+
if b.Height[x2] > 9 {
129+
continue
130+
}
131+
b.Put(x2, game.CPU)
132+
break
133+
}
134+
}
135+
136+
// checkCPUCell CPUの手(期待値)のチェック
137+
func checkCPUCell(x, y, c int, z game.Char, b *game.Board) bool {
138+
cbord := b.Board
139+
cbord[y][x] = z
140+
return b.CheckCellCount(x, y, c, z, cbord)
141+
}
142+
143+
// IsCPULostNextTurn 次の手でCPUが負けるか
144+
func IsCPULostNextTurn(x, y int, b *game.Board) bool {
145+
if b.Height[x] > 8 {
146+
return false
147+
}
148+
cbord := b.Board
149+
cbord[y][x] = game.CPU
150+
cbord[y-1][x] = game.Player
151+
152+
return b.CheckCellCount(x, y-1, 4, game.Player, cbord)
153+
}
154+
155+
// Abs 絶対値 mathだとfloatなので
156+
func Abs(x int) int {
157+
if x < 0 {
158+
return -x
159+
}
160+
return x
161+
}

0 commit comments

Comments
 (0)