Jump to content

Programming Code Thread

Recommended Posts

Just on the off chance that someone into programming is interested in what can be done with as little as 100 lines of code, or possibly looking for inspiration, this is for you.

 

This is a program my father wrote in 1983 for a computer programming class. It plays bingo, and then lists how many balls were played before a bingo, as well as what the card numbers were for the winning card. It can play as many as 150 cards at a time quite easily. The inventiveness of this program (something his professor has never seen anyone come close to producing before or since) is that it plays the cards in a 3D stack instead of a 2D plane like most people would think to do. By doing this, he was able to reduce the size of the program down from over 1500 lines of code, to exactly 100 lines. If more programmers did this reduction of the program (reducing redundant code) with modern programs, we wouldn't need an 8-core i7 processor with top-of-the-line GPU to do VR or AR gaming at high framerates, or terrabyte drives to hold more than a half dozen games.

 

$JOB	WATFIV
1				INTEGER		BINGO (4,5,5), CARD, COL, ROW
2				INTEGER		ARRAY (10), RANDOM, LAST, SEED /25/
3				INTEGER		CAGE (50), PICK, STEPS (4), /0,0,0,0/
4				LOGICAL		FOUND, DONE
5				DO 13 CARD=1,4
6					DO 12 ROW=1,5
7						DO 10 COL=1,10
8							ARRAY(COL)=COL+((ROW-1)*10)
9		10			CONTINUE
10					LAST=10
11					DO 11 COL=1,5
12						RANDOM=INT(URAND(SEED)*LAST)+1
13						BINGO(CARD,COL,ROW)=ARRAY(RANDOM)
14						ARRAY(RANDOM)=ARRAY(LAST)
15						LAST=LAST-1
16	11			CONTINUE
17	12		CONTINUE
18				BINGO(CARD,3,3)=0
19	13	CONTINUE
20			PRINT 91
21			EXECUTE LISTIT
22	80	DO 40 CARD=1,4
23				DONE=.FALSE.
24				DO 30 PICK=1,50
25					CAGE(PICK)=PICK
26	30		CONTINUE
27				LAST=50
28				WHILE (.NOT.DONE)
29					PICK=INT(URAND(SEED)*LAST)+1
30					EXECUTE MATCH
31	81			CAGE(PICK)=CAGE(LAST)
32					LAST=LAST-1
33					IF (LAST.LT.47) THEN
34						EXECUTE CHECK
35	83			END IF
36	84			FOUND=.FALSE.
37				END WHILE
38				PRINT 90
39				STEPS(CARD)=50-LAST
40	40	CONTINUE
41			EXECUTE LISTIT
42			PRINT 95,(STEPS(L),L=1,4)
43			PRINT 91
44	90	FORMAT('0')
45	91	FORMAT('1')
46	92	FORMAT(4('         B    I    N    G    O  '))
47	93	FORMAT(4('      --------------------------'))
48	94	FORMAT(4(6X,'|',1X,I2,1X,'|',1X,I2,1X,'|'1X,I2,1X,'|',1X,I2,1X,'|',1X,I2,1X,'|'))
49	95	FORMAT(4('          BINGO IN ',I2,' STEPS    '))
50	96	STOP
51			REMOTE BLOCK MATCH
52				DO 55 COL=1,5
53					DO 50 ROW=1,5
54						IF (CAGE(PICK).EQ.BINGO(CARD,COL,ROW)) THEN
55							BINGO(CARD,COL,ROW)=0
56						END IF
57	50			CONTINUE
58	55		CONTINUE
59			END BLOCK
60			REMOTE BLOCK CHECK
61				DO 101 ROW=1,5
62					DONE=.TRUE.
63					DO 100 COL=1,5
64						IF (BINGO(CARD,COL,ROW).NE.0) THEN
65							DONE=.FALSE.
66						END IF
67	100			CONTINUE
68					IF (DONE) THEN
69						QUIT3
70					END IF
71	101		CONTINUE
72				DO 111 COL=1,5
73					DONE=.TRUE.
74					DO 110 ROW=1,5
75						IF (BINGO(CARD,COL,ROW).NE.0) THEN
76							DONE=.FALSE.
77						END IF
78	110			CONTINUE
79					IF (DONE) THEN
80						QUIT3
81					END IF
82	111		CONTINUE
83				IF (BINGO(CARD,1,1).EQ.0.AND.BINGO(CARD,2,2).EQ.0.AND.BINGO(CARD,4,4).EQ.0.AND.BINGO(CARD,5,5).EQ.0) THEN
84					DONE=.TRUE.
85				END IF
86				IF (BINGO(CARD,5,1).EQ.0.AND.BINGO(CARD,4,2).EQ.0.AND.BINGO(CARD,2,4).EQ.0.AND.BINGO(CARD,1,5).EQ.0) THEN
87					DONE=.TRUE.
88				END IF
89			END BLOCK
90			REMOTE BLOCK LIST
91				PRINT 92
92				PRINT 90
93				PRINT 93
94				DO 20 COL=1,5
95					PRINT 94,((BINGO(CARD,COL,ROW),ROW=1,5),CARD=1,4)
96					PRINT 93
97	20		CONTINUE
98			PRINT 90
99			END BLOCK
100			END

	C$END

	C$ENTRY

 

