Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 95 96 [97] 98 99 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 902771 times)

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1440 on: February 12, 2012, 03:17:54 am »

I only know a bit of c++,
everyone said don't learn c++ as a first language because it's difficult,
but I honestly haven't run into any difficulties with it really. Maybe the hard parts are yet to come?
It's mostly the various things that can go horribly wrong, particularly with pointers, when you don't fully think through what it is you are doing. For example, if 3/4 of the way through an assignment you realize you created 2 linked lists which happen to use the same nodes, causing a pointer explosion when one of the lists is sorted, it can put one in a table flipping mood. But yeah, so long as you know what you are doing and think through it first, C++ isn't too bad.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1441 on: February 12, 2012, 03:26:17 am »

You really need to get used to writing in the IDE more. You know how to use comments, right?
comments are like one of the first things taught, how could I not know comments.


and vector, what's the project you're working on?


And thanks for the insight alway
« Last Edit: February 12, 2012, 03:29:17 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1442 on: February 12, 2012, 03:35:35 am »

It's mostly the various things that can go horribly wrong, particularly with pointers, when you don't fully think through what it is you are doing. For example, if 3/4 of the way through an assignment you realize you created 2 linked lists which happen to use the same nodes, causing a pointer explosion when one of the lists is sorted, it can put one in a table flipping mood. But yeah, so long as you know what you are doing and think through it first, C++ isn't too bad.
There is also the issue of when to deconstruct objects. If you don't know what you are doing, you get a mess of null pointers and memory leeks.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1443 on: February 12, 2012, 03:38:45 am »

I have a new CS project, which takes a dump from Twitter (wordplay intentional) and then finds out how people in various states feel about some subject on average.  It's an introduction to how object-oriented programming works, before we actually get to know what a class is and so on.

Most of the actually interesting work has already been done for us, so I just get to script-kiddie around ad nauseum.  At least I'll probably be able to submit it by the end of the day tomorrow.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1444 on: February 12, 2012, 03:45:10 am »

Eh, I'll write up part 2 soon enough. Busy eating, drinking and learning about Celtic warfare right now. Well other stuff too, aparently they were pretty good artists!

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1445 on: February 12, 2012, 03:46:08 am »

Sounds like a fun project.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Vector

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1446 on: February 12, 2012, 03:47:41 am »

Nah, Max White, don't worry about it.  I'm still not quite awake enough to peruse the first part, so I'll read that in the morning and return some questions.  Enjoy your Irish war.
Logged
"The question of the usefulness of poetry arises only in periods of its decline, while in periods of its flowering, no one doubts its total uselessness." - Boris Pasternak

nonbinary/genderfluid/genderqueer renegade mathematician and mafia subforum limpet. please avoid quoting me.

pronouns: prefer neutral ones, others are fine. height: 5'3".

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1447 on: February 12, 2012, 05:00:23 am »

Eh, read at your own leisure when you have time. Even if you are not interested, python is a very popular language for getting into making games thanks to pygame. I'm sure somebody out there can always use the help.

Anway, part 2!
The important thing to realise is that objects are types, just like ints or strings, we can have multiple objects of the same class with different properties. For example...

Spoiler (click to show/hide)
Try that out. As you can see, the first and second player are separate objects, meaning that player1's name is not the same name as player2's name. Now why don't we add that other thing we needed our player class to have.

import random

Spoiler (click to show/hide)
Now just like each player has their own separate name, each player also has their own target to guess. We can declare what ever values we need in the __init__ method for the class, but we can also do something else for our players. We can give them functions.

Spoiler (click to show/hide)
There, we have our guess method, and with that, our player class is pretty much done! It is now a fully functional class that can preform everything required by our project, but let's explain how that a little more.
Once again, we start by defining a function inside the class, give it a name, and once again we make sure the first parameter is self. Almost the same as __init__, but this time we give a more meaningful name to the function. After that, you need to remember that we are working inside an object, thus if we want to reference to anything in the object, we are referencing to itself, or self, as the case may be. Might sound a little tricky, but just think that when you call the function with object.function(), it is object that is put into 'self', thus using 'self' will use the object.

The rest should be straight forward enough, and all things you have seen before. If not, feel free to ask.
That is it for objects, as far as this lesson goes, but we might as well finish the game off for fun. It should be all old news by now anyway, but once again, please tell me if anything seems strange to you.
Spoiler (click to show/hide)

There we are! In just 37 lines, counting white space and imports, we made a fully functional game. Such is the power of objects, to keep logic where it needs to be, and encapsulate relevant data.

eerr

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1448 on: February 12, 2012, 05:07:38 am »

I messed up, the problem only happens with primtives.

Mostly the math library.
Logged

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1450 on: February 12, 2012, 10:00:15 am »

Java appears to have objections to me making arrays of custom classes. It throws a null pointer exception, which probably means the array wasn't initialized.

