summaryrefslogtreecommitdiff
path: root/main.go
blob: 8dc26d00a406c9ae6d1de3fd4384864f9396afae (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main

import (
	"fmt"
	tc "github.com/gdamore/tcell"
	tv "github.com/rivo/tview"
	"strconv"
	"strings"
)

func main() {

	settings := parameters{
		debug:   true,
		sshGame: false,
	}

	// SETUP:
	var playerOne = player{name: "Ben"}
	var playerTwo = player{name: "Hugo"}
	// Setting up prey for when using Hit function
	playerOne.InitBoard(&playerTwo)
	playerTwo.InitBoard(&playerOne)

	if settings.debug {

		// PLACING PLAYER ONE BOATS:
		// Carrier: (ID=0), horizontal, (1,1)
		playerOne.InitBoat(0, horizontal, 1, 1)
		// Battleship: (ID=1), horizontal, (0,9)
		playerOne.InitBoat(1, horizontal, 0, 9)
		// Destroyer: (ID=2), vertical, (5,6)
		playerOne.InitBoat(2, vertical, 5, 6)
		// Submarine: (ID=3), horizontal, (6,2)
		playerOne.InitBoat(3, horizontal, 6, 2)
		// Patrol Boat: (ID=4), vertical, (1,5)
		playerOne.InitBoat(4, vertical, 1, 5)

		// PLACING PLAYER TWO BOATS:
		// Carrier: (ID=0), vertical, (9,0)
		playerTwo.InitBoat(0, vertical, 9, 0)
		// Battleship: (ID=1), horizontal, (1,8)
		playerTwo.InitBoat(1, horizontal, 1, 8)
		// Destroyer: (ID=2), vertical, (5,3)
		playerTwo.InitBoat(2, vertical, 5, 3)
		// Submarine: (ID=3), horizontal, (2,2)
		playerTwo.InitBoat(3, horizontal, 2, 2)
		// Patrol Boat: (ID=4), vertical, (7,6)
		playerTwo.InitBoat(4, vertical, 6, 6)

		// HITTING PLAYER ONE AT DIFFERENT COORDINATES
		playerTwo.Hit(5, 6) // (F,6)
		playerTwo.Hit(5, 7) // (F,7)
		playerTwo.Hit(5, 8) // (F,8)
		playerTwo.Hit(3, 4) // (D,4)
		playerTwo.Hit(3, 9) // (D,9)
		playerTwo.Hit(6, 6) // (G,6)
		playerTwo.Hit(2, 9) // (C,9)

		// HITTING PLAYER TWO AT DIFFERENT COORDINATES
		playerOne.Hit(1, 4) // (B,4)
		playerOne.Hit(1, 8) // (B,8)
		playerOne.Hit(2, 7) // (C,7)
		playerOne.Hit(2, 8) // (C,8)
		playerOne.Hit(3, 8) // (D,8)
		playerOne.Hit(4, 8) // (E,8)
		playerOne.Hit(9, 2) // (I,2)

		// Display both primary boards in stdout
		fmt.Println("Player One (vue de ses propres pièces):", playerOne.DisplayPrimary())
		fmt.Println("Player Two (vue de ses propres pièces):", playerTwo.DisplayPrimary())
		fmt.Println("Player One (vue des pièces de son ennemi):", playerOne.DisplayTarget())
		fmt.Println("Player Two (vue des pièces de son ennemi):", playerTwo.DisplayTarget())

		/* TVIEW UI SETUP:
		┌------------------------------------------------------┐
		|dashboard                                             |
		|┌----------------------------------------------------┐|
		||headerBox                                           ||
		|└----------------------------------------------------┘|
		|┌----------------------------------------------------┐|
		||bottomFlex                                          ||
		||┌-------------------┐ ┌----------------------------┐||
		||| infoFlex          | | playFlex                   |||
		|||┌-----------------┐| |┌--------------------------┐|||
		|||| keybindingsBox  || || targetFlex               ||||
		||||                 || ||┌------------------------┐||||
		||||                 || ||| targetBox | gainsBox   |||||
		||||                 || |||           |            |||||
		||||                 || |||           |            |||||
		||||                 || ||└------------------------┘||||
		||||                 || |└--------------------------┘|||
		||||                 || |┌--------------------------┐|||
		|||└-----------------┘| || primaryFlex              ||||
		|||┌-----------------┐| ||┌------------------------┐||||
		|||| logBox          || ||| primaryBox | lossesBox |||||
		||||                 || |||            |           |||||
		||||                 || |||            |           |||||
		||||                 || ||└------------------------┘||||
		|||└-----------------┘| |└--------------------------┘|||
		||└-------------------┘ └----------------------------┘||
		|└----------------------------------------------------┘|
		└------------------------------------------------------┘

		DASHBOARD:
			flex structure containing everything.
		- Direction: rows

		BOTTOMFLEX:
			flex structure containing everything but the headerBox
		- Direction: columns

		INFOFLEX:
			flex structure containing general info related boxes:
				- keybindingsBox
				- logBox
		- Direction: rows

		PLAYFLEX:
			flex structure containing everything related to playing the game:
				- targetFlex
				- primaryFlex
			- Direction: rows

		TARGETFLEX:
			flex structure containing everything the player knows about his target:
				- targetBox
				- gainsBox
		- Direction: columns

		PRIMARYFLEX:
			flex structure containing everything the player knows about himself:
				- primaryBox
				- lossesBox
		- Direction: columns

		HEADERBOX:
			box which displays in it's title the current player.

		KEYBINDINGSBOX:
			simple box containing a list of all the keybindings one can use.

		LOGBOX:
			box which shows a log of the past moves each player made.

		TARGETBOX:
			box containing the board where the player attacks his opponent
			this box is focused by default and is of type table as it can be navigated.

		GAINSBOX:
			box showing the names of all the ennemies boats which are sunk.
			each sunk boat has a small display of the boat going with it.

		PRIMARYBOX:
			box containing the board showing the players boat layout.

		LOSSESBOX:
			box showing the names of all the players boats which are sunk.
			each sunk boat has a small display of the boat going with it.

		COMMANDBOX:
			box containing an input field which is to be used as a command prompt.
			coordinates can be inputed directly and typing quit will exit the game.

		*/

		// Initializing the application
		app := tv.NewApplication()

		headerBox := tv.NewBox().SetTitle(playerOne.name).
			SetBorder(true)

		keybindingsBox := tv.NewBox().
			SetTitle("Keybindings:").
			SetBorder(true)
		// TODO: Add text/documentation to box

		logBox := tv.NewBox().
			SetTitle("Log:").
			SetBorder(true)

		targetBox := tv.NewTable()
		RedrawTarget(&playerOne, targetBox)
		targetBox.SetFixed(1, 1).
			SetSelectable(true, true).
			SetDoneFunc(func(key tc.Key) {
				if key == tc.KeyEscape {
					// TODO: change this for a dopdown prompt "Are you sure? (Y/N)"
					app.Stop()
				}
				if key == tc.KeyEnter {
					x, y := targetBox.GetSelection()
					fmt.Println("X:", x)
					fmt.Println("Y:", y)
				}
			}).
			SetBorder(true).
			SetTitle("The enemy:")

		primaryBox := tv.NewTable()
		RedrawPrimary(&playerOne, primaryBox)
		primaryBox.SetFixed(1, 1).
			SetBorder(true).
			SetTitle("You:")

		gainsBox := tv.NewList().
			SetBorder(true).
			SetTitle("Gains:")

		lossesBox := tv.NewList().
			SetBorder(true).
			SetTitle("Losses:")

		commandBox := tv.NewInputField().
			SetBorder(true).
			SetTitle("Command:")

		targetFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
			AddItem(targetBox, 26, 0, true).
			AddItem(gainsBox, 26, 0, false)

		primaryFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
			AddItem(primaryBox, 26, 0, false).
			AddItem(lossesBox, 26, 0, false)

		playFlex := tv.NewFlex().SetDirection(tv.FlexRow).
			AddItem(targetFlex, 13, 0, true).
			AddItem(primaryFlex, 13, 0, false).
			AddItem(commandBox, 0, 1, false)

		infoFlex := tv.NewFlex().SetDirection(tv.FlexRow).
			AddItem(keybindingsBox, 0, 3, false).
			AddItem(logBox, 0, 1, false)

		bottomFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
			AddItem(infoFlex, 0, 1, false).
			AddItem(playFlex, 52, 0, true)

		dashboard := tv.NewFlex().SetDirection(tv.FlexRow).
			AddItem(headerBox, 2, 1, false).
			AddItem(bottomFlex, 0, 1, true)

		if err := app.SetRoot(dashboard, true).Run(); err != nil {
			panic(err)
		}
	}
}

func RedrawTarget(plyr *player, table *tv.Table) {
	// generating slice string for the table:
	// We initialize a slice containing all the cells
	// The first row will be the label of the columns
	boardData := strings.Split("  /A/B/C/D/E/F/G/H/I/J", "/")
	// For every row (r)
	for r := 0; r < 10; r++ {
		// Each row starts with the row label/number
		// A space makes the table centered by indenting it...
		str := " " + strconv.Itoa(r)
		boardData = append(boardData, str)
		// For every column (c)
		for c := 0; c < 10; c++ {
			// First thing: is the coordinate hit or not?
			switch plyr.target[r][c][0] {
			// The coordinate is NOT hit:
			case 0:
				// Add the `~` symbol
				boardData = append(boardData, boatchars[1][0])
			// If the coordinate is NOT hit:
			default:
				// Is the coordinate water?
				switch plyr.prey.primary[r][c][0] {
				// It IS water:
				case 6:
					// Add the `◌` symbol
					boardData = append(boardData, boatchars[0][0])
				// It's NOT water:
				default:
					// Is the ID of the boat at that coordinate marked as a gain?
					switch plyr.gains[plyr.prey.primary[r][c][0]] {
					// It IS marked as a gain:
					case true:
						// Show the boat as it's meant to be:
						// (hit boatchars: with `position` marked in the opponents primary)
						boardData = append(boardData, boatchars[0][plyr.prey.primary[r][c][1]])
					default:
						// Nope, you're getting a censored tile: `▣`
						boardData = append(boardData, misteryHit)
					}
				}
			}
		}
	}
	table.Clear()
	for r := 0; r < 11; r++ {
		for c := 0; c < 11; c++ {
			color, selectable := tc.ColorDarkCyan, true
			if r < 1 || c < 1 {
				color, selectable = tc.ColorPurple, false
			}
			if boardData[r*11+c] != `~` && !(r < 1 || c < 1) {
				selectable, color = false, tc.ColorRed
			}
			table.SetCell(r, c,
				tv.NewTableCell(boardData[r*11+c]).
					SetTextColor(color).
					SetAlign(tv.AlignCenter).
					SetSelectable(selectable))
		}
	}
}

func RedrawPrimary(plyr *player, table *tv.Table) {
	// generating slice string for the table:
	// We initialize a slice containing all the cells
	// The first row will be the label of the columns
	boardData := strings.Split("  /A/B/C/D/E/F/G/H/I/J", "/")
	// For every row (r)
	for r := 0; r < 10; r++ {
		// Each row starts with the row label/number
		// A space makes the table centered by indenting it...
		str := " " + strconv.Itoa(r)
		boardData = append(boardData, str)
		// For every column (c)
		for c := 0; c < 10; c++ {
			// Is the coordinate hit or not?
			switch plyr.primary[r][c][2] {
			// It is NOT
			case 0:
				// boatchars selects is character from the not-hit slice
				boardData = append(boardData, boatchars[1][plyr.primary[r][c][1]])
			// It IS
			case 1:
				// boatchars selects is character from the hit slice
				boardData = append(boardData, boatchars[0][plyr.primary[r][c][1]])
			}
		}
	}
	table.Clear()
	for r := 0; r < 11; r++ {
		for c := 0; c < 11; c++ {
			color := tc.ColorDarkCyan
			if r < 1 || c < 1 {
				color = tc.ColorPurple
			}
			if boardData[r*11+c] != `~` && !(r < 1 || c < 1) {
				color = tc.ColorRed
			}
			table.SetCell(r, c,
				tv.NewTableCell(boardData[r*11+c]).
					SetTextColor(color).
					SetSelectable(false).
					SetAlign(tv.AlignCenter))
		}
	}
}