-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess_undo_board.e
executable file
·121 lines (108 loc) · 2.32 KB
/
chess_undo_board.e
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
note
description: "Summary description for {CHESS_UNDO_BOARD}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
CHESS_UNDO_BOARD
inherit
ANY
redefine
out
end
create
make
feature --Constructor
make
do
create board.make_filled (create {EMPTY}.make ("NULL"), 4, 4)
ensure
empty_board:
across 1 |..| 4 is i all
across 1 |..| 4 is j all
board.item (i, j).type ~ "NULL"
end
end
end
feature --Board Implementation
board: ARRAY2[CHESS_PIECE] --multi-dimesional board for storing and representing chess pieces
x: INTEGER --for later use in ETF_MOVES
y: INTEGER --for later use in ETF_MOVES
moves_trigger: INTEGER --signal for initiating ET_MOVES output
feature --Commands
capture(r1:INTEGER; c1: INTEGER; r2: INTEGER; c2: INTEGER)
local
a, b: CHESS_PIECE
do
a := board.item (r1, c1)
create {EMPTY} b.make ("NULL")
board.compare_objects
board.put (b, r1, c1)
board.put (a, r2, c2)
end
print_moves(row: INTEGER_32 ; col: INTEGER_32): STRING
do
create Result.make_empty
across 1 |..| 4 is i loop
Result.append (" ")
across 1 |..| 4 is j loop
if i = row and j = col then
Result.append(board.item (i, j).type)
else
if board.item (row, col).is_valid_move (row, col, i, j) then
Result.append("+")
else
Result.append (".")
end
end
end
if i /= 4 then
Result.append ("%N")
end
end
end
print_board: STRING
do
create Result.make_empty
across 1 |..| 4 is i loop
Result.append (" ")
across 1 |..| 4 is j loop
if board.item (i, j).type ~ "NULL" then
Result.append(".")
else
Result.append (board.item (i, j).type)
end
end
if i /= 4 then
Result.append ("%N")
end
end
end
set_moves_trigger
do
moves_trigger := 1
end
set_x(row : INTEGER)
do
x := row
end
set_y(col : INTEGER)
do
y := col
end
feature --Redefined 'out' feature
out: STRING
do
create Result.make_empty
if moves_trigger = 1 then
Result.append(print_moves(x,y))
moves_trigger := 0
else
Result.append (print_board)
end
end
invariant
--Maximum available slots are '16'
four_by_four_board:
board.count <= 16
end