Design a chess game using OO principles

Design a chess game using object oriented principles.

My initial thoughts(code):

	public abstract class MyChessPiece {
		private int horizontal;
		private int vertical;

		public abstract boolean moveTo(Pair<Integer> des);

		public Pair<Integer> getPos() {
			return new Pair<Integer>(horizontal, vertical);
		}
	}

	public class MyKing extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyRook extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyBishop extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyQueen extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyKnight extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyPawn extends MyChessPiece {
		@Override
		public boolean moveTo(Pair<Integer> des) {
			return false;
		}
	}

	public class MyPlayer {
		private Map<Pair<Integer>, MyChessPiece> pieces;

		public boolean move(Pair<Integer> origin, Pair<Integer> des) {
			return pieces.get(origin).moveTo(des);
		}
	}

	public class MyChessGame {
		private MyPlayer black;
		private MyPlayer white;

		public void initialize() {
			// initialize the game by putting pieces for black and white
		}

		public boolean check(MyPlayer white, MyPlayer black) {
			return false;
		}

		public void handleCollision(MyPlayer white, MyPlayer black) {
			// follow the rule
		}

		public void play() {
			boolean gameOver = false;
			while (!gameOver) {
				// white moves
				white.move(new Pair<Integer>(0, 0), new Pair<Integer>(0, 0));
				black.move(new Pair<Integer>(0, 0), new Pair<Integer>(0, 0));

				gameOver = check(white, black);
			}
		}

	}

Solution:

	public class ChessPieceTurn {
	};

	public class GameManager {
		void processTurn(PlayerBase player) {
		};

		boolean acceptTurn(ChessPieceTurn turn) {
			return true;
		};

		Position currentPosition;
	}

	public abstract class PlayerBase {
		public abstract ChessPieceTurn getTurn(Position p);
	}

	class ComputerPlayer extends PlayerBase {
		@Override
		public ChessPieceTurn getTurn(Position p) {
			return null;
		}

		public void setDifficulty() {
		};

		public PositionEstimator estimater;
		public PositionBackTracker backtracter;
	}

	public class HumanPlayer extends PlayerBase {
		@Override
		public ChessPieceTurn getTurn(Position p) {
			return null;
		}
	}

	public abstract class ChessPieceBase {
		abstract boolean canBeChecked();

		abstract boolean isSupportCastle();
	}

	public class King extends ChessPieceBase {

		@Override
		boolean canBeChecked() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		boolean isSupportCastle() {
			// TODO Auto-generated method stub
			return false;
		}
	}

	public class Queen extends ChessPieceBase {

		@Override
		boolean canBeChecked() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		boolean isSupportCastle() {
			// TODO Auto-generated method stub
			return false;
		}
	}

	public class Position { // represents chess positions in compact form
		ArrayList<ChessPieceBase> black;

		ArrayList<ChessPieceBase> white;
	}

	public class PositionBackTracker {
		public Position getNext(Position p) {
			return null;
		}
	}

	public class PositionEstimator {
		public PositionPotentialValue estimate(Position p) {
			return null;
		}
	}

	public abstract class PositionPotentialValue {
		abstract boolean lessThan(PositionPotentialValue pv);
	}
Advertisements

2 Comments (+add yours?)

  1. Dmitri
    Dec 21, 2012 @ 18:23:46

    How come is King got isSupportCastle() set to false?

    Reply

  2. Kinshuk Chandra
    Mar 22, 2014 @ 16:15:35

    I have also created the design similar to you – here. Please suggest any corrections.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: