Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 43 44 [45] 46 47 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95963 times)

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #660 on: December 20, 2010, 12:46:39 pm »

I don't know what algorithm that is, but I would draw the ircle one quadrant at a time, first first moving in the x diirection, then the y direction.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #661 on: December 20, 2010, 01:31:55 pm »

That's pretty much what it does, just with octants.
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #662 on: December 20, 2010, 06:21:53 pm »

*Scratches Head*
Is my math right? I'm trying to draw a circle, but all I get are four pixels.

Try adding brackets around the multiplication step before casting to an int after the math functions. You might have got it right but I can never remember if it will cast to an int and then times by radius or do it after the multiplication. Assuming it is rounding first that would explain the four pixels.

I don't see anything else obviously wrong with it.
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

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #663 on: December 20, 2010, 06:31:11 pm »

Though it may be obvious to some, I would still like my question answered.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #664 on: December 20, 2010, 07:53:19 pm »

*Scratches Head*
Is my math right? I'm trying to draw a circle, but all I get are four pixels.

Try adding brackets around the multiplication step before casting to an int after the math functions. You might have got it right but I can never remember if it will cast to an int and then times by radius or do it after the multiplication. Assuming it is rounding first that would explain the four pixels.

I don't see anything else obviously wrong with it.
This, definitely. At this point it's casting the result of the sine and cosine to an integer before multiplying with the radius. since a sine yields values between -1 and 1 inclusive you're only ever getting 3 x and 3 y values and with the relations between sines and cosines that reduces further to the 4 points you see.
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #665 on: December 21, 2010, 12:17:13 am »

Since charstars are pointers to the first element of a char array, does that mean that, for example, char szString[0] = strtok(cInput, " ") would work just as well as char *cString = strtok(cInput, " ")?
If you already set the pointer to equal a string, you may get errors trying to change it.

(explicitly char x= "LK", or char y=x;)
I did not know that string literals could be used to initialize char *, but now that I do, just remember that const is not a meaningless term. Stop trying to edit const things.

now if cString is actually pointing to a char array or char, or something which isn't const or final, or whatnot, then cString[0]=strtok(cInput," ") will function perfectly fine.
Logged

Supermikhail

  • Bay Watcher
  • The Dwarf Of Steel
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #666 on: December 21, 2010, 04:58:25 am »

Well, it's been solved, but...
It'll draw certain pixels one on top of the other, at least it did when I used it a year ago for some Cortex Command mod.
But, never mind. I'm using the Bresenham circle algorithm, which works just fine.
in the case when the radius is too large, and there are more than 360 pixels to draw, the circle will be dotted?

Also, apparently, Java does values and references for you, is that right? And is that crazy?
Logged

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #667 on: December 21, 2010, 09:13:12 am »

I almost got it to work (after changing my methods a bit), but I'm having difficulty getting it to recognise commands again. It reads, passes around and tokenises the input just fine though. And for some reason, when I write more than one word it returns "Huh?" for as many words there are.

Any idea what I'm doing wrong? I admit, I don't fully understand what I'm messing around with, but I don't see where it's breaking.

Code: [Select]
#include "stdafx.h"
#include <iostream>
#include "ZedHead.h"
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>



using namespace std;

void Tokenize(const string& str,
                      vector<string>& tokens,
                      const string& delimiters = " ")
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

int testRoom()
{
    cout << "This is the test room! You find yourself surrounded by gray walls." << endl;
    cout << "There is a single, naked LIGHT bulb on the ceiling, and a CRATE on the floor next to you." << endl;
    vector<string> tokens (10);
    string strInputLine;
do
{
cout << endl << "> ";
cin >> strInputLine;
cout << endl;
Tokenize(strInputLine, tokens);
if (tokens[9] == "get")
{
if (tokens[8] == "light")
cout << "Ouch! the light bulb is too hot to touch." << endl;
else if (tokens[8] == "crate")
cout << "The crate is too heavy to move." << endl;
else
cout << "I don't know how to get that." << endl;
}
else if (tokens[9] == "die")
{
cout << "You die." << endl;
break;
}
else
cout << "Huh?" << endl;
} while (0==0);
return 0;
}

int main()
{
testRoom();
return 0;
}
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #668 on: December 21, 2010, 09:39:39 am »

in the case when the radius is too large, and there are more than 360 pixels to draw, the circle will be dotted?

Probably it will happen with less than 360 pixels due to rounding errors too.

Also, apparently, Java does values and references for you, is that right? And is that crazy?

Everything is either a reference or value, it's not crazy although does mean there are certain things you can't optimise away (then again should you really be doing code like that anyway). It's much easier for the memory manager to handle when you don't have people playing with pointers though.

I almost got it to work (after changing my methods a bit), but I'm having difficulty getting it to recognise commands again. It reads, passes around and tokenises the input just fine though. And for some reason, when I write more than one word it returns "Huh?" for as many words there are.

