Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
[JavaScript] Best way to implement a game loop for a board game?
Author Summary
blowout is in javascript
Post Body

I am creating a board game in JavaScript/HTML that has the option to play human vs. computer or computer vs. computer. It seems I get stuck in a loop when implementing the computer's turn. Here is an outline of the code:

function drawBoard() {
    canvas.drawImage(...); //draw pieces and grid lines.
}

function makeMove(x, y) {
    // all game logic
    drawBoard();
    if (player turn == computer) {
        computerMove();
    }
}

function humanPlayerMousedownEvent() {
    makeMove(x, y);
}

function computerMove() {
    // computer logic
    sleep(500 ms);
    makeMove(x, y);
}

For some reason the board is not redrawn until after the computer has made a move. E.g. mouseDown -> 500 ms -> computerMove -> drawBoard -- Even though drawBoard is before computerMove. If I set computer vs. computer I get an endless loop. I think the problem is that the computer calls makeMove from within makeMove causing recursion. Does anyone have advice on how to decouple the makeMove loop?

Thanks!

Author
Account Strength
80%
Account Age
14 years
Verified Email
Yes
Verified Flair
No
Total Karma
202
Link Karma
158
Comment Karma
20
Profile updated: 3 days ago
Posts updated: 1 month ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
9 years ago