Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerized terramino, added redis to docker compose, HVS optional #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Dockerfile.backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.22

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

# Build both the server and CLI client
RUN go build -o terramino
RUN go build -o terramino-cli cmd/cli/main.go

EXPOSE 8080

# Use environment variable to determine which binary to run
ENV RUN_MODE=server
CMD if [ "$RUN_MODE" = "cli" ]; then ./terramino-cli; else ./terramino; fi
6 changes: 6 additions & 0 deletions Dockerfile.frontend
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM nginx:alpine

COPY web /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 8081
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Terramino Go

A HashiCorp-themed Tetris-like game with web and CLI interfaces, built in Go.

## Quick Start

```bash
# Start all services
docker compose up

# Play in browser
open http://localhost:8081

# Play in terminal
docker compose exec -it backend ./terramino-cli
```

## Development

Prerequisites: Docker Compose, Go 1.22+

Environment variables (`.env`):

- `REDIS_HOST`: Redis hostname (default: redis)
- `REDIS_PORT`: Redis port (default: 6379)
- `TERRAMINO_PORT`: Backend port (default: 8080)

Local development:

```bash
# Run backend
go run main.go

# Run CLI
go run cmd/cli/main.go
```

## Project Structure
```
├── cmd/cli/ # CLI client
├── internal/ # Game logic & high scores
├── web/ # Frontend assets
└── *.go # Backend server
```
176 changes: 176 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package main

import (
"bufio"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/hashicorp-education/terraminogo/internal/game"
)

const (
clearScreen = "\033[H\033[2J"
moveCursor = "\033[%d;%dH"
escapeKey = 27
spaceKey = 32
)

func main() {
reader := bufio.NewReader(os.Stdin)

// Use alternate screen buffer and hide cursor
fmt.Print("\033[?1049h")
fmt.Print("\033[?25l")
defer fmt.Print("\033[?1049l\033[?25h")

clearTerminal()
fmt.Print("Terramino CLI\r\n")
fmt.Print("Controls:\r\n")
fmt.Print("← → : Move left/right\r\n")
fmt.Print("↑ : Rotate\r\n")
fmt.Print("↓ : Soft drop\r\n")
fmt.Print("Space : Hard drop\r\n")
fmt.Print("q : Quit\r\n")
fmt.Print("\r\nPress Enter to start...\r\n")

reader.ReadString('\n')

// Now set up raw mode after user presses Enter
rawMode := exec.Command("stty", "raw", "-echo")
rawMode.Stdin = os.Stdin
_ = rawMode.Run()

// Restore on exit
defer func() {
cookedMode := exec.Command("stty", "-raw", "echo")
cookedMode.Stdin = os.Stdin
_ = cookedMode.Run()
}()

startGame(reader)
}

func startGame(reader *bufio.Reader) {
g := game.NewGame()
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()

// Start input reading in a separate goroutine
inputChan := make(chan string)
go func() {
for {
char, err := reader.ReadByte()
if err != nil {
continue
}

if char == escapeKey {
// Read the next two bytes for arrow keys
secondChar, err := reader.ReadByte()
if err != nil {
continue
}
if secondChar == '[' {
thirdChar, err := reader.ReadByte()
if err != nil {
continue
}
switch thirdChar {
case 'A': // Up arrow
inputChan <- "up"
case 'B': // Down arrow
inputChan <- "down"
case 'C': // Right arrow
inputChan <- "right"
case 'D': // Left arrow
inputChan <- "left"
}
}
continue
}

if char == spaceKey {
inputChan <- "space"
} else if char == 'q' {
inputChan <- "quit"
}
}
}()

for {
select {
case <-ticker.C:
g.MovePiece(0, 1) // Move down automatically
renderGame(g)
case input := <-inputChan:
switch input {
case "quit":
return
case "left":
g.MovePiece(-1, 0)
case "right":
g.MovePiece(1, 0)
case "up":
g.RotatePiece()
case "down":
g.MovePiece(0, 1)
case "space":
// Hard drop - move down until collision
for g.MovePiece(0, 1) {
// Keep moving down
}
}
renderGame(g)
}

if g.Board.GameOver {
fmt.Printf("\nGame Over! Score: %d\n", g.Board.Score)
fmt.Print("Press 'q' to quit...\n")
for {
char, _ := reader.ReadByte()
if char == 'q' {
return
}
}
}
}
}

func renderGame(g *game.Game) {
clearTerminal()
state := g.Board.GetState()

// Print title
fmt.Print("Terramino\r\n")

// Print top border
fmt.Print("┌" + strings.Repeat("──", game.BoardWidth) + "┐\r\n")

// Print board
for _, row := range state.Grid {
fmt.Print("│")
for _, cell := range row {
fmt.Print(cell + cell) // Print each cell twice for better aspect ratio
}
fmt.Print("│\r\n")
}

// Print bottom border
fmt.Print("└" + strings.Repeat("──", game.BoardWidth) + "┘\r\n")
fmt.Printf("Score: %d\r\n", state.Score)
fmt.Print("Press 'q' to quit...\r\n")
}

func clearTerminal() {
if runtime.GOOS == "windows" {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
} else {
fmt.Print(clearScreen)
}
}
30 changes: 30 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
services:
frontend:
build:
context: .
dockerfile: Dockerfile.frontend
ports:
- "8081:8081"

backend:
build:
context: .
dockerfile: Dockerfile.backend
environment:
- REDIS_HOST=redis
- REDIS_PORT=6379
- TERRAMINO_PORT=8080
ports:
- "8080:8080"
depends_on:
- redis

redis:
image: redis:alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data

volumes:
redis_data:
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (
go.opentelemetry.io/otel/trace v1.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
Loading