The code, shortened to the relevant parts:

Code: [Select]
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;

public class Labyrinth extends Frame {
  int mapx = 19;
  int mapy = 13;
  Tile tiles[][] = new Tile[mapx][mapy];

  public class Tile {
    int index;
    boolean walkable;

    public void setProperties(int i, boolean w) {
      index = i;
      walkable = w;
    }

  }

public Labyrinth(String title) {
    generateMap();
  }

public void generateMap() {
    for (int x = 0; x < mapx; x++) {
      for (int y = 0; y < mapy; y++) {
        if (x != 9) {
          tiles[x][y].setProperties(1, false); << Null Pointer here
        } else {
          tiles[x][y].setProperties(2, true);
        }
      }
    }
  }

  public static void main(String[] args) {
    new Labyrinth("labyrinth");
  }

}

That could be an incredibly stupid problem... Unfortunately, I am an incredibly stupid coder, so I need pointers here.
« Last Edit: February 12, 2012, 10:03:10 am by Errol »
Logged
Girls are currently preparing signature, please wait warmly until it is ready.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1451 on: February 12, 2012, 10:11:22 am »

Errol, when you create an array of objects, all of the locations of that array initialize to null.  The problem with your code is that you're trying to call methods on those null objects.  Either instantiate them as you go or instantiate them all at the beginning.
Logged

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1452 on: February 12, 2012, 10:23:09 am »

You are trying to say that I should be going something like
Code: [Select]
tiles[x][y] = new Tile(index, walkable) during the generateMap method?
Logged
Girls are currently preparing signature, please wait warmly until it is ready.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1453 on: February 12, 2012, 10:33:03 am »

More or less...this should serve your purpose:

Code: [Select]
  public void generateMap() {
    for (int x = 0; x < mapx; x++) {
      for (int y = 0; y < mapy; y++) {
        tiles[x][y] = new Tile();
        if (x != 9) {
          tiles[x][y].setProperties(1, false); << Null Pointer here
        } else {
          tiles[x][y].setProperties(2, true);
        }
      }
    }
  }

What is that if(x != 9) block for?

And you may not have noticed that your Tile() inner class doesn't have a constructor.  Java creates default (no arg) constructors for you if you don't create any constructors.  Rather than set properties of Tiles, you might want to just instantiate them using the specific constructor parameters you need.  This eliminates the need for the setProperties() method.
Code: [Select]
// Tile code
  int mapx = 19;
  int mapy = 13;
  Tile tiles[][] = new Tile[mapx][mapy];

  public class Tile {
    int index;
    boolean walkable;

    public Tile(int index, boolean walkable) {
        this.index = index;
        this.walkable = walkable;
    }

  }
// Labyrinth code
  public void generateMap() {
    for (int x = 0; x < mapx; x++) {
      for (int y = 0; y < mapy; y++) {
        if (x != 9) {
          tiles[x][y] = new Tile(1, false);
        } else {
          tiles[x][y] = new Tile(2, true);
        }
      }
    }
  }
Logged

Errol

  • Bay Watcher
  • Heaven or Hell, Duel 1 -- Let's Rock!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1454 on: February 12, 2012, 10:42:00 am »

What is that if(x != 9) block for?

Was used to confirm that indeed different tiles work. Also looks better if you have a row of walkable dungeon floor tiles in place of just wall tiles.

And you may not have noticed that your Tile() inner class doesn't have a constructor.  Java creates default (no arg) constructors for you if you don't create any constructors.  Rather than set properties of Tiles, you might want to just instantiate them using the specific constructor parameters you need.  This eliminates the need for the setProperties() method.

This is pretty much what I was thinking of there. Thanks.

/e: And here we go again!

Code: [Select]
public Labyrinth() {

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) { dispose(); }
    });

    addKeyListener(new KeyListener() {
      public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == e.VK_UP) {
          movePC(-1, 0);
        }
        if(e.getKeyCode() == e.VK_DOWN) {
          movePC(1, 0);
        }
        if(e.getKeyCode() == e.VK_LEFT) {
          movePC(0, -1);
        }
        if(e.getKeyCode() == e.VK_RIGHT) {
          movePC(0, 1);
        }
      }
    });

  }

Labyrinth.java:44:38: error: <anonymous Labyrinth$2> is not abstract and does not override abstract method keyPressed(KeyEvent) in KeyListener
    addKeyListener(new KeyListener() {
                                     ^
1 error

I am kind of puzzled. The window listener has always worked, so why is the key listener complaining about lacking abstractness... or should the whole thing just go somewhere that is not the constructor.
« Last Edit: February 12, 2012, 11:23:20 am by Errol »
Logged
Girls are currently preparing signature, please wait warmly until it is ready.
Pages: 1 ... 95 96 [97] 98 99 ... 796