GAZAR

Principal Engineer | Mentor

Use arrow keys to move the robot

Robot Game

Robot Game

This is a simple game where you control a robot using arrow keys.

How to play

Use arrow keys to move the robot.

Rules

The robot can't move outside the board. The robot can't move to a cell that is already occupied by another robot. The robot can't move to a cell that is already occupied by a wall.

Code

class Cell {
  private readonly x: number;
  private readonly y: number;
  private occupied: boolean;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
    this.occupied = false;
  }

  public getX(): number {
    return this.x;
  }

  public getY(): number {
    return this.y;
  }

  public isOccupied(): boolean {
    return this.occupied;
  }

  public setOccupied(occupied: boolean): void {
    this.occupied = occupied;
  }

  public isFree(): boolean {
    return !this.occupied;
  }
}
export default Cell;

Cell helps to understand the state of a cell in the board.

import Cell from "./Cell";
export class Board {
  public static readonly WIDTH = 8;
  public static readonly HEIGHT = 8;

  private readonly cells: Cell[][];

  constructor() {
    this.cells = [];
    for (let i = 0; i < Board.WIDTH; i++) {
      this.cells[i] = [];
      for (let j = 0; j < Board.HEIGHT; j++) {
        this.cells[i][j] = new Cell(i, j);
      }
    }
  }

  public getCell(x: number, y: number): Cell | null {
    if (x < 0 || x >= Board.WIDTH || y < 0 || y >= Board.HEIGHT) {
      return null;
    }
    return this.cells[x][y];
  }

  public getCells(): Cell[][] {
    return this.cells;
  }

  public freeCell = (x: number, y: number): void => {
    this.cells[x][y].setOccupied(false);
  };

  public occcupyCell = (x: number, y: number): void => {
    this.cells[x][y].setOccupied(true);
  };
}

export default Board;

And Roboto:

import type Board from "./Board";
import { Direction } from "./Types";

class Robot {
  private x: number;
  private y: number;
  private board: Board;

  constructor(board: Board, x: number, y: number) {
    this.x = x;
    this.y = y;
    this.board = board;
    this.board.occcupyCell(x, y);
  }

  public getX(): number {
    return this.x;
  }

  public getY(): number {
    return this.y;
  }

  public setX(x: number): void {
    this.x = x;
  }

  public setY(y: number): void {
    this.y = y;
  }

  public calculateMyNewPostion = (direction: Direction) => {
    switch (direction) {
      case Direction.UP:
        return { x: this.x, y: this.y + 1 };
      case Direction.DOWN:
        return { x: this.x, y: this.y - 1 };
      case Direction.LEFT:
        return { x: this.x - 1, y: this.y };
      case Direction.RIGHT:
        return { x: this.x + 1, y: this.y };
    }
  };

  public move(direction: Direction): void {
    const { x, y } = this.calculateMyNewPostion(direction);
    const cell = this.board.getCell(x, y);
    if (!cell || cell.isOccupied()) {
      return;
    }

    this.board.freeCell(this.x, this.y);
    this.setX(x);
    this.setY(y);
    this.board.occcupyCell(x, y);
  }
}

export default Robot;