Try printing out all the tokens. I'm not sure why you expect token 9 to be 'get' as the code looks to me that that would only be true if there were 10 tokens found in total and printing them out would help see exactly what is in each. You might find it easier to use back() and pop_back() to pull the tokens out rather than the index number if this is the case, this would also match push_back().
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

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #669 on: December 21, 2010, 09:57:07 am »

By adding this:

Code: [Select]
cout << "[0] " << tokens[0] << endl;
cout << "[1] " << tokens[1] << endl;
cout << "[2] " << tokens[2] << endl;
cout << "[3] " << tokens[3] << endl;
cout << "[4] " << tokens[4] << endl;
cout << "[5] " << tokens[5] << endl;
cout << "[6] " << tokens[6] << endl;
cout << "[7] " << tokens[7] << endl;
cout << "[8] " << tokens[8] << endl;
cout << "[9] " << tokens[9] << endl;
cout << "[10] " << tokens[10] << endl;

Right after Tokenize, it printed the word get as being in slot 10 twice. Meaning it was two lists with get as the last item, and no other items.

What does that even mean?  ???

Is the program getting input incorrectly, or something?
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #670 on: December 21, 2010, 10:11:52 am »

Doing 1d midpoint displacement, I've got it planned out like so
Code: [Select]
Displace (x1,y1, x2,y2, iterations, list)
  dist = sqrt( (x2-x1)^2 + (y2-y1)^2 )
  midx = dist/2
  midy = (y1+y2)/2 + random
  list.add( point(midx, midy) )
  if (iterations > 0)
     midpointDisplacement (x1,y1, midx,midy, iterations-1, list)
     midpointDisplacement (midx,midy, x2,y2, iterations-1, list)
Connect (list)
  for(i = 0, i < list.size, i++0)
    drawline(list.get(i).x, list.get(i).y, list.get(i+1).x, list.get(i+1).y)
The only problem is the points are stored in the order they were made, and not in the order they appear.
Code: [Select]
This:
1 6 4 7 3 8 5 9 2   <- place in list
o-o-o-o-o-o-o-o-o

Instead of this:
1 2 3 4 5 6 7 8 9   <- place in list
o-o-o-o-o-o-o-o-o
How do I reorganize them?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #671 on: December 21, 2010, 10:21:05 am »

Pass an iterator around in the recursion that says "Insert Here"?
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))

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #672 on: December 21, 2010, 11:09:32 am »

That could work... And I tried drawing some complicated equations involving powers of 2 that sort it out somehow :P.

Anyway, on to importing. How do I import custom classes? I've got this so far, but it doesn't work.
Code: [Select]
import drawlib.*;
import java.io.File;

class Generator {
    public static void main(String[]args) {
    File file = new File("./image.dib");
    Canvas canvas =  new Canvas(64, 64, Draw.white);
    Bitmap DIB = new Bitmap (canvas);
    DIB.write(file);
    System.out.println("Ahem!");
    }
}

------

javac Generator.java
Generator.java:7: cannot access drawlib.Canvas
bad class file: ./drawlib/Canvas.class
class file contains wrong class: Canvas
Please remove or make sure it appears in the correct subdirectory of the classpath.
    Canvas canvas =  new Canvas(64, 64, Draw.white);
    ^
My classes are in the subdirectory.
Logged

Outcast Orange

  • Bay Watcher
  • [SOMETIMES_SQUID]
    • View Profile
    • The Outcast Orange
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #673 on: December 21, 2010, 12:41:43 pm »

I'm working in C++, and some of my cpp files are getting unwieldy.
Is there an way to split up a cpp into multiple parts?
My soul class is getting so long that I have finding functions in it even with a search function. (due to so many search results)

I figured using #include and multiple files would work, but there were issues with scope or something.

Any insight would be appreciated.
Logged
[7:53:55 PM] Armok, why did you demand that I don't eat you?
[7:54:34 PM] [Armok]: woooooo

Burried Houses - Platform Explorer Demo H - Cloud Scream

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #674 on: December 21, 2010, 12:44:39 pm »

The only problem is the points are stored in the order they were made, and not in the order they appear.
Code: [Select]
This:
1 6 4 7 3 8 5 9 2   <- place in list
o-o-o-o-o-o-o-o-o

Instead of this:
1 2 3 4 5 6 7 8 9   <- place in list
o-o-o-o-o-o-o-o-o
How do I reorganize them?
Use a heap, or if you need to access things at arbitrary places, sort it using your favorite sorting algorithm (for such a small data set, insertion sort or counting sort should do fine. Actually, counting sort would also work very good for larger data sets since the amount of possible values is equal to the amount of entries in your array, which means counting sort would run in linear time as opposed to other sorting algorithms, which run in O(n log n))
edit: I'm stupid. If you know where every point has to go beforehand, you could just initialize a second array, then for each element in the first array, calculate where it has to go and put it in that place in the second array.
« Last Edit: December 21, 2010, 12:55:51 pm by Virex »
Logged
Pages: 1 ... 43 44 [45] 46 47 ... 78