/[projet1]/users/barnsey123/VIKING-CHESS/main-old-6.c
Defence Force logotype

Contents of /users/barnsey123/VIKING-CHESS/main-old-6.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 476 - (show annotations)
Mon May 2 17:30:48 2011 UTC (9 years, 1 month ago) by barnsey123
File MIME type: text/plain
File size: 33284 byte(s)
pre-move pieces 
1 // main.c by Neil Barnes (a.k.a. Barnsey123)
2 // 22-03-2011 prog to draw a grid (for viking game)
3 // 23-03-2011 create new drawgrid function
4 // 01-04-2011 create new drawtile functions and use compact code (char instead of int etc)
5 // 02-04-2011 routine to read keyboard
6 // 06-04-2011 cursor drawing correct
7 // 11-04-2011 request help from DBUG - bug in OSDK. Have banged head against wall for far too long!
8 // 14-04-2011 DBUG fixed OSDK bug! :-)
9 // 15-04-2011 tidied code up
10 // 15-04-2011 tiles as a global variable, (will be used everywhere)
11 // 16-04-2011 Changed numeric settings for board (0=blank, 1=attackers tile, 2=defenders tile, 3=kings tile)
12 // 16-04-2011 drawing players
13 // 17-04-2011 improving tile drawing (saving few bytes in each routine and having different styles)
14 // 19-04-2011 added flashscreen (to flash screen with desired color - e.g. GREEN for OK, RED for !OK)
15 // 19-04-2011 context sensitive piece selection
16 // 19-04-2011 canpiecemove function (can a selected piece MOVE or not?)
17 // 21-04-2011 numerous bug-fixes to do with x,y co-ords vs north-south/east-west array positions
18 // 21-04-2011 brilliant arrow printing function (hover over one of your pieces and press 'P')
19 // 25-04-2011 fixed bugs in arrow printing routine and reduced code required (TOTAL 817 lines versus 957)
20 // 27-04-2011 fixed some other (all?) bugs in arrow printing routines (new, neater routines)
21 // 29-04-2011 saved a whole bunch of code (789 lines)
22 // 29-04-2011 changed flashscreen so that color can be returned to previous color mode
23 // 29-04-2011 Using POINTERS Huzzah! (never thought that would happen!) 786 lines!
24 // 01-05-2011 Once a piece is selected restrict cursor movement to legal destinations
25 // 02-05-2011 handle "skipping" of central square (if square beyond it is available)
26 // 02-05-2011 900 lines of code (including comments) versus 917 (more efficient and more features)
27 // 02-05-2011 907 lines including refinements
28 /* IMMEDIATE TODO LIST
29 *** BUG IN INVERSING KING SQUARE/POSSIBLY OTHERS
30 // MOVE PIECES !!!
31 */
32 #include <lib.h>
33 /******************* Function Declarations ************************/
34 void drawbox(unsigned char,unsigned char,unsigned char, char); // draws a box at x,y length z, fb/bg
35 void drawdiamond(unsigned char, unsigned char, char, char); // draws diamond at x,y size z, fb/bg
36 void drawcursor(unsigned char,unsigned char,unsigned char,unsigned char); // draws cursor at x,y, length z, foreground or background
37 void drawattackertile(unsigned char,unsigned char); // 1 concentric squares
38 void drawtile2(unsigned char,unsigned char); // 4 diamonds (one in each corner)
39 void drawdefendertile(unsigned char,unsigned char); // 3 concentric squares
40 void drawkingtile(unsigned char,unsigned char); // 5 squares (one in each corner, 1 central and inversed
41 void drawtile5(unsigned char,unsigned char); // funky x-type shape
42 void drawtile6(unsigned char,unsigned char, char); // left arrow
43 void drawtile7(unsigned char,unsigned char, char); // right arrow
44 void drawtile8(unsigned char,unsigned char, char); // up arrow
45 void drawtile9(unsigned char,unsigned char, char); // down arrow
46 void inverse(unsigned char, unsigned char, char); // inverse the color in the square
47 void blanksquare(unsigned char, unsigned char); // blank a square with background color
48 void drawpiece(unsigned char,unsigned char, char); // draws piece at x,y, type z
49 void drawtiles(unsigned char,unsigned char,unsigned char); // draws all tiles at board x,y boxsize z (uses draw*tile functions)
50 void drawgrid(unsigned char,unsigned char,unsigned char); // draws grid at board x,y boxsize z
51 void drawboard(unsigned char,unsigned char,unsigned char); // kicks off drawgrid/drawtiles
52 void playerturn(unsigned char,unsigned char,unsigned char, char); // takes user input to move cursor
53 void drawplayers(unsigned char, unsigned char, unsigned char); // draw players
54 void flashscreen(char,char); // flashes screen in selected color for a second or so
55 void fillbox(unsigned char, unsigned char, char); // *** NEEDS ATTENTION *** fills box with color
56 char canpiecemove(char, char); // can a selected piece move? 0=no, 1=yes
57 void printdestinations(unsigned char, unsigned char, char, char, char, char); // print arrows on tiles where a piece can move
58 void printpossiblemoves(unsigned char, unsigned char, char, char, char); // Print possible moves
59 void printarrowsorblanks(unsigned char, unsigned char, char, char, char, char, char); // PRINT ARROWS/BLANK EM OUT
60 void movecursor2(char,char *,char *,unsigned char *,unsigned char *, char, char, char, char);// move cursor routine (using pointers!!!)
61 /****************** GLOBAL VARIABLES *******************************/
62 /* Populate array with tile types
63 Tile types:
64 0=blank
65 1=attacker square
66 2=defender square
67 3=king square
68 */
69 const unsigned char tiles[11][11]={
70 {3,0,0,1,1,1,1,1,0,0,3},
71 {0,0,0,0,0,1,0,0,0,0,0},
72 {0,0,0,0,0,0,0,0,0,0,0},
73 {1,0,0,0,0,2,0,0,0,0,1},
74 {1,0,0,0,2,2,2,0,0,0,1},
75 {1,1,0,2,2,3,2,2,0,1,1},
76 {1,0,0,0,2,2,2,0,0,0,1},
77 {1,0,0,0,0,2,0,0,0,0,1},
78 {0,0,0,0,0,0,0,0,0,0,0},
79 {0,0,0,0,0,1,0,0,0,0,0},
80 {3,0,0,1,1,1,1,1,0,0,3}};
81 /* populate array with places of players
82 Players:
83 0=vacant
84 1=attacker resident
85 2=defender resident
86 3=king resident
87 4=corner square // added 21/04/2011
88 */
89 /* *** PROPER STARTING BOARD (comment out TESTING BOARD below before uncommenting this)
90 char players[11][11]={
91 {4,0,0,1,1,1,1,1,0,0,4},
92 {0,0,0,0,0,1,0,0,0,0,0},
93 {0,0,0,0,0,0,0,0,0,0,0},
94 {1,0,0,0,0,2,0,0,0,0,1},
95 {1,0,0,0,2,2,2,0,0,0,1},
96 {1,1,0,2,2,3,2,2,0,1,1},
97 {1,0,0,0,2,2,2,0,0,0,1},
98 {1,0,0,0,0,2,0,0,0,0,1},
99 {0,0,0,0,0,0,0,0,0,0,0},
100 {0,0,0,0,0,1,0,0,0,0,0},
101 {4,0,0,1,1,1,1,1,0,0,4}};
102 */
103 // ***** TESTING BOARD ****
104 char players[11][11]={
105 {4,0,0,1,1,1,1,1,0,0,4},
106 {0,0,0,0,0,1,0,0,0,0,0},
107 {2,0,0,0,0,0,0,0,0,0,2},
108 {1,0,0,0,0,0,0,0,0,0,1},
109 {1,2,0,0,0,0,0,0,0,0,1},
110 {1,1,0,2,0,0,3,2,0,1,1},
111 {1,0,0,0,2,1,2,0,0,0,1},
112 {0,0,0,0,0,0,0,0,0,0,1},
113 {2,0,0,0,0,0,0,0,1,0,2},
114 {0,1,0,0,0,0,0,0,0,0,0},
115 {4,0,1,0,0,0,1,1,2,0,4}};
116 /****************** MAIN PROGRAM ***********************************/
117 main()
118 {
119 const unsigned char boardx=22; // starting x co-ord of board
120 const unsigned char boardy=12; // starting y co-ord of board
121 const unsigned char boxsize=17; // set boxsize
122 char playertype=2; // player 1=attacker, 2=defender
123 paper(0);
124 ink(5);
125 drawboard(boardx,boardy,boxsize); // draw the board
126 playerturn(boardx,boardy,boxsize,playertype); // player input
127 printf("%c",19); // turn output to screen on
128
129 }
130 /********************* FUNCTION DEFINITIONS ************************/
131 // routine to move the cursor
132 void movecursor2(char mkey, char *ptrew, char *ptrns, unsigned char *ptrcx, unsigned char *ptrcy, char mz, char mode, char xoew, char xons)
133 {
134 /*
135 mkey = code of cursor key pressed 8=left/west, 9=right/east, 10=down/south, 11=up/north
136 ptrew= east-west square (0-10)
137 ptrns= north-south square (0-10)
138 ptrcx= cursor position (x axis)
139 ptrcy= cursor position (y axis)
140 mz = boxsize
141 mode = [0 or 1] 0=unrestricted (move anywhere), 1= restricted (only move to possible destinations)
142 xoew = original east-west board position of selected piece
143 xons = original north-south board position of selected piece
144 */
145 char canmovecursor=0;
146 char multiple=1; // concerning central square (how much to multiply the coords to SKIP the square
147 char z=mz+2; // copy of mz
148 char piecetype=players[xons][xoew]; // determines the piece type that is currently selected (used in mode 1)
149 char xptrns=*ptrns; // copy of NS
150 char xptrew=*ptrew; // copy of EW
151 char skipns=xptrns; // test skip to north/south
152 char skipew=xptrew; // test skip to east/west
153 char centralcheck=0; // for SKIPPING purposes
154 char modeonevalid=0; // is OK for mode 1? 0=no, 1=yes
155 if ((mkey == 8 )&&( (*ptrew)>0)) // west
156 {
157 if ( mode == 0 ) { canmovecursor=1;}
158 xptrew--; // decrement copyew
159 centralcheck=6;
160 skipew-=2;
161 modeonevalid=1;
162 }
163 if ((mkey == 9 )&&( (*ptrew)<10)) // east
164 {
165 if ( mode == 0 ) { canmovecursor=1;}
166 xptrew++;
167 centralcheck=4;
168 skipew+=2;
169 modeonevalid=1;
170 }
171 if ((mkey == 10)&&( (*ptrns)<10)) // south
172 {
173 if ( mode == 0 ) { canmovecursor=1;}
174 xptrns++;
175 centralcheck=4;
176 skipns+=2;
177 modeonevalid=1;
178 }
179 if ((mkey == 11)&&( (*ptrns)>0)) // north
180 {
181 if ( mode == 0 ) { canmovecursor=1;}
182 xptrns--;
183 centralcheck=6;
184 skipns-=2;
185 modeonevalid=1;
186 }
187 if (( mode == 1 ) && ( modeonevalid )) // if not at edge of board
188 {
189 if ( players[xptrns][xptrew] == 0 ) { canmovecursor=1;}
190 if (( piecetype == 3 )&&( tiles[xptrns][xptrew] == 3 )) { canmovecursor=1;} // if KING and corner
191 if (( (xptrns) == xons )&&( (xptrew) == xoew )) { canmovecursor=1;} // if back at start
192 if ((( piecetype != 3)&&( mkey < 10)&&(*ptrew == centralcheck))||(( piecetype != 3)&&( mkey > 9)&&(*ptrns == centralcheck))) // if non-king and directly adjacent to central square
193 {
194 if ( tiles[xptrns][xptrew] == 3 ) // if tile is central square
195 {
196 if (players[skipns][skipew] > 0){canmovecursor=0;}else{multiple=2;} // set to SKIP if possible
197 if ((players[skipns][skipew] > 0)&&(skipns==xons)&&(skipew=xoew)) // if returning to self...
198 {
199 canmovecursor=1;
200 multiple=2;
201 }
202 }
203 }
204 }
205 if (canmovecursor)
206 {
207 drawcursor(*ptrcx,*ptrcy,mz,0); // print blank cursor (effect=remove dots)
208 if ( mkey == 8 ) {(*ptrcx)-=(z*multiple);} // left
209 if ( mkey == 9 ) {(*ptrcx)+=(z*multiple);} // right
210 if ( mkey == 10 ){(*ptrcy)+=(z*multiple);} // down
211 if ( mkey == 11 ){(*ptrcy)-=(z*multiple);} // up
212 drawcursor(*ptrcx,*ptrcy,mz,1); // print dotted cursor
213 if ( mkey == 8 ) {(*ptrew)-=multiple;} // left
214 if ( mkey == 9 ) {(*ptrew)+=multiple;} // right
215 if ( mkey == 10 ){(*ptrns)+=multiple;} // down
216 if ( mkey == 11 ){(*ptrns)-=multiple;} // up
217 }
218 else
219 {
220 if ( mode == 0 ) {flashscreen(1,6);} // flash red , return to cyan
221 if ( mode == 1 ) {flashscreen(1,2);} // flash red , return to green
222 }
223 }
224
225 void inverse(unsigned char ix, unsigned char iy, char iz)
226 {
227 /* Draw an inversed colr box to highlight selected box
228 ix=screen x position
229 iy=screen y position
230 iz=boxsize
231 */
232 char loop=0; // loop counter
233 iz-=3;
234 ix--;
235 for (loop=0;loop<iz;loop++)
236 {
237 curset(ix,iy,3);
238 draw(iz,0,2); // draw inverse line
239 iy++;
240 }
241 }
242 void printpossiblemoves(unsigned char x, unsigned char y, char xew, char xns, char z)
243 {
244 /* kicks off functions that print appropriate arrows at all possible destinations and blanks
245 them out afterwards
246 x = xcoord
247 y = ycoord
248 xew = East-West position on board
249 xns = North-South position on board
250 z = boxsize
251 */
252 char k=0; // key entered
253 printdestinations(x,y,xew,xns,z,0); // print arrows on all destinations
254
255 printf("%c\n\n\n*** Press any key to continue ***%c",19,19);
256 k=getchar();
257 printdestinations(x,y,xew,xns,z,1); // blank out arrows on all destinations
258 }
259 // print arrows or blanks
260 void printarrowsorblanks(unsigned char x, unsigned char y, char xew, char xns, char z, char xblank, char orientation)
261 {
262 unsigned char a=x; // copy of x
263 unsigned char b=y; // copy of y
264 char pns=xns;
265 char pew=xew;
266 char arrow=1; // test to see if OK to place an arrow (0=no, 1=yes)
267 char playertype=players[xns][xew]; // playertype of the prospective piece to move
268 char xplayers=0; // player type at square under test
269 char tiletype=0; // tile type at square under test
270 // orientation 0,1,2,3 = N, S, E, W
271 if ( orientation == 0 ) { xplayers=players[xns-1][xew]; } // check north
272 if ( orientation == 1 ) { xplayers=players[xns+1][xew]; } // check south
273 if ( orientation == 2 ) { xplayers=players[xns][xew+1]; } // check east
274 if ( orientation == 3 ) { xplayers=players[xns][xew-1]; } // check west
275 if ( xplayers == 0 ) // if adjacent square is OK
276 {
277 arrow=1;
278 while ( arrow == 1 ) // keep checking until cannot move
279 {
280 if (( orientation == 0 ) && ( pns > 0 )) // check north
281 {
282 pns--; // decrement provisional north-south player position
283 if ( players[pns][pew] > 0) { arrow = 0;} // !ok if piece occupied or corner square
284 if (( playertype == 3)&&(players[pns][pew] == 4)) { arrow = 1; } // corner ok if king
285 b-=z;
286 }
287 if (( orientation == 1 ) && ( pns < 10 )) // check south
288 {
289 pns++; // increment provisional north-south player position
290 if ( players[pns][pew] > 0) { arrow = 0;} // !ok if piece occupied or corner square
291 if (( playertype == 3)&&(players[pns][pew] == 4)) { arrow = 1; } // corner ok if king
292 b+=z;
293 }
294 if ((orientation == 2 ) && ( pew < 10 )) // check east
295 {
296 pew++; // increment provisional east-west player position
297 if ( players[pns][pew] > 0) { arrow = 0;} // !ok if piece occupied or corner square
298 if (( playertype == 3)&&(players[pns][pew] == 4)) { arrow = 1; } // corner ok if king
299 a+=z;
300 }
301 if ((orientation == 3 ) && ( pew > 0 )) // check west
302 {
303 pew--; // decrement provisional east-west player position
304 if ( players[pns][pew] > 0) { arrow = 0;} // !ok if piece occupied or corner square
305 if (( playertype == 3)&&(players[pns][pew] == 4)) { arrow = 1; } // corner ok if king
306 a-=z;
307 }
308 tiletype=tiles[pns][pew]; // obtain type of tile
309 if (( xblank == 0 ) && ( arrow == 1 )) // if MODE is "draw an arrow"
310 {
311 //if ((pns == 5)&&(pew == 5)){;}
312 //else
313 if ( tiletype < 3 ) // draw arrows on all squares except central/corner square
314 {
315 if ( orientation == 0 ) { drawtile8(a,b,tiletype) ;} // draw NORTH arrow
316 if ( orientation == 1 ) { drawtile9(a,b,tiletype) ;} // draw SOUTH arrow
317 if ( orientation == 2 ) { drawtile7(a,b,tiletype) ;} // draw EAST arrow
318 if ( orientation == 3 ) { drawtile6(a,b,tiletype) ;} // draw WEST arrow
319 }
320 if ((tiletype > 2)&&(playertype == 3)) // only draw arrows on king squares if piece is a king
321 {
322 if ( orientation == 0 ) { drawtile8(a,b,tiletype) ;} // draw NORTH arrow
323 if ( orientation == 1 ) { drawtile9(a,b,tiletype) ;} // draw SOUTH arrow
324 if ( orientation == 2 ) { drawtile7(a,b,tiletype) ;} // draw EAST arrow
325 if ( orientation == 3 ) { drawtile6(a,b,tiletype) ;} // draw WEST arrow
326 }
327 }
328 if (( xblank == 1) && ( arrow == 1 )) // if MODE is "blank an arrow"
329 {
330 blanksquare(a,b); // blank it out
331 if ( tiletype == 1 ) { drawattackertile(a,b); } // draw attacker tile
332 if ( tiletype == 2 ) { drawdefendertile(a,b); } // draw defender tile
333 if ( tiletype == 3 ) { drawkingtile(a,b); } // print king tile
334 }
335 // have we reached the end of the board?
336 if (( orientation == 0 ) && ( pns == 0 )) { arrow=0;} // check north
337 if (( orientation == 1 ) && ( pns == 10 )) { arrow=0;} // check south
338 if (( orientation == 2 ) && ( pew == 10 )) { arrow=0;} // check east
339 if (( orientation == 3 ) && ( pew == 0 )) { arrow=0;} // check west
340 }
341 }
342 }
343 // print destinations
344 void printdestinations(unsigned char x, unsigned char y, char xew, char xns, char z, char blankmode)
345 {
346 /* print appropriate arrows at all possible destinations (or blanks em out)
347 x = xcoord
348 y = ycoord
349 xew = East-West position on board
350 xns = North-South position on board
351 z = boxsize
352 blankmode = blank out square or not 1=yes, 0=no
353 */
354 unsigned char a=x; // copy of x
355 unsigned char b=y; // copy of y
356 char pew=xew; // copy of xew
357 char pns=xns; // copy of xns
358 char arrow=1; // test to see if OK to place an arrow
359 char playertype=players[xns][xew]; // playertype of the prospective piece to move
360 // check north
361 if ( xns > 0 ) { printarrowsorblanks(a,b,pew,pns,z,blankmode,0);} // draws arrows/blanks em out (0=north)
362 // check south
363 if ( xns < 10 ){ printarrowsorblanks(a,b,pew,pns,z,blankmode,1);} // draws arrows/blanks em out (1=south)
364 // check east
365 if ( xew < 10 ){ printarrowsorblanks(a,b,pew,pns,z,blankmode,2);} // draws arrows/blanks em out (2=east)
366 // check west
367 if ( xew > 0 ) { printarrowsorblanks(a,b,pew,pns,z,blankmode,3);} // draws arrows/blanks em out (3=west)
368 }
369 // fill box with color
370 void fillbox (unsigned char x, unsigned char y, char effect)
371 {
372 curset(x+2,y,2);
373 fill(14,1,effect);
374 }
375 // CAN A SELECTED PIECE MOVE?
376 char canpiecemove(char px, char py)
377 {
378 /* returns 0 or 1 depending on wether a piece can move or not
379 px = north-south position on board
380 py = east-west position on board
381 */
382 int route=0; // number of possible routes
383 char piecetype=players[px][py]; // determine TYPE of selected piece (1=attacker, 2=defendr, 3=king)
384 /* for all piece types determine if adjacent square in any direction is blank or not
385 it won't bother checking a particular direction if piece is at edge of board.
386 */
387 if ( px > 0 ) // check north
388 {
389 if ( players[px-1][py] == 0 ) route++;
390 if (( piecetype == 3 )&&(players[px-1][py] == 4 )) route++; // KING: corner square OK
391 }
392 if ( px < 10 ) // check south
393 {
394 if ( players[px+1][py] == 0 ) route++;
395 if (( piecetype == 3 )&&(players[px+1][py] == 4 )) route++; // KING: corner square OK
396 }
397 if ( py < 10 ) // check east
398 {
399 if ( players[px][py+1] == 0 ) route++;
400 if (( piecetype == 3 )&&(players[px][py+1] == 4 )) route++; // KING: corner square OK
401 }
402 if ( py > 0 ) // check west
403 {
404 if ( players[px][py-1] == 0 ) route++;
405 if (( piecetype == 3 )&&(players[px][py-1] == 4 )) route++; // KING: corner square OK
406 }
407 /* In the case that the central square is unnocupied and a piece is adjacent to that square then - for
408 non-KING Pieces only - we need to check to see if the opposite square is occupied or not.
409 ROUTE will be decremented if that piece is occupied (as no piece can occupy the central square except for
410 the King but all pieces can traverse it */
411 if (( piecetype < 3 ) && ( players[5][5] == 0 )) // if not a king and central sqr unoccupied
412 {
413 if (( px == 5 ) && ( py == 4 )) // check east +2
414 {
415 if ( players[5][6] > 0 ) route--; // east occupied, dec route
416 }
417 if (( px == 5 ) && ( py == 6 )) // check west +2
418 {
419 if ( players[5][4] > 0 ) route--; // west occupied, dec route
420 }
421 if (( px == 4 ) && ( py == 5 )) // check south +2
422 {
423 if ( players[6][5] > 0 ) route--; // south occupied, dec route
424 }
425 if (( px == 6 ) && ( py == 5 )) // check north
426 {
427 if ( players[4][5] > 0 ) route--; // north occupied, dec route
428 }
429 }
430 if ( route > 0 ) route=1;
431 return route;
432 }
433 // DRAW ALL THE PLAYERS ON THE BOARD
434 void drawplayers(unsigned char x, unsigned char y, unsigned char z)
435 {
436 /* routine to draw the "playing pieces"
437 Arguments:
438 x=starting x coord
439 y=starting y coord
440 z=boxsize
441 */
442 unsigned char tilex=x; // copy of x: starting tile x position
443 unsigned char row=0; // loop through tiles array by row
444 unsigned char col=0; // loop through tiles array by column
445 tilex+=2; // starting tile x position (offset by 2 to avoid grid lines)
446 y+=2; // starting tile y position (offset by 2 to avoid grid lines)
447 for (row=0;row<11;row++)
448 {
449 for (col=0;col<11;col++)
450 {
451 if ( players[row][col] == 1 ) drawpiece(tilex,y,1); // draw attacker piece
452 if ( players[row][col] == 2 ) drawpiece(tilex,y,2); // draw defenders piece
453 if ( players[row][col] == 3 ) drawpiece(tilex,y,3); // draw king piece
454 tilex+=z; // the x position is advanced to the next square "across" - including blank squares
455 }
456 tilex=x; // reset x
457 tilex+=2; // tilex offset to avoid gridlines
458 y+=z; // advance y position to next "row"
459 }
460 }
461 // DRAWBOX
462 void drawbox(unsigned char x, unsigned char y, unsigned char z, char f)
463 {
464 /* - draw a box at x,y size z
465 x=xcoord for curset
466 y=ycoord for curset
467 z=size of line to draw
468 f= foreground/background 1 or 0, inverse 2, 3=nothing
469 */
470 curset(x,y,3);
471 draw(z,0,f);
472 draw(0,z,f);
473 draw(-z,0,f);
474 draw(0,-z,f);
475 }
476 // DRAW DIAMOND
477 void drawdiamond(unsigned char x, unsigned char y, char z, char f)
478 {
479 /* - draw a diamond at x,y size z, foreground or background
480 x= xcoord
481 y= ycoord
482 z= length of face
483 f= foreground/background (0/1)
484 */
485 curset(x,y,f);
486 draw(z,z,f);
487 draw(-z,z,f);
488 draw(-z,-z,f);
489 draw(z,-z,f);
490 }
491 // DRAWCURSOR
492 void drawcursor(unsigned char x, unsigned char y, unsigned char z, unsigned char f)
493 {
494 /* - draw the cursor at x,y size z, foreground/background color (1 or 0)
495 x=xcoord for curset
496 y=ycoord for curset
497 z=size of line to draw
498 f=foreground/background 1=visible, 0=blank (to overwrite old cursor position)
499 */
500 pattern(170);
501 curset(x,y,f);
502 draw(z,0,f);
503 draw(0,z,f);
504 draw(-z,0,f);
505 draw(0,-z,f);
506 pattern(255);
507 }
508 // blanksquare
509 void blanksquare(unsigned char x, unsigned char y)
510 {
511 /* (erase a square)
512 x=xcoord
513 y=xcoord
514 */
515 unsigned char loop=0; // loop counter
516 unsigned char z=9; // line length
517 for (loop=0;loop<=z;loop++)
518 {
519 curset(x+2,y+2,0);
520 draw(z,0,0);
521 y++; // inc y position
522 }
523 }
524 // drawattackertile
525 void drawattackertile(unsigned char x, unsigned char y)
526 {
527 /* (a series of 1 concentric squares )
528 x=xcoord for drawbox
529 y=xcoord for drawbox
530 */
531 //unsigned char loop=0; // loop counter
532 unsigned char z=13; // line length
533 //for (loop=0;loop<2;loop++)
534 // {
535 drawbox(x,y,z,1); // drawbox at x,y position
536 // z=z-4; // decrease size of line
537 // x+=2; // increase x position
538 // y+=2; // increase y position
539 // }
540 }
541 // TILE2
542 void drawtile2(unsigned char x, unsigned char y)
543 {
544 /* four diamonds
545 x=xcoord for drawbox
546 y=xcoord for drawbox
547 */
548 char loop=0; // loop counter
549 char z=10; // line length
550 drawdiamond(x+3,y,3,1);
551 drawdiamond(x+10,y,3,1);
552 drawdiamond(x+3,y+7,3,1);
553 drawdiamond(x+10,y+7,3,1);
554 }
555 // draw defender tile
556 void drawdefendertile(unsigned char x, unsigned char y)
557 {
558 /* a series of 3 concentric squares
559 x=xcoord for drawbox
560 y=xcoord for drawbox
561 */
562 unsigned char loop=0; // loop counter
563 unsigned char z=13; // line length
564 for (loop=0;loop<3;loop++)
565 {
566 drawbox(x,y,z,1); // drawbox at x,y position
567 z=z-4; // decrease size of line
568 x+=2; // increase x position
569 y+=2; // increase y position
570 }
571 }
572 // draw king tile
573 void drawkingtile(unsigned char x, unsigned char y)
574 {
575 /* ( a series of 5 squares - one in each corner of the tile and one
576 square straddling all 4 ) nice, simple pattern.
577 x=xcoord for drawbox
578 y=xcoord for drawbox
579 */
580 drawbox(x,y,5,1); // box 1
581 drawbox(x+8,y,5,1); // box 2
582 drawbox(x,y+8,5,1); // box 3
583 drawbox(x+8,y+8,5,1); // box 4
584 drawbox(x+2,y+2,9,2); // box 5 (central, larger box, inverse)
585 }
586 // TILE5
587 void drawtile5(unsigned char x, unsigned char y)
588 {
589 /* ( a series of 5 squares - one on outside and four inverse squares
590 x=xcoord for drawbox
591 y=xcoord for drawbox
592 */
593 drawbox(x,y,13,1); // box 1
594 drawbox(x+4,y,5,2); // box 2
595 drawbox(x+8,y+4,5,2); // box 3
596 drawbox(x+4,y+8,5,2); // box 4
597 drawbox(x,y+4,5,2); // box 5 (central, larger box, inverse)
598 }
599 // TILE 6
600 void drawtile6(unsigned char x, unsigned char y, char t)
601 {
602 /* left arrow
603 x=x coord of tile
604 y=y coord of tile
605 t=tiletype
606 */
607 if ( t > 0 ) { blanksquare(x,y); } // erase tile before drawing arrow
608 drawdiamond(x+4,y+4,2,1);
609 curset(x+5,y+6,1);
610 draw(5,0,1);
611 }
612 // TILE 7
613 void drawtile7(unsigned char x, unsigned char y, char t)
614 {
615 /* right arrow
616 x=x coord of tile
617 y=y coord of tile
618 t=type of tile
619 */
620 if ( t > 0 ) { blanksquare(x,y); } // erase tile before drawing arrow
621 drawdiamond(x+9,y+4,2,1);
622 curset(x+8,y+6,1);
623 draw(-5,0,1);
624 }
625 // TILE 8
626 void drawtile8(unsigned char x, unsigned char y, char t)
627 {
628 /* up arrow
629 x=x coord of tile
630 y=y coord of tile
631 t=type of tile
632 */
633 if ( t > 0 ) { blanksquare(x,y); } // erase tile before drawing arrow
634 drawdiamond(x+7,y+3,2,1);
635 curset(x+7,y+6,1);
636 draw(0,5,1);
637 }
638 // TILE 9
639 void drawtile9(unsigned char x, unsigned char y, char t)
640 {
641 /* down arrow
642 x=x coord of tile
643 y=y coord of tile
644 t=type of tile
645 */
646 if ( t > 0 ) { blanksquare(x,y); } // erase tile before drawing arrow
647 drawdiamond(x+7,y+7,2,1);
648 curset(x+7,y+8,1);
649 draw(0,-5,1);
650 }
651 // DRAW A PIECE
652 void drawpiece(unsigned char x, unsigned char y, char p)
653 {
654 /* draws the attackers piece
655 x=xcoord for drawbox
656 y=xcoord for drawbox
657 p=piece type 1= attacker, 2= defender, 3=king
658 */
659 unsigned char a=x; // copy of x
660 unsigned char b=y; // copy of y
661 // DRAW ELEMENTS COMMON TO ALL PIECES (ATTACKER only has this bit)
662 char loop=0; // loop counter
663 char z=4; // line length
664 a+=4;
665 b+=2;
666 for (loop=0;loop<4;loop++)
667 {
668 curset(a,b,0); // blank anything there
669 curset(a+1,b,1); // set to first point of line
670 draw(z,0,1);
671 curset(a+z+1,b,0); // blank anything there
672 z+=2; // increase size of line
673 a--; // decrease x position
674 b++; // increase y position
675 }
676 a++;
677 z=10;
678 curset(a,b,0); // blank anything there
679 draw(z,0,1);
680 curset(a+z+1,b,0); // blank anything there
681 b++;
682 curset(a,b,0); // blank anything there
683 draw(z,0,1);
684 curset(a+z+1,b,0); // blank anything there
685 b++;
686 curset(a,b,0); // blank anything there
687 draw(z,0,1);
688 curset(a+z+1,b,0); // blank anything there
689 for (loop=0;loop<4;loop++)
690 {
691 curset(a,b,0); // blank anything there
692 curset(a+1,b,1); // set to first point of line
693 draw(z,0,1);
694 curset(a+z+1,b,0); // blank anything there
695 z-=2; // decrease size of line
696 a++; // increase x position
697 b++; // increase y position
698 }
699 if ( p == 2 ) // if player type is DEFENDER
700 {
701 a=x+6;
702 b=y+3;
703 drawdiamond(a,b,3,0);
704 drawdiamond(a+1,b,3,0);
705 drawdiamond(a,b+1,3,0);
706 drawdiamond(a+1,b+1,3,0);
707 }
708 if ( p == 3 ) // if player piece is KING
709 {
710 a=x+6;
711 b=y+4;
712 curset(a,b,0);
713 draw(0,5,0);
714 curmov(1,0,0);
715 draw(0,-5,0);
716 a-=2;
717 b+=2;
718 curset(a,b,0);
719 draw(5,0,0);
720 curmov(0,1,0);
721 draw(-5,0,0);
722 }
723 }
724
725
726 // DRAW ALL THE TILES ON THE BOARD
727 void drawtiles(unsigned char x, unsigned char y, unsigned char z)
728 {
729 /* routine to draw the "background tiles"
730 Arguments:
731 x=starting x coord
732 y=starting y coord
733 z=boxsize
734 */
735 unsigned char tilex=x; // copy of x: starting tile x position
736 unsigned char row=0; // loop through tiles array by row
737 unsigned char col=0; // loop through tiles array by column
738 tilex+=2; // starting tile x position (offset by 2 to avoid grid lines)
739 y+=2; // starting tile y position (offset by 2 to avoid grid lines)
740 for (row=0;row<11;row++)
741 {
742 for (col=0;col<11;col++)
743 {
744 if ( tiles[row][col] == 1 ) drawattackertile(tilex,y); // draw attackers tile
745 if ( tiles[row][col] == 2 ) drawdefendertile(tilex,y); // draw defenders tile
746 if ( tiles[row][col] == 3 )
747 {
748 drawkingtile(tilex,y); // draw kings tile
749 }
750 tilex+=z; // the x position is advanced to the next square "across" - including blank squares
751 }
752 tilex=x; // reset x
753 tilex+=2; // tilex offset to avoid gridlines
754 y+=z; // advance tiley position to next "row"
755 }
756 }
757 // DRAWGRID
758 void drawgrid(unsigned char x, unsigned char y, unsigned char z)
759 {
760 /* drawgrid - draws main 11x11 grid
761 x=starting x position
762 y=starting y position
763 z=boxsize
764 */
765 unsigned char a=x; // copy of boardx (board offset x position)
766 unsigned char c=0; // loop counter
767 unsigned char length=z; // copy of boxsize
768 length*=11; // multiply length by 11 (determines length of line to draw)
769 for (c=0;c<=11;c++) // draw vertical lines
770 {
771 curset(a,y,1);
772 draw(0,length,1);
773 a+=z; // add boxsize to a
774 }
775 a=x; // reset a
776 for (c=0;c<=11;c++) // draw horizontal lines
777 {
778 curset(a,y,1);
779 draw(length,0,1);
780 y+=z; // add boxsize to y
781 }
782 }
783 // DRAW THE BOARD
784 void drawboard(unsigned char x, unsigned char y, unsigned char z)
785 {
786 /* drawboard
787 x=starting x coord
788 y=starting y coord
789 z=boxsize
790 */
791 hires();
792 ink(6); // boardcolor 0=black, 1=red, 2=green, 3=yellow, 4=blue, 5=magenta, 6=cyan,7=white
793 drawgrid(x,y,z); // draw the 11x11 gridlines
794 drawtiles(x,y,z); // draw the background tiles
795 drawplayers(x,y,z); // draw the players
796 printf("%c",19); // turn output to screen off
797 }
798 // flashscreen
799 void flashscreen(char c, char r)
800 {
801 /* Colors are as follows:
802 c=color
803 r=return to color
804 0=black, 1=red, 2=green, 3=yellow, 4=blue, 5=magenta, 6=cyan,7=white
805 */
806 unsigned int pause=1500; // flash screen so long...
807 unsigned int p=0; // pause counter
808 ink(c); // flash color
809 for (p=0;p<pause;p++){};
810 ink(r); // back to cyan
811 }
812 // Playerturn -
813 void playerturn(unsigned char x, unsigned char y, unsigned char z, char p ) // filter keyboard input
814 {
815 /*
816 x=x pos of board (top left)
817 y=y pos of board (top left)
818 z=boxsize
819 p=playertype, 1=attacker, 2=defender
820 */
821 unsigned char key=0; // code of key pressed
822 unsigned char xkey=0; // code of key pressed after piece selected
823 char ns=5; // default north/south position of central square
824 char ew=5; // default east/west position of central square
825 unsigned char cx=1+x+(z*ns); // cursor x screen position
826 unsigned char cy=1+y+(z*ew); // cursor y screen position
827 unsigned char ocx=cx; // original x screen position
828 unsigned char ocy=cy; // original y screen position
829 char ons=5; // original ns board position
830 char oew=5; // original ew board position
831 unsigned char cz=z-2; // size of cursor
832 unsigned char canselect=0; // 0=no, 1=yes (is the piece selectable?)
833 char cursormovetype=-1; // -1=no, 0=yes (n,s,e,w) 1=(north/south only), 2=(east/west only)
834 drawcursor(cx,cy,cz,1); // print dotted cursor at central square
835 if ( p==1 ) {printf ("%c\n\n\n* ATTACKERS MOVE: Arrows move cursor.\n X:Select Q:Quit P:Possibles%c",19,19);}
836 if ( p==2 ) {printf ("%c\n\n\n* DEFENDERS MOVE: Arrows move cursor.\n X:Select Q:Quit P:Possibles%c",19,19);}
837 while (key != 81) // repeat until 'Q' pressed
838 {
839 key=getchar(); // get code of pressed key
840 if (( key > 7 ) && ( key < 12 )) { movecursor2(key, &ew, &ns, &cx, &cy, cz, 0,9,9);} // freeform
841 /*******************************************************/
842 /* determine if X or P is selected (to select a piece) */
843 /*******************************************************/
844 if (( key == 88) || ( key == 80)) // if 'X' or 'P' is selected (88=X, 80=P)
845 {
846 canselect=0; // set piece to NOT SELECTABLE
847 if (( p == 1 )&&(players[ns][ew] == 1 )) { canselect=1;} // piece is selectable
848 if (( p == 2 )&&(players[ns][ew] > 1 )) { canselect=1;} // piece is selectable
849 if ( canselect )
850 {
851 if (canpiecemove(ns,ew))
852 {
853 flashscreen(2,6); // flash green
854 if ( key == 80 ) // if P is pressed
855 {
856 printpossiblemoves(cx+1,cy+1,ew,ns,z); // Print possible moves
857 printf ("%c\n\n\nX:Select Q:Quit P:Possibles%c",19,19);
858 }
859 }
860 else
861 {
862 flashscreen(1,6); // flash red
863 canselect=0; // unselectable, cannot move
864 }
865 }
866 else { flashscreen(1,6);} // flash red
867 if (( key == 88 )&&( canselect )) // if piece is SELECTED and CAN move
868 {
869 ink(2); // green to indicate piece is selected
870 printf("%c\n\n\n*** MOVE PIECE ROUTINE ***%c",19,19);
871 inverse(cx+1,cy+1,z); // highlight selected square (inverse color)
872 xkey=0; // ensure xkey at known value
873 // set Original cursor and board position of selected square
874 ocx=cx;
875 ocy=cy;
876 ons=ns;
877 oew=ew;
878 //cursormovetype= -1; // set cursortype to - cannot move cursor
879 while (( xkey != 88 ) && ( xkey != 81)) // move cursor until X or Q selected
880 {
881 xkey=getchar();
882 if (( ons == ns )&&( cursormovetype<0)) { cursormovetype=1; }// cursor allowed north-south
883 if (( oew == ew )&&( cursormovetype<0)) { cursormovetype=2; }// cursor allowed east-west
884 if (( ons == ns )&& (oew == ew )) { cursormovetype=0;} // cursor can move
885 if (( cursormovetype == 2) && (( xkey == 8)||(xkey == 9))) {cursormovetype=-1;}//!move
886 if (( cursormovetype == 1) && (( xkey == 10)||(xkey == 11))){cursormovetype=-1;}//!move
887 if (( cursormovetype == 0) && (( xkey == 8)||(xkey == 9))) {cursormovetype=1;} //move
888 if (( cursormovetype == 0) && (( xkey == 10)||(xkey == 11))){cursormovetype=2;} //move
889 if ( cursormovetype > 0 )
890 {
891 movecursor2(xkey, &ew, &ns, &cx, &cy, cz, 1, oew, ons);// move cursor (restricted)
892 }
893 else { flashscreen(1,2);} // flashscreen red
894 }
895 if ( xkey == 81 ) // Q has been selected, reset cursor to original positions
896 {
897 drawcursor(cx,cy,cz,0); // blank out cursor at current position
898 cx=ocx;
899 cy=ocy;
900 ns=ons;
901 ew=oew;
902 inverse(cx+1,cy+1,z); // inverse square
903 drawcursor(cx,cy,cz,1); // draw cursor at original selected position
904 }
905 }
906 ink(6); // back to cyan
907 }
908 }
909 }

  ViewVC Help
Powered by ViewVC 1.1.26