• Stars
    star
    245
  • Rank 164,307 (Top 4 %)
  • Language
    JavaScript
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Javascript AI battle game. Play by creating a javascript function to command your battleship.

ClashJS.com

Demo Online

This is an experiment. The idea is to create a battle game, where the participants code their AI, and then we make them fight! You can play by adding your own AI to the game!

How to run the demo.

You'll need nodejs https://nodejs.org/en/download/, tested with current LTS version 12.x.

  1. Clone the repo.
git clone [email protected]:javierbyte/clashjs.git
  1. Move to the repo directory, install dependencies.
cd clashjs
npm install
  1. Run the repo!
npm start

It should open your browser with the http://localhost:3000/ demo URL.

The current configured players will start to play right away, at this point you can edit them on src/players/<PLAYER_NAME>.js and the browser should refresh on save.

How to participate.

Add your player as specificed in player definition in

/src/players/YOU.js

And then require yourself in

/src/Players.js

And run the demo again. Have fun!

Read the game definitions to learn how to create your player. Have fun!

Game. Functional Spec.

Introduction.

Games and coding are fun! So I want to make a game where we can confront AI vs AI in javascript.

The game is simple: we will put all the players in a battle arena, and then make them fight to death. There will be ammo in the arena so they can shoot each other. The last player alive wins!

Game Rules.

  • Every player will have a position and direction on the grid. A player can not go over the grid limits, and can only face north, east, south or west.
  • The game will be turn based. Every turn we will excecute the AI of every player passing as arguments:
    • The state of the player.
    • The state of all other players.
    • A environment configuration option with:
      • Grid size.
      • The position of the ammo.
  • Every turn a player must execute one of the following actions:
    • Move one step in its current direction. (move).
    • Turn into any of the four directions. (north, east, south, west).
    • Shoot. (shoot).
  • A player can shoot to try to destroy another player.
  • A player can collect ammo in the moment it steps over it. New ammo may appear in any moment of the game.

Game Definitions.

Player Definition.

Let the player definition (playerDefinition) be an object with the player info and its AI function.

{
  info: {
    name: 'javierbyte',
    style: 2 // one of the 6 styles (0 to 5)
  },
  ai: function(playerState, enemiesStates, gameEnvironment) {
    // think...
    return 'move';
  }
}

The AI function will receive playerState, enemiesStates (array of all the other players playerStates), and gameEnvironment as arguments, and must return one of the following strings:

  • move: To move one tile in the current direction.
  • north, east, south or west: To turn to that direction.
  • shoot. To shoot if the user has enough ammo.

Any other response, trying to move outside the arena size (gameEnvironment.gridSize) or trying to shoot without ammo, will result in a no-op.

Player State.

Let the player state (playerState) be an object with a player information like the following:

{
  position: `[<number>, <number>]`,
  direction: `<string>`, // One of 'north', 'east', 'south' or 'west'
  ammo: `<number>`,
  isAlive: `<bool>`
}

Game Environment.

Let the game environment (gameEnvironment) be a configuration object like the following:

{
  gridSize: [<number>, <number>],
  ammoPosition: <array of [<number>, <number>] arrays>
}

Game State.

Let the game state (gameState) be an object with the array of all user states, and the game environment.

{
  playerStates: <array of `playerStates`>,
  gameEnvironment: <`gameEnvironment`>
}

Game Technical Spec.

Problem.

We should make an app that can take functions provided by the users, execute them, and render the game as specified in the functional spec.

Constraints.

  • The game mechanics should avoid accidentally benefitting players by its random nature. The order of execution of the AIs should not favor any particular player. The position of newly created coins should be fair for all players.
  • Be safe. A player code should not be able to modify anything other than itself.
  • Be resilient as possible. If a player crashes or stop responding, the show must go on.

How this was made.

Architecture.

We can divide the problem in 3 big steps.

  • AI Runner. This will take all the user provided functions and the current game state, and execute every function.
    • This will take care of catch errors on the functions, and stop non-responding functions to hang the window.
  • Game Core. This will take the responses that the AI Runners sends, and apply the game logic on them.
    • Handle killed players.
    • Move and turn players.
    • Collect and count coins.
    • Generate new coins if necessary.
    • Set the paralized turns to players that shot.
    • Count if too many inactive turns have passed.
    • Stop the game when it ends.
  • Render. This will take the game state and render it nicely.

They will interact as follows:

AI Runner. Spec.

Problem.

The AI runner should execute all the functions that the players provided, with the current user state, all user states, and game enrivonment as arguments.

Constraints.

  • Isolate the user functions. They should not be allowed to modify any other code.
  • Catch executions errors, and simply return null as response to the Game Core.
  • Detect if any functions gets stuck in an infinite loop, and return null as response.

Hypothesis.

We can run the functions as WebWorkers because:

  • They can not access the dom and modify things.
  • Runs in a sandbox. If they crash or stop responding we can detect it.
  • Bonus: We can parallelise the excecution.

