From ec8b9d3d17edc79b348122601579cc0b312d2d34 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Mon, 13 Jan 2020 23:02:03 -0500 Subject: Log works --- backend.go | 7 +-- main.go | 176 ++++++++++++++++++++++++++++++++----------------------------- 2 files changed, 95 insertions(+), 88 deletions(-) diff --git a/backend.go b/backend.go index 6979d59..3be4bd5 100644 --- a/backend.go +++ b/backend.go @@ -131,11 +131,12 @@ func (plyr *player) InitBoat(boatID, orientation, x, y int) { } func (plyr *player) Hit(x, y int) bool { + // We change the coord status to hit + // We add the id of the boat since we know it now. plyr.prey.primary[y][x][2] = 1 - // We change the coord status to hit and add the id of - // the boat since we know it now. plyr.target[y][x] = [2]int{1, plyr.prey.primary[y][x][0]} - if BoatID := plyr.prey.primary[y][x][0]; BoatID < 6 { + // If that coordinate contains a boat + if BoatID := plyr.prey.primary[y][x][0]; BoatID < 5 { switch plyr.prey.primary[y][x][1] { case 1, 2, 5: // If the hit boat was vertical // We suppose the boat is sunk since it only takes one unhit coordinate to prove this wrong diff --git a/main.go b/main.go index 630a2ca..e89e561 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,6 @@ func main() { playerTwo.InitBoard(&playerOne) if settings.debug { - // PLACING PLAYER ONE BOATS: // Carrier: (ID=0), horizontal, (1,1) playerOne.InitBoat(0, horizontal, 1, 1) @@ -69,106 +68,121 @@ func main() { 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. + /* 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 - BOTTOMFLEX: - flex structure containing everything but the headerBox - - Direction: columns - - INFOFLEX: - flex structure containing general info related boxes: - - keybindingsBox - - logBox - - Direction: rows + TARGETFLEX: + flex structure containing everything the player knows about his target: + - targetBox + - gainsBox + - Direction: columns - PLAYFLEX: - flex structure containing everything related to playing the game: - - targetFlex - - primaryFlex - - Direction: rows + PRIMARYFLEX: + flex structure containing everything the player knows about himself: + - primaryBox + - lossesBox + - Direction: columns - 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!" - 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. + // Until Somedody Wins: + for win := false; win == false; { + currentPlayer.Hit(9, 9) - */ + // 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(playerOne.name). + 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, "Game started!") + fmt.Fprintln(logBox, log) logBox.SetTitle("Log:"). SetBorder(true) - - // 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. targetBox := tv.NewTable() - RedrawTarget(&playerOne, targetBox) + 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) - playerOne.Hit(column-1, row-1) - fmt.Fprintf(logBox, "%v: %c%v\n", playerOne.name, letters[column-1], row-1) - RedrawTarget(&playerOne, targetBox) + 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 { @@ -182,55 +196,47 @@ func main() { // PRIMARYBOX: // box containing the board showing the players boat layout. primaryBox := tv.NewTable() - RedrawPrimary(&playerOne, primaryBox) + 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(&playerOne, gainsBox) + 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() - RedrawGains(&playerOne, lossesBox) + 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) -- cgit v1.2.3