Open Side Menu Go to the Top
Register
Programming homework and newbie help thread Programming homework and newbie help thread

03-11-2015 , 08:55 PM
Quote:
Originally Posted by econophile
Yeah, that's good advice. I was running into errors when I had used the same name for a data attribute (e.g., self.values) and a method (e.g., values()). So I switched the names of a lot of the data attributes to single letters. But I guess a better solution would be to name the methods something like getValues() so that I can still use a descriptive name for the attribute.
There some other options as well:

1. Use an underscore to distinguish the private attributes from the methods. (self._suit and suit(self))

2. Better yet, don't use getters/setters at all, just have public attributes with no methods. You don't have to write getters or setters if you don't have a particular reason.

2a. If you later determine you need to hide your attribute behind a getter/setter, you can use @property decoration to add getter/setters without having to change any calling code. (relevant Stack Overflow post: What's the pythonic way to use getters and setters?)
Programming homework and newbie help thread Quote
03-12-2015 , 03:18 AM
Quote:
Originally Posted by econophile
My previous programming experience has all been procedural, so OOP is new to me. The textbook I've been using introduced the concept, but didn't really make it clear to me when and when not to use objects.

When I was putting this program together, it did seem more convenient to define methods for things like cards and hands. And cards, hands, chips, and shoes all made sense to me to think of as objects. But I hadn't considered thinking of the game itself as an object. (Although now that you mention it I have an idea of how that might work.)

Is the general philosophy of OOP to make as many things objects as possible? Why is this preferred to the "procedural" approach?
As far as general philosophy of OOP? I don't know if this is a text book definition but I would have to say it is to make your program as modular as possible.
I don't think any paradigm is preferred over another in a general sense.
Some ways of designing a program are better than others. It really depends on the use case.

You could write your blackjack program in a procedural way. It might not be very maintainable or portable. If you wanted to create another game, you would most likely have to start over from scratch.
Programming homework and newbie help thread Quote
03-12-2015 , 08:25 AM
Quote:
Originally Posted by econophile
My previous programming experience has all been procedural, so OOP is new to me. The textbook I've been using introduced the concept, but didn't really make it clear to me when and when not to use objects.

When I was putting this program together, it did seem more convenient to define methods for things like cards and hands. And cards, hands, chips, and shoes all made sense to me to think of as objects. But I hadn't considered thinking of the game itself as an object. (Although now that you mention it I have an idea of how that might work.)

Is the general philosophy of OOP to make as many things objects as possible? Why is this preferred to the "procedural" approach?
The latter is a pretty tough question to answer briefly. Really it's the subject of college level programming courses.

Conceptually it would probably help if you thought about when things should be classes, rather than when they should be objects. For instance, thinking of a blackjack game as an "object" doesn't make sense in the everyday world. But thinking about blackjack games as a class of things that differ somewhat makes a ton of sense. You can have blackjack games with single decks or 8 decks, you can have blackjack games with early surrender or dealer hitting soft 17, but those things are details. Fundamentally, blackjack games operate in the same way.

OOP is all about finding commonalities in things. If you have a concept that can have various instances that differ in certain particulars, that thing should be a class. One of the main ideas of OOP is that I should be able to interact with a thing without knowing about the details of how it works if I don't want to. In a casino I can walk up and play a blackjack game while - if I want to - remaining totally unaware of how many decks are in the shoe. OOP aims to implement things the same way.
Programming homework and newbie help thread Quote
03-12-2015 , 08:34 AM
Quote:
Originally Posted by econophile
My previous programming experience has all been procedural, so OOP is new to me. The textbook I've been using introduced the concept, but didn't really make it clear to me when and when not to use objects.

When I was putting this program together, it did seem more convenient to define methods for things like cards and hands. And cards, hands, chips, and shoes all made sense to me to think of as objects. But I hadn't considered thinking of the game itself as an object. (Although now that you mention it I have an idea of how that might work.)

Is the general philosophy of OOP to make as many things objects as possible? Why is this preferred to the "procedural" approach?
Blackjack is a game.

Pinochle is a game.

Checkers is a game.

Chess is a game.

What do they all have in common? If nothing else they have players and rules.

So game could be a class from which game object are instantiated.

Pinochle and Blackjack are played with a deck of playing cards.

So you can have types of games that are played with playing cards.

Chess and checkers are board games.

So you can have board games.

Hopefully you can see how you could develop a hierarchy of classes with common traits. The objects are instantiations (at least in some languages) of classes. For instance the blackjack game at the Mirage would be an instantistion of a blackjack game and thus an object. The blackjack game at the Bellagio would be another object.
Programming homework and newbie help thread Quote
03-12-2015 , 09:09 AM
Thanks. I had a feeling those questions were of the overly broad, meaning-of-life type, and I appreciate the answers.
Programming homework and newbie help thread Quote
03-12-2015 , 12:33 PM
I made a blackjack game when I first learned python. I didnt understand classes, so I did the entire thing with just methods, which I guess would be procedural? Anyway, if I wanted to develop another similar game, I would have to do start from scratch basically. But if I had used objects, I could re use some of that stuff. Like a card class, or a dealer class or whatever. Or if say I just wanted to change something in the game itself, I could simply just pull that object out, and plug a new one in, which is a lot easier than if I have to go through the entire program and change a line here or there.

So when deciding if I want to make something a class, I usually think if its something that could be used somewhere else in a different program (I don't mean that it ever WILL get used, just that hypothetically it could) or if its something that I want to abstract away and be able to plug or unplug different forms of in and have the rest of the program still function the same.

Im still a relative noob, but thats my take on it, and its helped me when dealing with the confusion that comes from understanding objects
Programming homework and newbie help thread Quote
03-12-2015 , 03:13 PM
Quote:
Originally Posted by Alobar
I made a blackjack game when I first learned python. I didnt understand classes, so I did the entire thing with just methods, which I guess would be procedural? Anyway, if I wanted to develop another similar game, I would have to do start from scratch basically. But if I had used objects, I could re use some of that stuff. Like a card class, or a dealer class or whatever. Or if say I just wanted to change something in the game itself, I could simply just pull that object out, and plug a new one in, which is a lot easier than if I have to go through the entire program and change a line here or there.

So when deciding if I want to make something a class, I usually think if its something that could be used somewhere else in a different program (I don't mean that it ever WILL get used, just that hypothetically it could) or if its something that I want to abstract away and be able to plug or unplug different forms of in and have the rest of the program still function the same.

Im still a relative noob, but thats my take on it, and its helped me when dealing with the confusion that comes from understanding objects
Yes I think that is the general gist of OOAD. In my view, FWIW, it is an evolutionary process. In fact the metrics guru on software development, Capers Jones, basically states that. IIRC what he basically stated was that his research on software producity showed that getting the objects right takes some time (continuous refactoring) but once they are right, companies get a big boost in producivity as measured in terms of actual working/validated code.

Also, I described inheritance relationships. There is also composition relationships where basically delage methods are inputs to other methods. You can Google inheritance vs. composition to get a feel. A gentleman named Dusty Phillips wrote a decent book on Python OO development. It does cover inheritance and composition in the book.
Programming homework and newbie help thread Quote
03-12-2015 , 03:18 PM
Quote:
Originally Posted by econophile
Thanks. I had a feeling those questions were of the overly broad, meaning-of-life type, and I appreciate the answers.
Actually I thought they were great questions to ask.
Programming homework and newbie help thread Quote
03-15-2015 , 04:10 PM
Coding theory question

Consider the space F{0,1} 18 //18 written in superscript
with Hamming distance. Compute the volume of a sphere with radius 2.

I don't see the connection?? if i understand correctly, the space F{0,1}18 just means 18 bits of 0 or 1??? does the question want me to count all the possible strings with hamming distance 2?? so consufed, pls help
Programming homework and newbie help thread Quote
03-16-2015 , 02:45 AM
Hi Everyone,

I am newbie in this community. Feeling glad to be a part of this forum. Here I wanna share my ideas and views about Calculators.
Programming homework and newbie help thread Quote
03-16-2015 , 10:35 AM
sorry guys, another question:

Write a simple function that outputs the number of all linear codes of a given length over GF(q)
and dimension k.

how would i even go about it? is it asking me to find how many subspaces GF(q) has?
Programming homework and newbie help thread Quote
03-22-2015 , 09:22 PM
you people and your math problems

just took me an embarrassingly long time to remember that number * (number/number) is the same thing as (number * number)/number


Last edited by Anais; 03-22-2015 at 09:22 PM. Reason: or at least I sure hope it is in this case
Programming homework and newbie help thread Quote
03-23-2015 , 05:33 AM
Mega newb question:

Writing a blackjack game using Netbeans 6.9.1 and wanted to import a few classes from another project (Casino). I've managed to add the project jar files to to the blackjack 'libraries' file however the classes still aren't being recognised.

Anyone know where I'm going wrong? Thanks

Programming homework and newbie help thread Quote
03-23-2015 , 04:59 PM
having a function return a reference: standard operating procedure or clue that one should find another professor?
Programming homework and newbie help thread Quote
03-23-2015 , 05:13 PM
POW,

I don't code in Java so I'm just taking a guess here but isn't deck under CardGames? Thus you have to do package CardGames?
Programming homework and newbie help thread Quote
03-23-2015 , 05:28 PM
Quote:
Originally Posted by Anais
having a function return a reference: standard operating procedure or clue that one should find another professor?
IYO what is wrong with a function returning a reference?
Programming homework and newbie help thread Quote
03-23-2015 , 05:47 PM
Aside from it being something that hasn't been taught nor is it in our book?

I dunno. Dunno much about it. Just googled a compile error I got to see what it was and realized i was overlooking that part.
Programming homework and newbie help thread Quote
03-23-2015 , 09:17 PM
Quote:
Originally Posted by POW
Mega newb question:

Writing a blackjack game using Netbeans 6.9.1 and wanted to import a few classes from another project (Casino). I've managed to add the project jar files to to the blackjack 'libraries' file however the classes still aren't being recognised.

Anyone know where I'm going wrong? Thanks
I'm not a Netbeans person but stab in the dark, check the classpath for your project?

I didn't watch it but...should probably help you. Here you go: https://www.youtube.com/watch?v=t3ApvWiCVzQ
Programming homework and newbie help thread Quote
03-24-2015 , 02:04 AM
Quote:
Originally Posted by anfernee
I'm not a Netbeans person but stab in the dark, check the classpath for your project?

I didn't watch it but...should probably help you. Here you go: https://www.youtube.com/watch?v=t3ApvWiCVzQ
Quote:
POW,

I don't code in Java so I'm just taking a guess here but isn't deck under CardGames? Thus you have to do package CardGames?
Yep, needed to import CardGames once jar file was in 'libraries'

Thanks so much guys, leg-ends!
Programming homework and newbie help thread Quote
03-24-2015 , 03:09 AM
Quote:
Originally Posted by Anais
having a function return a reference: standard operating procedure or clue that one should find another professor?
Are you talking about an object on the heap? I read your comment earlier today and thought nothing of it.

Then I watched this video of Bjorne saying how returning an object on a heap by reference is bad practice as it brings in the question as to who is responsible for deleting it. Please note that I was skipping around so this may have been taking out of context.

https://m.youtube.com/watch?v=D5MEsboj9Fc
30 min mark I believe. On my phone so I cant really look
Programming homework and newbie help thread Quote
03-24-2015 , 03:32 AM
barrin dont you do anything fun, I know you're taking more classes than i am. go play a video game or something, lol.

I see his point though
Programming homework and newbie help thread Quote
03-24-2015 , 04:26 PM
Quote:
Originally Posted by Barrin6
Are you talking about an object on the heap? I read your comment earlier today and thought nothing of it.
Okay, don't mind me, i'm a complete idiot for the most part.

It was for the purpose of function chaining, which he actually has mentioned but we've seen in action so little that I didn't recognize wtf was going on.

in other news, trying to overload the binary operator + as a member function and not sure how to go about it best.

For now, I've got:

Code:
className className::operator+(const className & r) const
{
	className temp(*this);
	temp.valOne += r.getvalOne();
	temp.valTwo += r.getvalTwo();
	return temp;
}
and I feel like this isn't right.

The interwebs tell me it should be more of the form like this:

Code:
class Cents
{
private:
    int m_nCents;
 
public:
    Cents(int nCents) { m_nCents = nCents; }
 
    // Overload cCents + int
    Cents operator+(int nCents);
 
    int GetCents() { return m_nCents; }
};
 
// note: this function is a member function!
Cents Cents::operator+(int nCents)
{
    return Cents(m_nCents + nCents);
}
aside from not knowing what the "{ m_nCents = nCents; }" in what I assume is the constructor means, I'm not fully sure what's going on in the function itself.

Is it calling the constructor and returning what it creates?

Only stuff in our book is about overloading binary operators as friend functions.

*edit*

using the above format, I tried:

Code:
return className((*this).getBothValues() + r.getBothValues());
but that didn't work either

neither did setBothValues(); return *this;
I'm assuming because something about the const on the end of the function prototype

Last edited by Anais; 03-24-2015 at 04:51 PM.
Programming homework and newbie help thread Quote
03-24-2015 , 04:59 PM
i think what I had works fine, probably not optimal, but meh. Testing it seems to work and I suppose that's all that matters.
Programming homework and newbie help thread Quote
03-24-2015 , 05:53 PM
Quote:
Originally Posted by jmakin
barrin dont you do anything fun, I know you're taking more classes than i am. go play a video game or something, lol.

I see his point though
Haha I had my fair share of fun the first time through college, so no more video games for me.

That video i found was just through Twitter that some guy posted. I only watched a minute of it. I'll probably have it run in the background while I study for the c++ midterm.
Programming homework and newbie help thread Quote
03-24-2015 , 07:18 PM
I'm working on a method that checks whether pieces are in the way of horizontal, vertical, or diagonal chess moves. (Other types of moves have another method). If the move is from square A to B, the method needs to look at each square between A and B. This is what I came up with as a first draft.

Code:
def clear_path(self):
    (x1, y1), (x2, y2) = self.coords
    (xmin, xmax), (ymin, ymax) = sorted([x1, x2]), sorted([y1, y2])
    if x1 == x2:
        for y in range(ymin + 1, ymax):
            square = board.notation((x1, y))
            if board.piece(square) != None:
                print 'Move is not allowed because there is a',
                print board.piece(square), 'in the way.'
                return False
    elif y1 == y2:
        for x in range(xmin + 1, xmax):
            square = board.notation((x, y1))
            if board.piece(square) != None:
                print 'Move is not allowed because there is a',
                print board.piece(square), 'in the way.'
                return False
    elif xmax - xmin == ymax - ymin:
        if x1 < x2:
            x_change = 1
        else:
            x_change = -1
        if y1 < y2:
            y_change = 1
        else:
            y_change = -1
        for i in range(xmax - xmin - 1):
            x1 += x_change
            y1 += y_change
            square = board.notation((x1, y1))
            if board.piece(square) != None:
                print 'Move is not allowed because there is a',
                print board.piece(square), 'in the way.'
                return False                
    return True
That looked pretty ugly, so I figured out how to generalize a single approach that would handle all three types of moves and ended up with:

Code:
def clear_path(self):
    (x1, y1), (x2, y2) = self.coords
    xdist, ydist = (x1 - x2), (y1 - y2)
    dist = max(abs(xdist), abs(ydist))
    x_change, y_change = xdist/dist , ydist/dist
    if x1 == x2 or y1 == y2 or abs(xdist) == abs(ydist):
        for d in range(dist - 1):
            x1 += x_change
            y1 += y_change
            square = board.notation((x1, y1))
            if board.piece(square) != None:
                    print 'Move is not allowed because there is a',
                    print board.piece(square), 'in the way.'
                    return False
    return True
It's a lot shorter, but it still doesn't seem very elegant.

I don't like how I'm basically entering formulas twice. I could write loops, but sometimes it seems like overkill to write a loop to just do one thing twice.

Also, instead of

x_change, y_change = xdist/dist, ydist/dist

I could write something like

x_change, y_change = [i/dist for i in (xdist, ydist)]

But that also seems silly.

Any advice? Should I break the method up into multiple functions? Use more loops?
Programming homework and newbie help thread Quote

      
m