Added coordinate selection for main game

This commit is contained in:
raul 2024-03-14 11:26:41 +01:00
parent 860b50dd59
commit 0aad28a230
2 changed files with 43 additions and 7 deletions

45
game.go
View File

@ -4,6 +4,9 @@ import (
"fmt" "fmt"
"math/rand/v2" "math/rand/v2"
"os" "os"
"reflect"
"strconv"
"time"
) )
var ( var (
@ -46,15 +49,43 @@ func game() {
fmt.Scanln(&shipNum) fmt.Scanln(&shipNum)
clear() clear()
fillShips(shipNum) fillShips(shipNum)
fmt.Println("Player 1 (You):")
//fillMatrix(PLAYER1_REAL_MATRIX)
showMatrix(revealedShips1) showMatrix(revealedShips1)
fmt.Println() 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)
}
}
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) showMatrix(hiddenShips1)
os.Exit(0)
} }
func showMatrix(mat [][]string) { func showMatrix(mat [][]string) {

5
singleplayer.go Normal file
View File

@ -0,0 +1,5 @@
package main
func singleplayer() {
game()
}