-
Notifications
You must be signed in to change notification settings - Fork 0
/
拖拉机模拟.go
281 lines (243 loc) · 6.5 KB
/
拖拉机模拟.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)
// Constants for GetSaveFileName function
const (
OFN_PATHMUSTEXIST = 0x00000800
OFN_FILEMUSTEXIST = 0x00001000
OFN_NOCHANGEDIR = 0x00000008
OFN_OVERWRITEPROMPT = 0x00000002
)
// GameResult stores the result of a single game
type GameResult struct {
Winner string
AMoves int
BMoves int
}
// playGame simulates a single game
func playGame(rng *rand.Rand) GameResult {
deck := rng.Perm(52)
for i := range deck {
deck[i] = deck[i]%13 + 1
}
aDeck := deck[:26]
bDeck := deck[26:]
var table []int
aTurn := true
aMoves := 0
bMoves := 0
for len(aDeck) > 0 && len(bDeck) > 0 {
var card int
if aTurn {
card = aDeck[0]
aDeck = aDeck[1:]
aMoves++
} else {
card = bDeck[0]
bDeck = bDeck[1:]
bMoves++
}
table = append(table, card)
aTurn = !aTurn
for i := 0; i < len(table)-1; i++ {
if table[i] == card {
if aTurn {
aDeck = append(aDeck, table[i:]...)
} else {
bDeck = append(bDeck, table[i:]...)
}
table = table[:i]
break
}
}
}
winner := "A"
if len(aDeck) == 0 {
winner = "B"
}
return GameResult{Winner: winner, AMoves: aMoves, BMoves: bMoves}
}
// simulateGames simulates a number of games in parallel
func simulateGames(numGames, numWorkers int) (int, int, []int, []int) {
var wg sync.WaitGroup
gameResults := make(chan GameResult, numGames)
progress := make(chan int, numGames)
rngPool := sync.Pool{
New: func() interface{} {
return rand.New(rand.NewSource(time.Now().UnixNano()))
},
}
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
localRng := rngPool.Get().(*rand.Rand)
defer rngPool.Put(localRng)
for j := 0; j < numGames/numWorkers; j++ {
result := playGame(localRng)
gameResults <- result
progress <- 1
}
}()
}
go func() {
wg.Wait()
close(gameResults)
close(progress)
}()
progressStep := determineProgressStep(numGames)
totalProgress := 0
startTime := time.Now()
for p := range progress {
totalProgress += p
if totalProgress%progressStep == 0 {
elapsed := time.Since(startTime).Seconds()
estimatedTotalTime := elapsed / (float64(totalProgress) / float64(numGames))
remainingTime := estimatedTotalTime - elapsed
fmt.Printf("\r进度: %.4f%% - 剩余时间: %.2f秒", float64(totalProgress)/float64(numGames)*100, remainingTime)
}
}
fmt.Println("\n模拟完成")
aWins := 0
bWins := 0
aMovesList := make([]int, 0, numGames)
bMovesList := make([]int, 0, numGames)
for result := range gameResults {
if result.Winner == "A" {
aWins++
} else {
bWins++
}
aMovesList = append(aMovesList, result.AMoves)
bMovesList = append(bMovesList, result.BMoves)
}
return aWins, bWins, aMovesList, bMovesList
}
// determineProgressStep determines the progress update step based on the number of games
func determineProgressStep(numGames int) int {
switch {
case numGames > 10000000000:
return numGames / 100000
case numGames > 1000000000:
return numGames / 10000
case numGames > 100000000:
return numGames / 1000
default:
return numGames / 1000
}
}
// saveToFile saves the slice of integers to a file
func saveToFile(filename string, data []int) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for _, value := range data {
fmt.Fprintln(writer, value)
}
return writer.Flush()
}
// getSaveFileName opens a file dialog for saving a file and returns the selected file path
func getSaveFileName() (string, error) {
var ofn struct {
lStructSize uint32
hwndOwner uintptr
hInstance uintptr
lpstrFilter *uint16
lpstrCustomFilter *uint16
nMaxCustFilter uint32
nFilterIndex uint32
lpstrFile *uint16
nMaxFile uint32
lpstrFileTitle *uint16
nMaxFileTitle uint32
lpstrInitialDir *uint16
lpstrTitle *uint16
Flags uint32
nFileOffset uint16
nFileExtension uint16
lpstrDefExt *uint16
lCustData uintptr
lpfnHook uintptr
lpTemplateName *uint16
}
var szFile [260]uint16
ofn.lStructSize = uint32(unsafe.Sizeof(ofn))
ofn.lpstrFile = &szFile[0]
ofn.nMaxFile = uint32(len(szFile))
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT
r1, _, err := syscall.Syscall6(
procGetSaveFileName.Addr(),
1,
uintptr(unsafe.Pointer(&ofn)),
0,
0,
0,
0,
0,
)
if r1 == 0 {
return "", err
}
return syscall.UTF16ToString(szFile[:]), nil
}
var (
libComdlg32 = syscall.NewLazyDLL("comdlg32.dll")
procGetSaveFileName = libComdlg32.NewProc("GetSaveFileNameW")
)
func main() {
rand.Seed(time.Now().UnixNano())
reader := bufio.NewReader(os.Stdin)
fmt.Print("输入游戏数量: ")
numGamesStr, _ := reader.ReadString('\n')
numGamesStr = strings.TrimSpace(numGamesStr)
numGames, err := strconv.Atoi(numGamesStr)
if err != nil {
fmt.Println("无效的输入,请输入一个整数")
return
}
fmt.Print("输入线程数量: ")
numWorkersStr, _ := reader.ReadString('\n')
numWorkersStr = strings.TrimSpace(numWorkersStr)
numWorkers, err := strconv.Atoi(numWorkersStr)
if err != nil {
fmt.Println("无效的输入,请输入一个整数")
return
}
fmt.Println("选择保存文件的路径")
saveDir, err := getSaveFileName()
if err != nil {
fmt.Printf("无法选择文件路径: %v\n", err)
return
}
fmt.Printf("Simulating %d games using %d threads\n", numGames, numWorkers)
runtime.GOMAXPROCS(numWorkers)
aWins, bWins, aMovesList, bMovesList := simulateGames(numGames, numWorkers)
fmt.Printf("A赢了 %d 次\n", aWins)
fmt.Printf("B赢了 %d 次\n", bWins)
aMovesFile := saveDir + "_a_moves_list.txt"
bMovesFile := saveDir + "_b_moves_list.txt"
if err := saveToFile(aMovesFile, aMovesList); err != nil {
fmt.Printf("Failed to save A's moves to file: %v\n", err)
}
if err := saveToFile(bMovesFile, bMovesList); err != nil {
fmt.Printf("Failed to save B's moves to file: %v\n", err)
}
fmt.Printf("A每局游戏出牌次数已保存到 %s\n", aMovesFile)
fmt.Printf("B每局游戏出牌次数已保存到 %s\n", bMovesFile)
fmt.Println("按回车键退出")
reader.ReadString('\n')
}