Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 1 / JANUARY 1983 / PAGE 279

Brain warp. (puzzle program) Dub Scoggin.

Brain Warp

Brain Warp is a puzzle program designed to make use of the sound and color capabilities of the Commodore Vic-20. The program fits in standard Vic memory. The object is to manipulate the rows and columns of six squares and make order out of chaos. Six squares of scattered color are rearranged into six solidcolored squares. Sonds easy, huh? Try it.

There are thousands of paths to a solution and a total of 720 possible solutions. Most solutions will average around 60 moves, though it is possible to reach one in 30 or fewer moves. The more you practice, the better you'll get and the fewer moves you'll require.

Moves are made by specifying a pair of numbered squares and then designating a particular row or column. This row or column is then swapped between the two squares. For example, if squares two and five are specified and the designated row or column is row three, then row three of square two will swap places with row three of square five. This move is depicted in Figure 1.

The puzzle is designed around a three-dimensional array Z(SRC), where S is the square number and R and C are the row and column numbers, respectively. There are six squares consisting of four rows and four columns each, hence a total of 96 block positions. Z(2,2,3) designates the third column of the second row, while Z(2,3,4) designates the fourth column of the third row. Both of these squares, as we can see from the first number, are in square two. Rows are horizontal and numbered from top to bottom, while columns are vertical and numbered from left to right. See Figure 2 for an example of the block positions of square two.

Square three would be identical to this, but in each case the first number would be three. Of course, the contents, colors and screen locations would also be different.

By holding the square and row numbers constant and varying the column number from one to four, it is possible to isolate a row of a particular square and manipulate it. Holding the square and column numbers constant and varying the row number from one to four, we get the contents of a particular column. Again, Figure 2 should help make this clear.

Initially, the program reads a number into each of the 96 block positions. This number is the color code for the particular location and determines the color of the block. The colors used are black, white, red, cyan, blue, and yellow, and the codes are those found in the Vic manual.

Moving a color code to a different position in the array causes the block at that position to change color. Movements, of course, are made a row or column at a time, rather than by individual block.

Step 20 of the program reserves 96 blocks in memory for the contents of the three-dimensional array and an additional six on which the screen location of each square is based. Step 30 reads the values into these two arrays. K is the column number, J is the row number and I is the square number.

Step 40 puts the square numbers on the screen and marks the start of the main program loop. Steps 50-70 translate the current contents of arry Z into the six squares of sixteen blocks each on the screen, as well as providing the double-voiced sound effect.

In step 60 LO(I) is the screen position to the left and above where square I will be placed. Multiplying J by 22 moves down one space. Then, as K goes from 1 to 4, the top row of the square is set in place. When J then changes to 2, we drop to the second row and place its four columns, and so on.

POKEing 207 into each location gives each square its border and sets off the colors. In the latter half of step 60 SC is used to shift to the screen color coding and Z(IJK) determines the color of the block just positioned.

Steps 80-100 check for a correct solution by comparing the color of the first block (row one, column one) of each square to each of the other blocks. You may notice a slight delay here, especially as the puzzle nears solution. If a mismatch is found, the triple loop is terminated and the program continues. If no mismatches are found, then the puzzle is solved and step 110 initiates the end-of-game routine starting at line 3000.

To test the end-of-game routine you may temporarily change 120 in line 90 to 110. The program will then go directly to the end-of-game routine on a mismatch.

Beginning in step 130 and ending with the end of the main program loop in step 210, the user inputs the pair of squares and the row or column he wants to swap between them. The swaps are actually made in step 180 for rows or in step 200 for columns.

To swap rows, I hold the square and row number constant and vary the column number using M. F and T represent the input "from' and "to' squares, respectively, while R is the input row number to be swapped. The contents of Z(FRM) are temporarily placed in HO while the contents of Z(TRM) are moved to Z(FRM). The contents of HO are then placed in Z(TRM) and the swap is completed. A similar method is used for swapping columns.

Step 210 keeps count of the number of moves and marks the end of the main program loop. Control passes back to step 40 and the screen is set up for your next move.

Steps 1000-1050 are the contents of the LO array and the initial contents of the Z array. LO is the first number, while the next 16 are the color codes for that square. If, in trying to solve the puzzle, you run into a non-solvable setup, check to be sure these numbers are correct. Certain arrangements will preclude a solution, but not give an error message to tell you about it.

These values were obtained by setting up a solution, i.e., making the numbers for square one all zeros, etc., then using the program to scramble the squares. I then recorded the result, translated to the color codes and altered the data. The same method may be used to change the initial setup, but the values should not be altered haphazardly.

Steps 2000-2110 are the title display and direction subroutine called by line 10. The value of P in step 2010 is used to create the sound and color display at the start of the program run.

Steps 3000-3100 are the end-of-game subroutine called by line 110. Location 36864(H), which normally contains 5, is altered to produce the horizontal movement, while 36865 (normally 25) is altered to get vertical movement. These numbers are swapped back and forth in steps 3050 and 3060.

Step 3080 sets the value of A$, and step 3090 causes it to be printed one letter at a time. Step 3100 changes the screen and cursor to purple, causing the word "ready' and the cursor to be invisible. Pressing the CTRL key and one of the other keys will cause the cursor to reappear.

I have included Remarks in the program listing as an aid to understanding it, but recommend they not be typed in, because of the memory they consume.

Good luck, and I hope you enjoy Brain Warp.

Table: Variable Listing

Table: Listing 1.

Photo: Figure 1. In this example, swapping row three between squares two and five will produce a column of solid black which can then be joined with other such columns to form a black square.

Photo: Figure 2. This figure illustrates the block positions for square two and shows how they are numbered. It also gives the initial contents and corresponding colors for each block. The top number in each block is the screen location based on LO(2).