Skip to content

Commit

Permalink
State logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Petah committed Sep 4, 2020
1 parent bc00cca commit 7dcf8f6
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 36 deletions.
22 changes: 11 additions & 11 deletions src/lib/randomMove.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import { isFree } from './isFree';
import { log } from './log';
import { BTData } from '../types/BTData';
import { BTData, BTRequest } from '../types/BTData';
import { MoveDirection } from '../types/MoveDirection';
import { shuffle } from './shuffle';

export function randomMove(data: BTData) {
export function randomMove(request: BTRequest) {
let direction, x, y;
const directions = [MoveDirection.LEFT, MoveDirection.RIGHT, MoveDirection.UP, MoveDirection.DOWN];
shuffle(directions);
while (direction = directions.pop()) {
switch (direction) {
case MoveDirection.UP:
x = data.you.body[0].x;
y = data.you.body[0].y - 1;
x = request.body.you.body[0].x;
y = request.body.you.body[0].y - 1;
break;

case MoveDirection.DOWN:
x = data.you.body[0].x;
y = data.you.body[0].y + 1;
x = request.body.you.body[0].x;
y = request.body.you.body[0].y + 1;
break;

case MoveDirection.LEFT:
x = data.you.body[0].x - 1;
y = data.you.body[0].y;
x = request.body.you.body[0].x - 1;
y = request.body.you.body[0].y;
break;

case MoveDirection.RIGHT:
x = data.you.body[0].x + 1;
y = data.you.body[0].y;
x = request.body.you.body[0].x + 1;
y = request.body.you.body[0].y;
break;
}
if (isFree(data, x, y)) {
if (isFree(request.body, x, y)) {
log('randomMove', direction);
return direction;
}
Expand Down
22 changes: 21 additions & 1 deletion src/server/snakes/base-snake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Server } from '../Server';
import { BTRequest } from '../../types/BTData';
import { MoveDirection } from '../../types/MoveDirection';

export interface StateFunction {
(request: BTRequest): MoveDirection
}

export abstract class BaseSnake {
public info: any = {};
Expand All @@ -8,11 +13,26 @@ export abstract class BaseSnake {
public enabled: boolean = false;
public port: number;

protected states: StateFunction[];

constructor() {
this.info.name = this.name;
}

start(request: BTRequest): void {
public start(request: BTRequest): void {
}

public move(request: BTRequest) {
let direction;
for (const state of this.states) {
if (direction = state(request)) {
request.log(state.name);
return {
move: direction,
shout: state.name,
};
}
}
}

public get name() {
Expand Down
2 changes: 1 addition & 1 deletion src/server/snakes/dunno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Dunno extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
2 changes: 1 addition & 1 deletion src/server/snakes/keep-away.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class KeepAway extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
2 changes: 1 addition & 1 deletion src/server/snakes/project-z-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ProjectZ2 extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
2 changes: 1 addition & 1 deletion src/server/snakes/project-z.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ProjectZ extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
2 changes: 1 addition & 1 deletion src/server/snakes/rando.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Rando extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
32 changes: 15 additions & 17 deletions src/server/snakes/tail-chase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { randomMove } from '../../lib/randomMove';
import { smartRandomMove } from '../../lib/smartRandomMove';
import { moveTowardsTail } from '../../lib/moveTowardsTail';
import { moveAway } from '../../lib/moveAway';
import { BaseSnake } from './base-snake';
import { BaseSnake, StateFunction } from './base-snake';
import { ISnake } from './snake-interface';
import { closestEnemyHead } from '../../lib/closestEnemyHead';
import { MoveDirection } from '../../types/MoveDirection';

export class TailChase extends BaseSnake implements ISnake {
public port: number = 9005;
Expand All @@ -19,27 +20,24 @@ export class TailChase extends BaseSnake implements ISnake {
public headType = HeadType.PIXEL;
public tailType = TailType.HOOK;

move(request: BTRequest) {
let direction;
protected states: StateFunction[] = [
this.getFood,
this.runAway,
moveTowardsTail,
smartRandomMove,
randomMove,
];

private getFood(request: BTRequest): MoveDirection {
if (request.body.you.health < request.body.board.width || request.body.you.health < request.body.board.height) {
direction = moveTowardsFoodPf(request);
return moveTowardsFoodPf(request);
}
}

private runAway(request: BTRequest): MoveDirection {
const closest = closestEnemyHead(request);
if (closest && closest.path.length <= 4) {
direction = moveAway(request);
}
if (!direction) {
direction = moveTowardsTail(request);
}
if (!direction) {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
return moveAway(request);
}
return {
move: direction,
};
}
}
2 changes: 1 addition & 1 deletion src/server/snakes/tak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Tak extends BaseSnake implements ISnake {
direction = smartRandomMove(request);
}
if (!direction) {
direction = randomMove(request.body);
direction = randomMove(request);
}
return {
move: direction,
Expand Down
2 changes: 1 addition & 1 deletion src/web/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function loadGrid(request: BTRequest) {
$('.log').html('');
if (request.logs) {
for (const log of request.logs) {
$('<div>').text(log).appendTo('.log');
$('<div>').text(log.map(l => typeof l === 'string' ? l : JSON.stringify(l, null, 4)).join(' ')).appendTo('.log');
}
}

Expand Down

0 comments on commit 7dcf8f6

Please sign in to comment.