Object Oriented Design Issues

I am going to develop a Tic-Tac-Toe game using Java (or possibly other OO languages). Now I have a photo in my opinion about the general design.

Interface: Player, then I can implement a couple of classes of players based on how I want the opponent to be, for example, a random player, an intellectual player, etc.

Classes: Board class, with a two-dimensional array of integers, 0 indicates open, 1 indicates me, -1 indicates an adversary. The rating function will also be here to return the next best move based on the current board and in turn.

The referee class that will create the instance of the board and two players, then start the game.

This is a rough idea of ​​my OO design. Can someone give me any criticisms please? I find this really helpful. Thank you very much.

+4
source share
5 answers

When I think about the structure of an object, I think of my methods as one of two things:

1) by asking the question "object"

2), the commander of the "object" to do something

Saying this, it makes no sense for me to ask “advice” what the next best move is. The board should just store the values ​​and tell you about its status.

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

The board class, as you described, now has many responsibilities: maintaining state, allowing users to move, and think about how to move on. This is too much responsibility.

And after all this, I would say this: given that this program is not so massive, almost any solution will be fine.

Good luck

+8
source

I would define 3 interfaces:

  • Game interface
  • IPlayer Interface
  • Board interface

Here is a quick sketch of what each class will have:

The game:

  • players (can only contain 2 players)
  • (it can be a different class or only a 2d matrix)
  • start();
  • nextTurn ();
  • reboot ();
  • isEnded ();

Iplayer:

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

You can run the code as follows:

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 

This would be the Game class, which would determine which games will be correct and which will not. If a player tried to make the wrong game, he would block it and he would call the player OnTurn () event again.

This or instead of defining an IPlayer interface, you can define an abstract APlayer class that would allow you to make the right moves.

+5
source

I would say that using me and the opponent for game movements is not entirely true. This should be Xs and Os or the first and second player. In addition, you have a beginning. Where will the input be processed? Referee or on the board or somewhere else? Also, what about tracking statistics that exist outside of 1 game?

Just some ideas.

+1
source

The general idea should be to make objects as closed and like-minded as possible. This will allow you to develop things faster and stronger.

Also, don't be afraid of modeling even the smallest part as your own class. Interactions are much simpler if you can model everything from a high level right from the start.

+1
source

For one, you will want to use some symbolic values, such as enumerations or constants, to represent the state of the slots on your board.

+1
source

Source: https://habr.com/ru/post/1309371/


All Articles