Learning Code

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

nothinglikeit22
Posts: 19
Joined: Wed Apr 15, 2009 8:29 pm UTC

Learning Code

Postby nothinglikeit22 » Wed Apr 15, 2009 9:13 pm UTC

See the thing is... i'm not even sure what coding really is.
So if someone could please explain generaly what its all about i'd be much obliged.

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Learning Code

Postby quintopia » Wed Apr 15, 2009 9:20 pm UTC


Grumpy Code Monkey
Posts: 99
Joined: Tue Feb 19, 2008 4:10 pm UTC
Location: Blue Texas

Re: Learning Code

Postby Grumpy Code Monkey » Wed Apr 15, 2009 11:37 pm UTC

nothinglikeit22 wrote:See the thing is... i'm not even sure what coding really is.


A series of lesser failures, eventually punctuated by success, immediately followed by more failures. Like second marriages and second children, it's the triumph of hope over experience. It's a way to make a living while hating life.

Sorry. It's been a long and frustrating couple of days.

It's simply the act of writing, compiling, running, and debugging computer code, which is reams of highly structured text that can sometimes be mistaken for English (COBOL) or line noise (Lisp).

User avatar
quintopia
Posts: 2906
Joined: Fri Nov 17, 2006 2:53 am UTC
Location: atlanta, ga

Re: Learning Code

Postby quintopia » Wed Apr 15, 2009 11:52 pm UTC

Grumpy Code Monkey wrote:
It's simply the act of writing, compiling, running, and debugging computer code, which is reams of highly structured (or not) text that can sometimes be mistaken for English (COBOL) or line noise (Lisp)(TECO).


FTFY.

Outchanter
Posts: 669
Joined: Mon Dec 17, 2007 8:40 am UTC

Re: Learning Code

Postby Outchanter » Thu Apr 16, 2009 4:52 am UTC

If you're asking why people code: to make the computer do what they want. You can do a lot of things without coding - like reading web pages - but that's only because someone else (or a group of someones) already coded the web browser you use. Similarly, you can play many computer games, use word processors and drawing software and other applications, boot up your operating system and read and write files from CDs without coding - but only because the applications, operating system and device drivers you use were already written by other people.

If you want to do something there isn't already a program for (or no program that does things to your satisfaction), you might want to code your own.

sje46
Posts: 4730
Joined: Wed May 14, 2008 4:41 am UTC
Location: New Hampshire

Re: Learning Code

Postby sje46 » Thu Apr 16, 2009 5:15 am UTC

nothinglikeit22 wrote:See the thing is... i'm not even sure what coding really is.
So if someone could please explain generaly what its all about i'd be much obliged.

Coding is programming. Tellng a computer what to do by typing it. To get an idea of what it's all about, you can make your own code, to design a website. This is not "real" programming--like, you can't make your own games out of the html (which is what you call the type of code you use to make a website), but it will help you understand what coding exactly is.
I'm going to assume you have Windows, because I don't have much experience with mac. This should only take 2 minutes, if that.
Open up Notepad. It should be in accessories, I think . . .. Copy and paste this in:

Code: Select all

<html>
<head>
<title>Awesome webpage!</title>
</head>
<body>
<h1>Hello World!</h1>
<p align=right>Lorem ipsum blablablalbalbalbal <b>nonsense</b> Blalalalal</p>
<p>fdkjsalfdsajk jf lksk jdsakl </p>
</body>
</html>

Replace the gibberish with anything you want, as long as it's not the tags (the content in the <>'s). These tell the browser what information to display and how to display it.
Anyway, paste that into notepad, and save it. When you save it, give the file a name like anything, just make sure you end it ".html", so that your computer knows what file-type it is. But don't save it as a "txt" file (which is the default), save it as "all file types". For example, name it "example.html".
When you are done that, open up your browser, go to file, and do "open file" or whatever . .. Find your file, and open it. Your browser (and this is the important part) will interpret the text you wrote and render the information a certain way. It will see <h1> as a signifier that whatever is between it and </h1> will be a large header. <p> is paragraphs, and you can use <b> to bold, and <i> to italicize specific words. You can switch things up, but you have to save before you refresh to see the results.

Anyway, HTML is a very simple code, and might be a good starting place if you want to understand computers. To learn how to code in HTML (and CSS, and Javascript, and a lot of things to learn about web design and the internet in general), you can visit http://www.w3schools.com. It's free and interactive.

