2024-03-12 12:14:06 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand/v2"
|
|
|
|
"os"
|
2024-03-14 11:26:41 +01:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2024-03-12 12:14:06 +01:00
|
|
|
)
|
|
|
|
|
2024-03-13 15:15:49 +01:00
|
|
|
var (
|
|
|
|
hiddenShips1 [][]string
|
|
|
|
revealedShips1 [][]string
|
|
|
|
)
|
|
|
|
|
|
|
|
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] = "_"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(hiddenShips1); i++ {
|
|
|
|
for i1 := range hiddenShips1[i] {
|
|
|
|
hiddenShips1[i][i1] = "_"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-12 12:14:06 +01:00
|
|
|
func game() {
|
2024-03-14 11:11:33 +01:00
|
|
|
fmt.Printf("How many rows/columns would you like to have?\nChoice: ")
|
|
|
|
var shipNum int
|
|
|
|
fmt.Scanln(&shipNum)
|
|
|
|
clear()
|
|
|
|
fillShips(shipNum)
|
2024-03-13 15:15:49 +01:00
|
|
|
showMatrix(revealedShips1)
|
2024-03-14 11:26:41 +01:00
|
|
|
for {
|
|
|
|
clear()
|
|
|
|
gameStatus()
|
|
|
|
checkWin()
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-03-12 12:44:15 +01:00
|
|
|
|
2024-03-14 11:26:41 +01:00
|
|
|
}
|
2024-03-12 12:14:06 +01:00
|
|
|
|
2024-03-14 11:26:41 +01:00
|
|
|
func checkWin() {
|
|
|
|
var winStatus bool = reflect.DeepEqual(revealedShips1, hiddenShips1)
|
|
|
|
if winStatus == true {
|
|
|
|
fmt.Println("You win!")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func gameStatus() {
|
|
|
|
fmt.Println("Player 1 (You):")
|
|
|
|
showMatrix(hiddenShips1)
|
2024-03-12 12:14:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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] {
|
2024-03-13 15:15:49 +01:00
|
|
|
shipChance := rand.IntN(3-0) + 0
|
2024-03-12 12:14:06 +01:00
|
|
|
if shipChance == 1 {
|
|
|
|
mat[i][i1] = "S"
|
2024-03-12 12:44:15 +01:00
|
|
|
} else {
|
|
|
|
mat[i][i1] = "_"
|
2024-03-12 12:14:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-14 11:11:33 +01:00
|
|
|
|
|
|
|
// Trash zone:
|
|
|
|
// func gameStatus(player1 [][]string, player2 [][]string) {
|
|
|
|
// fmt.Println("Player 1 (You):")
|
|
|
|
// fillMatrix(PLAYER1_REAL_MATRIX)
|
|
|
|
// showMatrix(PLAYER1_REAL_MATRIX)
|
|
|
|
// fmt.Println()
|
|
|
|
// showMatrix(PLAYER1_HIDDEN_MATRIX)
|
|
|
|
// fmt.Printf("\n\n")
|
|
|
|
// fmt.Println("Player 2:")
|
|
|
|
// fillMatrix(PLAYER2_REAL_MATRIX)
|
|
|
|
// showMatrix(PLAYER2_REAL_MATRIX)
|
|
|
|
// fmt.Println()
|
|
|
|
// showMatrix(PLAYER2_HIDDEN_MATRIX)
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
// PLAYER1_REAL_MATRIX := [][]string{
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// }
|
|
|
|
// Holy hell this is a MUCH better way to initialize matrixes
|
|
|
|
// PLAYER1_REAL_MATRIX := make([][]string, 5)
|
|
|
|
// for i := 0; i < 5; i++ {
|
|
|
|
// PLAYER1_REAL_MATRIX[i] = make([]string, 5)
|
|
|
|
// }
|
|
|
|
//PLAYER1_HIDDEN_MATRIX := PLAYER1_REAL_MATRIX
|
|
|
|
// PLAYER1_HIDDEN_MATRIX := make([][]string, len(PLAYER1_REAL_MATRIX))
|
|
|
|
// copy(PLAYER1_HIDDEN_MATRIX, PLAYER1_REAL_MATRIX)
|
|
|
|
|
|
|
|
// PLAYER2_REAL_MATRIX := [][]string{
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// {"x", "x", "x", "x", "x", "x"},
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// PLAYER2_HIDDEN_MATRIX := PLAYER2_REAL_MATRIX
|
|
|
|
|
|
|
|
// Forget it, I'm just going to implement a single player for now
|
|
|
|
|
|
|
|
// for {
|
|
|
|
//
|
|
|
|
// fmt.Scanln()
|
|
|
|
// }
|