Академический Документы
Профессиональный Документы
Культура Документы
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
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 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.
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.
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.
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;
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.}