How to not puke while trying to learn to program?

Index » 喫茶店 (Koohii Lounge)

 
IceCream Closed Account
Registered: 2009-05-08 Posts: 3124

.

Last edited by IceCream (2011 February 05, 9:48 am)

JimmySeal Member
From: Kyoto Registered: 2006-03-28 Posts: 2279

I don't know if WPF is the best place to start programming.  I've been at this programming gig for a while, and WPF still makes my head hurt often.  In WPF, there's really no way around all those bindings and dependency properties, and it is a challenge to get a grip on them.
It might be worth the time cutting your teeth on some console-based stuff first.

As for your specific question, I suggest the following algorithm (pseudocode):

for(i = 1 to 3)
   do
      pick a grid cell at random
      if(there is no mark in the grid on or next to that spot)
         mark that spot
   while(the selected cell was no good)

Last edited by JimmySeal (2011 January 25, 8:19 am)

thurd Member
From: Poland Registered: 2009-04-07 Posts: 756

If you are not forced by external circumstances to use VB or the whole .NET thingy, I'd say ease yourself into programming by learning a language that is friendly to beginners & doesn't overwhelm you with enormous IDEs and lots of functionalities you'll never use.

There are tons of really basic guides to Java, Ruby or Python, some of them focus on creating simple windows, grids etc. A simple application like you describe is a matter of sub-100 lines of code in them (some wizards would probably manage with just a couple of lines wink).

Advertising (register and sign in to hide this)
JapanesePod101 Sponsor
 
Gingerninja Member
From: England Registered: 2008-08-06 Posts: 382

Tried to learn programming stuff once... took me 15 minutes to to piss on with it to get one line of text to appear on screen when I ran the code. At that point, I sacked it off. Good on you those who have the patience, I certainly didn't.

FooSoft Member
From: Seattle, WA Registered: 2009-02-15 Posts: 513 Website

It's OK, as a professional programmer I can say WPF makes me puke as well smile WPF is a terrible, terrible thing to learn as part of your first exposure to programming. I strongly recommend learning something like Python instead.

bombpersons Member
From: UK Registered: 2008-10-08 Posts: 907 Website

If you're trying to get into programming, I wouldn't suggest starting with Visual Basic. Basic isn't well designed to incorporate a OOP design, which VB.NET tries to do. Also, VB's syntax is ugly as sin. I know quite a few people who had to program in VB for their A-Levels and they hated it. It's also pretty much never used in industry.  I'd suggest looking at C# if you want to start programming in .NET, since it means you can easily move up to C++ later on if you want to. However, if you want something very easy to learn, then I'd suggest python.

I think Jimmy's got the right idea. All you need to do is generate a random number in a loop, then if the block that number refers to is not next to a block that contains a circle, mark that block and increase a counter and stop when it reaches 3.

C# / Psuedo code

Code:

int i = 0;
while (i <= 3)
{
    randomBlock = GetRandomBlock();
    if (!randomBlock.NextToCircle())
    {
        randomBlock.AddCircle();
        i++;
    }
}
wccrawford Member
From: FL US Registered: 2008-03-28 Posts: 1551

Wow, yeah, you jumped in pretty deep.  I'm going to repeat a lot of the advice above:

WPF = No!
VB = Possible, but...
C# = Much better.

If I were teaching someone to program today, C# would probably be the language I'd pick, despite not having used it for years now.  It's got an excellent (free) IDE, lots of community, good syntax, it's made for GUI stuff...  Pretty much everything I'd ask for.

wccrawford Member
From: FL US Registered: 2008-03-28 Posts: 1551

IceCream wrote:

@JimmySeal:
thanks for the basic layout of the code! umm, i have no idea how to get it to pick a grid square at all, put the circle inside, or know whether there's any circle in the square next to it though :\

Grid:  Look into multidimensional arrays.
Pick a block:  Look into the Random functions.
Know if there's a block next to this the chosen one:  Again, multidimensional arrays.

JimmySeal Member
From: Kyoto Registered: 2006-03-28 Posts: 2279

You can use C# with WPF just as much as you can use VB.NET, and I agree that it's a much nicer language to work with and probably more widely used overall.

But, if everyone else where you are (wherever that is) is using VB.NET, then you might be better off sticking it out.  You can't just mix and match the two that easily, and if these other people (whoever they are) need to modify your work, that might also be a problem.

But you haven't told us anything about your situation, so I can't say the above for sure.

Reply #10 - 2011 January 25, 9:25 am
thurd Member
From: Poland Registered: 2009-04-07 Posts: 756

wccrawford wrote:

