Rev 3924 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1805 | yogev_ezra | 1 | // |
2 | // KReversi.java |
||
3 | // The Othello Game, based on the algorithm of Muffy Barkocy |
||
4 | // (muffy@fish.com). |
||
5 | // |
||
6 | // The strategy is very very simple. The best move for the computer |
||
7 | // is the move that flip more pieces (preferring boards line and |
||
8 | // corners) and give less pieces to move to the opponent. |
||
9 | // |
||
10 | // Author: Alex "Kazuma" Garbagnati (kazuma@energy.it) |
||
11 | // Date: 20 Jan 96 |
||
12 | // L.R.: 26 Jan 96 |
||
13 | // Note: |
||
14 | // |
||
15 | |||
16 | |||
17 | |||
18 | #include |
||
19 | #include |
||
20 | #include |
||
21 | #include |
||
22 | #include |
||
23 | #include |
||
24 | |||
25 | typedef unsigned int u32; |
||
26 | |||
27 | int YSHIFT= 33; |
||
28 | |||
29 | int ENGLISH = 0; // |
||
30 | int ITALIAN = 1; // Languages |
||
31 | int EXTERNAL = 2; // |
||
32 | |||
33 | #define BLACK 0x000000; // |
||
34 | #define BLACK_S 0x333333; // |
||
35 | #define WHITE 0xffffff; // Colors |
||
36 | #define WHITE_S 0xaaaaaa; // |
||
37 | |||
38 | |||
39 | int NOMOVE = 0; // |
||
40 | int REALMOVE = 1; // Type of move |
||
41 | int PSEUDOMOVE = 2; // |
||
42 | |||
43 | int Empty = 0; // |
||
44 | int User = 1; // Board's owners |
||
45 | int Computer = 2; // |
||
46 | |||
47 | #define true 1 |
||
48 | #define false 0 |
||
49 | |||
50 | int UserMove = true; |
||
51 | int GameOver = false; |
||
52 | int CopyWinOn = false; |
||
53 | int StillInitiated = false; // This solve a little |
||
54 | // problem on reinit. |
||
55 | |||
56 | int TheBoard[8][8]; // Board |
||
57 | int Score[8][8]; |
||
58 | int OpponentScore[8][8]; |
||
59 | |||
60 | |||
61 | |||
62 | // |
||
63 | // DrawBoard |
||
64 | // (paint the Othello Board, a 8X8 green square table) |
||
65 | // |
||
66 | // Input: graphic (Graphics) |
||
67 | // Output: none |
||
68 | // Notes: |
||
69 | // |
||
70 | void DrawBoard() { |
||
3924 | Albom | 71 | |
1805 | yogev_ezra | 72 | int i; |
73 | for(i=0;i<=8;i++) |
||
74 | { |
||
75 | __asm__ __volatile__("int $0x40"::"a"(38),"b"(320),"c"(YSHIFT+(YSHIFT+(40*i))*65536+40*i),"d"(0x555555)); |
||
76 | __menuet__line((40*i),YSHIFT,(40*i),353,0x555555); // horizontal |
||
77 | |||
78 | } |
||
79 | |||
80 | } |
||
81 | // End of DrawBoard |
||
82 | |||
83 | |||
84 | // |
||
85 | // DrawPiece |
||
86 | // (paint a piece, black or white, I'm using an 8x8 array, so |
||
87 | // from the input values for rows and cols must be |
||
88 | // subtracted 1) |
||
89 | // |
||
90 | // Input: who (int), |
||
91 | // column (int), |
||
92 | // row (int) |
||
93 | // Output: none |
||
94 | // Notes: |
||
95 | // |
||
96 | void DrawPiece(int Who, int Col, int Row) { |
||
97 | int pCol = (40*(Col-1)+1); |
||
98 | int pRow = YSHIFT+(40*(Row-1)+1); |
||
99 | u32 pColor,pShadow; |
||
100 | |||
101 | if (Who == User) { |
||
102 | pColor = BLACK; |
||
103 | pShadow = BLACK_S; |
||
104 | } else { |
||
105 | pColor = WHITE; |
||
106 | pShadow = WHITE_S; |
||
107 | } |
||
108 | TheBoard[Col-1][Row-1] = Who; |
||
109 | |||
110 | __menuet__bar(pCol+9,pRow+9,19,19,pColor); |
||
111 | } |
||
112 | // End of DrawPiece |
||
113 | |||
114 | |||
115 | // |
||
116 | // MsgWhoMove |
||
117 | // (paint the message informing who's move) |
||
118 | // |
||
119 | // Input: is user ? (int) |
||
120 | // Output: none |
||
121 | // Notes: |
||
122 | // |
||
123 | void MsgWhoMove(int UM) { |
||
124 | } |
||
125 | // End of MsgWhoMove |
||
126 | |||
127 | |||
128 | // |
||
129 | // FlipRow |
||
130 | // (calculate number of pieces are flipped by a move |
||
131 | // and return it. Eventually do the complete or pseudo |
||
132 | // move) |
||
133 | // |
||
134 | // Input: who (int) |
||
135 | // which board (int[][]) |
||
136 | // position col, row (int) |
||
137 | // direction col, row (int) |
||
138 | // make move ? (int) |
||
139 | // |
||
140 | int FlipRow(int Who, int WhichBoard[8][8] , int C, int R, |
||
141 | int CInc, int RInc, int MakeMove) { |
||
142 | int NewCol; |
||
143 | int NewRow; |
||
144 | int Opponent = User + Computer - Who; |
||
145 | int CNT = 0; |
||
146 | |||
147 | NewCol = C - 1; |
||
148 | NewRow = R - 1; |
||
149 | while (true) { |
||
150 | if (((NewCol+CInc) < 0) || ((NewCol+CInc) > 7) || |
||
151 | ((NewRow+RInc) < 0) || ((NewRow+RInc) > 7)) { |
||
152 | return 0; |
||
153 | } |
||
154 | if (WhichBoard[NewCol+CInc][NewRow+RInc] == Opponent) { |
||
155 | CNT++; |
||
156 | NewCol += CInc; |
||
157 | NewRow += RInc; |
||
158 | } else if (WhichBoard[NewCol+CInc][NewRow+RInc] == Empty) { |
||
159 | return 0; |
||
160 | } else { |
||
161 | break; |
||
162 | } |
||
163 | } |
||
164 | if (MakeMove != NOMOVE) { |
||
165 | C--; |
||
166 | R--; |
||
167 | int v; |
||
168 | for(v=0; v<=CNT; v++) { |
||
169 | if (MakeMove == REALMOVE) { |
||
170 | DrawPiece(Who, C+1, R+1); |
||
171 | } else { |
||
172 | WhichBoard[C][R] = Who; |
||
173 | } |
||
174 | C += CInc; |
||
175 | R += RInc; |
||
176 | } |
||
177 | } |
||
178 | return CNT; |
||
179 | } |
||
180 | // End of FlipRow |
||
181 | |||
182 | |||
183 | // |
||
184 | // IsLegalMove |
||
185 | // (verify that the move is legal) |
||
186 | // |
||
187 | // Input: who (int) |
||
188 | // board (int[][]) |
||
189 | // position col, row (int) |
||
190 | // Output: is legal ? (int) |
||
191 | // Notes: |
||
192 | // |
||
193 | int IsLegalMove(int Who, int WhichBoard[8][8] , int C, int R) { |
||
194 | if (WhichBoard[C-1][R-1] != Empty) { |
||
195 | return false; |
||
196 | } |
||
197 | int CInc,RInc; |
||
198 | for (CInc=-1; CInc<2; CInc++) { |
||
199 | for (RInc=-1; RInc<2; RInc++) { |
||
200 | if (FlipRow(Who, WhichBoard, C, R, CInc, RInc, NOMOVE) > 0) { |
||
201 | return true; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | return false; |
||
206 | } |
||
207 | // End of IsLegalMove |
||
208 | |||
209 | |||
210 | // |
||
211 | // MakeMove |
||
212 | // (make the move) |
||
213 | // |
||
214 | // Input: who (int) |
||
215 | // position col, row (int) |
||
216 | // Output: false=EndGame, true=next player (int) |
||
217 | // Notes: |
||
218 | // |
||
219 | int MakeMove(int Who, int C, int R) { |
||
220 | int CInc,RInc; |
||
221 | for (CInc=-1; CInc<2; CInc++) { |
||
222 | for (RInc=-1; RInc<2; RInc++) { |
||
223 | FlipRow(Who, TheBoard, C, R, CInc, RInc, REALMOVE); |
||
224 | } |
||
225 | } |
||
226 | if (IsBoardComplete() || |
||
227 | ((!ThereAreMoves(Computer, TheBoard)) && (!ThereAreMoves(User, TheBoard)))) { |
||
228 | return false; |
||
229 | } |
||
230 | int Opponent = (User + Computer) - Who; |
||
231 | if (ThereAreMoves(Opponent, TheBoard)) { |
||
232 | UserMove = !UserMove; |
||
233 | } |
||
234 | return true; |
||
235 | } |
||
236 | // End of MakeMove |
||
237 | |||
238 | |||
239 | // |
||
240 | // EndGame |
||
241 | // (shows the winning message) |
||
242 | // |
||
243 | // Input: none |
||
244 | // Output: none |
||
245 | // Notes: |
||
246 | // |
||
247 | void EndGame() { |
||
248 | int CompPieces = 0; |
||
249 | int UserPieces = 0; |
||
250 | char *WinMsg_W = "Computer Won"; |
||
251 | char *WinMsg_L = "User Won"; |
||
252 | char *WinMsg_T = "???"; |
||
253 | char *TheMsg; |
||
254 | |||
255 | int StrWidth; |
||
256 | |||
257 | int c,r; |
||
258 | for (c=0; c<8; c++) { |
||
259 | for (r=0; r<8; r++) { |
||
260 | if (TheBoard[c][r] == Computer) { |
||
261 | CompPieces++; |
||
262 | } else { |
||
263 | UserPieces++; |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | if (CompPieces > UserPieces) { |
||
268 | TheMsg = WinMsg_W; |
||
269 | } else if (UserPieces > CompPieces) { |
||
270 | TheMsg = WinMsg_L; |
||
271 | } else { |
||
272 | TheMsg = WinMsg_T; |
||
273 | } |
||
274 | |||
275 | __menuet__write_text(100,8,0xff0000,TheMsg,strlen(TheMsg)); |
||
276 | |||
277 | } |
||
278 | // End of EndGame |
||
279 | |||
280 | |||
281 |