7 Replies - 5184 Views - Last Post: 04 July 2013 - 10:44 PM Rate Topic: -----

#1 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

How (not) to port a 30-year old BASIC game to Java.

Posted 28 September 2012 - 04:42 PM

So, I saw someone talking about Java's lack of a goto statement. Fear not: GOTO can be emulated using a while loop around a giant switch statement containing the lines of your program. Like so:
package basic.games;

import java.util.*;
import java.io.*;

public class Bagels
{
    public static int line = 0;
    public static boolean running = true;
    public static Random random;
    
    public static String
    TAB(int spaces)
    {
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<spaces; ++i)
            sb.append(" ");
        return sb.toString();
    }
    
    public static void
    PRINT( String s )
    {
        System.out.println(s);
    }
    
    public static void
    PRINT()
    {
        System.out.println();
    }
    
    public static void
    REM( String comment )
    {
    }
    
    public static String
    INPUT( String prompt )
    {
        return (System.console().readLine( prompt )).toUpperCase();
    }
    
    public static void
    END()
    {
        running = false;
    }
    
    public static void
    GOTO( int l )
    {
        line = l-1;
    }
    
    public static String
    LEFT$( String s, int len )
    {
        return s.substring(0,len);
    }
    
    public static double
    RND()
    {
        return random.nextDouble();
    }
    
    public static int
    INT( double v )
    {
        return (int)v;
    }
    
    public static int
    LEN( String s )
    {
        return s.length();
    }
    
    public static String
    MID$(String s, int start, int len )
    {
        return s.substring(start-1,start+len-1);
    }
    
    public static int
    ASC( String s )
    {
        if( s.length()==0 )
        {
            System.out.println("?ILLEGAL QUANTITY ERROR");
            System.exit(0);
        }
        return (int)s.charAt(0);
    }
    
    public static void
    main( String[] argv )
    {
        int[] A1 = new int[6];
        int[] A = new int[3];
        int[] B = new int[3];
        int T=0, Y=0, I=0, J=0, Z=0, C=0, D=0;
        String A$="";
        random = new Random();
        
        while( running )
        {
            switch(line)
            {
                //5 PRINT TAB(33);"BAGELS"
                case 5: PRINT( TAB(33) + "BAGELS" ); break;
                //10 PRINT TAB(15);"CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY":PRINT:PRINT
                case 10: PRINT( TAB(15) + "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY" ); PRINT(); PRINT(); break;
                //15 REM *** BAGLES NUMBER GUESSING GAME
                case 15: REM( "*** BAGELS NUMBER GUESSING GAME" ); break;
                //20 REM *** ORIGINAL SOURCE UNKNOWN BUT SUSPECTED TO BE
                case 20: REM( "*** ORIGINAL SOURCE UNKNOWN BUT SUSPECTED TO BE"); break;
                //25 REM *** LAWRENCE HALL OF SCIENCE, U.C. BERKELY
                case 25: REM( "*** LAWRENCE HALL OF SCIENCE, U.C. BERKELY"); break;
                //30 DIM A1(6),A(3),B(3)
                case 30: A1 = new int[6]; A = new int[3]; B = new int[3]; break;
                //40 Y=0:T=255
                case 40: Y=0; T=255; break;
                //50 PRINT:PRINT:PRINT
                case 50: PRINT();PRINT();PRINT(); break;
                //70 INPUT "WOULD YOU LIKE THE RULES (YES OR NO)";A$
                case 70: A$ = INPUT("WOULD YOU LIKE THE RULES (YES OR NO) "); break;
                //90 IF LEFT$(A$,1)="N" THEN 150
                case 90: if( LEFT$(A$,1).equals("N") ) GOTO(149); break;
                //100 PRINT:PRINT "I AM THINKING OF A THREE-DIGIT NUMBER.  TRY TO GUESS"
                case 100: PRINT();PRINT("I AM THINKING OF A THREE-DIGIT NUMBER.  TRY TO GUESS"); break;
                //110 PRINT "MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"
                case 110: PRINT("MY NUMBER AND I WILL GIVE YOU CLUES AS FOLLOWS:"); break;
                //120 PRINT "   PICO   - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"
                case 120: PRINT("   PICO   - ONE DIGIT CORRECT BUT IN THE WRONG POSITION"); break;
                //130 PRINT "   FERMI  - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"
                case 130: PRINT("   FERMI  - ONE DIGIT CORRECT AND IN THE RIGHT POSITION"); break;
                //140 PRINT "   BAGELS - NO DIGITS CORRECT"
                case 140: PRINT("   BAGELS - NO DIGITS CORRECT"); break;
                //150 FOR I=1 TO 3
                case 149: I=1; break;
                case 150: if( I==4 ) GOTO(210); break;
                //160 A(I)=INT(10*RND(1))
                /* Why doesn't java suppport OPTION BASE 1???? */
                case 160: A[I-1]=INT(10*RND()); break;
                //165 IF I-1=0 THEN 200
                case 165: if(I-1==0) GOTO(200); break;
                //170 FOR J=1 TO I-1
                case 169: J=1; break;
                case 170: if( J==I ) GOTO(200); break;
                //180 IF A(I)=A(J) THEN 160
                case 180: if(A[I-1]==A[J-1]) GOTO(160); break;
                //190 NEXT J
                case 190: J=J+1; GOTO(170); break;
                //200 NEXT I
                case 200: I=I+1; GOTO(150); break;
                //210 PRINT:PRINT "O.K.  I HAVE A NUMBER IN MIND."
                case 210: PRINT();PRINT("O.K.  I HAVE A NUMBER IN MIND."); break;
                //220 FOR I=1 TO 20
                case 219: I=1; break;
                case 220: if( I==21 ) GOTO(610); break;
                //230 PRINT "GUESS #";I,
                case 230: break;
                //240 INPUT A$
                case 240: A$ = INPUT("GUESS #"+I+" "); break;
                //245 IF LEN(A$)<>3 THEN 630
                case 245: if( LEN(A$)!=3 ) GOTO(630); break;
                //250 FOR Z=1 TO 3:A1(Z)=ASC(MID$(A$,Z,1)):NEXT Z
                case 250: for(Z=1; Z<=3; Z++) A1[Z-1] = ASC(MID$(A$,Z,1)); break;
                //260 FOR J=1 TO 3
                case 259: J=1; break;
                case 260: if(J==4) GOTO(295); break;
                //270 IF A1(J)<48 THEN 300
                case 270: if(A1[J-1]<48) GOTO(300); break;
                //280 IF A1(J)>57 THEN 300
                case 280: if(A1[J-1]>57) GOTO(300); break;
                //285 B(J)=A1(J)-48
                case 285: B[J-1]=A1[J-1]-48; break;
                //290 NEXT J
                case 290: ++J; GOTO(260); break;
                //295 GOTO 320
                case 295: GOTO(320); break;
                //300 PRINT "WHAT?"
                case 300: PRINT("WHAT?"); break;
                //310 GOTO 230
                case 310: GOTO(230); break;
                //320 IF B(1)=B(2) THEN 650
                case 320: if(B[0]==B[1]) GOTO(650); break;
                //330 IF B(2)=B(3) THEN 650
                case 330: if(B[1]==B[2]) GOTO(650); break;
                //340 IF B(3)=B(1) THEN 650
                case 340: if(B[2]==B[0]) GOTO(650); break;
                //350 C=0:D=0
                case 350: C=0; D=0; break;
                //360 FOR J=1 TO 2
                case 359: J=1; break;
                case 360: if(J==3) GOTO(420); break; 
                //370 IF A(J)<>B(J+1) THEN 390
                case 370: if(A[J-1]!=B[J]) GOTO(390); break;
                //380 C=C+1
                case 380: C=C+1; break;
                //390 IF A(J+1)<>B(J) THEN 410
                case 390: if(A[J]!=B[J-1]) GOTO(410); break;
                //400 C=C+1
                case 400: C=C+1; break;
                //410 NEXT J
                case 410: J=J+1; GOTO(360); break;
                //420 IF A(1)<>B(3) THEN 440
                case 420: if(A[0]!=B[2]) GOTO(440); break;
                //430 C=C+1
                case 430: C=C+1; break;
                //440 IF A(3)<>B(1) THEN 460
                case 440: if(A[2]!=B[0]) GOTO(459); break;
                //450 C=C+1
                case 450: C=C+1; break;
                //460 FOR J=1 TO 3
                case 459: J=1; break;
                case 460: if(J==4) GOTO(500); break;
                //470 IF A(J)<>B(J) THEN 490
                case 470: if(A[J-1]!=B[J-1]) GOTO(490); break;
                //480 D=D+1
                case 480: D=D+1; break;
                //490 NEXT J
                case 490: J=J+1; GOTO(460); break;
                //500 IF D=3 THEN 680
                case 500: if(D==3) GOTO(680); break;
                //505 IF C=0 THEN 545
                case 505: if(C==0) GOTO(545); break;
                //520 FOR J=1 TO C
                case 519: J=1; break;
                case 520: if(J==C+1) GOTO(545); break;
                //530 PRINT "PICO ";
                case 530: PRINT("PICO "); break;
                //540 NEXT J
                case 540: J=J+1; GOTO(520); break;
                //545 IF D=0 THEN 580
                case 545: if(D==0) GOTO(580); break;
                //550 FOR J=1 TO D
                case 549: J=1; break;
                case 550: if(J==D+1) GOTO(580); break;
                //560 PRINT "FERMI ";
                case 560: PRINT("FERMI "); break;
                //570 NEXT J
                case 570: J=J+1; GOTO(550); break;
                //580 IF C+D<>0 THEN 600
                case 580: if( C+D!=0 ) GOTO(600); break;
                //590 PRINT "BAGELS";
                case 590: PRINT("BAGELS"); break;
                //600 PRINT
                case 600: PRINT(); break;
                //605 NEXT I
                case 605: I=I+1; GOTO(220); break;
                //610 PRINT "OH WELL."
                case 610: PRINT("OH WELL."); break;
                //615 PRINT "THAT'S TWNETY GUESSES.  MY NUMBER WAS";100*A(1)+10*A(2)+A(3)
                case 615: PRINT("THAT'S TWENTY GUESSES.  MY NUMBER WAS"+(100*A[0]+10*A[1]+A[2])); break;
                //620 GOTO 700
                case 620: GOTO(700); break;
                //630 PRINT "TRY GUESSING A THREE-DIGIT NUMBER.":GOTO 230
                case 630: PRINT("TRY GUESSING A THREE-DIGIT NUMBER."); GOTO(230); break;
                //650 PRINT "OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND"
                case 650: PRINT("OH, I FORGOT TO TELL YOU THAT THE NUMBER I HAVE IN MIND"); break;
                //660 PRINT "HAS NO TWO DIGITS THE SAME.":GOTO 230
                case 660: PRINT("HAS NO TWO DIGITS THE SAME."); GOTO(230); break;
                //680 PRINT "YOU GOT IT!!!":PRINT
                case 680: PRINT("YOU GOT IT!!!"); PRINT(); break;
                //690 Y=Y+1
                case 690: Y=Y+1; break;
                //700 INPUT "PLAY AGAIN (YES OR NO)";A$
                case 700: A$ = INPUT("PLAY AGAIN (YES OR NO) "); break;
                //720 IF LEFT$(A$,1)="YES" THEN 150
                case 720: if( LEFT$(A$,1).equals("Y") ) GOTO(150); break;
                //730 IF Y=0 THEN 750
                case 730: if(Y==0) GOTO(750); break;
                //740 PRINT:PRINT "A";Y;"POINT BAGELS BUFF!!"
                case 740: PRINT(); PRINT("A " + Y + " POINT BAGELS BUFF!!"); break;
                //750 PRINT "HOPE YOU HAD FUN.  BYE."
                case 750: PRINT("HOPE YOU HAD FUN.  BYE."); break;     
                //999 END
                case 999: END(); break;
               
            }
            line += 1;
        }
    }
}