If I were teaching someone to program today, C# would probably be the language I'd pick, despite not having used it for years now.  It's got an excellent (free) IDE, lots of community, good syntax, it's made for GUI stuff...  Pretty much everything I'd ask for.

There are a few problems with C# though wink
1. It's a cheap & inferior rip-off of Java
2. Microsoft is satan

Reply #11 - 2011 January 25, 9:46 am
JimmySeal Member
From: Kyoto Registered: 2006-03-28 Posts: 2279

thurd wrote:

There are a few problems with C# though wink
1. It's a cheap & inferior rip-off of Java
2. Microsoft is satan

In what ways is C# inferior to Java, pray tell?

Reply #12 - 2011 January 25, 9:48 am
vgambit Member
Registered: 2007-06-21 Posts: 221

JimmySeal wrote:

thurd wrote:

There are a few problems with C# though wink
1. It's a cheap & inferior rip-off of Java
2. Microsoft is satan

In what ways is C# inferior to Java, pray tell?

Let's not start this here. This thread is about IceCream learning how to program. No elitism necessary.

That being said, I would suggest starting with Ruby. There are plenty of interactive lessons available online. It's the simplest-to-understand modern language I know.

Reply #13 - 2011 January 25, 9:56 am
JimmySeal Member
From: Kyoto Registered: 2006-03-28 Posts: 2279

IceCream wrote:

