Main Page   Alphabetical List   Data Structures   File List   Globals  

battle_functions.c File Reference

Definitions of functions declared in battle_core.h. More...


Functions

void initialise_battlefield ()
 Function used to initialise the elements of the battlefield array. More...

int getIndexInTable (char *name)
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...


Detailed Description

Definitions of functions declared in battle_core.h.

This file defines all functions in battle_core.h. These functions are being used thoroughly throughout the program
and in an extensive way.


Function Documentation

int attack_or_move int    z,
int    w
 

Detect if a player made an attack or a move.

Parameters:
z  Integer x-coordinate of the second part of the move command
w  Integer y-coordinate of the second part of the move command
Returns:
int returns TRUE (1) if we have an attack and FALSE (0) if we have a move
This function helps us detect if a placement performed by a player is 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 }

int belong_to_player int    player,
int    x,
int    y
 

Detect if piece on battlefield belongs to a specific player.

Parameters:
player  The integer id of the player that we want to check if it is his piece
x  The integer x coordinate of the board that this piece is on
y  The integer y coordinate of the board that this piece is on
Returns:
int returns TRUE (1) if the piece belongs to player, FALSE (0) otherwise
This functions is used by other functions in battle_functions.c. Its functionality allows us to tidily detect, if
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 }

int check_piece_clicked int    player,
int    x,
int    y,
int    z,
int    w
 

Check if piece clicked is moveable.

Parameters:
player  The integer id of the player that made the move
x  The integer x coordinate of the board that this piece is on
y  The integer y coordinate of the board that this piece is on
z  The integer x coordinate of the board that the player wants to move this piece to
w  The integer y coordinate of the board that the player wants to move this piece to
Returns:
int returns TRUE (1) if we have a valid move, FALSE (0) otherwise
This function is the next stage after checking piece ownership by the belong_to_player function.
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 }

int check_scout_move int    x,
int    y,
int    z,
int    w,
int    difference
 

Check if a Scout's move is valid.

Parameters:
x  The integer x coordinate of the board that this piece is on
y  The integer y coordinate of the board that this piece is on
z  The integer x coordinate of the board that the player wants to move this piece to
w  The integer y coordinate of the board that the player wants to move this piece to
difference  The integer calculated as described in the legal_move function
Returns:
int returns TRUE (1) if we have a valid move, FALSE (0) otherwise
This function accomodates for the rules of moving a scout. For a Scout move to be legal, it must move in the correct
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 }

int decide_winner int    x,
int    y,
int    z,
int    w
 

Decides who won a battle.

Parameters:
x  The integer x coordinate of the board that attacking piece is on
y  The integer y coordinate of the board that attacking piece is on
z  The integer x coordinate of the board that defending piece is on
w  The integer y coordinate of the board that defending piece is on
Returns:
int returns an int representing result of attack. Five possibilities:
  1. Return 1: piece on battlefield[x][y] won
  2. Return 2: piece on battlefield[z][w] won
  3. Return 3: the result was a draw
  4. Return 4: piece on battlefield[z][w] was a flag
  5. Return 5: none of the above occurred
Warning:
This function should never return 5.
This function is called whenever it is detected that an attack took place. Its result is used in the poin_Entry_Begin
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 }       

int get_bitmapno char *    name
 

Get the bitmap number of a piece.

Parameters:
name  The string representing the name of the piece
Returns:
int returns the integer identifier of the piece identified by name, -1 otherwise
This function is used to find the integer identifier of a piece corresponding to name. The number returned is then
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 }

int getIndexInTable char *    name
 

00021                                {
00022         
00023         int i;
00024 
00025         for (i=0; i<12; i++){
00026                 if ( strcmp(name , pieces_table[i].name) == 0)
00027                         return i;
00028         }
00029 
00030         return -1;
00031 }

void initialise_battlefield  
 

Function used to initialise the elements of the battlefield array.

Returns:
void
Basic functionality is to assign to each element of the array to a "no" player (i.e. value -1) and rank and name
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 }

int legal_move int    x,
int    y,
int    z,
int    w
 

Check if we have a valid move.

Parameters:
x  The integer x coordinate of the board that this piece is on
y  The integer y coordinate of the board that this piece is on
z  The integer x coordinate of the board that the player wants to move this piece to
w  The integer y coordinate of the board that the player wants to move this piece to
Returns:
int returns TRUE (1) if we have a valid move, FALSE (0) otherwise
This function checks the movement of a piece in order to decide if we have a legal move or not. It focuses on the
legality of coordinates moved. The rules state:
  1. One square at a time, forward, backward or sideways
  2. Can not jump over another piece even if it is a scout
At first, we check if x,y and z,w are different. If they are not then we are trying to move to teh same piece which
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:
If the difference of x+y and z+w is 1 then it is legal else we are trying to move either diagonally or more than one square.

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 }

void perform_move int    player,
int    x1,
int    y1,
int    x2,
int    y2,
int    reveal
 

Used to update data structures after a move.

Parameters:
player  The integer id of the player that made the move
x1  The integer x coordinate of the board that this piece was on
y1  The integer y coordinate of the board that this piece was on
x2  The integer x coordinate of the board that this piece is on
y2  The integer y coordinate of the board that this piece is on
reveal  The integer representing if this piece is revealed or not
Returns:
void
This function is used throughout the game by players, opponents and observers. It represents a move in the battlefield
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 }


Generated on Sat Mar 16 13:12:16 2002 for SLIP 2002 - Stratego2Go by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002