There are a lot of programming languages. HTML tells the browser how to display information, and CSS will tell it how to style that information (like fonts, colors, and a lot more) Javascript is a script language that makes your page interactive (you know, like pressing buttons to make something happen. Then there are more advanced langauges like Python, Perl, Lisp, I don't know. These are (well, at least python) free to download and install, and can do a lot. Also, programming languages are not only used for websites, of course. Most probably aren't. Just HTML and CSS are used only for the web, I think.

tl;dr: coding is writing in a "language" that your computer can understand and act out.
General_Norris: Taking pride in your nation is taking pride in the division of humanity.
Pirate.Bondage: Let's get married. Right now.

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Learning Code

Postby thoughtfully » Thu Apr 16, 2009 8:54 pm UTC

HTML isn't a programming language. It cannot be executed. It is not Turing Complete. It is a document format. Javascript is a programming language, and the desire to mix code and data (which always invites trouble) is why it was created. Your will have fewer headaches in the future if you keep this distinction in mind.
Last edited by thoughtfully on Thu Apr 16, 2009 9:21 pm UTC, edited 1 time in total.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Learning Code

Postby Emu* » Thu Apr 16, 2009 9:02 pm UTC

HTML is markup, not code.

Unless one considers it to be a fourth-level language. It isn't deterministic enough anyway.

Edit: must type faster.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

sje46
Posts: 4730
Joined: Wed May 14, 2008 4:41 am UTC
Location: New Hampshire

Re: Learning Code

Postby sje46 » Thu Apr 16, 2009 9:54 pm UTC

Okay, fine. It's not programming. I still think it's code, though. You are telling a computer what to do, aren't you? You call it source code. =/
Anyway, hopefully I was able to introduce to the OP the idea that you can write in a language in a file in a text editor like notepad, save it as a type of file, and your computer will interpret it a certain way. This should get him started, at least.
General_Norris: Taking pride in your nation is taking pride in the division of humanity.
Pirate.Bondage: Let's get married. Right now.

0xBADFEED
Posts: 687
Joined: Mon May 05, 2008 2:14 am UTC

Re: Learning Code

Postby 0xBADFEED » Thu Apr 16, 2009 11:11 pm UTC

sje46 wrote:I still think it's code, though. You are telling a computer what to do, aren't you?

Is this code ?

Code: Select all

ps -e  | grep python

I mean it's telling the computer what to do. It's being interpreted by another program. But I would just call this "using a computer". Not trying to be adversarial, I'm just wondering where the cutoff for "code" is.

IMO, writing HTML is not coding any more than writing LaTex is coding. And I've never heard anyone refer to using LaTex as "coding".

sje46
Posts: 4730
Joined: Wed May 14, 2008 4:41 am UTC
Location: New Hampshire

Re: Learning Code

Postby sje46 » Thu Apr 16, 2009 11:33 pm UTC

0xBADFEED wrote:
sje46 wrote:I still think it's code, though. You are telling a computer what to do, aren't you?

Is this code ?

Code: Select all

ps -e  | grep python

I mean it's telling the computer what to do. It's being interpreted by another program. But I would just call this "using a computer". Not trying to be adversarial, I'm just wondering where the cutoff for "code" is.

IMO, writing HTML is not coding any more than writing LaTex is coding. And I've never heard anyone refer to using LaTex as "coding".

I don't know what "ps -e | grep python" is. So it could be. *shrug*
My teacher and quite a few websites I went on call HTML code.
General_Norris: Taking pride in your nation is taking pride in the division of humanity.
Pirate.Bondage: Let's get married. Right now.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Learning Code

Postby Berengal » Thu Apr 16, 2009 11:43 pm UTC

0xBADFEED wrote:And I've never heard anyone refer to using LaTex as "coding".

http://www.haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf
First article: Rapid Prototyping in TEX by Stephen Hicks

It's quite an interesting read actually. I doubt you could do it in HTML though...
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Learning Code

Postby Xeio » Thu Apr 16, 2009 11:51 pm UTC

sje46 wrote:My teacher and quite a few websites I went on call HTML code.
That's more like a common misconception than anything else. It's like calling XML code, or a .doc file code (it's a formatted file). They aren't executed (code blocks within HTML or whatever notwithstanding), they are read and interpreted by some program and then usually displayed.

culled
Posts: 34
Joined: Mon Feb 19, 2007 11:50 pm UTC

Re: Learning Code

Postby culled » Fri Apr 17, 2009 1:17 am UTC

I'll go ahead and call HTML code, it's certainly not a programming language but it fits my definition of "code". HTML has a syntax and a set of keywords that collectively tell another program what to do. You have to follow its rules to get what you want, it's code.

0xBADFEED wrote:Is this code ?

Code: Select all

ps -e  | grep python

I mean it's telling the computer what to do. It's being interpreted by another program. But I would just call this "using a computer". Not trying to be adversarial, I'm just wondering where the cutoff for "code" is.

I wouldn't consider that code because it's just telling the computer to run some programs, it's really only different from opening a program in windows in that you can pipe your output. So HTML is code but double clicking on a HTML file so it opens in Firefox is not code.

User avatar
Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

Re: Learning Code

Postby Emu* » Fri Apr 17, 2009 1:22 am UTC

If I put that line in a shell script (or batch file) and double click it, the computer reads the file and decides what to do.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!

Arantor
Posts: 67
Joined: Tue Jan 01, 2008 11:34 pm UTC
Contact:

Re: Learning Code

Postby Arantor » Fri Apr 17, 2009 1:31 am UTC

It is a code by definition, just not program code. It is markup code.

Regarding the OP, there are any number of languages in which you can instruct a computer to do things on your behalf, it depends what you want to do, more than anything.

nothinglikeit22
Posts: 19
Joined: Wed Apr 15, 2009 8:29 pm UTC

Re: Learning Code

Postby nothinglikeit22 » Fri Apr 17, 2009 11:40 pm UTC

Thanks for the clarification. However i think i will start on Qbasic since its relativly easy and fairly idiot proof.
thx again

Lewton
Posts: 121
Joined: Tue Apr 08, 2008 9:15 am UTC

Re: Learning Code

Postby Lewton » Sat Apr 18, 2009 12:07 am UTC

nothinglikeit22 wrote:However i think i will start on Qbasic


No!.. No no no

That would be like learning gibberish as a preparation for learning french, completely pointless

sje46
Posts: 4730
Joined: Wed May 14, 2008 4:41 am UTC
Location: New Hampshire

Re: Learning Code

Postby sje46 » Sat Apr 18, 2009 12:19 am UTC

nothinglikeit22 wrote:Thanks for the clarification. However i think i will start on Qbasic since its relativly easy and fairly idiot proof.
thx again

Python is fun. Then again, besides Javascript, that's the only real programming language I know. IT seems easy, but I don't know too much yt.
General_Norris: Taking pride in your nation is taking pride in the division of humanity.
Pirate.Bondage: Let's get married. Right now.

dean.menezes
Posts: 135
Joined: Sat Nov 15, 2008 3:47 am UTC

Re: Learning Code

Postby dean.menezes » Sat Apr 18, 2009 12:34 am UTC

QuickBASIC is OK, as long as you don't use GOTO except to stimulate continue in C or break out of multiple loops. If you are using Windows or Linux, you should probably use the qb64 (http://qb64.net) or FreeBASIC (http://freebasic.net) compilers instead of Microsoft QuickBASIC or QBASIC for DOS, especially as DOS graphics don't work with Vista.

Python might be better because it can access features of modern OSes and because it allows you to do stuff like functional programming.

nothinglikeit22
Posts: 19
Joined: Wed Apr 15, 2009 8:29 pm UTC

Re: Learning Code

Postby nothinglikeit22 » Sat Apr 18, 2009 4:13 pm UTC

That would be like learning gibberish as a preparation for learning french, completely pointless

How so? asides from lacking some commands QuickBasic has, i dont see anything wrong with it. (then again i don't know that much..)

User avatar
Why Two Kay
Posts: 266
Joined: Sun Mar 23, 2008 6:25 pm UTC
Location: Plano, TX
Contact:

Re: Learning Code

Postby Why Two Kay » Sat Apr 18, 2009 4:57 pm UTC

nothinglikeit22 wrote:
That would be like learning gibberish as a preparation for learning french, completely pointless

How so? asides from lacking some commands QuickBasic has, i dont see anything wrong with it. (then again i don't know that much..)


It's basically a useless language. You can not do anything with it that has substance, it is very limited in scope, and generally teaches some bad programming practices. (I don't have any examples, but I'm sure there are some.) Yes, it is good for learning the difference between types of variables and how some (sometimes awkward in QuickBasic) control structures work. But then again, you could do that in another language just as well.

If you wanted to start with an easy language that is similar to QuickBasic, and has actual uses in many production environments, check out Python.

If you are looking for something you have a high chance of working with in a real programmer type of job, or if you want to develop actual applications to run on Windows/Linux, I would suggest C++ or Java. Or C if you really want to get down with lower level stuff.
tl;dr - I said nothing important.

Lewton
Posts: 121
Joined: Tue Apr 08, 2008 9:15 am UTC

Re: Learning Code

Postby Lewton » Sat Apr 18, 2009 5:08 pm UTC

You said exactly what I was gonna say (only in a much nicer, more corehent way)

I'd add C# to that last list though

dean.menezes
Posts: 135
Joined: Sat Nov 15, 2008 3:47 am UTC

Re: Learning Code

Postby dean.menezes » Sat Apr 18, 2009 6:25 pm UTC

Of course you can do complex tasks in QuickBASIC, it is turing complete. You could write a chess program in brainfuck if you wanted to.

Here is a chess program in QuickBASIC:
Spoiler:

Code: Select all

DEFINT A-Z
DECLARE SUB SQUARE (A, B, C)
DECLARE SUB SHOWMAN (A, B, FLAG)
DECLARE SUB SHOWBD ()
DECLARE SUB IO (A, B, X, Y, RESULT)
DECLARE FUNCTION INCHECK (X)
DECLARE SUB MAKEMOVE (A, B, X, Y)
DECLARE SUB KNIGHT (A, B, XX(), YY(), NDX)
DECLARE SUB KING (A, B, XX(), YY(), NDX)
DECLARE SUB QUEEN (A, B, XX(), YY(), NDX)
DECLARE SUB ROOK (A, B, XX(), YY(), NDX)
DECLARE SUB BISHOP (A, B, XX(), YY(), NDX)
DECLARE SUB MOVELIST (A, B, XX(), YY(), NDX)
DECLARE SUB PAWN (A, B, XX(), YY(), NDX)
DECLARE FUNCTION EVALUATE (ID, PRUNE)
DIM SHARED BOARD(0 TO 7, 0 TO 7)
DIM SHARED BESTA(0 TO 7), BESTB(0 TO 7), BESTX(0 TO 7), BESTY(0 TO 7)
DIM SHARED LEVEL, MAXLEVEL, SCORE
DIM SHARED WCKSFLAG, WCQSFLAG, INTFLAG
DIM SHARED WCKSOLD, WCQSOLD
LEVEL = 0
MAXLEVEL = 5 'change this to higher to make it think ahead more
DATA -500,-270,-300,-900,-7500,-300,-270,-500
DATA -100,-100,-100,-100, -100,-100,-100,-100
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 0, 0, 0, 0, 0, 0, 0, 0
DATA 100, 100, 100, 100, 100, 100, 100, 100
DATA 500, 270, 300, 900, 5000,300, 270, 500
FOR X = 0 TO 7
 FOR Y = 0 TO 7
  READ Z
  BOARD(X, Y) = Z
 NEXT Y
NEXT X
A = -1
RESULT = 0
CLS
LOCATE , 34
PRINT "QBASIC CHESS"
PRINT
PRINT "    CHESS is a game played between two players on a board of 64 squares."
PRINT " Chess was first invented in its current form in Europe during the late"
PRINT " fifteenth century.  It evolved from much earlier forms invented in India"
PRINT " and Persia."
PRINT "    The pieces are divided into Black and White.  Each player has 16 pieces:"
PRINT " 1 king, 1 queen, 2 rooks, 2 bishops, 2 knights, and 8 pawns.  White makes"
PRINT " the first move.  The players alternate moving one piece at a time.  Pieces"
PRINT " are moved to an unoccupied square, or moved onto a square occupied by an"
PRINT " opponent's piece, capturing it.  When the king is under attack, he is in"
PRINT " CHECK.  The player cannot put his king in check.  The object is to CHECKMATE"
PRINT " the opponent.  This occurs when the king is in check and there is no way to"
PRINT " remove the king from attack."
PRINT "   To move the pieces on the chessboard, type in your move in coordinate"
PRINT " notation, e.g. E2-E4 (not in English notation like P-K4).  To castle, type O-O"
PRINT " to castle kingside or O-O-O to castle queenside like in English notation."
PRINT " To exit the game, type QUIT."
PRINT
PRINT "Press any key to continue."
Z$ = INPUT$(1)
DO
 SCORE = 0
 CALL IO(A, B, X, Y, RESULT)
 CLS
 CALL SHOWBD
 RESULT = EVALUATE(-1, 10000)
 A = BESTA(1)
 B = BESTB(1)
 X = BESTX(1)
 Y = BESTY(1)
LOOP

SUB BISHOP (A, B, XX(), YY(), NDX)
 ID = SGN(BOARD(B, A))
 FOR DXY = 1 TO 7
  X = A - DXY
  Y = B + DXY
  IF X < 0 OR X > 7 OR Y < 0 OR Y > 7 THEN EXIT FOR
  GOSUB 3
  IF BOARD(Y, X) <> 0 THEN EXIT FOR
 NEXT
 FOR DXY = 1 TO 7
  X = A + DXY
  Y = B + DXY
  IF X < 0 OR X > 7 OR Y < 0 OR Y > 7 THEN EXIT FOR
  GOSUB 3
  IF BOARD(Y, X) <> 0 THEN EXIT FOR
 NEXT
 FOR DXY = 1 TO 7
  X = A - DXY
  Y = B - DXY
  IF X < 0 OR X > 7 OR Y < 0 OR Y > 7 THEN EXIT FOR
  GOSUB 3
  IF BOARD(Y, X) <> 0 THEN EXIT FOR
 NEXT
 FOR DXY = 1 TO 7
  X = A + DXY
  Y = B - DXY
  IF X < 0 OR X > 7 OR Y < 0 OR Y > 7 THEN EXIT FOR
  GOSUB 3
  IF BOARD(Y, X) <> 0 THEN EXIT FOR
 NEXT
 EXIT SUB
3 REM
 IF ID <> SGN(BOARD(Y, X)) THEN
  NDX = NDX + 1
  XX(NDX) = X
  YY(NDX) = Y
 END IF
 RETURN
END SUB

FUNCTION EVALUATE (ID, PRUNE)
 DIM XX(0 TO 26), YY(0 TO 26)
 LEVEL = LEVEL + 1
 BESTSCORE = 10000 * ID
 FOR B = 7 TO 0 STEP -1
  FOR A = 7 TO 0 STEP -1
   IF SGN(BOARD(B, A)) <> ID THEN GOTO 1
   IF (LEVEL = 1) THEN CALL SHOWMAN(A, B, 8)
   CALL MOVELIST(A, B, XX(), YY(), NDX)
   FOR I = 0 TO NDX
    X = XX(I)
    Y = YY(I)
    IF LEVEL = 1 THEN
     LOCATE 1, 1
     PRINT "TRYING: "; CHR$(65 + A); 8 - B; "-"; CHR$(65 + X); 8 - Y
     CALL SHOWMAN(X, Y, 8)
    END IF
    OLDSCORE = SCORE
    MOVER = BOARD(B, A)
    TARGET = BOARD(Y, X)
    CALL MAKEMOVE(A, B, X, Y)
    IF (LEVEL < MAXLEVEL) THEN SCORE = SCORE + EVALUATE(-ID, BESTSCORE - TARGET + ID * (8 - ABS(4 - X) - ABS(4 - Y)))
    SCORE = SCORE + TARGET - ID * (8 - ABS(4 - X) - ABS(4 - Y))
    IF (ID < 0 AND SCORE > BESTSCORE) OR (ID > 0 AND SCORE < BESTSCORE) THEN
     BESTA(LEVEL) = A
     BESTB(LEVEL) = B
     BESTX(LEVEL) = X
     BESTY(LEVEL) = Y
     BESTSCORE = SCORE
     IF (ID < 0 AND BESTSCORE >= PRUNE) OR (ID > 0 AND BESTSCORE <= PRUNE) THEN
      BOARD(B, A) = MOVER
      BOARD(Y, X) = TARGET
      SCORE = OLDSCORE
      IF (LEVEL = 1) THEN CALL SHOWMAN(X, Y, 0)
      IF (LEVEL = 1) THEN CALL SHOWMAN(A, B, 0)
      LEVEL = LEVEL - 1
      EVALUATE = BESTSCORE
      EXIT FUNCTION
     END IF
    END IF
    BOARD(B, A) = MOVER
    BOARD(Y, X) = TARGET
    SCORE = OLDSCORE
    IF (LEVEL = 1) THEN CALL SHOWMAN(X, Y, 0)
   NEXT
   IF (LEVEL = 1) THEN CALL SHOWMAN(A, B, 0)
1 NEXT
  NEXT
  LEVEL = LEVEL - 1
  EVALUATE = BESTSCORE
 END FUNCTION

FUNCTION INCHECK (X)
 DIM XX(27), YY(27), NDX
 FOR B = 0 TO 7
  FOR A = 0 TO 7
   IF BOARD(B, A) >= 0 THEN GOTO 6
   CALL MOVELIST(A, B, XX(), YY(), NDX)
   FOR I = 0 TO NDX STEP 1
    X = XX(I)
    Y = YY(I)
    IF BOARD(Y, X) = 5000 THEN
     PRINT "YOU ARE IN CHECK!"
     PRINT " "
     PRINT " "
     INCHECK = 1
     EXIT FUNCTION
    END IF
   NEXT
6 NEXT
  NEXT
  INCHECK = 0
 END FUNCTION

SUB IO (A, B, X, Y, RESULT)
 DIM XX(0 TO 26), YY(0 TO 26)
 CLS
 IF A >= 0 THEN
  IF RESULT < -2500 THEN
   PRINT "I RESIGN"
   SLEEP
   SYSTEM
  END IF
  PIECE = BOARD(Y, X)
  CALL MAKEMOVE(A, B, X, Y)
  PRINT "MY MOVE: "; CHR$(65 + A); 8 - B; "-"; CHR$(65 + X); 8 - Y
  IF PIECE <> 0 THEN
   PRINT "I TOOK YOUR ";
   IF PIECE = 100 THEN PRINT "PAWN"
   IF PIECE = 270 THEN PRINT "KNIGHT"
   IF PIECE = 300 THEN PRINT "BISHOP"
   IF PIECE = 500 THEN PRINT "ROOK"
   IF PIECE = 900 THEN PRINT "QUEEN"
   IF PIECE = 5000 THEN PRINT "KING"
  END IF
  NULL = INCHECK(0)
 END IF
 DO
  CALL SHOWBD
  VIEW PRINT 24 TO 24
  INPUT "YOUR MOVE: ", IN$
  IF UCASE$(IN$) = "QUIT" THEN CLS : END
  IF UCASE$(IN$) = "O-O" OR IN$ = "0-0" THEN
   IF WCKSFLAG <> 0 THEN GOTO 16
   IF BOARD(7, 7) <> 500 THEN GOTO 16
   IF BOARD(7, 6) <> 0 OR BOARD(7, 5) <> 0 THEN GOTO 16
   BOARD(7, 6) = 5000
   BOARD(7, 4) = 0
   BOARD(7, 5) = 500
   BOARD(7, 7) = 0
   WCKSFLAG = -1
   EXIT SUB
  END IF
  IF UCASE$(IN$) = "O-O-O" OR IN$ = "0-0-0" THEN
   IF WCQSFLAG <> 0 THEN GOTO 16
   IF BOARD(7, 0) <> 500 THEN GOTO 16
   IF BOARD(7, 1) <> 0 OR BOARD(7, 2) <> 0 OR BOARD(7, 3) <> 0 THEN GOTO 16
   BOARD(7, 2) = 5000
   BOARD(7, 4) = 0
   BOARD(7, 3) = 500
   BOARD(7, 0) = 0
   WCQSFLAG = -1
   EXIT SUB
  END IF
  IF LEN(IN$) < 5 THEN GOTO 16
  B = 8 - (ASC(MID$(IN$, 2, 1)) - 48)
  A = ASC(UCASE$(MID$(IN$, 1, 1))) - 65
  X = ASC(UCASE$(MID$(IN$, 4, 1))) - 65
  Y = 8 - (ASC(MID$(IN$, 5, 1)) - 48)
  IF B > 7 OR B < 0 OR A > 7 OR A < 0 OR X > 7 OR X < 0 OR Y > 7 OR Y < 0 THEN GOTO 16
  IF BOARD(B, A) <= 0 THEN GOTO 16
  IF Y = 2 AND B = 3 AND (X = A - 1 OR X = A + 1) THEN
    IF BOARD(B, A) = 100 AND BOARD(Y, X) = 0 AND BOARD(Y + 1, X) = -100 THEN
      IF BESTB(1) = 1 AND BESTA(1) = X THEN
        MOVER = BOARD(B, A)
        TARGET = BOARD(Y, X)
        CALL MAKEMOVE(A, B, X, Y)
        BOARD(Y + 1, X) = 0
        ENPASSANT = -1
        GOTO 15
      END IF
    END IF
  END IF
  CALL MOVELIST(A, B, XX(), YY(), NDX)
  FOR K = 0 TO NDX STEP 1
   IF X = XX(K) AND Y = YY(K) THEN
    MOVER = BOARD(B, A)
    TARGET = BOARD(Y, X)
    INTFLAG = -1
    CALL MAKEMOVE(A, B, X, Y)
    IF MOVER = 5000 THEN
      WCQSOLD = WCQSFLAG
      WCKSOLD = WCKSFLAG
      WCKSFLAG = -1
      WCQSFLAG = -1
    END IF
    IF (A = 0) AND (B = 7) AND (MOVER = 500) THEN
      WCQSOLD = WCQSFLAG
      WCQSFLAG = -1
    END IF
    IF (A = 7) AND (B = 7) AND (MOVER = 500) THEN
      WCKSOLD = WCKSFLAG
      WCKSFLAG = -1
    END IF
    INTFLAG = 0
15  IF INCHECK(0) = 0 THEN EXIT SUB
    BOARD(B, A) = MOVER
    BOARD(Y, X) = TARGET
    IF ENPASSANT THEN BOARD(Y + 1, X) = -100: ENPASSANT = 0
    IF (A = 0) AND (B = 7) AND (MOVER = 500) THEN WCQSFLAG = WCQSOLD
    IF (A = 7) AND (B = 7) AND (MOVER = 500) THEN WCKSFLAG = WCKSOLD
    IF MOVER = 5000 THEN WCQSFLAG = WCQSOLD
    GOTO 16
   END IF
  NEXT
16 CLS
 LOOP
END SUB

SUB KING (A, B, XX(), YY(), NDX)
 ID = SGN(BOARD(B, A))
 FOR DY = -1 TO 1
  IF B + DY < 0 OR B + DY > 7 THEN GOTO 12
  FOR DX = -1 TO 1
   IF A + DX < 0 OR A + DX > 7 THEN GOTO 11
   IF ID <> SGN(BOARD(B + DY, A + DX)) THEN
    NDX = NDX + 1
    XX(NDX) = A + DX
    YY(NDX) = B + DY
   END IF
11 NEXT
12 NEXT
END SUB

SUB KNIGHT (A, B, XX(), YY(), NDX)
 ID = SGN(BOARD(B, A))
 X = A - 1
 Y = B - 2
 GOSUB 5
 X = A - 2
 Y = B - 1
 GOSUB 5
 X = A + 1
 Y = B - 2
 GOSUB 5
 X = A + 2
 Y = B - 1
 GOSUB 5
 X = A - 1
 Y = B + 2
 GOSUB 5
 X = A - 2
 Y = B + 1
 GOSUB 5
 X = A + 1
 Y = B + 2
 GOSUB 5
 X = A + 2
 Y = B + 1
 GOSUB 5
 EXIT SUB
5 REM
 IF X < 0 OR X > 7 OR Y < 0 OR Y > 7 THEN RETURN
 IF ID <> SGN(BOARD(Y, X)) THEN NDX = NDX + 1: XX(NDX) = X: YY(NDX) = Y
 RETURN
END SUB

SUB MAKEMOVE (A, B, X, Y)
 BOARD(Y, X) = BOARD(B, A)
 BOARD(B, A) = 0
 IF Y = 0 AND BOARD(Y, X) = 100 THEN
   IF INTFLAG THEN
     DO
       VIEW PRINT 24 TO 24
       INPUT "PROMOTE TO: ", I$
       SELECT CASE UCASE$(I$)
         CASE "KNIGHT", "N", "Kt", "Kt.", "N."
           PROMOTE = 270
         CASE "BISHOP", "B", "B."
           PROMOTE = 300
         CASE "ROOK", "R", "R."
           PROMOTE = 500
         CASE "QUEEN", "Q", "Q."
           PROMOTE = 900
       END SELECT
     LOOP UNTIL PROMOTE <> 0
     BOARD(Y, X) = PROMOTE
   ELSE
     BOARD(Y, X) = -900
   END IF
 END IF
 IF Y = 7 AND BOARD(Y, X) = -100 THEN BOARD(Y, X) = -900
END SUB

SUB MOVELIST (A, B, XX(), YY(), NDX)
 PIECE = INT(ABS(BOARD(B, A)))
 NDX = -1
 IF PIECE = 100 THEN
  CALL PAWN(A, B, XX(), YY(), NDX)
 ELSEIF PIECE = 270 THEN CALL KNIGHT(A, B, XX(), YY(), NDX)
 ELSEIF PIECE = 300 THEN CALL BISHOP(A, B, XX(), YY(), NDX)
 ELSEIF PIECE = 500 THEN CALL ROOK(A, B, XX(), YY(), NDX)
 ELSEIF PIECE = 900 THEN CALL QUEEN(A, B, XX(), YY(), NDX)
 ELSE CALL KING(A, B, XX(), YY(), NDX)
 END IF
 
END SUB

SUB PAWN (A, B, XX(), YY(), NDX)
 ID = SGN(BOARD(B, A))
 IF (A - 1) >= 0 AND (A - 1) <= 7 AND (B - ID) >= 0 AND (B - ID) <= 7 THEN
  IF SGN(BOARD((B - ID), (A - 1))) = -ID THEN
   NDX = NDX + 1
   XX(NDX) = A - 1
   YY(NDX) = B - ID
  END IF
 END IF
 IF (A + 1) >= 0 AND (A + 1) <= 7 AND (B - ID) >= 0 AND (B - ID) <= 7 THEN
  IF SGN(BOARD((B - ID), (A + 1))) = -ID THEN
   NDX = NDX + 1
   XX(NDX) = A + 1
   YY(NDX) = B - ID
  END IF
 END IF
 IF A >= 0 AND A <= 7 AND (B - ID) >= 0 AND (B - ID) <= 7 THEN
  IF BOARD((B - ID), A) = 0 THEN
   NDX = NDX + 1
   XX(NDX) = A
   YY(NDX) = B - ID
   IF (ID < 0 AND B = 1) OR (ID > 0 AND B = 6) THEN
    IF BOARD((B - ID - ID), A) = 0 THEN
     NDX = NDX + 1
     XX(NDX) = A
     YY(NDX) = B - ID - ID
    END IF
   END IF
  END IF
 END IF
 
END SUB

SUB QUEEN (A, B, XX(), YY(), NDX)
 CALL BISHOP(A, B, XX(), YY(), NDX)
 CALL ROOK(A, B, XX(), YY(), NDX)
END SUB

SUB ROOK (A, B, XX(), YY(), NDX)
 ID = SGN(BOARD(B, A))
 FOR X = A - 1 TO 0 STEP -1
  IF ID <> SGN(BOARD(B, X)) THEN
   NDX = NDX + 1
   XX(NDX) = X
   YY(NDX) = B
  END IF
  IF (BOARD(B, X)) <> 0 THEN EXIT FOR
 NEXT
 FOR X = A + 1 TO 7 STEP 1
  IF ID <> SGN(BOARD(B, X)) THEN
   NDX = NDX + 1
   XX(NDX) = X
   YY(NDX) = B
  END IF
  IF (BOARD(B, X)) <> 0 THEN EXIT FOR
 NEXT
 FOR Y = B - 1 TO 0 STEP -1
  IF ID <> SGN(BOARD(Y, A)) THEN
   NDX = NDX + 1
   XX(NDX) = A
   YY(NDX) = Y
  END IF
  IF (BOARD(Y, A)) <> 0 THEN EXIT FOR
 NEXT
 FOR Y = B + 1 TO 7 STEP 1
  IF ID <> SGN(BOARD(Y, A)) THEN
   NDX = NDX + 1
   XX(NDX) = A
   YY(NDX) = Y
  END IF
  IF (BOARD(Y, A)) <> 0 THEN EXIT FOR
 NEXT
END SUB

SUB SHOWBD
 VIEW PRINT
 LOCATE 3, 30
 COLOR 7, 0
 PRINT "A  B  C  D  E  F  G  H"
 FOR K = 0 TO 25
  LOCATE 4, 28 + K
  COLOR 6, 0
  PRINT CHR$(220)
 NEXT
 FOR B = 0 TO 7
  LOCATE 2 * B + 5, 26
  COLOR 7, 0
  PRINT CHR$(56 - B)
  LOCATE 2 * B + 5, 28
  COLOR 6, 0
  PRINT CHR$(219)
  LOCATE 2 * B + 6, 28
  COLOR 6, 0
  PRINT CHR$(219)
  FOR A = 0 TO 7
   IF ((A + B) MOD 2) <> 0 THEN
    COLOUR = 2
   ELSE COLOUR = 15
   END IF
   CALL SQUARE(3 * A + 31, 2 * B + 5, COLOUR)
  NEXT
  LOCATE 2 * B + 5, 53
  COLOR 6, 0
  PRINT CHR$(219)
  LOCATE 2 * B + 6, 53
  COLOR 6, 0
  PRINT CHR$(219)
  LOCATE 2 * B + 6, 55
  COLOR 7, 0
  PRINT CHR$(56 - B)
 NEXT
 FOR K = 0 TO 25
  LOCATE 21, 28 + K
  COLOR 6, 0
  PRINT CHR$(223)
 NEXT
 LOCATE 22, 30
 COLOR 7, 0
 PRINT "A  B  C  D  E  F  G  H"
 FOR B = 0 TO 7
  FOR A = 0 TO 7
   CALL SHOWMAN(A, B, 0)
  NEXT
 NEXT
 COLOR 7, 0
END SUB

SUB SHOWMAN (A, B, FLAG)
 IF BOARD(B, A) < 0 THEN BACK = 0
 IF BOARD(B, A) > 0 THEN BACK = 7
 FORE = 7 - BACK + FLAG
 IF BOARD(B, A) = 0 THEN
  IF ((A + B) MOD 2) <> 0 THEN BACK = 2 ELSE BACK = 15
  FORE = BACK + -1 * (FLAG > 0)
 END IF
 N$ = " "
 PIECE = INT(ABS(BOARD(B, A)))
 IF PIECE = 0 THEN N$ = CHR$(219)
 IF PIECE = 100 THEN N$ = "P"
 IF PIECE = 270 THEN N$ = "N"
 IF PIECE = 300 THEN N$ = "B"
 IF PIECE = 500 THEN N$ = "R"
 IF PIECE = 900 THEN N$ = "Q"
 IF PIECE = 5000 OR PIECE = 7500 THEN N$ = "K"
 LOCATE 2 * B + 5 - (BOARD(B, A) > 0), 3 * A + 30
 COLOR FORE, BACK
 PRINT N$
 LOCATE 1, 1
 COLOR 7, 0
END SUB

SUB SQUARE (A, B, C)
 MT$ = CHR$(219)
 MT$ = MT$ + MT$ + MT$
 LOCATE B, A - 2
 COLOR C, C
 PRINT MT$
 LOCATE B + 1, A - 2
 COLOR C, C
 PRINT MT$
 COLOR 7, 0
END SUB

Here is another program that does something complicated (plays 3d tic tac toe). It uses the DOS interrupt 33 to use mouse, and probably will not work in Vista due to using DOS graphics. It will work on Linux in dosbox using DOS qbasic.exe:
Spoiler:

Code: Select all

DECLARE SUB ENDGAME (X%)
DECLARE SUB MAKEMOVE (X%, Y%, Z%, COLOUR%)
DECLARE SUB GETMOVE (X%, Y%, Z%)
DEFINT A-Z
TYPE REGTYPE
AX AS INTEGER
BX AS INTEGER
CX AS INTEGER
DX AS INTEGER
BP AS INTEGER
SI AS INTEGER
DI AS INTEGER
FLAGS AS INTEGER
END TYPE
DIM SHARED REGS AS REGTYPE
DIM E(7), PEEKB(1999)
RANDOMIZE TIMER
CLS
GOSUB INIT
E(1) = 254: E(2) = 18: E(3) = 2: E(4) = 1: E(5) = 2: E(6) = 66: E(7) = 255
Q = 564: G = 628: L = 768
FOR K = G TO G + 63
  PEEKB(K) = 128
NEXT
FOR K = S TO S + 75
  PEEKB(K) = 128
NEXT
100 CALL GETMOVE(C, R, P)
X = 16 * (P - 1) + 4 * (R - 1) + C - 1
IF PEEKB(G + X) <> 128 THEN GOTO 100
CALL MAKEMOVE(C, R, P, 1)
M = -1: GOSUB 1000
GOSUB 2000
IF W THEN CALL ENDGAME(-1)
IF T THEN CALL ENDGAME(0)
GOSUB 3000
M = 1: GOSUB 1000
GOSUB 2000
GOSUB 7000
IF W THEN CALL ENDGAME(1)
IF T THEN CALL ENDGAME(0)
GOTO 100

1000
PEEKB(G + X) = 128 + M
FOR K = L TO L + 303
  IF PEEKB(K) <> X THEN GOTO 1001
  Y = S + (K - L) \ 4: V = PEEKB(Y)
  IF V = 0 THEN GOTO 1001
  V = V - 128
  IF V = 0 THEN
    V = M + 128
   ELSE
     IF (SGN(V) = SGN(M)) THEN
       V = V + M + 128
     ELSE
       V = 0
     END IF
   END IF
   PEEKB(Y) = V
1001 NEXT
RETURN

2000
W = 0: T = 1
FOR K = S TO S + 75
 V = PEEKB(K)
 IF V THEN T = 0
 IF ABS(V - 128) = 4 THEN W = 1
NEXT
RETURN

3000
FOR K = Q TO Q + 63
  PEEKB(K) = 0
NEXT
FOR K = S TO S + 75
  N = PEEKB(K) - 128
  IF N = -128 THEN GOTO 3002
  Z = E(N + 4)
  F = L + 4 * (K - S)
  FOR J = F TO F + 3
    X = PEEKB(J)
    IF PEEKB(G + X) <> 128 THEN GOTO 3001
    V = PEEKB(Q + X)
    IF V >= 254 THEN GOTO 3001
    V = V + Z: IF Z >= 254 THEN V = Z
    IF V > 255 THEN V = 255
    PEEKB(Q + X) = V
3001 NEXT
3002 NEXT
V9 = 0
FOR K = 0 TO 63
  V = PEEKB(Q + K)
  IF V > 64 AND V < 128 THEN V = V - 64
  IF V > 16 AND V < 32 THEN V = V - 16
  IF V > V9 THEN V9 = V
  PEEKB(Q + K) = V
NEXT
IF V9 < 32 THEN GOTO 4000
3800 X = 0
DO
  IF PEEKB(Q + X) = V9 THEN RETURN
  X = X + 1
LOOP
4000 P4 = 16
FOR K = L TO L + 287 STEP 16
  P = 0
  FOR J = K TO K + 15
    P = P + PEEKB(PEEKB(J) + G) - 128
  NEXT
  IF P > P4 THEN GOTO 4002
  IF P < P4 THEN
    P4 = P: V4 = 0: N4 = 0
  END IF
  FOR J = K TO K + 15
    X1 = PEEKB(J)
    V = PEEKB(Q + X1)
    IF V = 0 THEN GOTO 4001
    IF V < V4 THEN GOTO 4001
    IF V > V4 THEN
      V4 = V: N4 = 1
    ELSE
      N4 = N4 + 1
      IF INT(RND(1) * N4) <> 0 THEN GOTO 4001
    END IF
    X = X1
4001 NEXT
4002 NEXT
IF V4 = 0 THEN GOTO 3800
RETURN

7000
P = X \ 16 + 1
X = X - 16 * (P - 1)
R = X \ 4 + 1
C = (X MOD 4) + 1
CALL MAKEMOVE(C, R, P, 4)
RETURN


INIT:
L = 768
FOR K = 0 TO 63
  PEEKB(L + K) = K
NEXT
L = L + 64
A = 4: B = 16
FOR S = 1 TO 4
  GOSUB 19000
NEXT
A = 16: B = 1
FOR S = 1 TO 13 STEP 4
  GOSUB 19000
NEXT
S = 1: A = 5: B = 16: GOSUB 19000
S = 13: A = -3: B = 16: GOSUB 19000
S = 1: A = 20: B = 1: GOSUB 19000
S = 49: A = -12: B = 1: GOSUB 19000
S = 1: A = 17: B = 4: GOSUB 19000
S = 49: A = -15: B = 4: GOSUB 19000
S = 1: D = 21: GOSUB 18000
S = 16: D = 11: GOSUB 18000
S = 4: D = 19: GOSUB 18000
S = 13: D = 13: GOSUB 18000
GOSUB DRAWBD
RETURN

18000
FOR K = S TO S + 3 * D STEP D
  PEEKB(L) = K - 1: L = L + 1
NEXT
RETURN

19000
FOR J = S TO S + 3 * B STEP B
  FOR K = J TO J + 3 * A STEP A
    PEEKB(L) = K - 1: L = L + 1
  NEXT
NEXT
RETURN

DRAWBD:
SCREEN 12
LINE (0, 0)-(639, 479), 7, BF
LINE (24, 24)-(615, 455), 3, BF
Y = 130: GOSUB GRID
Y = 230: GOSUB GRID
Y = 330: GOSUB GRID
Y = 430: GOSUB GRID
RETURN

GRID:
FOR K = 0 TO 4
  LINE (120 + 20 * K, Y - 20 * K)-(440 + 20 * K, Y - 20 * K), 0
  LINE (120 + 80 * K, Y)-(200 + 80 * K, Y - 80), 0
  LINE (117 - K, Y + 2)-(201 - K, Y - 82), 0
  LINE (437 + K, Y + 2)-(521 + K, Y - 82), 0
NEXT
FOR K = 0 TO 1
  LINE (117 - K, Y + K + 1)-(437 + K, Y + K + 1), 0
  LINE (201 - K, Y - 81 - K)-(521 + K, Y - 81 - K), 0
NEXT
RETURN

SUB ENDGAME (X)
  IF X = -1 THEN
    X$ = "  YOU WON  "
  ELSEIF X = 0 THEN
    X$ = "  TIE  "
  ELSE
    X$ = "  I WON  "
  END IF
  COL = 40 - LEN(X$) / 2
  LOCATE 13, COL
  PRINT CHR$(218) + STRING$(LEN(X$), CHR$(196)) + CHR$(191)
  LOCATE 14, COL
  PRINT CHR$(179) + X$ + CHR$(179)
  LOCATE 15, COL
  PRINT CHR$(192) + STRING$(LEN(X$), CHR$(196)) + CHR$(217)
END SUB

SUB GETMOVE (X, Y, Z)
  REGS.AX = 1
  CALL INTERRUPT(&H33, REGS, REGS)
GETPOS:
  IF INKEY$ = CHR$(27) THEN END
  REGS.AX = 3
  CALL INTERRUPT(&H33, REGS, REGS)
  Z = (REGS.DX - 30) \ 100 + 1
  IF Z < 1 OR Z > 4 THEN GOTO GETPOS
  Y = ((REGS.DX - 30) \ 20) MOD 5
  IF Y < 1 OR Y > 4 THEN GOTO GETPOS
  IF REGS.CX + REGS.DX - 150 - 100 * Z < 0 THEN GOTO GETPOS
  X = (REGS.CX + REGS.DX - 150 - 100 * Z) \ 80 + 1
  IF X < 1 OR X > 4 THEN GOTO GETPOS
  IF REGS.BX = 0 THEN GOTO GETPOS
  REGS.AX = 2
  CALL INTERRUPT(&H33, REGS, REGS)
END SUB

SUB MAKEMOVE (X, Y, Z, COLOUR)
  CIRCLE (80 * X - 20 * Y + 170, 100 * Z + 20 * Y - 60), 35, 8, , , 4 * (8 / 35) / 3
  PAINT STEP(0, 0), COLOUR, 8
  CIRCLE (80 * X - 20 * Y + 170, 100 * Z + 20 * Y - 60), 15, 8, , , 4 * (3 / 15) / 3
  PAINT STEP(0, 0), COLOUR + 8, 8
END SUB
Last edited by dean.menezes on Sat Apr 18, 2009 9:59 pm UTC, edited 1 time in total.

User avatar
Xeio
Friends, Faidites, Countrymen
Posts: 5101
Joined: Wed Jul 25, 2007 11:12 am UTC
Location: C:\Users\Xeio\
Contact:

Re: Learning Code

Postby Xeio » Sat Apr 18, 2009 7:29 pm UTC

Uh, dude, if you're gonna post that much code, please, spoiler it...[spoiler][/spoiler]

User avatar
b.i.o
Green is the loneliest number
Posts: 2519
Joined: Fri Jul 27, 2007 4:38 pm UTC
Location: Hong Kong

Re: Learning Code

Postby b.i.o » Sat Apr 18, 2009 11:38 pm UTC

dean.menezes wrote:Of course you can do complex tasks in QuickBASIC, it is turing complete.

There's a hell of a lot more to programming languages than being Turing complete.

And really, chess and tic-tac-toe are not complicated computational problems if you're trying to prove a point here. Now come back with say, an http server, and I might be mildly impressed. Of course, I'm then going to ask you why you didn't write it in the 50 or fewer lines of Python it would have taken.

dean.menezes wrote:You could write a chess program in brainfuck if you wanted to.

You could. But aside from it being an intellectual exercise, there's absolutely no reason to want to. QBasic doesn't even have the interesting intellectual exercise (or the potential for bragging rights) going for it.

nothinglikeit22
Posts: 19
Joined: Wed Apr 15, 2009 8:29 pm UTC

Re: Learning Code

Postby nothinglikeit22 » Mon Apr 20, 2009 12:06 am UTC

Ok ok ok , i switched to python. and it already seems to be much easier to use and less time consuming. thx for the advice

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: Learning Code

Postby MHD » Tue Apr 21, 2009 3:10 pm UTC

I'll go ahead and say: try lua.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 8 guests