Nobody else will have to modify my programs, so the benefits of using the language everyone else is using (being able to copy bits of code from existing programs, or ask for help if i'm really stuck) i guess should be weighed up against how difficult the language is to learn. I'd like to be self-sufficient as much as possible, and be able to write programs fairly quickly when i think of ideas for experiments. But it could be anything, so i need to use a language that has good flexibility & that i can control lots of different things.

I think VB.NET vs. C# is really a matter of preference and neither is much harder to learn than the other.  Both have the same sets of features (where it matters) because they operate on top of the .NET framework.  There is interoperability between them, in that if one of your colleagues compiles a bunch of functionality into a DLL and gives it out, you can utilize that regardless of which .NET language you're using.

If most of your colleagues are using VB.NET, I suggest using that, as it will be easier to get help from the people who are doing stuff similar to what you're doing.

Last edited by JimmySeal (2011 January 25, 9:58 am)

nadiatims Member
Registered: 2008-01-10 Posts: 1676

It's been a long time since I did any programming and have no idea what WPF is but I'll have a crack at an algorithm too. I have absolutely no idea if this algorithm would work though.
// marks comments

here's your grid. some kind of array holding boolean (marked/unmarked). By default they should be set as unmarked.

1,1;1,2;1,3;1,4;
2,1;2,2;2,3;2,4;
3,1;3,2;3,3;3,4;
4,1;4,2;4,3;4,4;

//------------------------code starts here----------------
for(i = 1 to 3)//place the marks
{
//set a boolean
vacant=1

//choose random grid coordinates
x=random(1 to 4)
y=random(1 to 4)

//check vacancy of grid spaces
for(j=-1 to 1)
{
for(k=-1 to 1)
{
if(x+j,y+k is marked){vacant=0}
else{}
j=j+1
k=k+1
}
}

//place mark if space is vacant
if(vacant=1){mark (x,y)}
else{}
}

Last edited by nadiatims (2011 January 25, 10:08 am)

Zarxrax Member
From: North Carolina Registered: 2008-03-24 Posts: 949

Wow, if you are just starting to program, what you are doing sounds pretty complicated.
You really, really need to start off learning the basics. Make a few fun little programs or follow some beginners tutorials to help get used to programming, and start to understand how it all works. Once you become familiar with the basics, its not too hard to add new, specific stuff into your arsenal.

battlehymnz Member
From: USA Registered: 2010-09-25 Posts: 16

I think you should focus on just using text to display your output. Once you get the logic code down, setting it up in a GUI won't sound nearly as difficult as it does now.

As far as how to set up the grid, I'd suggest using a multidimensional array. Something like:

Code:

int[,] grid = new int[3, 3]; /* C# Code */
const int empty = 0;
const int occupied = 1;

/* You can use a loop to assign all the spaces their value initially,
    but I'm just going to do them one by one here */
grid[0, 0] = empty;
grid[0, 1] = empty;
grid[0, 2] = empty;
grid[1, 0] = empty;
grid[1, 1] = empty;
grid[1, 2] = empty;
grid[2, 0] = empty;
grid[2, 1] = empty;
grid[2, 2] = empty;

/* The above can be thought of as:
    [0, 0] [0, 1] [0, 2]
    [1, 0] [1, 1] [1, 2]
    [2, 0] [2, 1] [2, 2]
    and right now, all cells would be empty:
    [X] [X] [X]
    [X] [X] [X]
    [X] [X] [X]
*/

grid[0,1] = occupied;
grid[2,2] = occupied;

/* Now:
    [X] [O] [X]
    [X] [X] [X]
    [X] [X] [O]
*/

I think once you can convert the abstract idea of a grid into a form the compiler can understand, the rest is just using logic and iteration to do whatever you want.

EDIT: Is it just me or is the formatting of this post a little screwed up?

Last edited by battlehymnz (2011 January 25, 10:35 am)

Blahah Member
From: Cambridge, UK Registered: 2008-07-15 Posts: 715 Website

For what it's worth, I'm in a similar situation (have to start producing useful programs at short notice), although I do have some very minor programming experience. I've never done any GUI programming, but I've made basic command-line tools in various languages before losing the will to live.

If you do it using Python we can learn together!

pm215 Member
From: UK Registered: 2008-01-26 Posts: 1354

battlehymnz wrote:

I think you should focus on just using text to display your output. Once you get the logic code down, setting it up in a GUI won't sound nearly as difficult as it does now.

Yeah. A lot of programming is about breaking down problems properly so that you can deal with it one tiny bit at a time. Otherwise complexity in one area will spread its tentacles through the entire program and it'll be impossible to understand.

In this case, as battlehymnz says, if you distinguish "how do I work out where I can put these random circles?" from "how do I draw the things on the screen" you've got two hopefully easier problems rather than one hard one.

The other thing to note is that the answer to the "random circles" part is not very dependent on your choice of programming language or graphics or whatever. The syntax might differ, but a reasonable solution is going to look pretty similar in most procedural languages (ie most ones you might be using). Solving this kind of problem is really about logic and knowing the basic data structures and features of the programming language (and is the kind of problem that might be used as a teaching problem for learning to program). Actually drawing circles, on the other hand, is where you end up with something that's strongly tied to your programming language, graphics library and so on. Solving problems in this part is likely to involve a lot of looking things up in the documentation to answer questions like "how do I draw a circle in red rather than blue?". This is another reason this is a good place to split the problem up -- you don't have to keep switching how you're thinking about things.

PS: if it is important for the research that the random circle layout has an exactly 1 in N chance of being one of the N valid layouts of circles, you might need to think carefully about how you're generating it to be sure you're not accidentally making some layouts more likely than others. If you just want it to be "vaguely random" then this probably isn't a problem.

bombpersons Member
From: UK Registered: 2008-10-08 Posts: 907 Website

The way I would do it would be to use create a node like object for each block. Each node would contain a list of pointers to other nodes that are next to them. That way all you would need to do is add a method to loop through all the references and check if the node is marked to the node class.

in C#

Code:

class Node
{
    // List of nodes that are next to this one
    List<Node> nextNodes;
    
    // bool to indicate whether or not this node is circled
    public bool IsCircled = false;
    
    // Returns true if there is a node next to this one that is circled
    public bool IsNextCircled()
    {
        foreach (Node node in nextNodes)
        {
            if (node.IsCircled)
            {
                return true;
            }
        }
        
        return false;
    }
}

Then just create a 2d array of Nodes and link the nodes together.

I've used this method before to create a boggle game that could solve itself using radix trees for one of my university module assignments.

Reply #20 - 2011 January 25, 1:30 pm
chamcham Member
Registered: 2005-11-11 Posts: 1444

NOTE: I thinking of a 4x4 grid in terms of x,y coordinates. They range from (0,0) to (3,3).
Also, I'm assuming no wrap-around (i.e. one edge of the 4x4 matrix wrapping around
to the other side).

Here's how to check for adjacency (i.e. are the points next to each other):

For example, Let's take points A and B.
a)Subject the x-coordinates of A and B. Take the absolute value of that.
b)Subject the y-coordinates of A and B. Take the absolute value of that.
c)If either of those numbers is greater than 1, then you're good to go..... :-p

It makes sense if you think about it. If either the x or y coordinates
is more than 1 square away, then the circles are NOT next to each other.
Otherwise, they are next to each other.

Considering that we need to make 3 points here's what to do (for a 4x4 square).
Point A= random point in the square
Point B = take point A and move 2 or 3 (chosen at random) points in the x-direction
Point C = take point A and move 2 or 3 (chosen at random) points in the y-direction

If you think about it, in a 4x4 square, two points are either 0, 1, 2, or 3 squares away (including diagonals). Zero squares is not acceptable since we want 2 distinct points.
One square is not acceptable, since we don't want the squares to be next to each other.
So that leaves us with being 2 or 3 squares away.

