-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvc_chess.cpp
More file actions
42 lines (35 loc) · 1.24 KB
/
mvc_chess.cpp
File metadata and controls
42 lines (35 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//chess game, player could drag piece to make a movee
//view
//has method for mouse drag, delegate player action to controller
void ChessView::handleMouseDrag(Point start, Point end){
controller->handleMouseDrag(start, end);
}
//controller
//translates user interface even to game model
void ChessController::handleMouseDrag(Point start, Point end){
Piece piece = findPiece(start);
Position position = findBoardPosition(end);
model->movePieceTo(piece, position); //sends it to model to handle game action
}
//model
//logic to handle game action
void ChessModel::movePieceTo(Piece piece, Position position){
if(isValidMove(piece, position)){
//update state of internal fields to put piece in the new position
//position: check other consequences of new piece position
//e.g. if the player won the game, capture an enemy piece
notifyViews();
}
}
void ChessModel::notifyViews(){
//subject implementation
//notifys all observers
for( int i = 0; i < views.size(); i++){
views.at[i].update();
}
}
//each view has concreate implementation of the updated method
void ChessView::update(){
//read the updated state from tthe model
//redraw the game board in the user interface
}