He did a test run of it for the class he was doing, and it took a total of 0.95 seconds to complete a run and play 4 cards to a bingo. When he first submitted it for grading, it was graded by a TA, and he got a 0. When he showed it to the professor and asked why it was failed, the professor took a closer look, and it resulted in a 100, a perfect A.

 

Here is a scan of the original printout from the class. (it excludes the last page, which is a duplicate of the first page, except the timestamps say "6.56.59")

 

vrs9uqfh.jpg

Click to Enlarge

Don't insult me. I have trained professionals to do that.

Share this post


Link to post

I guess TA either was tired, or just thought "Not enough code. 0."

 

Me, I program in Swift, which is a programming language for iOS and the like (basically I can make apps iOS apps). I can add more info, but I'm tired, so here are links with what I made.

A code of a program that I made by myself. It's basically an attempt to replicate the potion mechanic from Dungeon Siege.

This is a screenshot of what I'm currently making (yes, it's Tetris)

A.K.A. UberCatSR

Favorite game: Quake 1.

--------

Avatar made by Neffertity

Share this post


Link to post

I'm a KISS C# programmer that works with Unity 3D. I'm not really cut out for making apps so I make games. With apps I have the complete opposite problem of the "ideas guy". I can program but I haven't been able to come up with any ideas for apps. With games all I have to do is make them fun whereas with apps they have to be useful and I don't know what's useful.

 

Even the "games" I've made I would hesitate to call them that in the traditional sense of the word. They're more akin to experiments with interactive elements that may or may not be fun to play. They don't have well developed core gameplay loops or win states. That's how you can tell I'm a programmer and not a designer. Though I am working on it and hopefully I will make an actual game one day.

I'm not saying I started the fire. But I most certain poured gasoline on it.

Share this post


Link to post
I'm a KISS C# programmer that works with Unity 3D. I'm not really cut out for making apps so I make games. With apps I have the complete opposite problem of the "ideas guy". I can program but I haven't been able to come up with any ideas for apps. With games all I have to do is make them fun whereas with apps they have to be useful and I don't know what's useful.

 

Even the "games" I've made I would hesitate to call them that in the traditional sense of the word. They're more akin to experiments with interactive elements that may or may not be fun to play. They don't have well developed core gameplay loops or win states. That's how you can tell I'm a programmer and not a designer. Though I am working on it and hopefully I will make an actual game one day.

