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
This commit is contained in:
parent
666d4c18c5
commit
7b0d6f7501
147
game.go
147
game.go
|
@ -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] = "_"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
148
singleplayer.go
148
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] = "_"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue