function stack calls - stop propagating functions RRS feed

  • Question

  • I've implemented tictactoe game for Windows 8 app with JavaScript. Everything works fine so far except one problem.  I've following function stack calls.

    ready() { // html page loads with a div with 9 canvas in a box. //another div to update "who's turn". This update div also shows who wins at the end of the game/or if it's tie.


    userTurn() {

    //do stuff;

    show it is computer's turn in update div.  //

    computerTurn(); //call computer's move.  Now this is a very quick move, computer takes it turn sooner than the "update div" can show "Computer's turn".


    computerMove() {

    //choose Location and draw computer's symbol.

    //show it is "user's turn" in update div.


    checkForWinners() {

    //check and update who's winning. OR if the game is tie.



    playAgain() {

    show MessageDialog with Windows.UI.Popups.MessageDiagllog("Play Again?");  //


    In PlayAgain, if computer wins it shows the message Dialog box, but update div shows "user's turn" because in background playAgain calls back checkForWinners which calls back userTurn.

    When a user wins, there comes a bigger problem. CheckForWinner checks that user is winning, so it calls PlayAgain which calls Message dialog Async method. Before it finishes or show popups, in background, function stack call unwinds and propagates to computerMove, which trys to take it turn and draw it's symbol in next blank canvas. Which starts calling rest of the functions.

    I'm using Windows.UI.Popup diaglog box to display who wins at the end of the game. It works fine


    Monday, February 10, 2014 9:56 PM

All replies

  • Several things you can do here.

    1) Set the "user's turn" div just before starting the user's turn, after checking winners, etc.

    2) Turn off the user's turn when you've discovered somebody's won

    Essentially you have a state machine here with different states for the different turns and the game over / play again states. You can bind / set the display when entering the new state rather than when exiting the previous state. That way the display will always match the current state no matter how you got there.


    Tuesday, February 11, 2014 7:23 AM
  • Rob, Thank you very much for the reply. I'm still trying to figure out how to turn off a user's turn. I'm using return statements anywhere I can think of, but nothing is working. I'll keep trying. But definitely need an expert's help.

    Thanks a lot.



    Wednesday, February 12, 2014 7:20 PM