summaryrefslogtreecommitdiff
path: root/main.go
blob: 964492cd7a83a1c8c566af46f6c95e35c2f92304 (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
package main

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

func main() {

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

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

	formApp := tv.NewApplication()

	form := tv.NewForm().
		AddInputField("Player 1 name:", playerOne.name, 30, nil, func(text string) {
			playerOne.name = text
		}).
		AddInputField("Player 2 name:", playerTwo.name, 30, nil, func(text string) {
			playerTwo.name = text
		}).
		AddButton("Start", func() {
			formApp.Stop()
		}).
		AddButton("Quit", func() {
			for i := 0; i < 5; i++ {
				playerOne.gains[i] = true
				playerTwo.gains[i] = true
			}
			formApp.Stop()
		})

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

	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

	*/

	currentPlayer := &playerTwo
	var log string = "Game Started!"

	// Until Somedody Wins:
	for winner := currentPlayer; winner.gains != [5]bool{true, true, true, true, true}; {

		// Make the loop toggle between both players
		if currentPlayer == &playerTwo {
			currentPlayer = &playerOne
		} else {
			currentPlayer = &playerTwo
		}

		// Initializing the application
		app := tv.NewApplication()
		// HEADERBOX:
		// 	box which displays in it's title the current player.
		headerBox := tv.NewBox().SetTitle(currentPlayer.name).
			SetBorder(true)
		// KEYBINDINGSBOX:
		// 	simple box containing a list of all the keybindings one can use.
		keybindingsBox := tv.NewTextView()
		fmt.Fprintf(keybindingsBox, keybindings)
		keybindingsBox.SetTitle("Keybindings:").
			SetBorder(true)
		// LOGBOX:
		// 	box which shows a log of the past moves each player made.
		logBox := tv.NewTextView()
		fmt.Fprintln(logBox, log)
		logBox.SetTitle("Log:").
			SetBorder(true)
		// 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.
		targetBox := tv.NewTable()
		RedrawTarget(currentPlayer, targetBox)
		targetBox.SetFixed(1, 1).
			SetSelectable(true, true).
			SetSelectedFunc(func(row, column int) {
				// Hit the target at the chosen coordinate
				fmt.Fprintf(logBox, log)
				currentPlayer.Hit(column-1, row-1)
				// The coordinate where the player is hitting will no longer be selectable
				targetBox.SetSelectable(false, false)
				logEntry := fmt.Sprintf("%v: %c%v\n", currentPlayer.name, letters[column-1], row-1)
				log = fmt.Sprintf("%v%v", logEntry, log)
				RedrawTarget(currentPlayer, targetBox)
				app.Stop()
			}).
			SetDoneFunc(func(key tc.Key) {
				if key == tc.KeyEscape {
					// TODO: change this for a dopdown prompt "Are you sure? (Y/N)"
					for i := 0; i < 5; i++ {
						currentPlayer.prey.gains[i] = true
						currentPlayer.gains[i] = true
					}
					app.Stop()
				}
			}).
			SetBorder(true).
			SetTitle("The enemy:")

		// PRIMARYBOX:
		// 	box containing the board showing the players boat layout.
		primaryBox := tv.NewTable()
		RedrawPrimary(currentPlayer, primaryBox)
		primaryBox.SetFixed(1, 1).
			SetBorder(true).
			SetTitle("You:")
		// 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.
		gainsBox := tv.NewTextView()
		RedrawGains(currentPlayer, gainsBox)
		gainsBox.SetBorder(true).
			SetTitle("Gains:")
		// 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.
		lossesBox := tv.NewTextView()
		RedrawLosses(currentPlayer, lossesBox)
		lossesBox.SetBorder(true).
			SetTitle("Losses:")
		// 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.
		commandBox := tv.NewInputField().
			SetBorder(true).
			SetTitle("Command:")
		// Setting up the flexbox hell!
		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)
		}

		waitScreen := tv.NewApplication()
		waitText := tv.NewTextView().SetDoneFunc(func(key tc.Key) {
			if key == tc.KeyEnter {
				waitScreen.Stop()
			}
			if key == tc.KeyEscape {
				for i := 0; i < 5; i++ {
					playerOne.gains[i] = true
					playerTwo.gains[i] = true
					waitScreen.Stop()
				}
			}
		})

		fmt.Fprint(waitText, "Waiting for:\n", figletWrite(currentPlayer.prey.name), "\nPress Enter to continue...")

		if winner.gains != [5]bool{true, true, true, true, true} {
			if err := waitScreen.SetRoot(waitText, true).Run(); err != nil {
				panic(err)
			} else if (winner.gains == [5]bool{true, true, true, true, true}) && (winner.prey.gains != [5]bool{true, true, true, true, true}) {
				waitText.Clear()
				fmt.Fprint(waitText, figletWrite(winner.name), "\n", figletWrite("is the winner!"))
			}
		}

	}
}