Fixed win condition not applying properly

Players could only win if they bombed every single cell, now they
actually win when they manage to find every ship
This commit is contained in:
raul 2024-03-17 11:27:25 +01:00
parent b92ab38e8d
commit 666d4c18c5
1 changed files with 38 additions and 65 deletions

103
game.go
View File

@ -4,7 +4,7 @@ import (
"fmt"
"math/rand/v2"
"os"
"reflect"
//"reflect"
"strconv"
"time"
)
@ -12,6 +12,8 @@ import (
var (
hiddenShips1 [][]string
revealedShips1 [][]string
realShips int = 0
foundShips int = 0
)
func fillShips(tiles int) {
@ -50,10 +52,11 @@ func game() {
clear()
fillShips(shipNum)
showMatrix(revealedShips1)
fillRealShips(revealedShips1)
for {
clear()
gameStatus()
checkWin()
msg := gameStatus()
fmt.Println(msg)
fmt.Printf("X coordinate: ")
x := scanLine()
@ -71,33 +74,53 @@ func game() {
// }
fmt.Printf("\nBombed!")
time.Sleep(time.Second)
checkShip(xint, yint)
checkShip(revealedShips1, hiddenShips1, xint, yint)
checkWin()
}
}
func checkShip(x int, y int) {
if revealedShips1[x][y] == "S" {
hiddenShips1[x][y] = "S"
func checkShip(visibleMat [][]string, hiddenMat [][]string, x int, y int) {
if visibleMat[x][y] == "S" {
hiddenMat[x][y] = "S"
foundShips++
} else {
hiddenShips1[x][y] = "X"
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 = reflect.DeepEqual(revealedShips1, hiddenShips1)
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() {
fmt.Println("_ = Unknown")
fmt.Println("X = Missed")
fmt.Println("S = Ship")
fmt.Println("Player 1 (You):")
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) {
@ -115,60 +138,10 @@ func fillMatrix(mat [][]string) {
shipChance := rand.IntN(3-0) + 0
if shipChance == 1 {
mat[i][i1] = "S"
realShips++
} else {
mat[i][i1] = "_"
}
}
}
}
// 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()
// }