0

I'm making a N*N queens problem with gui. I want the gui to stop for x seconds each move of every queen, problem is, the program just stacks all the waits together and then runs everything at speed. I'm giving the code here: http://pastebin.com/s2VT0E49

EDIT: This is my workspace:

board := MyBoard new initializeWithStart: 8.
Transcript show:'something'.
3 seconds asDelay wait.
board solve.
3 seconds asDelay wait.
board closeBoard.

This is where i want the wait to happen

canAttack: testRow x: testColumn
    | columnDifference  squareMark |
    columnDifference := testColumn - column.
    ((row = testRow
        or: [row + columnDifference = testRow])
        or: [row - columnDifference = testRow]) ifTrue: [
            squareDraw := squareDraw
            color: Color red.
            0.2 seconds asDelay wait.           
            ^ true ].

  squareDraw := squareDraw color: Color black.
  ^ neighbor canAttack: testRow x: testColumn
Max Leske
  • 5,007
  • 6
  • 42
  • 54
Ariel Pinchover
  • 654
  • 3
  • 17

3 Answers3

5

Since you're using Morphic you should use stepping for animation, not processes or delays. In your Morph implement a step method. This will be executed automatically and repeatedly. Also implement stepTime to answer the interval in milliseconds, e.g. 4000 for every 4 seconds.

Inside the step method, calculate your new state. If each queen is modeled as a separate Morph and you just move the positions, then Morphic will take care of updating the screen. If you have your own drawOn: method then call self changed in your step method so that Morphic will later invoke your drawing code.

See this tutorial: http://static.squeak.org/tutorials/morphic-tutorial-1.html

codefrau
  • 4,583
  • 17
  • 17
  • Im using the squeak rectangle, i'm not going to change its code. on the step thingy, could you provide a code example? – Ariel Pinchover May 10 '16 at 15:39
  • You cannot do this from a workspace (at least not cleanly). Put your code in an actual class (see the tutorial link I added). There is a gazillion examples in the image: browse the implementers of `step`. Squeak's source code is like a hypertext database linked via "senders" and "implementers". You select a message name and press cmd-n or cmd-m to see them. – codefrau May 11 '16 at 09:15
4

The process you're suspending is the one your program is running in. This process also happens to be the UI process. So when you suspend your program you also suspend the UI and therefore the UI elements never get a chance to update themselves. Try running your program in a separate process:

[ MyProgram run ] forkAt: Processor userBackgroundPriority.

Note that the UI process usually runs at priority 40. #userBackgroundPriority is 30. This makes sure that you can't lock up the UI.

Max Leske
  • 5,007
  • 6
  • 42
  • 54
2

To make your workspace code work insert this before the delay:

World doOneCycle.

This will cause the Morphic world to be redisplayed.

Note that this is quick-and-very-dirty hack and not the proper way to do it (see my other answer). Delays block the whole UI process, whereas the whole point of Morphic is that you can do many things simultaneously while your code is executing.

codefrau
  • 4,583
  • 17
  • 17