summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2020-01-10 14:43:48 -0500
committerBenjamin Chausse <benjamin@chausse.xyz>2020-01-10 14:43:48 -0500
commit42948584297d0986c55cdff4c0139017b707b57d (patch)
tree46f26e3fbd186ffcc7feec841dc2cc9822de5b59
parent6d852e08a93eec6f1fb6940253bf21ebc2dacb9e (diff)
Target shows a table and Hit needs polishing...
-rw-r--r--backend.go12
-rw-r--r--main.go227
2 files changed, 187 insertions, 52 deletions
diff --git a/backend.go b/backend.go
index ad9149b..cb258b6 100644
--- a/backend.go
+++ b/backend.go
@@ -93,11 +93,9 @@ type player struct {
// initBoat places a boat on a players primary grid.
// boat sizes are defined by the boatlist variable.
// Consult it for more info.
-func initBoat(plyr *player, boat [4]int) {
- boatID := boat[0]
+func (plyr *player) initBoat(boatID, orientation, x, y int) {
boat_length := len(boatlist[boatID][0])
- x, y := boat[2], boat[3]
- if boat[1] == 0 { // Boat is HORIZONTAL
+ if orientation == 0 { // Boat is HORIZONTAL
for i := 0; i < boat_length; i++ {
char := boatlist[boatID][0][i]
if char == 0 { // Compensating for boatlist having
@@ -121,8 +119,7 @@ func initBoat(plyr *player, boat [4]int) {
}
}
-func (plyr *player) Hit(coord [2]int) bool {
- x, y := coord[0], coord[1]
+func (plyr *player) Hit(x, y int) bool {
plyr.prey.primary[y][x][2] = 1
plyr.target[y][x] = [2]int{1, plyr.prey.primary[y][x][0]}
@@ -162,3 +159,6 @@ func (plyr *player) Hit(coord [2]int) bool {
return false // Returns false if water was hit
}
}
+
+const horizontal = 0
+const vertical = 1
diff --git a/main.go b/main.go
index fc3203d..7242be1 100644
--- a/main.go
+++ b/main.go
@@ -2,7 +2,10 @@ package main
import (
"fmt"
+ tc "github.com/gdamore/tcell"
tv "github.com/rivo/tview"
+ "strconv"
+ "strings"
)
func main() {
@@ -23,126 +26,258 @@ func main() {
// PLACING PLAYER ONE BOATS:
// Carrier: (ID=0), horizontal, (1,1)
- initBoat(&playerOne, [4]int{0, 0, 1, 1})
+ playerOne.initBoat(0, horizontal, 1, 1)
// Battleship: (ID=1), horizontal, (0,9)
- initBoat(&playerOne, [4]int{1, 0, 0, 9})
+ playerOne.initBoat(1, horizontal, 0, 9)
// Destroyer: (ID=2), vertical, (5,6)
- initBoat(&playerOne, [4]int{2, 1, 5, 6})
+ playerOne.initBoat(2, vertical, 5, 6)
// Submarine: (ID=3), horizontal, (6,2)
- initBoat(&playerOne, [4]int{3, 0, 6, 2})
+ playerOne.initBoat(3, horizontal, 6, 2)
// Patrol Boat: (ID=4), vertical, (1,5)
- initBoat(&playerOne, [4]int{4, 1, 1, 5})
+ playerOne.initBoat(4, vertical, 1, 5)
// PLACING PLAYER TWO BOATS:
// Carrier: (ID=0), vertical, (9,0)
- initBoat(&playerTwo, [4]int{0, 1, 9, 0})
+ playerTwo.initBoat(0, vertical, 9, 0)
// Battleship: (ID=1), horizontal, (1,8)
- initBoat(&playerTwo, [4]int{1, 0, 1, 8})
+ playerTwo.initBoat(1, horizontal, 1, 8)
// Destroyer: (ID=2), vertical, (5,3)
- initBoat(&playerTwo, [4]int{2, 1, 5, 3})
+ playerTwo.initBoat(2, vertical, 5, 3)
// Submarine: (ID=3), horizontal, (2,2)
- initBoat(&playerTwo, [4]int{3, 0, 2, 2})
+ playerTwo.initBoat(3, horizontal, 2, 2)
// Patrol Boat: (ID=4), vertical, (7,6)
- initBoat(&playerTwo, [4]int{4, 1, 6, 6})
+ playerTwo.initBoat(4, vertical, 6, 6)
// HITTING PLAYER ONE AT DIFFERENT COORDINATES
- playerTwo.Hit([2]int{5, 6}) // (F,6)
- playerTwo.Hit([2]int{5, 7}) // (F,7)
- playerTwo.Hit([2]int{5, 8}) // (F,8)
- playerTwo.Hit([2]int{3, 4}) // (D,4)
- playerTwo.Hit([2]int{3, 9}) // (D,9)
- playerTwo.Hit([2]int{6, 6}) // (G,6)
- playerTwo.Hit([2]int{2, 9}) // (C,9)
+ 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([2]int{1, 4}) // (B,4)
- playerOne.Hit([2]int{1, 8}) // (B,8)
- playerOne.Hit([2]int{2, 7}) // (C,7)
- playerOne.Hit([2]int{2, 8}) // (C,8)
- playerOne.Hit([2]int{3, 8}) // (D,8)
- playerOne.Hit([2]int{4, 8}) // (E,8)
- playerOne.Hit([2]int{9, 2}) // (I,2)
+ 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:", playerOne.DisplayPrimary())
fmt.Println("Player Two:", playerTwo.DisplayPrimary())
- // Setting the Player header box
+ /* 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)
- // Setting up the keybindings box
- // Where the keybindings list will be
keybindingsBox := tv.NewBox().
SetTitle("Keybindings:").
SetBorder(true)
// TODO: Add text/documentation to box
- // Setting the log box whoch shows a history of past moves
logBox := tv.NewBox().
SetTitle("Log:").
SetBorder(true)
- // Setting up the target box
- targetBox := tv.NewTable().
+ 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()
+ }
+ }).
SetBorder(true).
- SetTitle("The ennemy:")
+ SetTitle("The enemy:")
- // Setting up the primary box
primaryBox := tv.NewTable().
SetBorder(true).
SetTitle("You:")
- // Setting up the gains box
gainsBox := tv.NewList().
SetBorder(true).
SetTitle("Gains:")
- // Setting up the losses box
lossesBox := tv.NewList().
SetBorder(true).
SetTitle("Losses:")
- // Setting up the command prompt box
commandBox := tv.NewInputField().
SetBorder(true).
SetTitle("Command:")
- // Setting up the target flex
targetFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
AddItem(targetBox, 26, 0, true).
AddItem(gainsBox, 26, 0, false)
- // Setting up the primary flex
primaryFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
AddItem(primaryBox, 26, 0, false).
AddItem(lossesBox, 26, 0, false)
- // // Setting up the play flex
playFlex := tv.NewFlex().SetDirection(tv.FlexRow).
- AddItem(targetFlex, 13, 0, false).
+ AddItem(targetFlex, 13, 0, true).
AddItem(primaryFlex, 13, 0, false).
AddItem(commandBox, 0, 1, false)
- // // Setting up the info flex
infoFlex := tv.NewFlex().SetDirection(tv.FlexRow).
AddItem(keybindingsBox, 0, 3, false).
AddItem(logBox, 0, 1, false)
- // Setting up the bottom flex
bottomFlex := tv.NewFlex().SetDirection(tv.FlexColumn).
AddItem(infoFlex, 0, 1, false).
- AddItem(playFlex, 52, 0, false)
+ AddItem(playFlex, 52, 0, true)
- // Setting up the application layout
- app := tv.NewApplication()
- // Dashboard is the Flex containing everything.
dashboard := tv.NewFlex().SetDirection(tv.FlexRow).
AddItem(headerBox, 2, 1, false).
- AddItem(bottomFlex, 0, 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:
+ boardData := strings.Split(" /A/B/C/D/E/F/G/H/I/J", "/")
+ for i := 0; i < 10; i++ {
+ str := " " + strconv.Itoa(i)
+ boardData = append(boardData, str)
+ for j := 0; j < 10; j++ {
+ switch plyr.target[i][j][0] {
+ case 0: // Tile is unhit (water since we don't know)
+ boardData = append(boardData, boatchars[1][0])
+ case 1: // Tile has already been hit
+ if plyr.gains[plyr.prey.primary[i][j][0]] { // Boat is sunk:
+ // Boat ID at that coordinate coresponds to ID of a sunk boat
+ boardData = append(boardData, boatchars[0][plyr.prey.primary[i][j][1]])
+ } else {
+ switch plyr.prey.primary[i][j][0] {
+ case 6:
+ boardData = append(boardData, boatchars[0][0])
+ default:
+ boardData = append(boardData, mistery_hit)
+ }
+ }
+ }
+ }
+ }
+
+ table.Clear()
+ for r := 0; r < 11; r++ {
+ for c := 0; c < 11; c++ {
+ color, selectable := tc.ColorDarkBlue, true
+ if r < 1 || c < 1 {
+ color, selectable = tc.ColorPurple, false
+ }
+ if boardData[r*11+c] != `~` {
+ selectable = false
+ }
+ table.SetCell(r, c,
+ tv.NewTableCell(boardData[r*11+c]).
+ SetTextColor(color).
+ SetAlign(tv.AlignCenter).
+ SetSelectable(selectable))
+ }
+ }
+}