From 7b0d6f750104d3281b022e14bffe8e629d2575ae Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 18 Mar 2024 08:15:02 +0100 Subject: [PATCH] Pause the project and reorganize files I realized that I was overly ambitious with the goals I had set for this project, as I still am far too inexperienced with Go to complete this project in a clean manner, time to return to the drawing board and study more about Go itself --- game.go | 147 ----------------------------------------------- server.go | 4 +- singleplayer.go | 148 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 148 insertions(+), 151 deletions(-) delete mode 100644 game.go diff --git a/game.go b/game.go deleted file mode 100644 index 64340d2..0000000 --- a/game.go +++ /dev/null @@ -1,147 +0,0 @@ -package main - -import ( - "fmt" - "math/rand/v2" - "os" - //"reflect" - "strconv" - "time" -) - -var ( - hiddenShips1 [][]string - revealedShips1 [][]string - realShips int = 0 - foundShips int = 0 -) - -func fillShips(tiles int) { - - hiddenShips1 = make([][]string, tiles) - revealedShips1 = make([][]string, tiles) - - for i := 0; i < tiles; i++ { - hiddenShips1[i] = make([]string, tiles) - revealedShips1[i] = make([]string, tiles) - } - - for i := 0; i < len(revealedShips1); i++ { - for i1 := range revealedShips1[i] { - shipChance := rand.IntN(3-0) + 0 - if shipChance == 1 { - revealedShips1[i][i1] = "S" - } else { - revealedShips1[i][i1] = "X" - } - } - } - - for i := 0; i < len(hiddenShips1); i++ { - for i1 := range hiddenShips1[i] { - hiddenShips1[i][i1] = "_" - } - } - -} - -func game() { - fmt.Printf("How many rows/columns would you like to have?\nChoice: ") - var shipNum int - fmt.Scanln(&shipNum) - clear() - fillShips(shipNum) - showMatrix(revealedShips1) - fillRealShips(revealedShips1) - for { - clear() - msg := gameStatus() - fmt.Println(msg) - - fmt.Printf("X coordinate: ") - x := scanLine() - fmt.Printf("Y coordinate: ") - y := scanLine() - xint, _ := strconv.Atoi(x) - yint, _ := strconv.Atoi(y) - xint-- - yint-- - // bombingText := fmt.Sprintf("Bombing X: %v / Y: %v.", x, y) - // for i := 0; i < 3; i++ { - // fmt.Printf("\r%v", bombingText) - // time.Sleep(time.Second) - // bombingText = bombingText + "." - // } - fmt.Printf("\nBombed!") - time.Sleep(time.Second) - checkShip(revealedShips1, hiddenShips1, xint, yint) - checkWin() - } - -} - -func checkShip(visibleMat [][]string, hiddenMat [][]string, x int, y int) { - if visibleMat[x][y] == "S" { - hiddenMat[x][y] = "S" - foundShips++ - } else { - hiddenMat[x][y] = "X" - } -} - -func fillRealShips(mat [][]string) { - for i := 0; i < len(mat); i++ { - for i1 := range mat[i] { - if mat[i][i1] == "S" { - realShips++ - } - } - } - -} - -func checkWin() { - var winStatus bool = realShips == foundShips - //var winStatus bool = reflect.DeepEqual(revealedShips1, hiddenShips1) - if winStatus == true { - clear() - msg := gameStatus() - fmt.Print(msg) - fmt.Println("You win!") - os.Exit(0) - } -} - -func gameStatus() (msg string) { - msg = fmt.Sprintln("_ = Unknown") - msg += fmt.Sprintln("X = Missed") - msg += fmt.Sprintln("S = Ship") - msg += fmt.Sprintln("Player 1 (You):") - msg += fmt.Sprintf("Real ships: %v\nFound ships: %v\n", realShips, foundShips) - showMatrix(hiddenShips1) - - return msg -} - -func showMatrix(mat [][]string) { - for i := 0; i < len(mat); i++ { - for _, v := range mat[i] { - fmt.Printf("%v ", v) - } - fmt.Println() - } -} - -func fillMatrix(mat [][]string) { - for i := 0; i < len(mat); i++ { - for i1 := range mat[i] { - shipChance := rand.IntN(3-0) + 0 - if shipChance == 1 { - mat[i][i1] = "S" - realShips++ - } else { - mat[i][i1] = "_" - } - } - } -} diff --git a/server.go b/server.go index e06b331..63e50d7 100644 --- a/server.go +++ b/server.go @@ -32,7 +32,7 @@ func handleConn(conn net.Conn) { fmt.Println() address := getIP(conn) fmt.Printf("%v connected!\n", address) - go game() + //go game() for { gameStatus() conn.Write([]byte(gameStatus())) @@ -51,6 +51,6 @@ func handleConn(conn net.Conn) { catchErr(err) xint-- yint-- - checkShip(xint, yint) + //checkShip(xint, yint) } } diff --git a/singleplayer.go b/singleplayer.go index 97748d9..c1543f4 100644 --- a/singleplayer.go +++ b/singleplayer.go @@ -1,5 +1,149 @@ package main -func singleplayer() { - game() +import ( + "fmt" + "math/rand/v2" + "os" + //"reflect" + "strconv" + //"time" +) + +var ( + hiddenShips1 [][]string + revealedShips1 [][]string + realShips int = 0 + foundShips int = 0 +) + +func fillShips(tiles int) { + + hiddenShips1 = make([][]string, tiles) + revealedShips1 = make([][]string, tiles) + + for i := 0; i < tiles; i++ { + hiddenShips1[i] = make([]string, tiles) + revealedShips1[i] = make([]string, tiles) + } + + for i := 0; i < len(revealedShips1); i++ { + for i1 := range revealedShips1[i] { + shipChance := rand.IntN(3-0) + 0 + if shipChance == 1 { + revealedShips1[i][i1] = "S" + } else { + revealedShips1[i][i1] = "X" + } + } + } + + for i := 0; i < len(hiddenShips1); i++ { + for i1 := range hiddenShips1[i] { + hiddenShips1[i][i1] = "_" + } + } + +} + +func singleplayer() { + fmt.Printf("How many rows/columns would you like to have?\nChoice: ") + var shipNum int + fmt.Scanln(&shipNum) + clear() + fillShips(shipNum) + showMatrix(revealedShips1) + fillRealShips(revealedShips1) + for { + clear() + msg := gameStatus() + fmt.Println(msg) + + fmt.Printf("X coordinate: ") + x := scanLine() + fmt.Printf("Y coordinate: ") + y := scanLine() + xint, _ := strconv.Atoi(x) + yint, _ := strconv.Atoi(y) + xint-- + yint-- + // bombingText := fmt.Sprintf("Bombing X: %v / Y: %v.", x, y) + // for i := 0; i < 3; i++ { + // fmt.Printf("\r%v", bombingText) + // time.Sleep(time.Second) + // bombingText = bombingText + "." + // } + // go func() { + // fmt.Printf("\nBombed!") + // time.Sleep(time.Second) + // }() + checkShip(revealedShips1, hiddenShips1, xint, yint) + checkWin() + } + +} + +func checkShip(visibleMat [][]string, hiddenMat [][]string, x int, y int) { + if visibleMat[x][y] == "S" { + hiddenMat[x][y] = "S" + foundShips++ + } else { + hiddenMat[x][y] = "X" + } +} + +func fillRealShips(mat [][]string) { + for i := 0; i < len(mat); i++ { + for i1 := range mat[i] { + if mat[i][i1] == "S" { + realShips++ + } + } + } + +} + +func checkWin() { + var winStatus bool = realShips == foundShips + //var winStatus bool = reflect.DeepEqual(revealedShips1, hiddenShips1) + if winStatus == true { + clear() + msg := gameStatus() + fmt.Print(msg) + fmt.Println("You win!") + os.Exit(0) + } +} + +func gameStatus() (msg string) { + msg = fmt.Sprintln("_ = Unknown") + msg += fmt.Sprintln("X = Missed") + msg += fmt.Sprintln("S = Ship") + msg += fmt.Sprintln("Player 1 (You):") + msg += fmt.Sprintf("Real ships: %v\nFound ships: %v\n", realShips, foundShips) + showMatrix(hiddenShips1) + + return msg +} + +func showMatrix(mat [][]string) { + for i := 0; i < len(mat); i++ { + for _, v := range mat[i] { + fmt.Printf("%v ", v) + } + fmt.Println() + } +} + +func fillMatrix(mat [][]string) { + for i := 0; i < len(mat); i++ { + for i1 := range mat[i] { + shipChance := rand.IntN(3-0) + 0 + if shipChance == 1 { + mat[i][i1] = "S" + realShips++ + } else { + mat[i][i1] = "_" + } + } + } }