Code:
#include "othello.h"
/*Declare The Two Players*/
int row,col;
int playerturn = 1;
int player = 2; // ****Means that no-one can make a move until someone identifies themself as the host****
int board [BOARD_SIZE][BOARD_SIZE] = {0}; // the game console
char g_sBuffer[512];
/*
* gameEventsDispatcher : Main function to handle game events
* eventID : Unique ID number to identify events and help with debugging
* eventType : Indicates type of game event
* eventRow : The row identifier of the active board square
* eventCol : The column identifier of the active board square
* eventData : For REQUEST_PLAY_EVENT, MESSAGE_INIT_EVENT, MESSAGE_PLAY_EVENT event types, *
* : this string indicates the IP address and port number of the opponent
*/
void gameEventsDispatcher( long eventID, int eventType, int eventRow, char eventCol, char *eventData )
{
switch ( eventType )
{
case REQUEST_INFO_EVENT :
sprintf( g_sBuffer, "Your game instance is: %s:%d", getIPAddress(),getPortNumber() );
displayMessage( g_sBuffer );
break;
case REQUEST_PLAY_EVENT :
notifyInit( eventData );
player = 1; // ****Means this is the host and player 1 ****
for(row=0;row<BOARD_SIZE;row++);
{
for(col=0;col<BOARD_SIZE;col++);
{
board [row][col]=EMPTY_SQUARE;
}
board [4]['D'-'A']=WHITE_SQUARE;
board [3]['E'-'A']=WHITE_SQUARE;
board [3]['D'-'A']=BLACK_SQUARE;
board [4]['E'-'A']=BLACK_SQUARE;
/*Places White And Blacks on the Board*/
updateBoard( 5, 'D', board [4]['D'-'A'] );
updateBoard( 4, 'E', board [3]['E'-'A'] );
updateBoard( 4, 'D', board [3]['D'-'A'] );
updateBoard( 5, 'E', board [4]['E'-'A'] );
}
break;
case REQUEST_BOARD_EVENT :
if ((player == 1) && (playerturn == 1))
{
updateBoard( eventRow, eventCol, BLACK_SQUARE );
putNewDisc (eventRow,eventCol);
playerturn = 2;
}
else if ((player == 2) && (playerturn == 2))
{
updateBoard( eventRow, eventCol, WHITE_SQUARE );
putNewDisc (eventRow,eventCol);
playerturn = 1;
}
break;
case MESSAGE_INIT_EVENT :
if ( ! notifyPlay() ) notifyInit( eventData );
/*Displays Initial Message To Show Status Of Game*/
displayMessage( "Game Activated - Lets Begin!" );
break;
case MESSAGE_PLAY_EVENT :
for(row=0;row<BOARD_SIZE;row++);
{
for(col=0;col<BOARD_SIZE;col++);
{
board [row][col]=EMPTY_SQUARE;
}
board [4]['D'-'A']=WHITE_SQUARE;
board [3]['E'-'A']=WHITE_SQUARE;
board [3]['D'-'A']=BLACK_SQUARE;
board [4]['E'-'A']=BLACK_SQUARE;
/*Places White And Blacks on the Board*/
updateBoard( 5, 'D', board [4]['D'-'A'] );
updateBoard( 4, 'E', board [3]['E'-'A'] );
updateBoard( 4, 'D', board [3]['D'-'A'] );
updateBoard( 5, 'E', board [4]['E'-'A'] );
}
break;
case MESSAGE_BOARD_EVENT :
if (player == 1)
{
respond(eventRow,eventCol);
updateBoard( eventRow, eventCol, WHITE_SQUARE ); //**** Updates Board With Whites ****
playerturn = 1; // **** This is the recieving code for when player 2 makes a move, this the turn should go back to 1 ****
}
else if (player == 2)
{
respond(eventRow,eventCol);
updateBoard( eventRow, eventCol, BLACK_SQUARE ); // **** Same as above ****
playerturn = 2; // **** Same as above ****
}
//updateBoard( eventRow, eventCol, player2 );
//displayMessage( "Please Make A Move" );
break;
case MESSAGE_RESPONSE_EVENT :
displayMessage( g_sBuffer );
break;
case MESSAGE_END_EVENT :
sprintf( g_sBuffer, "Your opponent has given up!" );
displayMessage( g_sBuffer );
break;
}
}
/* end of gameEventsDispatcher */