Finally, when you have the coordinates, you can build the array as follows:
Initialize an array to zero.
Circle1 = A
Circle2 = B
Circle3 = C

No loops needed.... :-)

If you need to account for wrap-around, some changes are necessary.

Last edited by chamcham (2011 January 25, 2:05 pm)

Reply #21 - 2011 January 25, 2:44 pm
brianobush Member
From: Portland Registered: 2008-06-28 Posts: 241 Website

Just to chime in with others against VB. It isn't the best way to start off. Something simpler is usually best to get your feet wet.  python is a good choice.

Reply #22 - 2011 January 25, 3:00 pm
iSoron Member
From: Canada Registered: 2008-03-24 Posts: 490

chamcham wrote:

Considering that we need to make 3 points here's what to do (for a 4x4 square).
Point A= random point in the square
Point B = take point A and move 2 or 3 (chosen at random) points in the x-direction
Point C = take point A and move 2 or 3 (chosen at random) points in the y-direction

You will never generate this configuration:

    ● ○ ○ ○
    ○ ○ ● ○
    ○ ○ ○ ○
    ○ ● ○ ○

And if you are unlucky, you may end up with two adjacent points:

   ○ ● ○ ○  Three ← steps results in ○ ● ● ○ (assuming you wrap around)

EDIT: Here's a python implementation. Squares are numbered from 0 to 15 like this:
0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

Code:

from random import randint
def dist(p1, p2):
    return max(abs(p1%4-p2%4), abs(p1/4-p2/4))

available = range(16)
for i in range(3):
    pick = available[randint(0, len(available)-1)]
    available = [x for x in available if dist(x,pick) > 1]
    print pick

Last edited by iSoron (2011 January 25, 3:40 pm)

Reply #23 - 2011 January 25, 3:04 pm
Thora Member
From: Canada Registered: 2007-02-23 Posts: 1691

Icecream, does this mean they extended your short-term stay in Kyoto? Whatever the plan, I'm so glad it's working out for you.

I showed your report to my friend who had left the investment banking jungle to research african jungles. I was surprised to see photos of the chimpanzee's using computers! [edit: in your report]  He knew about your lab and said your boss is well respected. This friend did his masters in something related to wildlife conservation and is now worked on his PhD in something related to animal congnition and evolution. He's made 2 wildlife documentaries and a children's TV show (kind of an animal version of mythbusters). I thought you might find his career change story inspiring. :-)

Excuse the interruption folks - back to your wizardry. (I don't think my experience with pencilled and punched cards, paper ribbons, dingy basement lab and overnight waits for the results would help much.) lol

Last edited by Thora (2011 January 25, 6:45 pm)

Reply #24 - 2011 January 25, 3:44 pm
shadysaint Member
From: Pennsylvania Registered: 2008-09-07 Posts: 88

Just to throw my 2 cents in...

Learn to program with Java and Python.  Both of these languages are rather straightforward as far as the fundamentals of programming are concerned.  And more importantly, they are actually useful (unlike VB).

Although if you're an engineer, I would suggest learning Octave (and/or Matlab).

Reply #25 - 2011 January 25, 4:06 pm
chamcham Member
Registered: 2005-11-11 Posts: 1444

iSoron wrote:

chamcham wrote:

Considering that we need to make 3 points here's what to do (for a 4x4 square).
Point A= random point in the square
Point B = take point A and move 2 or 3 (chosen at random) points in the x-direction
Point C = take point A and move 2 or 3 (chosen at random) points in the y-direction

You will never generate this configuration:

    ● ○ ○ ○
    ○ ○ ● ○
    ○ ○ ○ ○
    ○ ● ○ ○

And if you are unlucky, you may end up with two adjacent points:

   ○ ● ○ ○  Three ← steps results in ○ ● ● ○ (assuming you wrap around)

EDIT: Here's a python implementation. Squares are numbered from 0 to 15 like this:
0  1  2  3
4  5  6  7
8  9  10 11
12 13 14 15

Code:

from random import randint
def dist(p1, p2):
    return max(abs(p1%4-p2%4), abs(p1/4-p2/4))

available = range(16)
for i in range(3):
    pick = available[randint(0, len(available)-1)]
    available = [x for x in available if dist(x,pick) > 1]
    print pick

I wasn't assuming wrap around and even stated that.
But on second thought, my first solution was incorrect.

I thought about wrap around and it actually makes the problem easier.

Point A= random point in the square
Point B = take point A, move 2 points in the x-direction and choose any y-coordinate
Point C = take point A, move 2  points in the y-direction and choose any x-coordinate

It's a right triangle and none of the points will ever be adjacent to one another.