If you're interested, I have a non-paying project for a combination of win32 and Android mobile app with a very specific intention, and I don't currently know either programming language, so I could use some help. (if you're interested, contact me through Steam: BTGbullseye)

Don't insult me. I have trained professionals to do that.

Share this post


Link to post

What's the programming language? I can work with just about any variant of C.

 

Can I contact you via some other means? I've mentioned this to you before but my Steam account was banned years ago. I really don't want to deal with Valve again by getting another one.

I'm not saying I started the fire. But I most certain poured gasoline on it.

Share this post


Link to post

I should pop in this subforum more often. Only just now saw this. -.-

 

Looking over the Bingo code I'd say by modern standards, that's a pretty standard structured code. For when it was done however (and being done for a class as well) that's pretty impressive(especially on . Back then coding practice was still in it's development so there was a lot of unnecessary or inefficiently coded programs. Good use of loops especially, since it would seem a majority of the heavy lifting is done in that. The lack of whitespace is distressing to look at but the computer doesn't read whitespace anyways and obviously it wouldn't be in 100 lines if it was there.

 

@Helio: Primary Unity programmer here as well. C# is my strongest language but I also can do C++ and Java(to a lesser level since I'm less familiar with the syntax of those 2 but enough to work with) and Visual Basic. I don't recommend VB for much of anything outside of standard computer programs. It's got a lot some annoying limitations that take far too much to deal with that C does much better and easier, such as arrays and event handling.

 

If you don't know about it already, Catlike Coding is an insanely good learning resource for intermediate to advanced unity programming. The tutorials this guy makes are VERY in-depth and it's some of the best coding practices I've ever seen. I've even used his clock tutorial in a game I'm working on for a clock tower (alterted to serve it's purpose for ingame time instead of realtime)

 

Is it possible to get an understanding of what level you're at? Like code example or projects you've done? I'd like to know if I can go to you for help sometimes, help you with anything sometimes, or even both. :P

 

 

Also, some of the Unity practices I learned through my time. There's more but I can't recall them right this moment:

 

  • deltaTime is extremely useful. It converts the passage of time between frames into a usable measurement, where a deltaTime of 1 would be 1 second. This can be applied to anything such as movement speed, to camera look speed(Especially helpful for cameras because it removes framerate dependency from the camera sensitivity.)
  • try to treat your scripts as standalone aspects that handle individual tasks instead of a grouping of loosely related functions. For example, when creating a Player class, you may be tempted to put everything in there. Instead, have Player.cs(Things like health, level, attributes, etc) like usual, then have PlayerMotor.cs(for movement), PlayerInteraction.cs(what are they looking at, keypress dependencies like action keys, etc.), PlayerInventory.cs, and so on. This keeps code cleaner and easier to find and organize your functions.
  • In C# Delegations are god's gift to programmers everywhere. If you don't know what they are then I STRONGLY recommend learning them. They aren't hard to use(but also not hard to misuse either. Improper use can lead to memory leaks and other troubles) and they make code significantly cleaner and more functional. It's essentially a variable in which you store functions. Explanation | General Example | Unity Example | Just an interesting topic about delegation by Unity users
    Apparently there's ways to do this in other languages but not nearly as simple as C#'s delegates. Templates in C++ can achieve something similar from what I understand.
  • Singleton patterns can be very helpful but also very easily misused.(even easier to misuse than a delegate) If using a singleton, try to limit your reliance on it to only things important to the overall functionality of the program. Never tried these in general programs so I can't say if you'd even need one there but in games, it should really only be used for things that need to be carried over into other parts of the game(menus, levels, etc), or anything that affects a significantly large portion of the code. Singletons are mostly used for game state managers.

 

 

And if anyone cares, for the visual basic class I had last quarter I made a short GUI driven text adventure. This apparently really impressed the instructor enough to boost my grade to an A when I fell just short. Link

Retired Forum Moderator

Share this post


Link to post

@Rarity Thanks for the info but I already knew all of that. I'm also kind of done with programming right now. While I can program and my programs do run I haven't been able to apply that skill in any practical sense. For instance I can write scripts for several tiny insignificant experiments all of which have nothing in common with eachother. But I was never able to make a fully functional, marketable and fun game. Never really got the hang of it with applications either.

 

Here's some examples of code I've written.

 

 

This code below is for generating a single strand of randomly generated lightning. It attaches to a line renderer parent and three empty gameobjects(a point origin, a point and a point destination). The strand stops going once it's reach the point destination and will reset once you let go of the equals key.

 

using UnityEngine;

