Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Vasquez authored Oct 17, 2023
0 parents commit 0640c57
Show file tree
Hide file tree
Showing 10 changed files with 1,582 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store

build
node_modules
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:16

ENV NODE_ENV=production

WORKDIR /app

COPY . .
RUN npm install --production

CMD [ "npm", "start" ]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Battlesnake Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Battlesnake TypeScript Starter Project

An official Battlesnake template written in TypeScript. Get started at [play.battlesnake.com](https://play.battlesnake.com).

![Battlesnake Logo](https://media.battlesnake.com/social/StarterSnakeGitHubRepos_TypeScript.png)

This project is a great starting point for anyone wanting to program their first Battlesnake in TypeScript. It can be run locally or easily deployed to a cloud provider of your choosing. See the [Battlesnake API Docs](https://docs.battlesnake.com/api) for more detail.

[![Run on Replit](https://repl.it/badge/github/BattlesnakeOfficial/starter-snake-typescript)](https://replit.com/@Battlesnake/starter-snake-typescript)

## Technologies Used

This project uses [TypeScript](https://www.typescriptlang.org/), [Node.js](https://nodejs.org/en/), and [Express](https://expressjs.com/). It also comes with an optional [Dockerfile](https://docs.docker.com/engine/reference/builder/) to help with deployment.

## Run Your Battlesnake

Install dependencies using npm

```sh
npm install
```

Start your Battlesnake

```sh
npm run start
```

You should see the following output once it is running

```sh
Running Battlesnake at http://0.0.0.0:8000
```

Open [localhost:8000](http://localhost:8000) in your browser and you should see

```json
{"apiversion":"1","author":"","color":"#888888","head":"default","tail":"default"}
```

## Play a Game Locally

Install the [Battlesnake CLI](https://github.com/BattlesnakeOfficial/rules/tree/main/cli)
* You can [download compiled binaries here](https://github.com/BattlesnakeOfficial/rules/releases)
* or [install as a go package](https://github.com/BattlesnakeOfficial/rules/tree/main/cli#installation) (requires Go 1.18 or higher)

Command to run a local game

```sh
battlesnake play -W 11 -H 11 --name 'TypeScript Starter Project' --url http://localhost:8000 -g solo --browser
```

## Next Steps

Continue with the [Battlesnake Quickstart Guide](https://docs.battlesnake.com/quickstart) to customize and improve your Battlesnake's behavior.

**Note:** To play games on [play.battlesnake.com](https://play.battlesnake.com) you'll need to deploy your Battlesnake to a live web server OR use a port forwarding tool like [ngrok](https://ngrok.com/) to access your server locally.
102 changes: 102 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Welcome to
// __________ __ __ .__ __
// \______ \_____ _/ |__/ |_| | ____ ______ ____ _____ | | __ ____
// | | _/\__ \\ __\ __\ | _/ __ \ / ___// \\__ \ | |/ // __ \
// | | \ / __ \| | | | | |_\ ___/ \___ \| | \/ __ \| <\ ___/
// |________/(______/__| |__| |____/\_____>______>___|__(______/__|__\\_____>
//
// This file can be a nice home for your Battlesnake logic and helper functions.
//
// To get you started we've included code to prevent your Battlesnake from moving backwards.
// For more info see docs.battlesnake.com

import runServer from './server';
import { GameState, InfoResponse, MoveResponse } from './types';

// info is called when you create your Battlesnake on play.battlesnake.com
// and controls your Battlesnake's appearance
// TIP: If you open your Battlesnake URL in a browser you should see this data
function info(): InfoResponse {
console.log("INFO");

return {
apiversion: "1",
author: "", // TODO: Your Battlesnake Username
color: "#888888", // TODO: Choose color
head: "default", // TODO: Choose head
tail: "default", // TODO: Choose tail
};
}

// start is called when your Battlesnake begins a game
function start(gameState: GameState): void {
console.log("GAME START");
}

// end is called when your Battlesnake finishes a game
function end(gameState: GameState): void {
console.log("GAME OVER\n");
}

// move is called on every turn and returns your next move
// Valid moves are "up", "down", "left", or "right"
// See https://docs.battlesnake.com/api/example-move for available data
function move(gameState: GameState): MoveResponse {

let isMoveSafe: { [key: string]: boolean; } = {
up: true,
down: true,
left: true,
right: true
};

// We've included code to prevent your Battlesnake from moving backwards
const myHead = gameState.you.body[0];
const myNeck = gameState.you.body[1];

if (myNeck.x < myHead.x) { // Neck is left of head, don't move left
isMoveSafe.left = false;

} else if (myNeck.x > myHead.x) { // Neck is right of head, don't move right
isMoveSafe.right = false;

} else if (myNeck.y < myHead.y) { // Neck is below head, don't move down
isMoveSafe.down = false;

} else if (myNeck.y > myHead.y) { // Neck is above head, don't move up
isMoveSafe.up = false;
}

// TODO: Step 1 - Prevent your Battlesnake from moving out of bounds
// boardWidth = gameState.board.width;
// boardHeight = gameState.board.height;

// TODO: Step 2 - Prevent your Battlesnake from colliding with itself
// myBody = gameState.you.body;

// TODO: Step 3 - Prevent your Battlesnake from colliding with other Battlesnakes
// opponents = gameState.board.snakes;

// Are there any safe moves left?
const safeMoves = Object.keys(isMoveSafe).filter(key => isMoveSafe[key]);
if (safeMoves.length == 0) {
console.log(`MOVE ${gameState.turn}: No safe moves detected! Moving down`);
return { move: "down" };
}

// Choose a random move from the safe moves
const nextMove = safeMoves[Math.floor(Math.random() * safeMoves.length)];

// TODO: Step 4 - Move towards food instead of random, to regain health and survive longer
// food = gameState.board.food;

console.log(`MOVE ${gameState.turn}: ${nextMove}`)
return { move: nextMove };
}

runServer({
info: info,
start: start,
move: move,
end: end
});
Loading

0 comments on commit 0640c57

Please sign in to comment.