From c451cac149a5c02f87185e191a32352e01457dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ryb=C3=A1rsky?= Date: Mon, 17 Jun 2024 22:34:04 +0200 Subject: [PATCH] Init --- .idea/.gitignore | 8 +++++ .idea/BattleShit.iml | 9 +++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ board.go | 19 +++++++++++ client.go | 8 +++++ go.mod | 3 ++ main.go | 50 ++++++++++++++++++++++++++++ match.go | 8 +++++ ship.go | 78 ++++++++++++++++++++++++++++++++++++++++++++ square.go | 13 ++++++++ 11 files changed, 210 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/BattleShit.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 board.go create mode 100644 client.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 match.go create mode 100644 ship.go create mode 100644 square.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/BattleShit.iml b/.idea/BattleShit.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/BattleShit.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..83fda4c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/board.go b/board.go new file mode 100644 index 0000000..70c2ff2 --- /dev/null +++ b/board.go @@ -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 +} diff --git a/client.go b/client.go new file mode 100644 index 0000000..28d2ee3 --- /dev/null +++ b/client.go @@ -0,0 +1,8 @@ +package main + +type client struct { + Username string + ID int + match *match + board *board +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0cd06b9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module BattleShit + +go 1.22 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a1f4840 --- /dev/null +++ b/main.go @@ -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() +} diff --git a/match.go b/match.go new file mode 100644 index 0000000..5266093 --- /dev/null +++ b/match.go @@ -0,0 +1,8 @@ +package main + +type match struct { + host *client + guest *client + hostBoard board + guestBoard board +} diff --git a/ship.go b/ship.go new file mode 100644 index 0000000..78110ab --- /dev/null +++ b/ship.go @@ -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 +} diff --git a/square.go b/square.go new file mode 100644 index 0000000..eb1fb0a --- /dev/null +++ b/square.go @@ -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 +}