I would like to solicit your thoughts and ideas (code can come later) about how you would approach a project idea I just tossed together. I chose this as a testbed for me to play with objects, specifically multiple instances of a few specific objects, and as I thought about it, it raised a lot of questions about how to manage, contain, signal all of those objects that do almost the same thing at the same time.
I want to build an OO bingo simulator. While it is not necessarily the most efficient approach, let's academically assume it needs to be object-oriented.
We have 75 ball objects, which can be picked randomly by a machine, and which therefore need an awareness of being picked(), since no ball can come up twice in a game.
We have a scorecard grid with 25 square objects on it, and appropriate values for each column of the grid (B: 1-15,i:16-30, etc.), which are chosen randomly on each card. If we work with all 75 possible numbers (maybe not the best approach), each number needs to know if it's on the bingo card for this game, whether it's matched a number that's already been picked(), and whether it's a Freebie (middle square on the card - 'FREE' instead of a number)
Initial questions:
Can like objects be grouped by some mechanism such that changing an external parameter has them respond to that change, or do they each need to be iterated/manipulated each time to become "aware" of global changes? For example, a number is picked() - can the squares "know" if one matches the new number, or do I need to trigger each one to check?
Maybe I'm thinking in more of a parallel-thread mode here, one thread per object, which remains "active" during the game. Is that way out there? It's probably over my head at this point anyway.
Reel me in; tell me what you think.
27 2180 bartonc 6,596
Recognized Expert Expert
We have 75 ball objects, which can be picked randomly by a machine, and which therefore need an awareness of being picked(), since no ball can come up twice in a game.
Actually, I believe that the machine is the object in question and that balls are data that it dispences. The machine (object) has a better chance of encapsulating awareness of which numbers have been dispensed than a ball does.
We have a scorecard grid with 25 square objects on it, and appropriate values for each column of the grid (B: 1-15,i:16-30, etc.), which are chosen randomly on each card. If we work with all 75 possible numbers (maybe not the best approach), each number needs to know if it's on the bingo card for this game, whether it's matched a number that's already been picked(), and whether it's a Freebie (middle square on the card - 'FREE' instead of a number)
Initial questions:
Can like objects be grouped by some mechanism such that changing an external parameter has them respond to that change, or do they each need to be iterated/manipulated each time to become "aware" of global changes? For example, a number is picked() - can the squares "know" if one matches the new number, or do I need to trigger each one to check?
I've got similar thoughts here. Going to look for some examples
Maybe I'm thinking in more of a parallel-thread mode here, one thread per object, which remains "active" during the game. Is that way out there? It's probably over my head at this point anyway.
Reel me in; tell me what you think.
Do you have Robin Dunn's book: wxPython in Action?
Actually, I believe that the machine is the object in question and that balls are data that it dispences. The machine (object) has a better chance of encapsulating awareness of which numbers have been dispensed than a ball does.
I've got similar thoughts here. Going to look for some examples
Do you have Robin Dunn's book: wxPython in Action?
I haven't read it all, just looked up a few things.
I'm not really interested in the GUI in particular, more the logic. If the GUI objects are easier to build/attach to than abstract code items, that's fine.
The reason for the balls, instead of the machine, as objects is that I want to exercise multiple identical objects in action. JFYI.
bartonc 6,596
Recognized Expert Expert
The reason for the balls, instead of the machine, as objects is that I want to exercise multiple identical objects in action. JFYI.
Ok, I'll buy that. So if each ball instance needs to know which number was just picked() here's how I'd (almost) lay out the structure: - # I do this a bit differently, but Mr. Dunn says something like this:
-
-
class NumberDispenser(object):
-
def __init__(self):
-
self.clients = []
-
-
def RegisterClient(self, client):
-
self.clients.append(client)
-
-
def UpdateClients(self, data):
-
for client in self.clients:
-
client.Update(data) # This part I don't like:
-
# having to know the name of the client function here
-
-
class NumberClient(object):
-
def __init__(self, server, name):
-
self.name = name
-
server.RegisterClient(self)
-
-
def Update(self, data):
-
print self.name, data
-
-
-
if __name__ == "__main__":
-
server = NumberDispenser()
-
client1 = NumberClient(server, "client #1")
-
client2 = NumberClient(server, "client #2")
-
-
# call this in response to some event or whatever
-
server.UpdateClients(6545)
bartonc 6,596
Recognized Expert Expert
I haven't read it all, just looked up a few things.
I'm not really interested in the GUI in particular, more the logic. If the GUI objects are easier to build/attach to than abstract code items, that's fine.
Actually, I don't use the GUI stuff from the book. I really like (and use similar design) the Model-View-Controler idea. There is also threading discussion toward the end.
Actually, I don't use the GUI stuff from the book. I really like (and use similar design) the Model-View-Controler idea. There is also threading discussion toward the end.
OK, that's not ringing any bells. Can you elaborate on this - what, where, refs?
Ok, I'll buy that. So if each ball instance needs to know which number was just picked() here's how I'd (almost) lay out the structure: -
def UpdateClients(self, data):
-
for client in self.clients:
-
client.Update(data) # This part I don't like:
-
# having to know the name of the client function here
-
-
...
-
if __name__ == "__main__":
-
server = NumberDispenser()
-
client1 = NumberClient(server, "client #1") # <===
-
client2 = NumberClient(server, "client #2") # <===
-
Regarding that, my original thought, which is admittedly BASIC-esque, was to define an array where each element would become an instance. I don't know how to translate that here, but such a construct would alleviate much of the naming overhead. I guess it would also be redundant, since if I did that I wouldn't need the objects at all. :) But like I said, it's an academic exercise...
Can I use some form of eval() to create something like (this is just off the top of my head, trying to get the idea across): -
varhdr = 'client'
-
-
for i in range(75):
-
eval(varhdr+str(i)+' = NumberClient(server, "client #'+str(i)+'")')
-
Don't know if that's close to a proper use of eval; in SB we had a means of constructing dynamic lines of code like that so they could be reused, as in a loop.
This problem is what I was referring to when I mentioned a curiousity about how people would "group"cont rol of these objects.
Actually, the use of a class variable, as discussed in that other thread, may make sense here. Such as, when a number is picked, - NumberClient.LastPicked=32
-
-
for square in grid:
-
# execute instance.CheckPicked code here
-
# each instance of NumberClient class now has
-
# a self.LastPicked value of 32 to process against
-
-
bartonc 6,596
Recognized Expert Expert
Regarding that, my original thought, which is admittedly BASIC-esque, was to define an array where each element would become an instance. I don't know how to translate that here, but such a construct would alleviate much of the naming overhead. I guess it would also be redundant, since if I did that I wouldn't need the objects at all. :) But like I said, it's an academic exercise...
Can I use some form of eval() to create something like (this is just off the top of my head, trying to get the idea across): -
varhdr = 'client'
-
-
for i in range(75):
-
eval(varhdr+str(i)+' = NumberClient(server, "client #'+str(i)+'")')
-
Don't know if that's close to a proper use of eval; in SB we had a means of constructing dynamic lines of code like that so they could be reused, as in a loop.
This problem is what I was referring to when I mentioned a curiousity about how people would "group"cont rol of these objects.
Actually, the use of a class variable, as discussed in that other thread, may make sense here. Such as, when a number is picked, - NumberClient.LastPicked=32
-
-
for square in grid:
-
# execute instance.CheckPicked code here
-
# each instance of NumberClient class now has
-
# a self.LastPicked value of 32 to process against
-
-
A list comprehension is what you are looking for. A comprehension is a very handy tool when you don't need much stuff going on in a loop, but you do need elements appended to a list. This technique creates the list object and fills it in all at once. Notice, also, the C-line string formatting. I use it all the time. - class NumberDispenser(object):
-
def __init__(self):
-
self.clients = []
-
-
def RegisterClient(self, client):
-
self.clients.append(client)
-
-
def UpdateClients(self, data):
-
for client in self.clients:
-
client.Update(data) # This part I don't like:
-
# having to know the name of the client function here
-
-
class NumberClient(object):
-
def __init__(self, server, name):
-
self.name = name
-
server.RegisterClient(self)
-
-
def Update(self, data):
-
print self.name, data
-
-
-
if __name__ == "__main__":
-
server = NumberDispenser()
-
# list comprehension syntax uses [] on around the expession, generators use ()
-
instanceList = [NumberClient(server, "client%d" %i) for i in range(5)]
-
for inst in instanceList:
-
print inst.name
bartonc 6,596
Recognized Expert Expert
The list comprehension here - instanceList = [NumberClient(server, "client%d" %i) for i in range(5)]
is equivalent to - instanceList = []
-
for i in range(5):
-
instanceList.append(NumberClient(server, "client%d" %i))
which may be easier to read. I learned comprehensions so long ago that I have forgotten if the benefits actually outweigh the mashed syntax. If I get a break, I'll look it up and get back to you.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Slash |
last post by:
Hello everyone
I'm looking for seminar topics (and project ideas too) for my final
year in Computer Engineering. I'm considering doing something on
C++/Linux, considering that I love it so much -- I'm still learning it
though.
The topic could be anything computer-related at all, anything new and
interesting perhaps :) Perhaps a research topic.
|
by: Tom |
last post by:
I am looking for some ideas for how to design the layout of the form for
data entry and to display the data for the following situation:
There are many sales associates. A sales associate can work for multiple
companies and work for multiple divisions within each company. Within each
division he can work in multiple departments and within each department he
can work with multiple groups. In each group he works on multiple projects.
All the...
|
by: micangello |
last post by:
i need ideas on what my final year project can be about
i have studied 2 years of pure math (advanced calculus and algebra, topology, statistics..... )
i then switched ot computer science
now in in my last year in Computer Science
i have taken courses in
Numerical analysis
Operation Research
|
by: Kinokunya |
last post by:
Hi guys,
My group and I will be working on our final year project, the scope to
do a program/web-based application similar areas of functionalities
like the PyLint and PyChecker; a Python syntax checker. We have no
Python background, equipped only with some knowledge of Java and Dot
net.
We did some research on PyLint and found out that there are 2 common
modules that PyLint & PyChecker are using, namely logilab-astng and
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |