Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS Game Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iOS Game Development
This is the forum to discuss the Wrox book Beginning iOS Game Development by Patrick Alessi ; ISBN: 978-1-1181-0732-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS Game Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 16th, 2012, 09:04 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default In ScrambleWord game, why use extra class ComputerPlayer besides MVC?

This is also in

http://stackoverflow.com/questions/1...-but-why-use-a

Why the choice of design of the classes (MVC + ComputerPlayer)? Why the fourth class?


I am somewhat confused that, in a book, the design of the game on iOS is by using the MVC pattern. So we have

View, which is done by the Interface Builder, and drawing connections out for the controls' properties / values (as outlets), and event handlers (as actions)

Model, which stores the states of the game, such as time remaining, and current player's score (human player)

Controller, which runs after the View is loaded, to populate the Model's data to the view, set up the Timer for the game, handle the user's tapping on the screen, etc (event handlers), and implement the event handler for the Timer events.

So this makes sense, but there is a also a fourth class called ComputerPlayer, which is to handle the game's rules and mechanics, such as creating the words for the human player to guess, tell how many remaining words for the human player to guess, and provide for the next word for the human player to guess. (Update: this ComputerPlayer is 100% used by the Model class... the Model code instantiated the ComputerPlayer object and then make calls to it).

I don't quite understand why in this MVC model, there is an extra class ComputerPlayer? Why not make this part of the Model class, or even part of the Controller class?

(It sort of looks like helpers, if compared to Ruby on Rails's MVC pattern. But helpers are mostly called by the View code.)
Reply With Quote
  #2 (permalink)  
Old April 16th, 2012, 02:01 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Your suggestion breaks done if you have more than one player. Consider the game as a networked game of individuals (or even multiple computers), who each create their own scrambled wordlist. It makes sense for each potential player to be an independent object, so that multiple players can participate. Consider a scenario with 3 players, one presenting a word and 2 players guessing, you would not want multiple game instances, but multiple player instances populating the game model.The game model would track the results for each player, but the player is clearly independent of the game model.
The game presents the word, but the player provides the scrambled word and the correct solution to the game model. With the player as a separate object, the player can be real or virtual, multiple players can exist. The use of a separate class is more flexible and more easily extended.


Bob
Reply With Quote
  #3 (permalink)  
Old April 16th, 2012, 02:18 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by thepianoguy View Post
Your suggestion breaks [down] if you have more than one player. Consider the game as a networked game of individuals (or even multiple computers), who each create their own scrambled wordlist. It makes sense for each potential player to be an independent object, so that multiple players can participate. Consider a scenario with 3 players, one presenting a word and 2 players guessing, you would not want multiple game instances, but multiple player instances populating the game model.The game model would track the results for each player, but the player is clearly independent of the game model.
The game presents the word, but the player provides the scrambled word and the correct solution to the game model. With the player as a separate object, the player can be real or virtual, multiple players can exist. The use of a separate class is more flexible and more easily extended.


Bob
I think the original code will break down if there are more than 1 human player, because there is no HumanPlayer class. I also wonder why not... probably it is more complete to have a HumanPlayer class, but the game loop / Timer event handler already handles everything, so there need not to be a HumanPlayer class... it is just a simpler / lazy way of implementing it.

Are you saying that, beside 3 classes, each of M, V, and C in MVC, then also have a class for a "ComputerPlayer" which is like a dealer or boss that mandates the rules of the game?

If that is so, I also wonder, doesn't the Controller mandates the rule of the game as well? How does it break down into whether Controller should mandate or ComputerPlayer should mandate? Or maybe everything should be in ComputerPlayer if possible. The Controller should be as thin as possible?
Reply With Quote
  #4 (permalink)  
Old April 17th, 2012, 10:51 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The game as written in the book is one player. The user against the computer. The positing of additional players was done to explain the rational for having a separate class for the player. As written, multiple players can be instantiated and only minor changes would need to be made to the Scrambler model to accommodate this. Design decisions should be made that promote extensibility. If you wrote the program with all of the word generation code in the ViewController, that would work fine but it is a dead end. Should you choose to make the game interact with another person (or computer) over a network rather than the local device, for example, the first thing you would do is create a separate class to represent that player. If you then chose to allow multiple players to interact and compete against each other, you would instantiate multiple instances of players not multiple instances of the ScramblerModel. The ScramblerModel would be responsible for tracking the individual players, asking a player for a word, presenting the word for the other players, running the timer, adding or subtracting points for the various responses. The game as written can currently have multiple players, but the code to manage this would need to be written.

MVC does not suggest that there is only one model, view, and controller and nothing else in the program. The idea is that each view is supported by a model and controller. Objects will exist that do not have corresponding views. (Along the lines of the Foundation framework) If we move the game off of the computer and into the real world, what would be needed? A player to provide the words (ScramblerPlayer which is an object, but not a view), something to display the words, score and keep time (ScramblerModel+ViewController+View), and the user.

When making design decisions it is very useful to try to make a real world analogy and try to determine what functions each object should have. There is not the sense of a "dealer" or "boss", but that each object has specific capabilities. The player knows how to generate the wordlist, shuffle and provide the words, and evaluate correct responses. The player does not know or care about the scoring method, timer or how things are displayed. The ScramblerModel, through its ViewController, knows how to ask for and display the word, ask the player if the response is correct and score appropriately. The ScramblerModel does not care where the word list came from, or how it was generated.

Referring to your statement about a "HumanPlayer" class. The ScramblerPlayer class is currently hardcoded for its word list initialization. This is clearly not the only way to initialize the list. Modifying this initialization process is all that would be needed to allow virtual or real players. A separate class would not be needed. Basically, you would check to see if there was a "human" player, and if not, initialize from a preexisting wordlist or randomly from another source such as the web or a dictionary.

Bob

Last edited by thepianoguy; April 17th, 2012 at 04:23 PM.
Reply With Quote
  #5 (permalink)  
Old April 18th, 2012, 02:10 PM
Wrox Author
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I agree with everything the Bob mentioned, particularly, "MVC does not suggest that there is only one model, view, and controller and nothing else in the program." MVC just suggests an overall architecture of model being decoupled from view and controller linking the two.

My decision to have a separate ComputerPlayer class stems from the fact that a developer may want to swap out player models. Not necessarily in this game, but imagine that you are building a chess game. Wouldn't it be nice to be able to easily build many different computer algorithms to play chess and swap them out easily? It makes it easier to build your AI if it is encapsulated in a class that you can easily swap.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Errors while upgrade theBeerHouse mvc project form asp.net mvc 1.0 to 2.0 vanbach1304 BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 0 April 20th, 2010 06:43 AM
TheBeerHouse MVC preview ASP.NET MVC Website Programming Problem Design Solution jminatel BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 July 18th, 2009 09:16 AM
extra space??? crazeydazey XSLT 2 August 11th, 2008 05:55 AM
How to eliminate all the extra zero? kuku SQL Server 2000 3 June 29th, 2005 09:47 PM
Extra Processor ryant SQL Server 2000 2 October 9th, 2003 08:55 AM



All times are GMT -4. The time now is 04:16 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.