challenge time is now seconds-per-40-moves
[rattatechess.git] / mattracking.cpp
blobdeb7cdb50543152399cb5414a3ee96a8a1c6bbff
1 /***************************************************************************
2 check.cpp - description
3 -------------------
4 begin : Mon Sep 19 2005
5 copyright : (C) 2005 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #include "board.h"
21 void
22 Board::mat_add(uint8_t piece, uint8_t piece_pos)
24 int p = PIECE_TO_12(piece);
25 int c = mat_tracking[ p ].count;
27 ASSERT(c>=0 && c<=11 && p<12 && p>=0);
29 mat_tracking[ p ].pos[ mat_tracking[ p ].count++ ] = piece_pos;
30 mat_idx[ piece_pos ] = c;
33 void
34 Board::mat_remove(uint8_t piece, uint8_t piece_pos)
36 int p = PIECE_TO_12(piece);
37 int c = mat_tracking[ p ].count;
38 int idx = mat_idx[ piece_pos ];
40 ASSERT(c>=0 && c<=11 && p<12 && p>=0);
42 c--;
43 if(idx<c)
45 mat_tracking[p].pos[idx] = mat_tracking[p].pos[c];
46 mat_idx[ mat_tracking[p].pos[idx] ] = idx;
49 mat_tracking[p].count = c;
52 void
53 Board::mat_move(uint8_t piece, uint8_t from, uint8_t to)
55 int p = PIECE_TO_12(piece);
56 int idx = mat_idx[ from ];
58 ASSERT(p<12 && p>=0);
60 mat_tracking[p].pos[idx] = to;
61 mat_idx[ to ] = idx;
64 void
65 Board::recalc_mat_tracking()
67 for(int j=0;j<12;j++)
68 mat_tracking[j].count = 0;
70 int piece_pos = 128;
71 for(int i=7;i>=0;i--)
73 piece_pos -= 8;
74 for(int j=7;j>=0;j--)
76 piece_pos--;
77 if(data[piece_pos] && data[piece_pos]!=STONE)
79 int p = PIECE_TO_12(data[piece_pos]);
80 int c = mat_tracking[ p ].count;
82 ASSERT(c>=0 && c<=11 && p<12 && p>=0);
84 mat_tracking[ p ].pos[ mat_tracking[ p ].count++ ] = piece_pos;
85 mat_idx[ piece_pos ] = c;
91 void
92 Board::check_mat_tracking()
94 int tot = 0;
95 for(int j=0;j<12;j++)
97 uint8_t p = j%6 + 1 + (j/6 ? WHITE : BLACK);
98 for(int n = 0;n< mat_tracking[ j ].count; n++)
100 ASSERT(!OUT_OF_BOARD(mat_tracking[ j ].pos[n]));
101 ASSERT(data[mat_tracking[ j ].pos[n]] == p);
103 (void)(p);
104 tot += mat_tracking[ j ].count;
107 int piece_pos = 128;
108 for(int i=7;i>=0;i--)
110 piece_pos -= 8;
111 for(int j=7;j>=0;j--)
113 piece_pos--;
114 if(data[piece_pos])
115 tot--;
118 ASSERT(!tot);