2024-03-11 08:43:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-16 11:59:57 +01:00
|
|
|
"bufio"
|
2024-03-11 08:43:14 +01:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2024-03-16 11:59:57 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-11 08:43:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func server() {
|
2024-03-16 11:59:57 +01:00
|
|
|
ln, err := net.Listen("tcp", ":1302")
|
|
|
|
fmt.Printf("Listening on port 1302...\n")
|
|
|
|
catchErr(err)
|
|
|
|
for {
|
2024-03-11 08:43:14 +01:00
|
|
|
|
2024-03-16 11:59:57 +01:00
|
|
|
conn, err := ln.Accept()
|
|
|
|
catchErr(err)
|
|
|
|
go handleConn(conn)
|
|
|
|
}
|
2024-03-11 08:43:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIP(conn net.Conn) (address *net.TCPAddr) {
|
|
|
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
|
|
|
return addr
|
|
|
|
//fmt.Printf("Received connection from: %v\n", addr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConn(conn net.Conn) {
|
|
|
|
fmt.Println()
|
|
|
|
address := getIP(conn)
|
|
|
|
fmt.Printf("%v connected!\n", address)
|
2024-03-16 11:59:57 +01:00
|
|
|
go game()
|
|
|
|
for {
|
|
|
|
gameStatus()
|
|
|
|
conn.Write([]byte(gameStatus()))
|
|
|
|
conn.Write([]byte("X: "))
|
|
|
|
x, err := bufio.NewReader(conn).ReadString('\n')
|
|
|
|
catchErr(err)
|
|
|
|
|
|
|
|
conn.Write([]byte("Y: "))
|
|
|
|
y, err := bufio.NewReader(conn).ReadString('\n')
|
|
|
|
x = strings.TrimRight(x, "\n")
|
|
|
|
y = strings.TrimRight(y, "\n")
|
|
|
|
catchErr(err)
|
|
|
|
xint, err := strconv.Atoi(x)
|
|
|
|
catchErr(err)
|
|
|
|
yint, err := strconv.Atoi(y)
|
|
|
|
catchErr(err)
|
|
|
|
xint--
|
|
|
|
yint--
|
|
|
|
checkShip(xint, yint)
|
|
|
|
}
|
2024-03-11 08:43:14 +01:00
|
|
|
}
|