Go to the source code of this file.
Defines | |
#define | BATTLEFIELD_DIMENSIONS 10 |
Functions | |
void | initialise_battlefield () |
Function used to initialise the elements of the battlefield array. More... | |
int | belong_to_player (int player, int x, int y) |
Detect if piece on battlefield belongs to a specific player. More... | |
int | attack_or_move (int z, int w) |
Detect if a player made an attack or a move. More... | |
int | check_piece_clicked (int player, int x, int y, int z, int w) |
Check if piece clicked is moveable. More... | |
int | legal_move (int x, int y, int z, int w) |
Check if we have a valid move. More... | |
int | check_scout_move (int x, int y, int z, int w, int difference) |
Check if a Scout's move is valid. More... | |
int | decide_winner (int x, int y, int z, int w) |
Decides who won a battle. More... | |
void | perform_move (int player, int x1, int y1, int x2, int y2, int reveal) |
Used to update data structures after a move. More... | |
int | get_bitmapno (char *name) |
Get the bitmap number of a piece. More... | |
Variables | |
piece | pieces_table [12] |
Statically defined array. More... | |
battlefield_piece | battlefield [BATTLEFIELD_DIMENSIONS][BATTLEFIELD_DIMENSIONS] |
Main array for a console. More... |
|
This variable is used while defining the battlefield array. The battlefield array is 10x10 which is also the size of |
|
Detect if a player made an attack or a move.
This function is called after we have checked that the piece being moved belongs to the player and that the square that we are trying to place the piece on doesn't. Thus, to detect if we have a move, we check the z,w coordinates on the battlefield. If they refer to an empty slot then it is a move else an attack. For example, if we receive a move(1, 2 , 3 , 4 , 5) and the first tests are passed (checks on if 2,3 belongs to 1, and tests on whether 4,5 doesn't), then if battlefield[4][5] is empty then we move else we attack.
00041 { 00042 00043 if (battlefield[z][w].player == -1) 00044 return FALSE; 00045 else return TRUE; 00046 } |
|
Detect if piece on battlefield belongs to a specific player.
a piece on the board that a player tries to move, belongs to him or not. If it does then he can make a move if the second part of the move (placement) is legal. If teh piece does not belong to him then we have an illegal reference, and thus an illegal move.
00033 { 00034 00035 if (battlefield[x][y].player == player) 00036 return TRUE; 00037 else return FALSE; 00038 } |
|
Check if piece clicked is moveable.
Since when using it we know that the pieces that we are referencing are valid, we now need to check if the pieces we are trying to move are moveable and if the piece which we are trying to move the piece to does not belong to the same player. All pieces in Stratego2Go are moveable apart from a Bomb and a Flag. Should the move be illegal, we output the appropriate debug information.
00049 { 00050 00051 if (belong_to_player(player , x , y) ){ 00052 if ( (strcmp(battlefield[x][y].occupied.name,"Bomb") != 0) && 00053 (strcmp(battlefield[x][y].occupied.name,"Flag") != 0) ){ 00054 if (!(belong_to_player(player , z , w)) )/* if second click does not belong to player */ 00055 return TRUE; 00056 else { /* second click belongs to same player */ 00057 vccPrintPdxDebugInfo("Piece [%d][%d] is yours so you can not attack it\n",z,w); 00058 return FALSE; 00059 } 00060 } else { 00061 vccPrintPdxDebugInfo("Piece [%d][%d] is a %s and can not be moved\n",x,y,battlefield[x][y].occupied.name); 00062 return FALSE; 00063 } 00064 } else { 00065 vccPrintPdxDebugInfo("Piece [%d][%d] (%s) does not belong to you\n",x,y,battlefield[x][y].occupied.name); 00066 return FALSE; 00067 } 00068 } |
|
Check if a Scout's move is valid.
direction and not jump over another piece. Furthermore, a scout can not move and attack at the same time. This function is called from, inside the legal_move function and its results are delegated and outputted as the results of this function.
00106 { 00107 00108 int first_test = 0; /* first test represents the check of if the scout jumped over one piece or not */ 00109 /* if it didn't set to 1 and then test if he moved and attacked */ 00110 int i; 00111 00112 if (z==x) { 00113 if (w > y){ 00114 for (i=y+1; i<w; i++){ 00115 if (battlefield[x][i].player != -1){ /* piece occupied */ 00116 vccPrintPdxDebugInfo("Scout can not jump over another piece"); 00117 return FALSE; 00118 }/* if */ 00119 }/* for */ 00120 } else if ( w < y){ 00121 for (i=w+1; i<y; i++){ 00122 if (battlefield[x][i].player != -1){ /* piece occupied */ 00123 vccPrintPdxDebugInfo("Scout can not jump over another piece"); 00124 return FALSE; 00125 }/* if */ 00126 }/* for */ 00127 }/* if */ 00128 first_test = 1; 00129 } else if (w == y){ 00130 if (z > x){ 00131 for (i=x+1; i<z; i++){ 00132 if (battlefield[i][y].player != -1){ /* piece occupied */ 00133 vccPrintPdxDebugInfo("Scout can not jump over another piece\n"); 00134 return FALSE; 00135 }/* if */ 00136 }/* for */ 00137 } else if ( z < x){ 00138 for (i=z+1; i<x; i++){ 00139 if (battlefield[i][y].player != -1){ /* piece occupied */ 00140 vccPrintPdxDebugInfo("Scout can not jump over another piece\n"); 00141 return FALSE; 00142 }/* if */ 00143 }/* for */ 00144 }/* if */ 00145 first_test = 1; 00146 } else { 00147 vccPrintPdxDebugInfo("The scout can not move diagonally\n"); 00148 return FALSE; 00149 }/* else */ 00150 00151 00152 /* 00153 if we reach this point then teh scout did not jump over another piece 00154 test that he can not move and attack. 00155 if difference > 1 and the piece that he moves to is not empty then illegal 00156 */ 00157 if ( (difference > 1) && (battlefield[z][w].player != -1) ){ 00158 vccPrintPdxDebugInfo("A scout can not move and attack at the same move\n"); 00159 return FALSE; 00160 } else return TRUE; 00161 } |
|
Decides who won a battle.
function in white.c to provide further information to the user and act appropriately on the outcome of the attack. Appart from checking ranks of pieces, it also takes into consideration the type of each piece and the rules of the game. Thus there is special consideration for a spy vs a Marshal attack, a bomb vs a miner etc...
00163 { 00164 00165 if ( strcmp(battlefield[z][w].occupied.name , "Flag") == 0) 00166 return 4; 00167 00168 if ( strcmp(battlefield[z][w].occupied.name , "Bomb") == 0){ 00169 if ( strcmp(battlefield[x][y].occupied.name , "Miner") == 0){ 00170 return 1; 00171 } else return 2; 00172 } 00173 00174 if ( strcmp(battlefield[x][y].occupied.name , "Spy") == 0){ 00175 if ( strcmp(battlefield[z][w].occupied.name , "Marshal") == 0){ 00176 return 1; 00177 } else return 2; 00178 } 00179 00180 vccPrintPdxDebugInfo("%d vs %d\n",battlefield[x][y].occupied.rank , battlefield[z][w].occupied.rank); 00181 if (battlefield[x][y].occupied.rank < battlefield[z][w].occupied.rank) 00182 return 1; 00183 00184 if (battlefield[x][y].occupied.rank > battlefield[z][w].occupied.rank) 00185 return 2; 00186 00187 if (battlefield[x][y].occupied.rank == battlefield[z][w].occupied.rank) 00188 return 3; 00189 00190 00191 return 5; 00192 } |
|
Get the bitmap number of a piece.
passed on to the Graphics module through the first parameter of the DrawSprite command. The integer, is fetched from the pieces_table array defined in the battle_core.h header file.
00212 { 00213 00214 int i; 00215 00216 for ( i = 0; i < 12; i++){ 00217 if ( strcmp(name , pieces_table[i].name) == 0) 00218 return i; 00219 } 00220 00221 return -1; 00222 } |
|
Function used to initialise the elements of the battlefield array.
to something that does not characterise a piece. This is because at the start of the game, no piece on the board is captured by a piece. These are getting occupied one by one as the players place their pieces on the board. This function is automatically called by the poin_entry_Init function in the white.c file, when the game is commenced.
00008 { 00009 int i , j; 00010 00011 for (i=0; i<10; i++){ 00012 for (j=0; j<10; j++){ 00013 battlefield[i][j].occupied.name = "UNOCCUPIED"; 00014 battlefield[i][j].occupied.rank = -1; 00015 battlefield[i][j].occupied.revealed = -1; 00016 battlefield[i][j].player = -1; 00017 } 00018 } 00019 } |
|
Check if we have a valid move.
legality of coordinates moved. The rules state:
is illegal. Then we check the number of squares and direction. If the piece we are referencing is a scout then these rules are different (a Scout can move multiple squares) so we call the check_scout_move function.
To check the direction and number of squares an easy to use method is devised:
00075 { 00076 00077 int first_sum; 00078 int second_sum; 00079 int difference; 00080 00081 first_sum = x + y; 00082 second_sum = z + w; 00083 difference = abs(first_sum - second_sum); 00084 00085 if ((x == z) && (y == w)){ 00086 vccPrintPdxDebugInfo("Moved to same square so that is no move\n"); 00087 return FALSE; 00088 } 00089 00090 if (strcmp(battlefield[x][y].occupied.name,"Scout") == 0){ /* trying to move scout */ 00091 if (check_scout_move(x , y , z , w , difference)) 00092 return TRUE; 00093 } else {/* not a scout */ 00094 if (difference == 1){ /* trying to move to adjacent piece */ 00095 return TRUE; 00096 } else { 00097 vccPrintPdxDebugInfo("A %s can only move one square at a time and not diagonally\n",battlefield[x][y].occupied.name); 00098 return FALSE; 00099 }/* else */ 00100 }/* else */ 00101 00102 return FALSE; 00103 } |
|
Used to update data structures after a move.
array. Whenever there is a move, the x1,y1 becomes empty and its data are moved onto x2,y2 which are the coordinates of the destination square. The reveal parameter is used to pass information from white.c and the game to the data structures operating on the background. The revealed field is set when a piece has to be revealed to the opponent. This occurs after an attack involving this piece has taken place. Immediately after the attack, the reveal param is set to propagate this information.
00198 { 00199 00200 /* perform move in data structures */ 00201 battlefield[x2][y2].occupied = battlefield[x1][y1].occupied; 00202 battlefield[x2][y2].occupied.revealed = reveal; 00203 battlefield[x2][y2].player = player; 00204 00205 /* make piece empty */ 00206 battlefield[x1][y1].occupied.name = "UNOCCUPIED"; 00207 battlefield[x1][y1].occupied.rank = -1; 00208 battlefield[x1][y1].player = -1; 00209 } |
|
Main array for a console.
is a battlefield_piece structure, information provided concerns which player is on it and what is the rank and name of that piece. Every function in the game references this array to obtain information about the game status. Its dimensions are suited to the size of the console for reasons of clarity. Each square on the board corresponds to an element on the battlefield array. i.e. the board with coordinates x=3 y=4 is represented by element battlefield[3][4]. |
|
Initial value: { { "Marshal" , 1}, { "General" , 2}, { "Colonel" , 3}, { "Major" , 4}, { "Captain" , 5}, { "Lieutenant" , 6}, { "Sergeant" , 7}, { "Miner" , 8}, { "Scout" , 9}, { "Spy" , 0}, { "Bomb" , 0}, { "Flag" , 0} }
This array holds the information concerning all the pieces used in the game. This array is accessed from many
|