-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
317 lines (248 loc) · 7.47 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"encoding/json"
_ "image/png"
"log"
"os"
"os/signal"
"time"
"game/game"
"github.com/gorilla/websocket"
ebiten "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
screenWidth = 800
screenHeight = 400
)
var (
Rules = false
win bool
lose bool
moscowDrowned = false
MyID string
usersInServer game.Users
msg = `When the window opens - you need to place the ships. To locate, right-click on the desired cell.
Also keep in mind that ships must be placed in a certain order: 4x, 3x, 3x, 2x, 2x, and 3 single-deck.
After that, the program will start sending requests to the server and wait for a response.
When the opponent also deploys all the ships - the game will begin.
Each player takes turns shooting at the opponent's ships. To do this,
use the left mouse button, which is pressed on the desired cell.
If the player is simply wounded, the injured deck is blue, if the ship sank (Like Moscow) - it is painted black.`
)
type Game struct{}
func (g *Game) Update() error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
game.Move(usersInServer)
} else if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) {
game.PlacingMyWarships(usersInServer)
} else if ebiten.IsMouseButtonPressed(ebiten.MouseButtonMiddle) {
Rules = !Rules
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
for x := 0; x < 10; x++ {
for y := 0; y < 10; y++ {
game.DrawAllPlace(x, y, screen, usersInServer)
}
}
if Rules {
ebitenutil.DebugPrintAt(screen, msg, 20, 270)
}
if win {
ebitenutil.DebugPrintAt(screen, " U win", 300, 20)
} else if lose {
ebitenutil.DebugPrintAt(screen, " U lose", 300, 20)
}
if moscowDrowned {
ebitenutil.DebugPrintAt(screen, "Neptune found moscow", 265, 50)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func initialiseGame() {
//MyID = game.MyID
game.AddPlayer(MyID)
for {
usersInServer = game.UsersInServer
if len(usersInServer) > 0 {
log.Println(usersInServer[MyID].UserID)
log.Println(len(usersInServer))
////////////initialise plase
game.InitialPlace(usersInServer)
game.InitialMyPlace(usersInServer)
////////initialise warships
return
}
time.Sleep(1 * time.Second)
}
///////////
}
/////
var Ready bool
var done chan interface{}
var interrupt chan os.Signal
var bufferWriteX = 10000
var bufferWriteY = 10000
var bufferReadY int
var bufferReadX int
func checkEnemyDead() {
bufferForAll := 0
for i := range game.UsersInServer[MyID].MyWarships {
bufferSize := 0
for _, data := range game.UsersInServer[MyID].MyWarships[i] {
if game.UsersInServer[MyID].ArrayMyPlace[(data[0]*10)+data[1]].WasShot {
bufferSize++
}
}
if bufferSize == len(game.UsersInServer[MyID].MyWarships[i]) {
for _, data := range game.UsersInServer[MyID].MyWarships[i] {
game.UsersInServer[MyID].ArrayMyPlace[(data[0]*10)+data[1]].Kill()
bufferForAll++
}
bufferInArray := true
for _, data := range game.UsersInServer[MyID].DeadMyWarships {
if data == i {
bufferInArray = false
break
}
}
if bufferInArray {
game.UsersInServer[MyID].DeadMyWarships = append(game.UsersInServer[MyID].DeadMyWarships, i)
}
}
}
//if len(bufferUser.DeadWarships) == 8 {
if len(game.UsersInServer[MyID].DeadMyWarships) == 8 {
lose = true
return
}
}
func checkMyDead() {
for i := range game.UsersInServer[MyID].EnemyWarships {
bufferSize := 0
for _, data := range game.UsersInServer[MyID].EnemyWarships[i] {
if game.UsersInServer[MyID].ArrayEnemyPlace[(data[0]*10)+data[1]].WasShot {
bufferSize++
}
}
if bufferSize == len(game.UsersInServer[MyID].EnemyWarships[i]) {
for _, data := range game.UsersInServer[MyID].EnemyWarships[i] {
game.UsersInServer[MyID].ArrayEnemyPlace[(data[0]*10)+data[1]].Kill()
}
bufferInArray := true
for _, data := range game.UsersInServer[MyID].DeadWarships {
if data == i {
bufferInArray = false
break
}
}
if bufferInArray {
game.UsersInServer[MyID].DeadWarships = append(game.UsersInServer[MyID].DeadWarships, i)
if i == 0 {
moscowDrowned = true
}
}
}
log.Println("Num of dead warship", len(game.UsersInServer[MyID].DeadWarships))
}
////////////
if len(game.UsersInServer[MyID].DeadWarships) == 8 {
log.Println("U win")
win = true
return
}
}
func receiveHandler(conn *websocket.Conn) {
defer close(done)
i := 0
for {
if MyID == "" && i == 0 {
_, id, err := conn.ReadMessage()
if err != nil {
log.Println("I don't know who are you", err)
}
MyID = string(id)
log.Println("MyID", MyID)
i++
} else {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Println("Error in receive:", err)
return
}
bufferUser := game.User{}
json.Unmarshal(msg, &bufferUser)
go checkEnemyDead()
if game.UsersInServer[MyID].NumberOfMyWarship >= 8 && game.UsersInServer[MyID].UserID != bufferUser.UserID {
game.UsersInServer[MyID].CanMove = !bufferUser.CanMove
game.UsersInServer[MyID].EnemyMoveX = bufferUser.LastMoveX
game.UsersInServer[MyID].EnemyMoveY = bufferUser.LastMoveY
game.UsersInServer[MyID].EnemyWarships = bufferUser.MyWarships
bufferReadX = bufferUser.LastMoveX
bufferReadY = bufferUser.LastMoveY
game.EnemyMove(usersInServer)
log.Println("user can move ", game.UsersInServer[MyID].CanMove)
}
}
}
}
func main() {
done = make(chan interface{}) // Channel to indicate that the receiverHandler is done
interrupt = make(chan os.Signal) // Channel to listen for interrupt signal to terminate gracefully
signal.Notify(interrupt, os.Interrupt) // Notify the interrupt channel for SIGINT
socketUrl := "ws://localhost:9999" + "/socket"
conn, _, err := websocket.DefaultDialer.Dial(socketUrl, nil)
if err != nil {
log.Fatal("Error connecting to Websocket Server:", err)
}
defer conn.Close()
go receiveHandler(conn)
// Our main loop for the client
// We send our relevant packets here
go func(conn *websocket.Conn) {
for {
select {
case <-time.After(time.Duration(1) * time.Millisecond * 100):
////////////////
go checkMyDead()
if game.UsersInServer[MyID].NumberOfMyWarship >= 8 {
bufferOfUserForSend, _ := json.Marshal(game.UsersInServer[MyID])
err := conn.WriteMessage(websocket.TextMessage, []byte(bufferOfUserForSend))
if err != nil {
log.Println("Error during writing to websocket:", err)
return
}
bufferWriteX = game.UsersInServer[MyID].LastMoveX
bufferWriteY = game.UsersInServer[MyID].LastMoveY
continue
}
case <-interrupt:
// We received a SIGINT (Ctrl + C). Terminate gracefully…
log.Println("Received SIGINT interrupt signal. Closing all pending connections")
// Close our websocket connection
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("Error during closing websocket:", err)
return
}
select {
case <-done:
log.Println("Receiver Channel Closed! Exiting….")
case <-time.After(time.Duration(1) * time.Second):
log.Println("Timeout in closing receiving channel. Exiting….")
}
return
}
}
}(conn)
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("My Game")
log.Println("My ID ", MyID)
go initialiseGame()
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}