From 2dce3d01308fda5104d26792ee3e305a5f2cbd96 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Fri, 10 Jan 2020 23:35:28 -0500 Subject: Target UI +++ Target backend WORKS --- aesthetics.go | 68 +++++++++++++++++++++++++++--------------------------- backend.go | 31 +++++++++++++++++-------- main.go | 74 +++++++++++++++++++++++++++++++++++++---------------------- 3 files changed, 102 insertions(+), 71 deletions(-) diff --git a/aesthetics.go b/aesthetics.go index e8f88a4..343b468 100644 --- a/aesthetics.go +++ b/aesthetics.go @@ -45,49 +45,49 @@ func (plyr player) DisplayPrimary() string { return text } -// printTarget displays an ASCII version of the target battleship board -// The target board is the one which shows a player what he knows about -// his opponent. This is the board a player would play on. -// Here is an example -// A B C D E F G H I J -// 0 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 1 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 2 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 3 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 4 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 5 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 6 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 7 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// 8 ~ ~ ~ ~ ▣ ▣ ▣ ~ ~ ~ -// 9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -// Since the boat in F8, E8, G8 is not sunk, The player does not see -// it's shape. Upon sinking it, he will be able to see it. -func (plyr player) DisplayTarget() string { +func (plyr *player) DisplayTarget() string { + // First row labelling the columns text := "\n A B C D E F G H I J \n" - for i := 0; i < 10; i++ { - text += strconv.Itoa(i) - text += " " - for j := 0; j < 10; j++ { - switch plyr.target[i][j][0] { + // For every row (r) + for r := 0; r < 10; r++ { + // Add the row number at the start of each line + text += strconv.Itoa(r) + // For every column (c) + for c := 0; c < 10; c++ { + // Separate each character with a space + text += " " + // First thing: is the coordinate hit or not? + switch plyr.target[r][c][0] { + // The coordinate is NOT hit: case 0: + // Add the `~` symbol text += boatchars[1][0] - case 1: - if plyr.gains[plyr.prey.primary[i][j][0]] { - text += boatchars[0][plyr.prey.primary[i][j][1]] - } else { - switch plyr.prey.primary[i][j][0] { - case 0: - text += boatchars[0][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 + text += 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) + text += boatchars[0][plyr.prey.primary[r][c][1]] default: - text += mistery_hit + // Nope, you're getting a censored tile: `▣` + text += misteryHit } } } - text += " " } text += "\n" } - // fmt.Println(text) return text } @@ -110,4 +110,4 @@ var boatchars = [2][7]string{ // This constant keeps information about boats that aren't totally sunk secret. // It therefore substitues the shape of a boat on the target board when it is unsunk. -const mistery_hit = `▣` +const misteryHit = `▣` diff --git a/backend.go b/backend.go index cb258b6..868739d 100644 --- a/backend.go +++ b/backend.go @@ -5,9 +5,9 @@ import ( ) type parameters struct { - vs_cpu bool - ssh_game bool - debug bool + vsCPU bool + sshGame bool + debug bool } type userError struct { @@ -90,13 +90,25 @@ type player struct { prey *player } +func (plyr *player) InitBoard(opponent *player) { + plyr.prey = opponent + + for i := 0; i < 10; i++ { + for j := 0; j < 10; j++ { + plyr.primary[i][j] = [3]int{6, 0, 0} + plyr.target[i][j] = [2]int{0, 6} + + } + } +} + // initBoat places a boat on a players primary grid. // boat sizes are defined by the boatlist variable. // Consult it for more info. -func (plyr *player) initBoat(boatID, orientation, x, y int) { - boat_length := len(boatlist[boatID][0]) +func (plyr *player) InitBoat(boatID, orientation, x, y int) { + boatLength := len(boatlist[boatID][0]) if orientation == 0 { // Boat is HORIZONTAL - for i := 0; i < boat_length; i++ { + for i := 0; i < boatLength; i++ { char := boatlist[boatID][0][i] if char == 0 { // Compensating for boatlist having break // zeros at the end of slices @@ -107,7 +119,7 @@ func (plyr *player) initBoat(boatID, orientation, x, y int) { x++ // HORIZONTAL: Therefore the loop increments the x axis } } else { // Boat is VERTICAL - for i := 0; i < boat_length; i++ { + for i := 0; i < boatLength; i++ { char := boatlist[boatID][1][i] if char == 0 { break @@ -121,9 +133,10 @@ func (plyr *player) initBoat(boatID, orientation, x, y int) { func (plyr *player) Hit(x, y int) bool { 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 > 0 { + if BoatID := plyr.prey.primary[y][x][0]; BoatID < 6 { 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 2b4c7cc..1dbef92 100644 --- a/main.go +++ b/main.go @@ -11,42 +11,42 @@ import ( func main() { settings := parameters{ - debug: true, - ssh_game: false, + debug: true, + sshGame: false, } // SETUP: var playerOne = player{name: "Ben"} var playerTwo = player{name: "Hugo"} // Setting up prey for when using Hit function - playerOne.prey = &playerTwo - playerTwo.prey = &playerOne + 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) + playerOne.InitBoat(0, horizontal, 1, 1) // Battleship: (ID=1), horizontal, (0,9) - playerOne.initBoat(1, horizontal, 0, 9) + playerOne.InitBoat(1, horizontal, 0, 9) // Destroyer: (ID=2), vertical, (5,6) - playerOne.initBoat(2, vertical, 5, 6) + playerOne.InitBoat(2, vertical, 5, 6) // Submarine: (ID=3), horizontal, (6,2) - playerOne.initBoat(3, horizontal, 6, 2) + playerOne.InitBoat(3, horizontal, 6, 2) // Patrol Boat: (ID=4), vertical, (1,5) - playerOne.initBoat(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) + playerTwo.InitBoat(0, vertical, 9, 0) // Battleship: (ID=1), horizontal, (1,8) - playerTwo.initBoat(1, horizontal, 1, 8) + playerTwo.InitBoat(1, horizontal, 1, 8) // Destroyer: (ID=2), vertical, (5,3) - playerTwo.initBoat(2, vertical, 5, 3) + playerTwo.InitBoat(2, vertical, 5, 3) // Submarine: (ID=3), horizontal, (2,2) - playerTwo.initBoat(3, horizontal, 2, 2) + playerTwo.InitBoat(3, horizontal, 2, 2) // Patrol Boat: (ID=4), vertical, (7,6) - playerTwo.initBoat(4, vertical, 6, 6) + playerTwo.InitBoat(4, vertical, 6, 6) // HITTING PLAYER ONE AT DIFFERENT COORDINATES playerTwo.Hit(5, 6) // (F,6) @@ -241,24 +241,42 @@ func main() { 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 i := 0; i < 10; i++ { - str := " " + strconv.Itoa(i) + // 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 j := 0; j < 10; j++ { - switch plyr.target[i][j][0] { - case 0: // Tile is unhit (water since we don't know) + 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]) - 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]) + // 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: - boardData = append(boardData, mistery_hit) + // Nope, you're getting a censored tile: `▣` + boardData = append(boardData, misteryHit) } } } -- cgit v1.2.3