Skip to content

cydy/ttt-ssh

Repository files navigation

SSH Tic-Tac-Toe

A text-based Tic-Tac-Toe game accessible via SSH, built with Go. Features both CPU opponent and multiplayer modes with room codes.

Features

  • ๐ŸŽฎ Text-based UI - Beautiful ASCII art game board
  • ๐Ÿค– CPU Opponent - Play against an AI with balanced difficulty
  • ๐Ÿ‘ฅ Multiplayer Mode - Play with friends using room codes
  • ๐Ÿ” SSH Access - Connect from anywhere using SSH
  • ๐Ÿณ Docker Support - Easy deployment with Docker

Quick Start with Docker

The easiest way to run the game is using Docker:

# Build and run with docker-compose
docker-compose up -d

# Or using make
make docker-run

Connect to the game:

ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost

Manual Setup

Prerequisites

  • Go 1.21 or higher
  • (Optional) Docker and Docker Compose

Build from Source

# Clone the repository
git clone <your-repo-url>
cd tictactoe-ssh

# Download dependencies
go mod download

# Build the binary
go build -o tictactoe-ssh .

# Run the server
./tictactoe-ssh

Usage

Connecting to the Game

# Connect to localhost (default port 2222)
ssh -p 2222 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost

# Connect to a remote server
ssh -p 2222 username@your-server.com

Game Modes

1. Play Against CPU

  • Choose option 1 from the main menu
  • You play as X, CPU plays as O
  • Enter moves as row col (e.g., 1 2 for row 1, column 2)

2. Multiplayer - Create Room

  • Choose option 2 from the main menu
  • You'll receive a 6-character room code
  • Share this code with your friend
  • Wait for them to join
  • You play as X (first player)

3. Multiplayer - Join Room

  • Choose option 3 from the main menu
  • Enter the room code provided by your friend
  • You play as O (second player)

Game Controls

  • Enter moves as: row col (e.g., 1 2)
  • Board coordinates:
         1   2   3
       โ”Œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”
     1 โ”‚   โ”‚   โ”‚   โ”‚
       โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค
     2 โ”‚   โ”‚   โ”‚   โ”‚
       โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ค
     3 โ”‚   โ”‚   โ”‚   โ”‚
       โ””โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”˜
    

Docker Commands

# Build the Docker image
make docker-build

# Run the container
make docker-run

# View logs
make logs

# Stop the container
make docker-stop

# Clean up
make clean

Configuration

The SSH port can be configured via environment variable:

# Set custom port
export SSH_PORT=3000
./tictactoe-ssh

Or in docker-compose.yml:

environment:
  - SSH_PORT=3000
ports:
  - "3000:3000"

Project Structure

tictactoe-ssh/
โ”œโ”€โ”€ main.go                 # Application entry point
โ”œโ”€โ”€ pkg/
โ”‚   โ”œโ”€โ”€ game/
โ”‚   โ”‚   โ”œโ”€โ”€ board.go       # Game board logic
โ”‚   โ”‚   โ”œโ”€โ”€ ai.go          # CPU opponent AI
โ”‚   โ”‚   โ””โ”€โ”€ multiplayer.go # Multiplayer room management
โ”‚   โ””โ”€โ”€ server/
โ”‚       โ””โ”€โ”€ server.go      # SSH server implementation
โ”œโ”€โ”€ Dockerfile             # Docker build configuration
โ”œโ”€โ”€ docker-compose.yml     # Docker Compose configuration
โ”œโ”€โ”€ Makefile              # Build and run commands
โ”œโ”€โ”€ go.mod                # Go module definition
โ””โ”€โ”€ README.md             # This file

How It Works

SSH Server

  • Uses gliderlabs/ssh library for SSH server functionality
  • No authentication required (public access)
  • Each connection gets its own game session

CPU AI

  • Balanced difficulty algorithm:
    1. Try to win (50% of the time)
    2. Block opponent from winning (30% of the time)
    3. Take center if available (40% of the time)
    4. Take corners (30% of the time)
    5. Make random moves (more often for easier gameplay)

Multiplayer

  • Room-based system with 6-character codes
  • First player creates room (plays as X)
  • Second player joins with code (plays as O)
  • Rooms are automatically cleaned up after game ends

Deployment

Deploy to a VPS

  1. Copy files to server:

    scp -r . user@your-server:/path/to/tictactoe-ssh
  2. SSH into server:

    ssh user@your-server
    cd /path/to/tictactoe-ssh
  3. Run with Docker:

    docker-compose up -d
  4. Configure firewall (if needed):

    sudo ufw allow 2222/tcp

Deploy to Cloud

The application can be deployed to any cloud provider that supports Docker:

  • AWS ECS/Fargate
  • Google Cloud Run
  • Azure Container Instances
  • DigitalOcean App Platform
  • Heroku

Simply build the Docker image and deploy it with port 2222 exposed.

Development

Run locally without Docker:

# Install dependencies
go mod download

# Run the server
go run main.go

# Or use the convenience script
./run.sh

Testing

# Run all tests
go test ./...

# Run tests with coverage
go test ./... -cover

# Run tests in verbose mode
go test ./... -v

Run with hot reload (using air):

# Install air
go install github.com/cosmtrek/air@latest

# Run with hot reload
air

Troubleshooting

"Connection refused"

  • Ensure the server is running: docker-compose ps
  • Check if port 2222 is available: lsof -i :2222
  • Verify firewall settings

"Room not found"

  • Room codes are case-sensitive
  • Rooms are deleted after games end
  • Ensure the room creator hasn't disconnected

SSH connection issues

  • Try with verbose mode: ssh -v -p 2222 localhost
  • Ensure SSH client is installed
  • Check if port is accessible from your network

License

MIT License - feel free to use and modify as needed.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •