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 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] = "_" } } } }