This is a joke, BTW. It does work though. I thought about adding a 'Moore's Law-based slowdown' to keep it at 1980 speeds into the indefinite future. Maybe that's a feature for version 2.0.

Happy Friday! :)

Is This A Good Question/Topic? 2
  • +

Replies To: How (not) to port a 30-year old BASIC game to Java.

#2 Duta  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 21-June 12

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 06 October 2012 - 03:50 AM

Holy ohmygod.
Was This Post Helpful? 0
  • +
  • -

#3 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 23 October 2012 - 06:20 PM

You know, you could just use "continue".
Was This Post Helpful? 0
  • +
  • -

#4 antolyevich  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 12
  • Joined: 02-September 12

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 14 December 2012 - 07:59 PM

That is awesome.....and so pointless at the same time lol
Was This Post Helpful? 0
  • +
  • -

#5 Pederson  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-July 13

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 03 July 2013 - 01:47 PM

This is hilarious! I strongly dislike GoTo's due to working in VB6 and having to deal with them all the time. This is probably my favorite Nightmare yet haha!
Was This Post Helpful? 1
  • +
  • -

#6 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 04 July 2013 - 07:51 PM

View PostPederson, on 03 July 2013 - 08:47 PM, said:

This is hilarious! I strongly dislike GoTo's due to working in VB6 and having to deal with them all the time. This is probably my favorite Nightmare yet haha!


