# TicTacToe Project

This project contains a TCP Game Server, a Java Swing Client, and a Spring Boot Web Client.

## Structure

- `server/`: Java TCP Server (Port 1870). Includes Anticheat logic (validates turns, moves, wins).
- `client/`: Standalone Java Swing Client. Defaults to `dokploy.lona-development.org:1870` but allows custom host/port.
- `web-client/`: Spring Boot Web Application. Acts as a proxy to the TCP Server via WebSocket.

## Running with Docker Compose

To start the Server and Web Client:

```bash
docker-compose up --build
```

- **Server** runs on port `1870` (exposed).
- **Web Client** runs on `http://localhost:8080`.

## Building and Running the Standalone Java Client

Go to the `client` directory and run the build script:

```bash
cd client
chmod +x build.sh
./build.sh
```

### Usage

By default, the client attempts to connect to `dokploy.lona-development.org:1870`.

```bash
java -jar target/client-1.0-SNAPSHOT.jar
```

**Custom Host/Port (Local Testing):**
If you want to connect to a local server (e.g., running via Docker Compose), pass the host and port as arguments:

```bash
java -jar target/client-1.0-SNAPSHOT.jar localhost 1870
```

## Game Protocol (TCP 1870)

The server creates game sessions identified by a unique 6-character code.

- **CREATE**: Starts a new game. Server returns `GAME_CREATED <CODE>`.
- **JOIN &lt;CODE&gt;**: Joins an existing game. Server returns `JOIN_SUCCESS` or `ERROR`.
- **MOVE &lt;ROW&gt; &lt;COL&gt;**: Places your symbol (0-2). Validated by server.
- **SURRENDER**: Forfeits the game immediately.
- **LEAVE**: Leaves the current lobby. If empty, the lobby acts as closed.
- **RESTART**: Resets the board for the current lobby so players can play again.

**Server Messages:**
- `WIN <SYMBOL>` or `DRAW`
- `OPPONENT_LEFT`: Sent when the other player leaves via the LEAVE command.
- `RESTART`: Sent when the game is restarted.

## Troubleshooting

- **Server Connection Refused?** Ensure the server container is running and port 1870 is mapped.
- **Client stuck connecting?** Verify the hostname is reachable. Use `localhost` if running locally.
