Hi!
When i was passing the test, i got this question:

"You are asked to design classes for a board game system. The board
has 8X8 squares. The board game will allow the user to play "Chess"
and "Checkers". The GUI is done by another team; you have to write the
supporting functions for the GUI. The GUI wants us to supply them with
2 services: "get initial state" and "move".
Design the classes to support this system. Take in account that we may
have to support new games (on the same 8X8 board) but with different
rules."

Can you explain me the answer or show it in C++ code?

>Can you explain me the answer or show it in C++ code?
:-| What was your answer?

>Can you explain me the answer or show it in C++ code?
:-| What was your answer?

Narue, I answered like this, but i don't think it was the correct answer:

Let’s explain this situation by the example on C++ program. We have some GUI, that is done by another team, that draws something. It is made by another team and we can’t change anything in this class. So we can present it like “GUIBoard” class, that has static function that draws any move (outputs any string). Than we create base class “Game” with some methods for game, like creating game menu, set game speed etc. We can play different games, so “Game” class should be universal and have virtual function that addresses to GUIBoard class and makes a move. So if we want to play chess we create “Chess” class, inherited by “Game” and define there rules for chess game. If we want to play checkers, we define another rules in “Checkers” class. But both classes have the same menue and set speed methods. If we want to move the figure, we call virtual function with the argument(draught can move forward, castle can - sideway). So we can simply add another kind of game, if we want, and we shouldn’t change “GUIBoard” or “Game” classes.

#include <iostream>
#include <string>

class GUIBoard
{
    public:
		static void SomeGUIFunctionByOtherTeam(std::string s)
	  {
		  std::cout<<s<<'\n';
	  }
};

class Game
{
    private:
       int speed;
       // other game data 
	public:
         // Constructor
	   Game(){speed = 1;}
	   // Menu, that is the same method for all games
	   void CreateMenu(){}

	   // Set game speed method, the same for all games
	   int SetGameSpeed(int value){ speed = value; }

	   // Method that accesses GUIBoard class with different                     
         // arguments
	   virtual void Move(std::string s)
	   { 
  		 GUIBoard::SomeGUIFunctionByOtherTeam(s);
	   }
};


class Chess: public Game
{
	// here we can declare rules for Chess game only
};

class Checkers: public Game
{
	// here we can declare rules for Checkers game only
};


int main()
{
	Game* pChess = new Chess;
	pChess->Move("It's chess move");

	Game* pCheckers = new Checkers;
	pCheckers->Move("It's checkers move");

	return 0;
}

You forgot to provide an interface for "get initial state". I also think that you're thinking in the wrong direction with "move". You provide the data, and the GUI uses that data to draw a representation of it. Something like this:

class GameBoard; // Not really important to the question
class GameMove; // Also not important

class Game {
public:
  virtual const GameBoard& GetInitialState() = 0;
  virtual const GameBoard& SetMove ( const GameMove& move ) = 0;
};

All communication is done through the Board and GameMove objects. You provide the GUI with the state of the board and the GUI provides you with move information that's dealt with polymorphically. Your class doesn't know or care how the GUI uses that information, just as the GUI doesn't know or care how you use the move information.

Narue, GameBoard that GUI class that we can't change, am I right?

>GameBoard that GUI class that we can't change, am I right?
No. You're the data layer, you don't (and shouldn't) know jack about the presentation layer. That's as simple as I can put it. The GameBoard class is an intermediary between the data layer and the presentation layer. It could be as simple as an 8x8 table of integers where each integer represents a different game piece. You set the values and the presentation layer uses those values to draw stuff. The data layer should be completely modular in that you can cut it out, plug it in to a completely different presentation layer, and everything will work as long as the presentation layer uses GameBoard and GameMove according to your interface.

The only thing that can't change is the public interface of your classes. That is, whatever the public interface of GameBoard and GameMove is, and the two member functions that Game defines. Everything else can change because doing so won't screw up what the GUI people are working with.

Narue, Ok, I think I understand it.

Thank you! =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.