Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 566 567 [568] 569 570 ... 796

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

Mesa

  • Bay Watcher
  • Call me River.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8505 on: November 28, 2015, 09:43:28 pm »

Kinda giving Python another shot over at Codecademy, a bit under halfway into the course so far.
It's much more fun than C++ or JavaScript (especially the former), barring the for+list+dict nonsense I had to slog through because I'm a dumbo when it comes to syntax apparently. :r

I wrote this kind of pointless 4-digit (pseudo)random number generator but it's mine goddamnit and it works. Plus there's the placebo effect making me think it's better than random's default randint generation in some weird way.
Code: [Select]
from random import randint


# Random seed generator of some sort
# The 4 digits of the seed, integers in the range of 0 (1 for the 1st) to 9
# They're generated as strings so they can be concatenated
def random():
        rng1 = str(randint(1,9))
        rng2 = str(randint(0,9))
        rng3 = str(randint(0,9))       
        rng4 = str(randint(0,9))       
        # The digits are concatenaded and converted to an int to create the seed
        seed = rng1+rng2+rng3+rng4
        seed = int(seed)
        return seed

Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8506 on: November 28, 2015, 09:52:26 pm »

Uh, they shouldn't be? The generator should only need to know the location of a block, to decide if it should be air or stone.
Well.  Air or not-air.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8507 on: November 29, 2015, 12:53:31 am »

I have a question.  So I have a hex grid in C# (the fact that its a hex grid is irrelevant) which is recorded in a 1-dimensional array because simplicity.  I have a function that takes an x and y coordinate and returns the correct hex.

What I would *like* to do is be able to enter an invalid coordinate (as in, a negative number or something outside the grid) and have it return false.  I'm used to python where I can just return any old thing, but this is clearly not the case in C#.  So... how do you do that, or something functionally similar?

Code: [Select]
        public Hex getHex (int x, int y)
        {
            int num = 0;
            num += y * gridWidth;
            num += x;
            return grid[num];
        }
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8508 on: November 29, 2015, 01:21:55 am »

1) If the coordinates are invalid, return null or a special "Invalid hex" object and the caller is responsible for checking that.

2) Crash, and be careful when and how you use the GetHex function.

EDIT:

I've got it so that my function produces unconnected open rectangles. How does this idea sound for connecting them? It's a bit late and I'd like to sleep, so I'll take a crack at actually putting this in tomorrow.

Spoiler (click to show/hide)
« Last Edit: November 29, 2015, 01:39:55 am by itisnotlogical »
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8509 on: November 29, 2015, 02:53:47 am »

@EnigmaHat: what he said, return a null then check for that. That works in C# since all object variables are references and null references are allowed. In C++ you'd need to either make a static "invalid" Hex which you return and return a reference to that, or use pointers and return null.

Another issue is that sometimes invalid x and y will return a (wrong) valid hex, e.g. if "x" is out of bounds, then it could return a Hex from a different y-column without crashing, whereas y out of bounds will push the array right out of bounds (unless paired with an invalud "x" that pushes it back into bounds). For example, if your x and y bounds are 10, then valid x and y are 0 to 9 in each dimension, with Hexes 0 to 99. The formula is "num = 10y + x". If y = 5 and x = -1 then you get num=49, which is the last cell of column 4. if y=-1 and x > 9, then this will also give a valid cell, even though both x and y on their own should be invalid.

So, definitely check that both x and y are valid individually before computing "num'.