GOTOs are evil in Java. There's much more of a reason to dislike GOTOs in Java than that. GOTOs destroy your structural integrity of your code. Not really, but GOTO statements are pretty much breeding grounds for bugs and glitches.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,883
  • Joined: 19-March 11

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 04 July 2013 - 08:18 PM

View Postextremeblueness, on 04 July 2013 - 09:51 PM, said:

View PostPederson, on 03 July 2013 - 08:47 PM, said:

This is hilarious! I strongly dislike GoTo's due to working in VB6 and having to deal with them all the time. This is probably my favorite Nightmare yet haha!


GOTOs are evil in Java. There's much more of a reason to dislike GOTOs in Java than that. GOTOs destroy your structural integrity of your code. Not really, but GOTO statements are pretty much breeding grounds for bugs and glitches.



GOTOs are not evil in Java, because they don't exist in Java. Can a nonexistent operator be evil?

As for the original port - wouldn't it make more sense to actually implement that functionality in Java? It's not a bad exercise, actually.
Was This Post Helpful? 0
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: How (not) to port a 30-year old BASIC game to Java.

Posted 04 July 2013 - 10:44 PM

I was thinking about GOTOs while writing a program in Java recently. The problem is, I was writing a silly procedural program. One that would perhaps be more suited to a procedural language, such as BASIC.

It's the first time I ever felt the need to "backtrack" into my code in Java.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1