{"id":17352952,"url":"https://github.com/gmartins-dev/snake-game","last_synced_at":"2025-03-27T13:14:07.319Z","repository":{"id":254022585,"uuid":"845189039","full_name":"gmartins-dev/snake-game","owner":"gmartins-dev","description":"A simplified version of the nostalgic snake game from old Nokia phones, built using React and Golang.","archived":false,"fork":false,"pushed_at":"2025-01-25T16:30:44.000Z","size":68,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T17:25:12.548Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmartins-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-20T18:55:36.000Z","updated_at":"2024-08-21T22:47:25.000Z","dependencies_parsed_at":"2024-08-21T00:10:51.698Z","dependency_job_id":"6a37a6ba-c5a6-4729-9bbb-1d1f3d2a27d5","html_url":"https://github.com/gmartins-dev/snake-game","commit_stats":null,"previous_names":["gmartins-dev/snake-game"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmartins-dev%2Fsnake-game","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmartins-dev%2Fsnake-game/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmartins-dev%2Fsnake-game/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmartins-dev%2Fsnake-game/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmartins-dev","download_url":"https://codeload.github.com/gmartins-dev/snake-game/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245850360,"owners_count":20682647,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T17:15:04.973Z","updated_at":"2025-03-27T13:14:07.299Z","avatar_url":"https://github.com/gmartins-dev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Snake Game 🐍\n\nA simplified version of the nostalgic snake game from old Nokia phones, built using React and Golang.\n\n## Backend (API)\n\n### Overview\n\nThe Snake Game API allows you to play the classic Snake game through HTTP requests. You can start a game, make moves, and validate them.\n\n### Setup\n\n1. Clone the repository:\n\n```\ngit clone https://github.com/yourusername/snake-game.git\ncd snake-game/api\n```\n\n2. Install dependencies:\n\n```\ngo mod download\n```\n\n3. Create a .env file:\n\n```\ncp .env.example .env\n```\n\n4. Update the .env file with your configuration:\n\n```\nSERVER_ADDRESS=:3001\n```\n\n### Running the Server\n\nTo start the server, run the following command from the `api/cmd/api` directory:\n\n```\ngo run main.go\n```\n\nThe server will start on the address specified in the .env file (default is :3001).\n\n### API Endpoints\n\n#### 1. Start Game\n\n- **POST /start**\n- Description: Initialize a new game.\n- Request Body (optional):\n  ```json\n  {\n    \"width\": 10,\n    \"height\": 10\n  }\n  ```\n- Response:\n  - Returns the initial game state, including grid size, snake position, apple position, and score.\n\n#### 2. Validate Moves\n\n- **POST /validate**\n- Description: Validate one or more moves for the snake.\n- Request Header: Content-Type: application/json\n- Request Body:\n  ```json\n  [\n    { \"x\": 1, \"y\": 0 },\n    { \"x\": 0, \"y\": 1 }\n  ]\n  ```\n- Responses:\n  - 200 OK: Updated game state with snake position, apple position, and score.\n  - 400 Bad Request: Game over if the snake hits a wall or an invalid move is made. Invalid JSON returns an error.\n\n#### 3. End Game\n\n- **POST /end**\n- Description: End the current game and reset the game state.\n- Request Body: None\n- Response: None\n\n#### Example Postman Collection\n\nImport the following JSON into Postman to test the API:\n\n```json\n{\n  \"info\": {\n    \"name\": \"Snake Game API\",\n    \"description\": \"API for the Snake Game\",\n    \"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n  },\n  \"item\": [\n    {\n      \"name\": \"Start Game\",\n      \"request\": {\n        \"method\": \"POST\",\n        \"header\": [],\n        \"body\": {\n          \"mode\": \"raw\",\n          \"raw\": \"\"\n        },\n        \"url\": {\n          \"raw\": \"http://localhost:3001/start\",\n          \"protocol\": \"http\",\n          \"host\": [\"localhost\"],\n          \"port\": \"3001\",\n          \"path\": [\"start\"]\n        }\n      },\n      \"response\": []\n    },\n    {\n      \"name\": \"Validate Moves\",\n      \"request\": {\n        \"method\": \"POST\",\n        \"header\": [\n          {\n            \"key\": \"Content-Type\",\n            \"value\": \"application/json\"\n          }\n        ],\n        \"body\": {\n          \"mode\": \"raw\",\n          \"raw\": \"[\\n    {\\\"x\\\": 1, \\\"y\\\": 0},\\n    {\\\"x\\\": 0, \\\"y\\\": 1}\\n]\"\n        },\n        \"url\": {\n          \"raw\": \"http://localhost:3001/validate\",\n          \"protocol\": \"http\",\n          \"host\": [\"localhost\"],\n          \"port\": \"3001\",\n          \"path\": [\"validate\"]\n        }\n      },\n      \"response\": []\n    },\n    {\n      \"name\": \"End Game\",\n      \"request\": {\n        \"method\": \"POST\",\n        \"header\": [],\n        \"body\": {\n          \"mode\": \"raw\",\n          \"raw\": \"\"\n        },\n        \"url\": {\n          \"raw\": \"http://localhost:3001/end\",\n          \"protocol\": \"http\",\n          \"host\": [\"localhost\"],\n          \"port\": \"3001\",\n          \"path\": [\"end\"]\n        }\n      },\n      \"response\": []\n    }\n  ]\n}\n```\n\n## Frontend (Client)\n\n### Overview\n\nThe Snake Game frontend application is built using React. It communicates with the backend API to manage the game state.\n\n### Setup Instructions\n\n#### Prerequisites\n\n- Node.js (version 14.x or higher)\n- npm (version 6.x or higher) or yarn (version 1.x or higher)\n\n#### Installation\n\n1. Clone the repository:\n\n```shell\ngit clone \u003crepository-url\u003e\n```\n\n2. Install the dependencies:\n\n```shell\nnpm install\n```\n\nor\n\n```shell\nyarn install\n```\n\n3. Start the development server:\n\n```shell\nnpm start\n```\n\nor\n\n```shell\nyarn start\n```\n\nThe application will be available at [http://localhost:5173](http://localhost:5173).\n\n### Project Structure\n\n```\nsnake-game/client\n├── public/\n│ ├── index.html\n│ └── ...\n├── src/\n│ ├── components/\n│ │ ├── Apple.tsx\n│ │ ├── GameBoard.tsx\n│ │ ├── Snake.tsx\n│ │ └── ...\n│ ├── App.tsx\n│ ├── index.tsx\n│ └── ...\n├── package.json\n├── tsconfig.json\n└── ...\n```\n\n#### Main Components\n\n##### App.tsx\n\nThe root component of the application. It sets up the main layout and includes the GameBoard component.\n\n##### GameBoard.tsx\n\nManages the game state and renders the game board. Communicates with the backend API to start the game, validate moves, and end the game.\n\n##### Snake.tsx\n\nRenders the snake on the game board based on its current position.\n\n##### Apple.tsx\n\nRenders the apple on the game board based on its current position.\n\n### API Integration\n\nThe frontend communicates with the backend API to manage the game state. The main API endpoints are:\n\n- `POST /api/start`: Starts a new game and initializes the game state.\n- `POST /api/validate`: Validates the moves and updates the game state.\n- `POST /api/end`: Ends the game and resets the game state.\n\n### Conclusion\n\nThis documentation provides an overview of the Snake Game backend (API) and frontend (Client) applications. It includes setup instructions, project structure, main components, and API integration details. For more information, refer to the source code and comments within the code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmartins-dev%2Fsnake-game","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmartins-dev%2Fsnake-game","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmartins-dev%2Fsnake-game/lists"}