Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Design Questions

Tags:

oop

I am going to develop a Tic-Tac-Toe game using Java(or maybe other OO Languages). Now I have a picture in my mind about the general design.

Interface: Player, then I will be able to implement a couple of Player classes based on how I want the opponent to be, for example, random player, intelligent player, etc.

Classes: Board class, with a two-dimensional array of integers, 0 indicates open, 1 indicates me, -1 indicates opponent. The evaluation function will be in here as well, to return the next best move based on the current board arrangement and whose turn it is.

Referee class, which will create instance of the Board and two player instances, then get the game started.

This is a rough idea of my OO design. Could anybody give me any critiques please? I find this is really beneficial. Thank you very much.

like image 833
Kevin Avatar asked May 10 '10 23:05

Kevin


People also ask

What is design in object-oriented design?

System design is the designing the software/application as a whole [high level] that may include analysis, modelling, architecture, Components, Infrastructure etc. whereas the objected-oriented design is the set of defined rules/concepts to implement the functionalities within a software.


3 Answers

When I think of object structure, I think of my methods as doing one of two things:

1) asking an 'object' a question

2) commanding the 'object' to do something

That being said, it doesn't make sense to me to ask the 'board' what the next best move is. The board should simply hold values and tell you about its state.

I would probably have an object that is dedicated to determining the best next move for a given 'intelligence'. Let's call it the 'move_brain'. Then you can say, "hey move_brain, given this board and this level of intelligence, what's the next best move?"

The board class as you have outlined it now has many responsibilities: holding state, allowing users to move, AND thinking about how to move next. That's too much responsibility.

And after all that, I would say this: given that this program is not THAT massive, pretty much any solution will be okay.

Best of luck!

like image 133
DexterW Avatar answered Oct 10 '22 07:10

DexterW


I'd define 3 interfaces:

  • Game interface
  • IPlayer interface
  • Board interface

Here's a quick sketch of what would each class have:

Game:

  • players (only could hold 2 players)
  • board (this could be other class or just a 2d matrix)
  • start();
  • nextTurn();
  • restart();
  • isEnded();

IPlayer:

  • onTurn() (this is the event that tells you its your turn to play)
  • play(int x, int y);
  • onWin() (this event tells you you won)
  • onLose() (you lost)
  • onTie(); (you tied)

You could run the code like the following:

IPlayer meAsAPlayer = new UserPlayer(); //you'd have to implement this
                                        //as an "empty" class that would let the user
                                        //specify the actions
IPlayer AIPlayer = new AIPlayer(); //one AI class you'd have implemented
Game game = new Game(meAsAPlayer, AIPlayer);
game.start(); //this would run one game to the end
game.start(); //this would be the second game already

It would be the Game class that'd decide which plays would be correct and which wouldn't.If a player tried to make an incorrect play, it'd block it and it'd call again that player's OnTurn() event.

That or instead of defining the IPlayer interface you could define an APlayer abstract class that'd only let you make correct movements.

like image 24
devoured elysium Avatar answered Oct 10 '22 06:10

devoured elysium


I would say that having using me and opponent for player moves isn't quite right. It should be either Xs and Os or first and second player. Beyond that, you've got a start. Where is input going to be handled? Referee or in the board square or somewhere else? Also, how about stat tracking that exists beyond 1 game?

Just some ideas.

like image 33
Michael Dorgan Avatar answered Oct 10 '22 08:10

Michael Dorgan