Code Monkey home page Code Monkey logo

horse-race's Introduction

Horse Racing Simulator

Setup

  1. Get the source code

    git clone [email protected]:shadowinek/horse-race.git
    
  2. Create local config file

    cp .env.example .env
    
  3. Set your APP_KEY and database connection in the .env file

  4. Install the app dependencies

    composer install
    
  5. Run migrations

    php artisan migrate
    
  6. Run your local server

    php -S localhost:8000 -t public
    
  7. Open the app in your browser and enjoy

Notes/Diary

03.10.2019 0:15
  • I played around the math and the first functions for the horse calculations.
  • The first idea is to not save the race progress, but to calculate it on fly for every view.
  • Implementation will be probably done in Lumen.
  • Open questions:
    • How to recognize/mark when the race is finished?
03.10.2019 11:35
  • Added base migrations and base classes
  • Working on the horse repository to randomly generate horses
  • On the horse creation I will calculate the final time and final step when the horse finish the race. This will allow me to mark the final step of the race and by the comparison, I can decide if the race is finished or not.
  • All floats will be saved as integers and divided by 10 afterwards.
  • TODO:
    • remove not needed files from Lumen
03.10.2019 15:40
  • Functionality is finished
  • TODO:
    • Improve the look
    • Add pages for Race and Horse
    • Add comments
    • Extract time format function into helper function
03.10.2019 17:30
  • I am out of time, but the task is finished
  • Added documentation
  • What is missing:
    • There are no tests. At least the calculation should be covered with tests
    • There are no visible validations. But I consider this ok as far as there are no user value inputs.
  • Challenges:
    • I wanted to avoid saving floats into the DB, but the calculations are less clear because of this.
    • I have config file for the given restrictions, but the app is not really configurable, when there are already data in the DB. The old data would be broken when I would change the values.

horse-race's People

Contributors

shadowinek avatar

horse-race's Issues

Late night thoughts on the core functions

<?php

class Test {

  private $speed;
  private $strength;
  private $endurance;

  private $baseSpeed = 5; // 5m/s
  private $distance = 1500; // 1500m

  public function __construct($speed, $strength, $endurance) {
    $this->speed = $speed;
    $this->strength = $strength;
    $this->endurance = $endurance;

    var_dump($speed, $strength, $endurance);
  }

  public function calculateSlowdown() {
    $reduction = $this->strength * 8 / 100;
    $slowdown = 5 * (1 - $reduction);

    return $slowdown;
  }

  public function calculateFullSpeedDistance() {
    return $this->endurance * 100;
  }

  public function calculateFullSpeedTime() {
    return $this->calculateFullSpeedDistance() / $this->calculateSpeed();
  }

  public function calculateSpeed() {
    return $this->speed + $this->baseSpeed;
  }

  public function calculateDistance($step) {
    $time = $step * 10; // each step in progress is 10s
    $distance = $time * $this->calculateSpeed();
    $fullSpeedDistance = $this->calculateFullSpeedDistance();

    if ($distance > $fullSpeedDistance) {
      $fullSpeedTime = $this->calculateFullSpeedTime();
      $reducedSpeedDistance = ($time - $fullSpeedTime) * ($this->calculateSpeed() - $this->calculateSlowdown());

      $distance = $fullSpeedDistance + $reducedSpeedDistance;
    }

    if ($distance > $this->distance) {
      return $this->distance;
    }

    return round($distance, 2) ;
  }

  public function calculateFinalTime() {
    $time = 0;
    $fullSpeedTime = $this->calculateFullSpeedTime();
    $fullSpeedDistance = $this->calculateFullSpeedDistance();

    $remainingDistance = $this->distance - $fullSpeedDistance;

    $remainingTime = $remainingDistance / ($this->calculateSpeed() - $this->calculateSlowdown());

    $time = $fullSpeedTime + $remainingTime;

    return round($time, 2);
  }
}

$test = new Test(10, 10, 9);

echo PHP_EOL;
echo 'slowdown: ' . $test->calculateSlowdown() . PHP_EOL;
echo 'fullspeeddistance: ' . $test->calculateFullSpeedDistance() . PHP_EOL;
echo 'fullspeedtime: ' . $test->calculateFullSpeedTime() . PHP_EOL;
echo 'speed: ' . $test->calculateSpeed() . PHP_EOL;
echo PHP_EOL;
for($i=1;$i<20;$i++) {
  echo '#' . $i .' distance: ' . $test->calculateDistance($i) . 'm' .  PHP_EOL;
}
echo PHP_EOL;
echo 'final time: ' . $test->calculateFinalTime() . 's' .  PHP_EOL;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.