The game is designed to make irrelevant the order of execution of the AIs. We are safe in running all this asynchronously.

Solution.

To prevent the functions from taking too much time processing (probably because an infinite loop), we will create an array of nulls, where we will put the responses of the workers as they arrive. If X seconds passes (enough processing time for almost everything, except infinite loops, of couse) then we will pass the nullified response of that worker, and the Game Core will kill that player.

Game Core.

Player Class.

This javascript class will recive a playerDefinition and return a player instance.

Arguments:

  • playerDefinition.
  • [evtCallback] A callback that will receive the arguments evt and data.

Methods:

  • getInfo. Will return the player info.
  • execute. Will receive the following arguments:
    • playerState. The current player state.
    • enemiesStates. An array of all the other live players playerStates.
    • gameEnvironment. The game environment object.

CashJS Class.

This class will receive all the player definitions, generate the game states, and execute the players AIs.

Arguments:

Methods:

  • getState. Will return the current gameState.
  • nextStep. Will execute a step for every player (all individual plys). Will return the game state.
  • nextPly. Will execute the AI for the player in turn. Will return the game state.

Render.

React.

More Repositories

1

img2css

Convert any image to pure CSS. Recreates images using only box-shadows.
JavaScript
2,465
star
2

visual-center

Tool to better align logos in the center of a container.
JavaScript
1,410
star
3

control-user-cursor

Small experiment to 'control' the user cursor with JavaScript and CSS.
JavaScript
1,064
star
4

pintr

Create single line illustrations from your pictures. Get a drawing, SVG or coordinates for a CNC.
TypeScript
706
star
5

cohesive-colors

Tool that help you to create more cohesive color schemes. Add a tint to your existing colors.
TypeScript
519
star
6

react-blur

React component to blur image backgrounds using canvas.
JavaScript
455
star
7

emoji-to-scale

Your favorite emojis. To real-life scale, more or less.
JavaScript
348
star
8

docs2epub

Doc scraper and ebook generator / library. [unmaintained]
JavaScript
235
star
9

gitpage-timemachine

Visualize the evolution of your website in a 3D time-machine fashion.
Vue
160
star
10

brutalita

Brutalita is an experimental font and font editor, edit in your browser and download OTF.
TypeScript
152
star
11

react-number-easing

React component to interpolate rendering of numbers in the frontend.
JavaScript
150
star
12

react-textgradient

Text gradients with CSS with SVG fallback. [unmaintained]
JavaScript
122
star
13

morphin

Create pixel art interpolations from your own sprites and embed them in your website with only CSS.
JavaScript
81
star
14

canvas-image-utils

Utils for image data.
TypeScript
63
star
15

react-textselect

Select dropdown over text React component. [unmaintained]
JavaScript
49
star
16

colorblendjs

Color blends for javascript.
HTML
36
star
17

timelapses

JavaScript
35
star
18

react-tour

Create nice step-by-step intros with React. [unmaintained]
JavaScript
30
star
19

droste-creator

Create recursive images with the droste effect.
JavaScript
29
star
20

triangulator

Delaunay triangulation image generator.
JavaScript
16
star
21

react-native-ui

Fancy UI components for react-native
JavaScript
12
star
22

sentence-length-art

Tool that helps you visualize the length of your sentences.
JavaScript
12
star
23

color-sort

A simple lib for sorting colors by perception.
JavaScript
8
star
24

the-only-website-builder

[WIP] You are going to design a website like this anyway... let's do it the easy way
CSS
8
star
25

get-video-screenshot

Library to capture screnshots / thumbnails as images from a videos. Only works in the browser.
JavaScript
7
star
26

react-clickaway

Clickaway mixin for react, based on https://github.com/callemall work
JavaScript
7
star
27

react-app-starter

React starter boilerplate for small apps.
JavaScript
5
star
28

basic-stl-generator

Basic utils to generate a STL file in javascript
JavaScript
4
star
29

actualizar

Update every npm dependency to `@latest`.
JavaScript
4
star
30

react-color-range

Super tiny color input ranges for React.
JavaScript
3
star
31

lightroom-interpolator

Interpolate lightroom edits between photos for better timelapses.
JavaScript
3
star
32

swtimer

Startupweekend Timer
JavaScript
3
star
33

tronmmo

A TRON-style MMO game, made with nodejs and canvas.
JavaScript
3
star
34

cutereset

Small and cute flexbox ready css reset.
CSS
2
star
35

dancing-dots

A js lib to get nice moving dot coordinates.
JavaScript
2
star
36

points

Experiment with live image filters
Vue
1
star
37

time-remaning-estimator

Estimate the remaining time for any process with known progress.
1
star
38

webworker-demo

Demo para GDLJS
JavaScript
1
star
39

project-euler

JavaScript
1
star