# Catch The Dot Game

*By* [*adamlubek*](https://github.com/adamlubek)

This recipe shows usage of scan operator for state management in simple game

## Example Code

( [StackBlitz](https://stackblitz.com/edit/rxjs-catch-the-dot-game?file=index.ts) )

![Catch the dot](https://drive.google.com/uc?export=view\&id=1VKje20yXoplC2MPgzxP-OykpvhDuv6el)

### index.ts

```javascript
// RxJS v6+
import { fromEvent, interval } from 'rxjs';
import { tap, scan, map, switchMap, takeWhile } from 'rxjs/operators';
import {
  dot,
  updatedDot,
  setTimerText,
  resetDotSize,
  moveDot
} from './dom-updater';

interface State {
  score: number;
  intrvl: number;
}
const makeInterval = (val: State) =>
  interval(val.intrvl).pipe(
    map(v => 5 - v),
    tap(setTimerText)
  );
const gameState: State = { score: 0, intrvl: 500 };
const nextState = (acc: State) => ({
  score: (acc.score += 1),
  intrvl: acc.score % 3 === 0 ? (acc.intrvl -= 50) : acc.intrvl
});
const isNotGameOver = intervalValue => intervalValue >= 0;

const game$ = fromEvent(dot, 'mouseover').pipe(
  tap(moveDot),
  scan < Event,
  State > (nextState, gameState),
  tap(state => updatedDot(state.score)),
  switchMap(makeInterval),
  tap(resetDotSize),
  takeWhile(isNotGameOver)
);

game$.subscribe(n => {}, e => {}, () => setTimerText('ouch!'));
```

### dom-updater.ts

```javascript
const random = () => Math.random() * 300;
const elem = id => document.getElementById(id);
const setElementText = (elem, text) => (elem.innerText = text.toString());
const timer = elem('timer');
const setDotSize = size => {
  dot.style.height = `${size}px`;
  dot.style.width = `${size}px`;
};

export const dot = elem('dot');
export const updatedDot = score => {
  if (score % 3 === 0) {
    dot.style.backgroundColor =
      '#' + ((Math.random() * 0xffffff) << 0).toString(16);
  }
  setElementText(dot, score);
};
export const setTimerText = text => setElementText(timer, text);
export const moveDot = () => {
  setDotSize(5);
  dot.style.transform = `translate(${random()}px, ${random()}px)`;
};
export const resetDotSize = () => setDotSize(30);
```

#### html

```
<style>
  #dot {
    margin-top: 10px;
    height: 30px;
    width: 30px;
    background-color: lightgray;
    border-radius: 50%;
    transition: all 0.6s ease-in-out;
    text-align: center;
    color: white;
  }

  #timer {
    position: absolute;
    top: 150px;
    left: 150px;
    opacity: 0.1;
    font-size: 60px;
  }
</style>

<div id="timer"></div>
<div id="dot"></div>
```

* [fromEvent](/learn-rxjs-korean/learn-rxjs/recipes-1/creation/fromevent.md)
* [interval](/learn-rxjs-korean/learn-rxjs/recipes-1/creation/interval.md)
* [map](/learn-rxjs-korean/learn-rxjs/recipes-1/transformation/map.md)
* [scan](/learn-rxjs-korean/learn-rxjs/recipes-1/transformation/scan.md)
* [switchMap](/learn-rxjs-korean/learn-rxjs/recipes-1/transformation/switchmap.md)
* [takeWhile](/learn-rxjs-korean/learn-rxjs/recipes-1/filtering/takewhile.md)
* [tap](/learn-rxjs-korean/learn-rxjs/recipes-1/utility/do.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://junwoo45.gitbook.io/learn-rxjs-korean/learn-rxjs/recipes/catch-the-dot-game.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