public class LightningScript : MonoBehaviour {

public LineRenderer lightning;
public CapsuleCollider cap;
public Transform source;
public Transform destination;

public float timeToPowerUp = 0.5f;

public int numVertices;

public AudioSource aud;


// Update is called once per frame
void Update () {
	if (Input.GetKey(KeyCode.Equals)){
		/*
		if (aud.pitch < 3.0f) {
			aud.pitch += Time.deltaTime;
		}
		*/
		numVertices = Mathf.RoundToInt(Vector3.Distance(source.position, destination.position));
		cap.height = Vector3.Distance(source.position, destination.position);
		Vector3 currentV = source.localPosition;
		lightning.SetVertexCount(numVertices);
		for (int i = 0; i < numVertices; i++) {
			float tmpVfl = (Random.value * 1.0f - 0.5f);
			lightning.SetPosition(i, new Vector3(currentV.x + tmpVfl,currentV.y + tmpVfl,currentV.z + tmpVfl));
			cap.radius = tmpVfl;
			currentV += (destination.localPosition - source.localPosition) / numVertices;
		}
		if(numVertices > 0){
			lightning.SetPosition(0, source.localPosition);
			lightning.SetPosition(numVertices-1, destination.localPosition);
		}
	}
	if(Input.GetKeyUp(KeyCode.Equals)){
		aud.pitch = 0;
		numVertices = 0;
		cap.height = 0;
		lightning.SetVertexCount(0);
		cap.radius = 0;
	}
}
}

 

 

 

These two scripts were designed for top down open world generation. The first script attaches to a parent, the second script attaches to all of it's nine 2D box collider children.

How it works is that when the player triggers one of the parent's children's colliders that particular child would call the parent and have it move over to the child's current position taking all of nine children with it. Effectively creating a procedural bounds system that the player can't escape and possibly screw up how the level is procedurally generated. It's two of the scripts I'm most proud of for how clever they are.

 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class OnStartInstantiate : MonoBehaviour {
public Collider2D playerCollider;
public GameObject generator;
public List pastGeneratorPositions = new List();
   public void Start() {
	if(playerCollider != null){
		if(!pastGeneratorPositions.Contains(new Vector2(transform.position.x,transform.position.y))){
			GameObject currentGenerator = Instantiate(generator, transform.position, Quaternion.identity) as GameObject;
			currentGenerator.GetComponent().playerCollider = playerCollider;
			pastGeneratorPositions.Add(new Vector2(currentGenerator.transform.position.x,currentGenerator.transform.position.y));
		}
	}
   }
}

using UnityEngine;
using System.Collections.Generic;
public class MoveParentInstantiate : MonoBehaviour {
public Collider2D playerCollider;
void Start() 
{
	playerCollider = transform.parent.GetComponent().playerCollider;
}
   void OnTriggerEnter2D(Collider2D other) {
	if(other == playerCollider){
		transform.parent.position = transform.position;
		transform.parent.GetComponent().Start();
	}
   }
}

 

 

I have quite a bit more code but this should give you a rough idea as to what I'm able to write. All the scripts I've written are designed to be as simplistic as possible without compromise for easy readability.

 

I'm kind of done with programming right now because of the reason I mentioned above. These scripts all came from tiny insignificant little experiments I was toying with no practical value whatsoever. I'm not sure what I'm lacking. But I'm lacking in it so severely that I can't apply my programming skills in any practical sense.

 

I've tried making my own game. A year was spent and a game didn't come out of it. What came out it was a fucking hodgepodgey mess of a failure. I didn't even bother to publish the thing it was so bad. It was very evident that I bit off more than I could chew. I'm a programmer that made nothing practical for a year and it shows. I'm still upset that I wasted a fucking year trying to make whatever the hell that was which ultimately turned out to be fucking nothing.

 

But it would've been so much worse had I put more time into it. It was just shit layered on top of shit layered on top of shit layered on top of shit and I would've just been digging myself into a hole. I could keep going as to how fucking excruciatingly egregious it all was. By the time a year had past I was out for blood and wanted to fucking die. I don't even want to think about it. Such a waste. :cry:

I'm not saying I started the fire. But I most certain poured gasoline on it.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in the community.

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Who's Online   0 Members, 0 Anonymous, 501 Guests (See full list)

    • There are no registered users currently online
×
×
  • Create New...

This website uses cookies, as do most websites since the 90s. By using this site, you consent to cookies. We have to say this or we get in trouble. Learn more.