I used ChatGPT (free version) to create a ZX Spectrum BASIC quiz game. I did not adjust the code afterwards at all.
I saw a few attempts at using ChatGPT in the ZX Spectrum Facebook group, where the consensus looked like "impressive, but we're not there yet". The LLM had failed to understand the Sinclair BASIC dialect, so although the logic of the simple programs could be sound they would not run on a Speccy.
But I figured, well, ChatGPT can be instructed to avoid the mistakes?
What I set out to do is a quiz game where the computer gives a name of an U.S. president, and the player has to know or guess when he was first elected.
I first made ChatGPT produce a list of presidents and the election year. As the result had some ambiguities and complexities, I asked to simplify the list so there are only first-time election years and no problem cases.
Then I asked it to format the list into ZX Spectrum DATA statements, starting from the line 1000, with 10 increments.
Only after seeing this list was formatted correctly, I asked ChatGPT to prepend the list with a Quiz-type BASIC game.
Although you can ask ChatGPT to use ZX Spectrum BASIC, this is not enough and the result is likely to be more generic BASIC dialect.
So, it is better to give a set of iron rules. Instead of trying to teach the peculiarities of Sinclair BASIC, I simplified the orders further, to avoid getting ChatGPT confused or produce something that would be difficult to debug. For example, dimensioned strings could be difficult to handle, as they work differently in the Spectrum.
bas2tap converts plain text listings into a Spectrum emulator TAP file. It's quite pedantic (i.e. accurate) so some of the rules below address this issue too. Bas2tap is an important tool as otherwise it would be too slow to test the listing, now I can just copypaste the ChatGPT output to a text file and run a command from terminal.
So, after outlining the game program, I told ChatGPT:
Remember that ZX Spectrum BASIC follows these rules:
-Use only one-letter length for variable names, and dimensioned variable names.
-Do not use IF-ENDIF reaching on multiple lines. Each IF-THEN part must only happen on one line.
-Do not use END. Use STOP instead.
-When assigning a (non-dimensioned) variable, you need to explicitly use LET
-When using GOTO or GOSUB, use GO TO and GO SUB instead.
-Do not use dimensioned strings. This could be achieved by looping and reading the DATA list for each item. e.g. RESTORE can reset the reading of the DATA list.
-The first element of a dimensioned array is 1, not 0.
You could also forbid it using RANDOMIZE, as chatGPT may use it to reset the random seed, which is not really necessary.
That point about dimensioned strings and data lists is only relevant if the program has to go through a data list in the first place.
The game itself is quite silly, as I gave rather silly instructions to ChatGPT. You get to guess presidents, but if you fail it will tell you the year. The game ends if you guess one president right. Well, that's not a huge problem.
The way the program works is also a little quirky, but this too is to be blamed on the instructions I had given to ChatGPT. The program stores the year values to an array, but not the names. So it will have to scan the DATA statements again to get the name.
What I liked most about this exercise was how I could populate the data table so fast and easy, making a potentially boring task a wheeze. Granted, a list of presidents could be googl-eyed, or acquired from Wolfram Aplha, but there's still dullness in cleaning up and reformatting the results to DATA lines suitable for the limitations of this particular listing.
The listing: (Ok I did remove line 10 that had the unnecessary RANDOMIZE)
20 DIM Y(40)
30 PRINT "PRESIDENT QUIZ! GUESS THE YEAR A PRESIDENT WAS FIRST ELECTED."
40 LET S=1000
50 RESTORE S
60 FOR I=1 TO 40
70 READ A$, B
80 LET Y(I)=B
90 NEXT I
100 LET Q=INT(RND*40)+1
110 RESTORE S
120 FOR I=1 TO Q
130 READ A$, B
140 NEXT I
150 PRINT "WHAT YEAR WAS ";A$;" ELECTED?"
160 INPUT A
170 IF A=Y(Q) THEN PRINT "CORRECT! WELL DONE!": GO TO 200
180 PRINT "WRONG! THE CORRECT YEAR IS ";Y(Q)
190 PRINT "LET'S CONTINUE.": GO TO 100
200 PRINT "DO YOU WANT TO PLAY AGAIN? (Y/N)"
210 INPUT B$
220 IF B$="Y" OR B$="y" THEN GO TO 100
1000 DATA "George Washington", 1788
1010 DATA "John Adams", 1796
1020 DATA "Thomas Jefferson", 1800
1030 DATA "James Madison", 1808
1040 DATA "James Monroe", 1816
1050 DATA "John Quincy Adams", 1824
1060 DATA "Andrew Jackson", 1828
1070 DATA "Martin Van Buren", 1836
1080 DATA "William Henry Harrison", 1840
1090 DATA "James K. Polk", 1844
1100 DATA "Zachary Taylor", 1848
1110 DATA "Franklin Pierce", 1852
1120 DATA "James Buchanan", 1856
1130 DATA "Abraham Lincoln", 1860
1140 DATA "Ulysses S. Grant", 1868
1150 DATA "Rutherford B. Hayes", 1876
1160 DATA "James A. Garfield", 1880
1170 DATA "Grover Cleveland", 1884
1180 DATA "Benjamin Harrison", 1888
1190 DATA "William McKinley", 1896
1200 DATA "Theodore Roosevelt", 1904
1210 DATA "William Howard Taft", 1908
1220 DATA "Woodrow Wilson", 1912
1230 DATA "Warren G. Harding", 1920
1240 DATA "Calvin Coolidge", 1924
1250 DATA "Herbert Hoover", 1928
1260 DATA "Franklin D. Roosevelt", 1932
1270 DATA "Harry S. Truman", 1948
1280 DATA "Dwight D. Eisenhower", 1952
1290 DATA "John F. Kennedy", 1960
1300 DATA "Lyndon B. Johnson", 1964
1310 DATA "Richard Nixon", 1968
1320 DATA "Jimmy Carter", 1976
1330 DATA "Ronald Reagan", 1980
1340 DATA "George H. W. Bush", 1988
1350 DATA "Bill Clinton", 1992
1360 DATA "George W. Bush", 2000
1370 DATA "Barack Obama", 2008
1380 DATA "Donald Trump", 2016
1390 DATA "Joe Biden", 2020
No comments:
Post a Comment