This commit is contained in:
Bruno Rybársky 2024-06-17 22:34:04 +02:00
commit c451cac149
11 changed files with 210 additions and 0 deletions

8
.idea/.gitignore vendored Normal file

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

9
.idea/BattleShit.iml Normal file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/BattleShit.iml" filepath="$PROJECT_DIR$/.idea/BattleShit.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

19
board.go Normal file

@ -0,0 +1,19 @@
package main
type board struct {
owner *client
width uint8
height uint8
matrix [][]square
}
func newBoard(width, height uint8) *board {
b := new(board)
b.width = width
b.height = height
b.matrix = make([][]square, b.height)
for i := range b.matrix {
b.matrix[i] = make([]square, b.width)
}
return b
}

8
client.go Normal file

@ -0,0 +1,8 @@
package main
type client struct {
Username string
ID int
match *match
board *board
}

3
go.mod Normal file

@ -0,0 +1,3 @@
module BattleShit
go 1.22

50
main.go Normal file

@ -0,0 +1,50 @@
package main
import (
"fmt"
"log"
"net"
"os"
"time"
)
const (
HOST = "localhost"
PORT = "8750"
TYPE = "tcp"
)
func main() {
listen, err := net.Listen(TYPE, HOST+":"+PORT)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// close listener
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
// incoming request
buffer := make([]byte, 1024)
_, err := conn.Read(buffer)
if err != nil {
log.Fatal(err)
}
// write data to response
time := time.Now().Format(time.ANSIC)
responseStr := fmt.Sprintf("Your message is: %v. Received time: %v", string(buffer[:]), time)
conn.Write([]byte(responseStr))
// close conn
conn.Close()
}

8
match.go Normal file

@ -0,0 +1,8 @@
package main
type match struct {
host *client
guest *client
hostBoard board
guestBoard board
}

78
ship.go Normal file

@ -0,0 +1,78 @@
package main
type ship struct {
name string
shape uint64
board *board
owner *client
x uint8
y uint8
rotation uint8
}
func (ship *ship) nextSquare() bool {
var target uint8 = 8
if ship.rotation%2 == 0 {
target = 0
if ship.x == 0 && ship.y == 0 {
ship.x = 8
ship.y = 8
}
}
if ship.y == target {
return true //stop
}
if ship.x == target {
ship.x = 0
}
return false
}
func (ship *ship) placeShip() {
var row uint8 = 0
var col uint8 = 0
for {
// Calculate the bit position (0 to 63)
bitPos := (7-row)*8 + (7 - col)
// Extract the bit value
bit := (ship.shape>>bitPos)&1 == 1
if bit {
square := &ship.board.matrix[row][col]
square.ship = ship
square.status = 2
}
if ship.nextSquare() {
break
}
}
}
func (ship *ship) removeShip() {
var row uint8 = 0
var col uint8 = 0
for {
bitPos := (7-row)*8 + (7 - col)
bit := (ship.shape>>bitPos)&1 == 1
if bit {
square := &ship.board.matrix[row][col]
if square.status == 2 {
square.status = 0
square.ship = nil
}
}
if ship.nextSquare() {
break
}
}
}
func newShip(name string, shape uint64, rotation uint8, owner *client) *ship {
this := new(ship)
this.name = name
this.shape = shape
this.owner = owner
this.board = owner.board
this.rotation = rotation
this.placeShip()
return this
}

13
square.go Normal file

@ -0,0 +1,13 @@
package main
type square struct {
status uint8 //0 is empty, 1 is missed, 2 is alive, 3 is destroyed
ship *ship
}
func (square *square) hit() uint8 {
if square.status%2 == 0 {
square.status++
}
return square.status
}