Vous êtes sur la page 1sur 6

1.

Tell me how you would create a program for a simple game, like chess or checkers in an
OOL
Object Oriented design represents the real world objects in code.
Think of how you play chess:

o
o
o

o
o

o
o

A Board is collection of blocks (Block)


A Player is collection of pieces ( Player = Piece )
A Game has :
one board
one black player
one white player
When making a move a Player chooses a Piece
Depending on type of piece, the Player checks validmoves.Which means that validity of move is :
tightly coupled to type of piece
is a property of piece
When thinking about valid moves, the player is aware of
the board
location of all the pieces on the board
Last point implies that the move validation routine should have access location of all the pieces on the board
On the board, block contains a piece

Gameplay
1.
A Player gets a turn [ Game.play() ]
2.
Player makes a plays a turn and returns the played move to Game [ Player.playTurn() ](selected
piece, from block and to block are encapsulated in a data-type Move)

3.

4.
5.

Game delegates the move validation processing to the moved Piece instance, passing destination-block and
board instances. ( Piece.isValidMove(...): boolean )(Board instance is required to check is a piece lies
in path of the move)
Piece calculates validity of the move and returns a boolean to Game
If move is valid, GameController
A.
updates location of Piece on board (Game.updateBoard(...))
B.
checks if move has performed any kills ( Game.checkKills(...))
C.
checks if any of game terminations conditions are valid ( Game.isGameOver(...))

class Piece is the abstract class, which is extended by class King,

Queen, Knight, etc.;

Every Piece has a virable to mark which Player it belongs to;

class Block is cell on the chess board, there might be Piece on Block;

class Game is the entry of this small project, it initializes the chess game,
control the action rounds, and determine when the game stops.

Codes
Piece
Piece has the responsibility to judge whether a move is valid or not, the extended
class, such as Queen, Knight will implement the judge function boolean
isValidMove.
1.
2.
3.
4.
5.
6.
7.
8.

public abstract class Piece {


private Player player;
public Piece(Player p) { player = p;}
public abstract boolean isValidMove(Block[][] board,
int fromX, int fromY, int toX, int toY);
public Player getPlayer() { return player; }
}

Knight which extends Piece


The movement of Knight is independent from other pieces between the original
block and the destionation, so the board variable is unused here.
1.
2.
3.
4.
5.

public class Knight extends Piece {


public Knight(Player p) {
super(p);
}

6.
7.
@Override
8.
public boolean isValidMove(Block[][] board,
9.
int fromX, int fromY, int toX, int toY) {
10.
/*
11.
Piece orig = board[fromX][fromY].getPiece();
12.
Piece dest = board[toX][toY].getPiece();
13.
*/
14.
int x = Math.abs(formX - toX);
15.
int y = Math.abs(fromY - toY);
16.
if (x * y = 2) {
17.
return true;
18.
} else {
19.
return false;
20.
}
21.
}
22.
23. }
Player
As simple as it can. Or, you can add some functions to track every pieces belonging
to this player.
1.
2.
3.
4.
5.
6.

public class Player {


private boolean result;
public boolean getResult() { return result; }
public void setResult(boolean r) { result = r; }
}

Block
There might be a Piece on the Block, so getPiece() return null when there
isNO piece on it, otherwise return the piece object.
1.
2.
3.
4.
5.
6.
7.
8.
9.

public class Block {


private Piece piece;
public Block(Piece p) { piece = p; }
public Piece getPiece() {
return piece;
}
public void setPiece(Piece p) {

10.
piece = p;
11.
}
12. }
Game
There are two players black & white defined as virables, and a two dimensional
arrayBlock[][] board acts as the chessboard.
I use function void initialize() to setup a new chess game: reset the board and the
two players' status.
The function boolean move() has several responsibilities:

1) decide there is a Piece belongs to the current player, if not return false;

2) decide the destionation Block is a blank Block; or if it is not blank, there is


a Piece belongs to the opponent; if not return false;

3) decide the movement from Block[fromX][fromY] to Block[toX][toY] is valid


according to the regulation of different Pieces (e.g. Root moves straight routes,
and Bishop moves diagonal routes) respectively, as well as considering the other
Pieces which might be hinders in the route.
So, we need to pass the reference of Block[][] board to
thePiece.isValidMove(board, fromX, fromY, toX, toY).

4) function viod play() rounds the players' actions one by one


1. public class Game {
2. private Player black, white;
3. private Block[][] board;
4.
5. private void initialize() {
6.
black = new Player();
7.
white = new Player();
8.
//...
9.
board[0][0] = new Block(new Rook(black));
10. board[0][1] = new Block(new Knight(black));
11. //...
12. board[7][6] = new Block(new Knight(white));
13. board[7][7] = new Block(new Rook(white));
14. }
15.
16. private boolean isEnd() {
17. return !(white.getResult() && black.getResult());
18. }

19.
20. public Piece getPiece(int x, int y) {
21. return board[x][y].getPiece();
22. }
23.
24. public boolean move(int fromX, int fromY, int toX, int toY, Player player) {
25. Piece piece = board[fromX][fromY].getPiece();
26. if (piece == null) return false;
27. if (!piece.getPlayer().equals(player)) {
28.
return false;
29. }
30. /* where to judge there is not obstacle between [fromX][fromY] and
[toX][toY]?
31.
* here in Game class, or in Piece class? */
32. if (!piece.isValidMove(board, fromX, fromY, toX, toY)){
33.
return false;
34. }
35.
36. /* kill? */
37. Block dest = board[toX][toY];
38. if (dest.getPiece() == null) {
39.
return true;
40. }
41. if (dest.getPiece() != null &&
42.
!dest.getPiece().getPlayer().equals(player)) {
43.
//TODO KILL
44.
Player opponent = dest.getPiece().getPlayer();
45.
// Checkmate
46.
if (dest.getPiece() instanceof King) {
47.
opponent.setResult(false);
48.
}
49.
return true;
50. }
51. return false;
52. }
53.
54. public void play() {
55. boolean gameOver = false;
56. while (!gameOver) {
57.
while (!move(0,0,2,1,white));
58.
gameOver = isEnd();
59.
while (!move(0,0,2,1,black));
60.
gameOver = isEnd();
61. }
62. }
63.}

Vous aimerez peut-être aussi