Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 16 17 [18] 19 20 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100612 times)

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #255 on: August 23, 2011, 12:20:12 pm »

Glyph is the great Ruby expert, I believe. I never touched the stuff. It looks yucky.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

quinnr

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #256 on: August 23, 2011, 12:39:02 pm »

I don't know too much about ruby, but typically you'd use a 2-dimensional array for that, which stores either structures, classes (if you know what data each cell can hold beforehand.) or hash maps (if you don't).

Ahh, that actually helped quite a bit. Enough to get me to where I can possibly work something out. Thanks!
Haven't ever done any object oriented programming...it's quite different to what I'm used to.
Logged
To exist or not exist, that is the query. For whether it is more optimal of the CPU to endure the viruses and spam of outragous fortune, or to something something something.

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #257 on: August 24, 2011, 01:08:34 pm »

I'm having some problem with some java code. I'm not very good with java, so this code has been cannibalized from two seperate sources, but I've been spending quite some time trying to get it to work. I have created a class called 'Configuration', and I am trying to save and load a configuration called tcf. This is the code I have now:

Code: (code for loading) [Select]
    private void OpenActionPerformed(java.awt.event.ActionEvent evt) {                                     
    int returnVal = OpenChooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = OpenChooser.getSelectedFile();
        try {
          // What to do with the file, e.g. display it in a TextArea
FileInputStream fis = new FileInputStream(file.getAbsolutePath()+".tcf");
ObjectInputStream ois = new ObjectInputStream(fis);
                try {
                    tcf =    (Configuration) ois.readObject();
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
ois.close();
        } catch (IOException ex) {
          System.out.println("problem accessing file"+file.getAbsolutePath());
        }
    } else {
        System.out.println("File access cancelled by user.");
    }
    }   

Code: (code for saving) [Select]
    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {                                     
    int returnVal = SaveChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = SaveChooser.getSelectedFile();
        try {
          // What to do with the file, e.g. display it in a TextArea
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+".tcf");
ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tcf);
oos.close();
        } catch (IOException ex) {
          System.out.println("problem accessing file"+file.getAbsolutePath());
        }
    } else {
        System.out.println("File access cancelled by user.");
    }
    }                                   

The save code thows an IO exception when I try to run it, though it does manage to create a file. When I try to load the file, it throws another IO exception, but I do not know whether this is because the loading code is bad, or because the file was not saved properly.
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #258 on: August 24, 2011, 01:37:11 pm »

Have you tried printing the IOException so you can see what the actual error is? And possibly the stack trace as well so you can see exactly what command threw the error.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #259 on: August 24, 2011, 03:33:47 pm »

I modified the saving code so the IO exception handling ran two extra lines of code, one printing the exception message and one printing the stack trace:
Code: [Select]
    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {                                     
    int returnVal = SaveChooser.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = SaveChooser.getSelectedFile();
        try {
          // What to do with the file, e.g. display it in a TextArea
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+".tcf");
ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tcf);
oos.close();
        } catch (IOException ex) {
          //System.out.println("problem accessing file"+file.getAbsolutePath());
          System.out.println(ex.getMessage());
          System.out.println(ex.getStackTrace());
        }
    } else {
        System.out.println("File access cancelled by user.");
    }
    }                                   

I ended up getting the following outputs:
Code: [Select]
Configuration
[Ljava.lang.StackTraceElement;@31a3ca10
I'm afraid that I can't really make heads or tails of the stack trace message. The IO exception message stating 'Configuration' makes me think that there is something wrong with my configuration class, though. Is there something I need to add to the Configuration code in order for this code to work?
Logged

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: Programming Help Thread (For Dummies)
« Reply #260 on: August 24, 2011, 04:18:49 pm »

You want to do ex.printStackTrace(), that should give you more information.


Might be permissions. Are you saving this in Program Files or something like that?

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #261 on: August 24, 2011, 04:26:21 pm »

Also ex.toString() will give slightly more information than getMessage(). Namely which of the IOException subclasses are being thrown, although that should show in the stack trace.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

ed boy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #262 on: August 24, 2011, 04:42:02 pm »

I changed the "System.out.println(ex.getStackTrace());" to "System.out.println(ex.toString());" and added "ex.printStackTrace". I tried saving it to the desktop, and got the following:
Code: [Select]
Configuration
java.io.NotSerializableException: Configuration
java.io.NotSerializableException: Configuration
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at MainMenu.SaveActionPerformed(MainMenu.java:456)
at MainMenu.access$300(MainMenu.java:23)
at MainMenu$4.actionPerformed(MainMenu.java:364)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I then tried saving it to the C drive and got:
Code: [Select]
C:\asdf.tcf (Access is denied)
java.io.FileNotFoundException: C:\asdf.tcf (Access is denied)
java.io.FileNotFoundException: C:\asdf.tcf (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
at java.io.FileOutputStream.<init>(FileOutputStream.java:84)
at MainMenu.SaveActionPerformed(MainMenu.java:454)
at MainMenu.access$300(MainMenu.java:23)
at MainMenu$4.actionPerformed(MainMenu.java:364)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

From a quick googling of "NotSerializableInterface", it appears that the errors are being thrown are because I need to add some extra code to my "Configuration" class. I'll try to sort it out tomorrow.
Logged

3

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #263 on: August 24, 2011, 05:55:57 pm »

This is more a conceptual post than one to do with actual implementation, but I'll ask here first:

I'm writing a roguelike (via libtcod + python), and I want to implement a facing system so the player can only see in one of the eight directions at any one time. libtcod doesn't support this sort of thing directly so I'll probably have to write my own algo. My current plan is to use a similar implementation to the existing libtcod permissive FOV, but only processing one of the quadrants (actually overlapping octants in this case) at a time. Where the difficulty lies is in emulating libtcod's approach so as to make the player FOV aesthetically similar to other lighting (for which I'll probably be using libtcod's algos).

So my questions are, to anyone more experienced in the field than I, is there some flaw in the theory that I'm not seeing, and/or is there a more efficient way of going about this?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #264 on: August 24, 2011, 06:07:39 pm »

Perhaps make "virtual walls" in the directions the player isn't looking?
I haven't used the FOV in libtcod yet, so I don't know how easy that is to set up.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Python help
« Reply #265 on: August 24, 2011, 08:22:29 pm »

I'm trying to write a program that adds data to a dictionary from a file in Python. The data is all textual, but I can't get it to become lower case. (It's one word then a space than another word on each line)
 This doesn't work:

dataDict = {}
dataDictR={}
t = open('data.txt','r+')
t.lower()
    for line in t:
        eng, oram = string.split(line)
        dataDict[eng] = oram
        dataDictR[oram] = eng

When I try it I am told I can't lower casefy a file.

If I put:
dataDict = {}
dataDictR={}
t = open('data.txt','r+')
t = str(t)
t.lower()
    for line in t:
        eng, oram = string.split(line)
        dataDict[eng] = oram
        dataDictR[oram] = eng

I just get "open('data.txt','r+')"

How do I make it lower case?

Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #266 on: August 24, 2011, 08:50:48 pm »

I'm too lazy to test, but try getting rid of t.lower()

Then modify the for loop
Code: [Select]
for line in t:
  eng, oram = line.split()
  eng = eng.lower()
  oram = oram.lower()
  - continue as normal

I suppose I should specify that this would be for the first code block you posted.
« Last Edit: August 24, 2011, 08:53:25 pm by Mephisto »
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #267 on: August 24, 2011, 09:49:48 pm »

Thanks. It worked (though I ran into a ValueError: too many values to unpack. I'll probably just split the data.txt into 2 .txts)
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games

Mephisto

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #268 on: August 24, 2011, 10:35:03 pm »

Thanks. It worked (though I ran into a ValueError: too many values to unpack. I'll probably just split the data.txt into 2 .txts)

That's not what that particular ValueError means. I think it means that one of the lines in your text file contains more than two space-delimited strings. If you don't see any, I suppose it could be caused by a leading or trailing space character on a line. I recall a Java program I made having that problem.
Logged

Kofthefens

  • Bay Watcher
  • Keep calm and OH GOD CAPYBARAS
    • View Profile
    • Marshland Games
Re: Programming Help Thread (For Dummies)
« Reply #269 on: August 24, 2011, 11:15:33 pm »

OK, I'll see what I can do
Logged
I don't care about your indigestion-- How are you is a greeting, not a question.

The epic of Îton Sákrith
The Chronicles of HammerBlaze
My website - Free games
Pages: 1 ... 16 17 [18] 19 20 ... 91