Chess in Java - Object orientation vs Efficiency
NickName:David Wood Ask DateTime:2015-08-27T19:05:40

Chess in Java - Object orientation vs Efficiency

I'm developing a chess program in Java and considering the following problem:

  • The Rook piece requires implementation for moving in straight lines.
  • The Bishop piece requires implementation for moving in diagonal lines.

However,

  • The Queen piece requires implementation for both of the above movement patterns.

I can't think of a clean solution to modelling this relationship, I have considered some but none of them are both compliant with good object-orientated design and code efficient.

  1. Java doesn't support multiple inheritance, so Queen cannot borrow implementation from Rook and Bishop

  2. If Rook and Bishop extended Queen, I'd need to extract the logic for each kind of movement into separate methods, this would serious bloat my current design for how movement is validated.

Neither of the above solutions seem elegant enough to beat just:

  1. Putting ALL of the movement implementation into the parent class of all the pieces, this way they all can share all of the common implementation (of which there is quite a bit)

I'm aware solution 3 is in violation of good Java design, but in this case, that design pattern seems to only be forcing bloated, inelegant solutions.

Probably this could be avoided with a complete restructuring of the program, but everything up to this point looks pretty efficient to me, does good OO design always come at the cost of directness of functionality and structure? Is my approach just wrong for the style of the language?

How would you solve this problem?

Copyright Notice:Content Author:「David Wood」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32247718/chess-in-java-object-orientation-vs-efficiency

Answers
Rob Audenaerde 2015-08-27T11:11:54

While some classes might behave in a similar way, this does not automatically mean that they are in one hierarchy!\n\nFor example, both a Human and a Crab can move sideways, but it is silly for a Human to extend Crab.\n\nIf you really want to re-use movement code, you can use encapsulation, use a Movement type and make it like this:\n\nclass Human\n{\n List<Movement> movements;\n}\n\nclass Crab\n{\n List<Movement> movements;\n}\n\nclass MoveSideWays extends Movement\n{\n move();\n}\n\nclass MoveForward extends Movement\n{\n move();\n}\n\n\nBut it feels like over-engineering. I would have a Piece class with getPossibleMoves() and just implement it directly. There is not too much overlap and Rooks have specialized movements (Castling) as well. \n\nclass Rook extends Piece\n{\n List<Move> getPossibleMoves() {...}\n}\n\nclass Queen extends Piece\n{\n List<Move> getPossibleMoves() {...}\n}\n",


Tagir Valeev 2015-08-27T11:13:29

You should not add a superclass-subclass relations to the individual pieces. The Queen is not the special case of the Rook, neither the Rook is the special case of the Queen. They are just different pieces. You may create an interface (or an abstract class) Piece and provide different implementations like Rook, Bishop, Queen and so on.",


slartidan 2015-08-27T11:11:20

You could move both methods to the superclass, but make them protected static (you could also put those methods in a PieceHelper). In the subclasses you can then implement the adequate methods, referencing to one or the other of those methods (making them public).\n\nSuperclass:\n\nabstract class Piece {\n protected static void moveStraight(Piece p) {...}\n protected static void moveDiagonal(Piece p) {...}\n}\n\n\n(Optional) Interfaces:\n\ninterface DiagonalMover {\n void moveDiagonal();\n}\ninterface StraightMover {\n void moveStraight();\n}\n\n\nSubclasses:\n\nclass Rook extends Piece implements StraightMover {\n public void moveStraight() {Piece.moveStraight(this);}\n}\nclass Bishop extends Piece implements DiagonalMover {\n public void moveDiagonal() {Piece.moveDiagonal(this);}\n}\nclass Queen extends Piece implements StraightMover, DiagonalMover {\n public void moveStraight() {Piece.moveStraight(this);}\n public void moveDiagonal() {Piece.moveDiagonal(this);}\n}\n",


whitecoffee 2015-08-27T11:16:54

Straight line and diagonal movements are behaviors\nThus they can be modeled using interface more fittingly than inheritance\n\nRookie implements StraightMovement\n\nBishiop implements DiagonalMovement\n\nQueen implements StraightMovement,DiagonalMovement",


More about “Chess in Java - Object orientation vs Efficiency” related questions

Chess in Java - Object orientation vs Efficiency

I'm developing a chess program in Java and considering the following problem: The Rook piece requires implementation for moving in straight lines. The Bishop piece requires implementation for movi...

Show Detail

Efficiency of java for loops

As an addendum to this question Java loop efficiency (&quot;for&quot; vs. &quot;foreach&quot;) I have a simple question. Does the enhanced for-loop have a larger memory footprint, or will they both

Show Detail

MSTest + CHESS in VS 2010

Can I unit test my multi-threaded code using CHESS &amp; MSTest in VS 2010. I tried this [TestMethod] [HostType("Chess")] [TestProperty("ChessExpectedResult", "deadlock")] public void TestMyMetho...

Show Detail

Chess engine in c or java, performance difference?

I have a simple chess app based on the Fruit engine on iOS devices. Now I'd like to do a chess app for Android. It seems like I have the following options: port the c Fruit engine to Java use ano...

Show Detail

Programming Chess in Functional programming

A year ago I programmed a chess AI using the Alphabeta prunning algorithm. This was relatively straight forward to do in c++. One of the main issues I considered while doing this was making my code

Show Detail

Building a simple Chess board with recursion in java

I'm trying to build a simple chess board layout in java via recursion but my problem is, that in each recursive call the amount of fields are reduced by one which is wrong. Black fields are represe...

Show Detail

Java: efficiency of writeObject vs writeExternal

It's said that Java's default serialization mechanism is not very efficient because a)it discovers which fields to write/read through reflection which is usually slow b) it writes extra data to str...

Show Detail

Java Chess Game Regex

I am currently working on a chess game in java. I want to create a method to check the format of the string input. A typical chess move in my game is: e2 e4 a1 a3 In general, its: [letter][nu...

Show Detail

Object orientation, data orientation, cache pollution and cache obviousness

In a regular object oriented practice it is not that rare objects have multiple unrelated member properties. And when objects are being processed, it is not rare that it is done in different passes,

Show Detail

String.split vs StringTokenizer on efficiency level

I work on a large scale data set and as of that I am interested in the most efficient way to split a String. Well I found that Scanner vs. StringTokenizer vs. String.Split and that string tokenize...

Show Detail