Welcome! Log In Create A New Profile

Advanced

Let’s get started with Assignment 3 colleagues

Posted by Anonymous User 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Re: Let’s get started with Assignment 3 colleagues
September 13, 2011 04:43PM
I suppose if you did the graphics you'd end up with a better idea of how they might've produced the various characters in the games you know. You definitely would if it's new to you. Extremely interesting but also a little bit frustrating when code doesn't work as intended. (Because you get a blank screen - and often that's misleading. Your graphics object is there, only either you can't see it because you're inside it or else you've made it much smaller and more distant than you'd intended. Case in point: I had a little model of "the Sun" for the current assignment which I thought was just failing to display at all. For some or other reason I ended up "zoomed far out", and "whoosh", something small and yellow shot past me ... Wait a bit ... whoosh ... there goes the "Sun" again.)

With your C++/ Qt you're getting a better idea of how the whole project fits together. But you know that already. It also makes producing awesome games as easy as clubbing baby seals. grinning smiley
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 07:42AM
Interesting grinning smiley Will see if I have the motivation to make a simple game one day. WAIT!!!!! Actually, I'm making a simple game for my assignment 3!!!! It's called BlackJack grinning smiley haha! I forgot!
avatar Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 10:45AM
If you take all the bells and whistles out of a computer game, there's very little difference between it and any other kind of computer program.

The MVC (Model, View, Controller) pattern comes in quite handy for this. You have your entire game's state stored inside a class or group of classes (the model) that is modified by other classes that handle user interaction (the controller). If necessary, the controller indicates to other classes to update what is displayed on the user interface (the view). Note that the view can be what's displayed on the console, or it can be a fully 3D GUI. The point is that the View is what's displayed to the user.

In Qt, the QWidgets can perform the some of the role of the View and also some of the role of the Controller.
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 12:49PM
That's interesting smiling smiley

/ START RANT

Boy, I HATE QUESTION 2!!!! It's way too vague. We are supposed to create a program by looking at the BlackJack UML diagram alone! I have no idea as to how to implement some member functions of a class because I don't know their purpose. Ezust, you suck at teaching C++/Qt.

I'm tempted to write a REAAAALLLLLY SIMPLE BLACKJACK GAME WITH 2 or 3 classes in it even though it may cost me a lot of points.

/ END RANT
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:00PM
Greetings,

I'm trying to implement shuffle function and my compiler seems to fail to find <stdlib.h> libary so i am getting this 'time' was not declared in this scope

below is my program

Language: C++ (QT)
#include <QTextStream> #include <stdlib.h> #include "CardPack.h"   - - - - void CardPack::randomShuffle(){ srand(time(0)); for (int 1 = 0; i < 100; i++){ append(takeAt(rand) % (size() - 1))); } }

'time' was not declared in this scope
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:04PM
Lol, that idea crossed my mind. I'm thing of drawing the picture with no actions
avatar Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:06PM
@crunx

Try using qsrand(QDateTime::currentDateTime(). toTime_t())

and then use qrand() whenever you need a random number.
avatar Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:08PM
@Spectre

This is normally how requirement specs sometimes turn out. You have to use a lot of imagination. How you do it is less important than getting it done.

I would suggest going through a number of iterations of divide-and-conquer and you'll end up with quite a number of classes. Don't limit yourself to only those in the UML diagram.
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:20PM
Hmm, in other words, do not matter how you implement it as long as it works just like any BlackJack game.

Well, thanks. I feel better now.

@crunz, haha but hey it's better than skipping question 2. Damn, still working on it. Love being under pressure!
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:34PM
thanks Rob,

I will try that one
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 01:48PM
@Spectre

thus is my point. If you can demonstrate how to apply GUI widgets, the better. there are asking too much from us. anyway lets give it a go. with People like Robanaurochs and Slow E around, we can make it
Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 02:47PM
crunx Wrote:
-------------------------------------------------------
> @Spectre
>
> thus is my point. If you can demonstrate how to
> apply GUI widgets, the better. there are asking
> too much from us. anyway lets give it a go. with
> People like Robanaurochs and Slow E around, we can
> make it

That's reassuring grinning smiley Thanks. Been working on this question 2 all day. I have learned a lot of things while working on it.

I need some advice as to how to remove QLabels at runtime.

Language: C++ (QT)
void HandView::clearHand()   { Card* ptrCard;   for (int i = 0; i < m_Hand->size(); i++)   { ptrCard = m_Hand->at(i); m_HandLayout->removeWidget(ptrCard->label()); }   m_WidgetHand->setLayout(m_HandLayout); }

It doesn't work. Did I miss something?

But this works perfectly.


Language: C++ (QT)
HandView::HandView(Hand *hand)   { m_Hand = hand; Card *ptrCard;   m_WidgetHand = new QWidget(); m_HandLayout = new QHBoxLayout();   for (int i = 0; i < hand->size(); i++)   { ptrCard = hand->at(i); m_HandLayout->addWidget(ptrCard->label()); }   m_WidgetHand->setLayout(m_HandLayout); }
avatar Re: Let’s get started with Assignment 3 colleagues
September 14, 2011 03:59PM
Try setVisible(false) ?

I have something similar somewhere in my assignment. I think remove did sort-of remove the button I was doing this too, but it left a little vestigial button.

Another thing to do with labels is set the text to ""
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 09:01AM
slow_eddy Wrote:
-------------------------------------------------------
> Try setVisible(false) ?
>
> I have something similar somewhere in my
> assignment. I think remove did sort-of remove the
> button I was doing this too, but it left a little
> vestigial button.
>
> Another thing to do with labels is set the text to
> ""

I forgot to edit my post above. I meant to say how to remove widgets from a layout in runtime. ie. the program I'm trying to create, BlackJack. Whenever the game is over, what I want is that when the deal hand action is called, it should delete all the cards from the layout and shuffle the deck then display 4 cards (2 cards for each player - picked from the same deck) again.

I use QLabel + QPixmap combo to display a card. A deck is an instance of class Deck (QList<Card*>winking smiley.
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 11:25AM
@Spectre

QLayout (and it's derivatives) are just layout managers, they only affect the position and size of QWidgets. Removing a QWidget from a layout will not make it disappear, it will probably just move it to the upper left-hand corner of the parent QWidget.
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 12:29PM
robanaurochs Wrote:
-------------------------------------------------------
> @Spectre
>
> QLayout (and it's derivatives) are just layout
> managers, they only affect the position and size
> of QWidgets. Removing a QWidget from a layout will
> not make it disappear, it will probably just move
> it to the upper left-hand corner of the parent
> QWidget.

Can't believe that I have been wasting my time try to make this damn layout disappear. I'm kinda under pressure right now. Can't think clearly sad smiley I can run my BlackJack game. It works just like a normal BlackJack program except that when you start a new game/deal hand again, you have to close the program then open it again. That's the last step I need to fix. I'm reallly tempted to just submit this assignment now.
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 12:42PM
@Spectre and @crunx

This task is not beyond your capabilities, nor are the lecturers asking too much of you. You're just panicking. The trick to becoming a good programmer is to be able to break a problem down into manageable subtasks. Once you've done that, you'll see that the subtasks are fairly easy to solve individually and that once you've put them altogether, your entire game will work perfectly. I suspect you're just feeling a bit overwhelmed because you don't know where to start.

I suggest you start by taking the UML diagram and creating skeleton classes for all the classes mentioned in the UML diagram. You need to take note of the association lines. In particular, notice that the Deck owns the Cards, not the Hand. The Deck owns exactly 52 Cards, no more, no less. That means that Hand must neither create nor destroy any Card instances and can only hold onto references (in this case, pointers) that it receives via the operator<<

The Blackjack class owns everything else and assumes the role of a controller class. The Deck and Hand classes assume the role of model classes and the HandView is a view class.

Deck:: cardsLeft(int) and Hand:: handChanged() are marked as protected. This can safely be assumed to mean that these are expected to be signals.
View classes need to know when their models have changed so either the model informs the view of changes, or the contoller does. My guess is that HandView:: rescanHand() will be a slot so I think you should be able to figure out what to do with it.

Next thing you can do, is to go back to requirements and create lists. Apart from setting up the GUI, there are only six ways that the user can interact with the program (via the QActions) and the program only responds to user interactions (meaning that it doesn't respond to system events, e.g. a timer). You can basically say that the system state only changes as a response to what the user does, otherwise it stays the same.

Make lists of each of the six interactions and go through the requirements and classify each requirement under one of those six points. Make sure that each requirement is unique. If a sentence in a requirement lists multiple tasks, separate these in your list. If a task contains many elements in order to accomplish it, then make a sub-list and make each of those a unique point under that task.

Once you've got a highly-details list of micro-points, you'll find they're not so difficult to solve.
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 01:03PM
@Spectre

New Hand and Deal hand are two different tasks.

Remember that your HandView is in charge of displaying the cards. It doesn't own them, nor should it keep hold of references to them, that's the job of the Hand class. When you reset the game, the Hand instances should clear all references to cards and then let the HandView instance know of any changes.

The HandView class should then cycle through all QLabel children and remove them, then query the Hand for the new QLabels. For an empty hand, this will be empty.

The removing of children will be a two-step process, first, you should remove the QLabel from the layout and then remove it from the HandView's list of children. This is done calling QObject:: setParent(0) on each of the QLabels.

WARNING: You need to decide what you're going to do with the QLabel afterwards. If it doesn't have a parent, then it won't be automatically deleted when the program closes.

From the UML diagram, it appears that the QLabel should be owned by the Card class and thus it's the Card class' responsibility to delete the QLabel but this requires some care.

If you end the program, make sure that the HandView is reset before each of the Card instances get destroyed. If you don't and the HandView still has QLabels as children, it will try to delete them, when your Card gets deleted, you'll be trying to double-delete the same QLabel.
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 01:24PM
@ Rob
I have just completed the carddeck class and is working fine. I agree with you that this question is not that difficult. I guest our problem is not the difficulty of the question but rather the time factor. I'm definitely enjoying this module but yet again it can be frustrating when trying to learn something with additional job of solving a complex problem like this one at the same time. this module has been squeeze. the deadline is today and i'm thinking of extending it to sunday as I believe I can solve this problem.
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 02:14PM
robanaurochs Wrote:
-------------------------------------------------------
> @Spectre
>
> New Hand and Deal hand are two different tasks.
>
> Remember that your HandView is in charge of
> displaying the cards. It doesn't own them, nor
> should it keep hold of references to them, that's
> the job of the Hand class. When you reset the
> game, the Hand instances should clear all
> references to cards and then let the HandView
> instance know of any changes.
>
> The HandView class should then cycle through all
> QLabel children and remove them, then query the
> Hand for the new QLabels. For an empty hand, this
> will be empty.
>
> The removing of children will be a two-step
> process, first, you should remove the QLabel from
> the layout and then remove it from the HandView's
> list of children. This is done calling QObject::
> setParent(0) on each of the QLabels.
>
> WARNING: You need to decide what you're going to
> do with the QLabel afterwards. If it doesn't have
> a parent, then it won't be automatically deleted
> when the program closes.
>
> From the UML diagram, it appears that the QLabel
> should be owned by the Card class and thus it's
> the Card class' responsibility to delete the
> QLabel but this requires some care.
>
> If you end the program, make sure that the
> HandView is reset before each of the Card
> instances get destroyed. If you don't and the
> HandView still has QLabels as children, it will
> try to delete them, when your Card gets deleted,
> you'll be trying to double-delete the same QLabel.

I see... I thought it was easy that way. I'm wrong. So I have to rewrite the HandView all over again sad smiley I'm really confused now. How do you add a QLabel card to the HandView's children list? I feel so noob and stressed now. When I'm done with this assignment, I will get a new better Qt programming textbook.

@crunz, I have to agree with what you said about this module sad smiley I have to say the Ezust textbook's full of crap.

Read tutorial letter 101. It's not possible to extend the due date to later sad smiley
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 02:24PM
@Spectre

Don't panic. It's not as difficult as you think.

Go back to the basics. QWidget is a derivative of QObject so it can contain children. In the case of QWidget, any child QWidget will be drawn inside it unless it's visible property is set to false. Thus, in order to get any QWidget to draw inside another one, just set the one as the parent of the other.

Inside HandView:: rescanHand():

1. Clear out all existing Cards (remove parent and remove from layout)

2.
Language: C++ (QT)
QLabel *cardLabel = card->label(); cardLabel-> setParent(this); layout-> addWidget(cardLabel);

This should do the trick.

[Edit]
Sorry, I had the Card instance in there instead of the QLabel
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 03:15PM
robanaurochs Wrote:
-------------------------------------------------------
> @Spectre
>
> Don't panic. It's not as difficult as you think.
>
> Go back to the basics. QWidget is a derivative of
> QObject so it can contain children. In the case of
> QWidget, any child QWidget will be drawn inside it
> unless it's visible property is set to false.
> Thus, in order to get any QWidget to draw inside
> another one, just set the one as the parent of the
> other.
>
> Inside HandView:: rescanHand():
>
> 1. Clear out all existing Cards (remove parent and
> remove from layout)
>
> 2.
>
Language: C++ (QT)
QLabel *cardLabel = card->label(); > cardLabel-> setParent(this); > layout-> addWidget(cardLabel);
>
> This should do the trick.
>
> [Edit]
> Sorry, I had the Card instance in there instead of
> the QLabel

Thanks sir smiling smiley

I want to display 2 cards but it just doesn't work. Instead it only displays the first card. If I get it all figured out then I might be able to finish this project by 7pm tonight grinning smiley

Language: C++ (QT)
HandView::HandView(Hand *hand)   {   // testing   layout = new QHBoxLayout;   Card* ptrCard;   QLabel* cardLabel1; cardLabel1 = new QLabel;   QLabel* cardLabel2; cardLabel2 = new QLabel;   // cout << hand->size() << endl; // for debugging purpose   ptrCard = hand->at(0); cardLabel1 = ptrCard->label();   ptrCard = hand->at(1); cardLabel2 = ptrCard->label();   cardLabel1->setParent(this); cardLabel2->setParent(this);   layout->addWidget(cardLabel1); layout->addWidget(cardLabel2); }
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 03:37PM
Your HandView should not be creating QLabels. It should be querying the Hand instance for what Cards it has and then getting a pointer to those Cards' labels.

Also, you've got duplication here. The constructor and HandView:: setModel(Hand *hand) should basically be doing the same things.

Apart from other initialisation, your HandView constructor should be like:

Language: C++ (QT)
HandView:: HandView(Hand *hand) { // other initialisations setModel(hand) }

and your setModel() should be something along the lines of:
void HandView:: setModel(Hand *hand){
   m_hand = hand;
   rescanHand();
}

and your rescanHand() should be something along the lines of:
void HandView:: rescanHand(){
   // 1. Clear out all existing QLabel children (including removing from the layout)

   // 2. Get a list of cards from the Hand model class

   // 3. For each Card in the list of cards
   //    a. Get the QLabel from the Card
   //    b. Set the QLabel as a child of the HandView
   //    c. Add the QLabel to the layout
}

I would suggest making the clearing of old QLabel children in a separate function so that you can call that from your destructor as well without duplicating code (prevents double deletion in the Card destructor).
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 04:17PM
robanaurochs Wrote:
-------------------------------------------------------
> Your HandView should not be creating QLabels. It
> should be querying the Hand instance for what
> Cards it has and then getting a pointer to those
> Cards' labels.
>
> Also, you've got duplication here. The constructor
> and HandView:: setModel(Hand *hand) should
> basically be doing the same things.
>
> Apart from other initialisation, your HandView
> constructor should be like:
>
>
Language: C++ (QT)
HandView:: HandView(Hand *hand) { > // other initialisations > setModel(hand) > }
>
> and your setModel() should be something along the
> lines of:
>
void HandView:: setModel(Hand *hand){
>    m_hand = hand;
>    rescanHand();
> }
>
> and your rescanHand() should be something along
> the lines of:
>
void HandView:: rescanHand(){
>    // 1. Clear out all existing QLabel children
> (including removing from the layout)
> 
>    // 2. Get a list of cards from the Hand model
> class
> 
>    // 3. For each Card in the list of cards
>    //    a. Get the QLabel from the Card
>    //    b. Set the QLabel as a child of the
> HandView
>    //    c. Add the QLabel to the layout
> }
>
> I would suggest making the clearing of old QLabel
> children in a separate function so that you can
> call that from your destructor as well without
> duplicating code (prevents double deletion in the
> Card destructor).

IT WORKS!!!!! T_T

Sir, you are the best Qt teacher! I learned a lot about Qt programming by doing this assignment! That's really good. Too bad, I'm learning Qt stuff while under pressure. It's no fun sad smiley
avatar Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 04:36PM
You're welcome.

The easiest part of learning to program is learning the language and a particular Framework/Toolkit. The hardest part is learning how to analyse a problem and to design a solution. That's what this course and the 3rd level course are really trying to do. Using Qt is largely irrelevant.
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 04:44PM
Cool! That's the fun part of programming, right? grinning smiley

Erm, I have a little problem. When I try to click Hit button, all the cards are disappeared and I get this error message "QLayout: Attempting to add QLayout "" to QWidget "", which already has a layout". I think I forgot to remove all the cardLabels from the layout -_-

EDIT: It's realy weird!

Language: C++ (QT)
void HandView::rescanHand() // update the hand graphically   { QLabel *cardLabel; QHBoxLayout *layout = new QHBoxLayout(this); Card* ptrCard;   deleteChildren(); // add the QLabel card to the HandView's children list for (int i = 0; i < m_Hand->size(); i++)   { ptrCard = m_Hand->at(i); cardLabel = ptrCard->label(); cardLabel->setParent(this); // set the parent of cardLabel to this layout->addWidget(cardLabel); } }   void HandView::deleteChildren()   { QLabel *cardLabel; QList<QLabel*> allCardLabels = findChildren<QLabel*>(); // *This is not right. What I typed, look below!   for (int i = 0; i < allCardLabels.size(); i++)   { cardLabel = allCardLabels.at(i); cardLabel->setParent(0); } }

*QList<QLabel*> allCardLabels = findChildren<QLabel*>();

EDIT2: NVM! I FOUND A BUG grinning smiley Now it's working properly.
Anonymous User
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 08:55PM
Good for you Spectre.

I gave up and I will send what have done. only three buttons are working
Good luck and thanks to everybody who contributed to this phorum
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 09:34PM
@crunz, as long as you created all the required classes. You should get some points. Good luck. We all will pass this module grinning smiley Did you check out past exam papers? They are easy, really grinning smiley
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 10:28PM
Hey spectre, sorry just wanted to know where you saw the past papers from?
Re: Let’s get started with Assignment 3 colleagues
September 15, 2011 11:52PM
Well, I have this whole thing working... Now myUnisa won't show my courses and I can't find the lecturer's email address or name... sigh.
If you looking for code to delete the cards after a deal. Here you go..

Language: C++
void BlackJack::deleteCurrentCards() { QLayoutItem *child; while ((child = ui->dealerLayout->takeAt(0)) != 0) { delete child->widget(); delete child; } while ((child = ui->playerLayout->takeAt(0)) != 0) { delete child->widget(); delete child; } }
Sorry, only registered users may post in this forum.

Click here to login