Another option is to use assert statements (c# or c++) and write your code so that it never triggers the assert. Then, for your release build you can disable the assert's and get more performance than the version with manual bounds checking.
« Last Edit: November 29, 2015, 02:57:51 am by Reelya »
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8510 on: November 29, 2015, 04:40:36 am »

Another way you can do it, which I use sometimes, is have a function in the form of:

bool GetHex(int x, int y, out Hex hex)
{
    Stuff
}


Then you return true if it's a valid hex, and false if it's not, and the hex is returned through the 'out Hex hex' parameter.

But in your case, just returning null for an invalid hex should be fine.
Logged

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8511 on: November 29, 2015, 02:54:36 pm »

My teacher just mailed me new homework (Frustrating. Yet this is still better than Uni i'm told) and it's asking about vectors. Which I understand to be similar to arrays.

The question goes something like this:
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:
3 2 1 Go!

She also gives a sample program that can somewhat be edited.


#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> countDown(0);
   int newSize = 0;
   int i = 0;

   newSize = 3;


  /* Solution goes here*/

   for (i = 0; i < newSize; ++i) {
      cout << countDown.at(i) << " ";
   }
   cout << "Go!" << endl;

   return 0;

}

((Bolded means it can't be edited. You can however make as many whitespaces as possible and declare new variables and of course input new things.))

My questions are basically this:
1. How do you use a Vector and what does it do?
2. If you know a bit of synthax and what it does please post that because it'll be uber helpful.
3. Just as a hint can I solve this by inputting a for loop and an if statement?
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8512 on: November 29, 2015, 03:23:41 pm »

Look up vector on cplusplus.com

I could explain this but I feel it'd be more useful for you to find out how 2 datastructures yourself.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8513 on: November 29, 2015, 06:22:20 pm »

I made my generator dig out corridors to connect any empty spaces.

Spoiler (click to show/hide)

For the first time in a long, long time, I've programmed something that I am genuinely proud of.

It still occasionally generates unconnected empty squares, and will often place the player inside of those. Fixing that is next on my to-do list. I'm debating whether I should force them to connect to the main branch, or fill them in with solid tiles so it's like they never existed. Or maybe even include a digging mechanic, so I can fill those unconnected spaces with goodies that the player can dig towards.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8514 on: November 30, 2015, 12:22:05 am »

1) If the coordinates are invalid, return null or a special "Invalid hex" object and the caller is responsible for checking that.
Thanks (and to everyone else who answered), that's what I was looking for.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8515 on: November 30, 2015, 04:51:34 am »

Trying to create a simple FTP program in Python. I've commented out code that I don't think is necessary. (I'm working under the assumption that sockets are full duplex, but I could be wrong.) The frustrating thing is that you can't simply send strings through the socket in Python3 as you can in Python2. I think you might have to convert them into bytearrays, but there's something about needing to set encoding (i.e., utf8, etc.) and I don't know how to specify that. File objects work. I don't know what the guarantees are about delivery; re-sending the hello might be unnecessary once it's confirmed to be in the socket.

I've detailed my intended protocol below. You can ignore the commands, I just want a working hello and goodbye on my control channel:

Spoiler: Protocol (click to show/hide)
Spoiler: Client Code (click to show/hide)
Spoiler: Server Code (click to show/hide)

I've certainly failed the assignment. It's mostly personal curiosity at this point.
« Last Edit: November 30, 2015, 04:56:18 am by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8516 on: November 30, 2015, 05:12:55 am »

Programming networked applications makes me want to cry. Good luck and best wishes are all I have to offer :P
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8517 on: November 30, 2015, 05:19:12 am »

I think you might have to convert them into bytearrays, but there's something about needing to set encoding (i.e., utf8, etc.) and I don't know how to specify that.
Code: [Select]
b"this string is converted to a bytearray, usually UTF-8 encoding, I think"
bytes ("the bytes function will return an encoded bytearray", "utf-8")
I know zilch about networking though so I can't really help further.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

nogoodnames

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8518 on: November 30, 2015, 10:07:51 am »

So I'm making a simple program to practice with C#. Right now it creates a board of hashes with arbitrary width and height and allows an ascii smileyface to move around it based on user input.

The problem is, every so often Visual Studio will hang when it tries to build the project. This happens randomly for no apparent reason. The build will start but not progress anywhere. VS won't allow me to abort the build, and it won't exit while a build is in progress so I have to end it via the task manager. After this happens, I won't be able to successfully build the project until I restart the computer. Other projects will still build correctly. I haven't done anything else this large, so I don't know if it's unique to this project or will happen with all C# projects.

Google has shown me people with similar problems, but not this exact one. Any ideas?
One possibility: both times it's happened, I had another instance of VS open with a Python project. Maybe it doesn't like working in two languages at once.
Logged
Life is, in a word, volcanoes.
                        - Random human lord

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8519 on: November 30, 2015, 11:19:22 am »

Hey is here someone confident in SFML who can help? The thing is that I have a set of CircleShape objects and the one of Text objects. What I need to do is to simply place each of those "texts" (their strings are numbers) in the centre of each of the circles. Like this:


The code snippets I'm trying to succeed in it with:

Code: [Select]
Vector2f center(width/2.0f, height/2.0f);
float angle = 0.0f;
float step = M_PI*2.0f/n;
float vxShapeRadius = 20.0f

for (int i = 0; i < n; i++)
{
vxShapes[i]->setFillColor(Color::Blue);
vxShapes[i]->setOrigin(vxShapeRadius, vxShapeRadius);
vxShapes[i]->setPosition(center.x + 200.0f*cos(angle), center.y - 200.0f*sin(angle));
angle += step;
}

for (int i = 0; i < n; i++)
{
char buff[255] = {0};
nums[i].setFont(font);
nums[i].setColor(Color::Red);
nums[i].setCharacterSize(32);
nums[i].setString(_itoa(i+1,buff,10));
FloatRect numRect = nums[i].getGlobalBounds();
Vector2f numRectCenter(numRect.width/2.0, numRect.height/2.0);
nums[i].setOrigin(numRectCenter);
nums[i].setPosition(vxShapes[i]->getPosition().x, vxShapes[i]->getPosition().y);
}


Though I'm not getting what is needed. In which line(s) am I wrong?

vxShapes is an array of pointers to a CircleShape, nums is an array of Text objects.
« Last Edit: November 30, 2015, 11:28:04 am by RoguelikeRazuka »
Logged
Pages: 1 ... 566 567 [568] 569 570 ... 796