Thread: C Programming 2d Array Question

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    11

    C Programming 2d Array Question

    I am set the task of writing part of a C program that will check if the user can move or not.
    The program would use a 2d array

    arrBoard[8][8]
    and counters

    #define EMPTY_SQUARE "E"
    #define WHITE_SQUARE "W"
    #define BMPTY_SQUARE "B"

    I know the basic ideas of moves that can be made in the array

    --------------------

    x+1 | x+1 | x+0 | x-1
    y+0 | y-1 | y-1 | y-1

    x-1 | x+1 | x+0 | x-1
    y+0 | y+1 | y+1 | y+1

    -----------------------

    I was wondering what the best way to check if the player can move is. Even after reading countless books i dont understand how you can read a position on an array.
    Also how to define the edges of the board.

    Im verymuch a newbie. (very basic idea of what im doing)

    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    I forgot this is for the game Othello!

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Heres the code so far... i've implemented the 2D Array but it doesnt seem to do what i want it to do

    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 */

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    11
    Here with the Header File

    Code:
    #include <arpa/inet.h>
    #include <fcntl.h>
    #include <glib.h>
    #include <gtk/gtk.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    
    /*
     * Descriptive string containing name and version number
     */
    #define APPLICATION_NAME_STR        "OTHELLO v1.0C"
    
    /*
     * Descriptive string to indicate the game board
     */
    #define APPLICATION_GAME_BOARD      "BOARD"
    
    /*
     * Descriptive string to indicate the message display
     */
    #define APPLICATION_MSGBOX_STR      "MESSAGES"
    
    /*
     * Descriptive string for button to start a game
     */
    #define APPLICATION_BUTTON_PLAY_STR "PLAY"
    
    /*
     * Descriptive string for button to display information about current game
     */
    #define APPLICATION_BUTTON_INFO_STR "INFO"
    
    /*
     * Descriptive string for button to end a game and exit application
     */
    #define APPLICATION_BUTTON_EXIT_STR "EXIT"
    
    /*
     * Descriptive string for title of dialog window
     */
    #define APPLICATION_DIALOG_STR      "CONTINUE?"
    
    /*
     * Descriptive string for dialog button for user response Yes
     */
    #define APPLICATION_DIALOG_YES_STR  "YES"
    
    /*
     * Descriptive string for dialog button for user response No
     */
    #define APPLICATION_DIALOG_NO_STR   "NO"
    
    /*
     * Descriptive string for dialog asking whether player wants to exit
     */
    #define APPLICATION_DIALOG_QUESTION "Are you sure you want to exit now?"
    
    /*
     * Descriptive string for title of dialog window when asking for opponent host info
     */
    #define APPLICATION_DIALOG_HOST_STR "OPPONENT HOST"
    
    /*
     * Descriptive string for dialog button to connect to opponent host
     */
    #define APPLICATION_DIALOG_HOST_YES "PLAY"
    
    /*
     * Descriptive string for dialog button for cancel play request
     */
    #define APPLICATION_DIALOG_HOST_NO  "CANCEL"
    
    /*
     * Max length permitted in text entry field
     */
    #define TEXT_ENTRY_MAX_LENGTH       128
    
    /*
     * Default width of application window
     */
    #define APPLICATION_WINDOW_WIDTH    350
    
    /*
     * Default height of application window
     */
    #define APPLICATION_WINDOW_HEIGHT   560
    
    /*
     * Default width of dialog window
     */
    #define APPLICATION_DIALOG_WIDTH    250
    
    /*
     * Default height of dialog window
     */
    #define APPLICATION_DIALOG_HEIGHT   80
    
    /*
     * Default value if network socket is invalid or not connected
     */
    #define NETWORK_SOCKET_CLOSED       -1
    
    /*
     * Color of main window background
     */
    #define COLOR_WINDOW_BG             "lightsteelblue3"
    
    /*
     * Color of normal grid backgrounds
     */
    #define COLOR_GRID_BG               "palegreen3"
    
    /*
     * Color of grid backgrounds when mouse is hovering above
     */
    #define COLOR_GRID_HOVER_BG         "gold"
    
    /*
     * Color of grid backgrounds when mouse button is pressed
     */
    #define COLOR_GRID_ACTIVE_BG        "palegreen4"
    
    /*
     * Color of message display background
     */
    #define COLOR_MSGBOX_BG             "azure3"
    
    /*
     * Name of XPM image for use with Play button
     * Requires image file to be in same directory as application
     */
    #define XPM_BUTTON_PLAY             "g_play.xpm"
    
    /*
     * Name of XPM image for use with Info button
     * Requires image file to be in same directory as application
     */
    #define XPM_BUTTON_INFO             "g_info.xpm"
    
    /*
     * Name of XPM image for use with Exit button
     * Requires image file to be in same directory as application
     */
    #define XPM_BUTTON_EXIT             "g_exit.xpm"
    
    /*
     * Name of XPM image to indicate Yes response on dialog window
     * Requires image file to be in same directory as application
     */
    #define XPM_BUTTON_DIALOG_YES       "g_yes.xpm"
    
    /*
     * Name of XPM image to indicate No response on dialog window
     * Requires image file to be in same directory as application
     */
    #define XPM_BUTTON_DIALOG_NO        "g_no.xpm"
    
    /*
     * Name of XPM image to indicate a game square without any discs
     * Requires image file to be in same directory as application
     */
    #define XPM_SQUARE_EMPTY            "g_no_disc.xpm"
    
    /*
     * Name of XPM image to indicate a game square with a black disc
     * Requires image file to be in same directory as application
     */
    #define XPM_SQUARE_BLACK            "g_black_disc.xpm"
    
    /*
     * Name of XPM image to indicate a game square with a white disc
     * Requires image file to be in same directory as application
     */
    #define XPM_SQUARE_WHITE            "g_white_disc.xpm"
    
    
    
    /*
     * Size of game board
     */
    #define BOARD_SIZE                   8
    
    
    
    /*
     * Game event type when a player clicks on the PLAY button
     */
    #define REQUEST_PLAY_EVENT          111
    
    /*
     * Game event type when a player clicks on the INFO button
     */
    #define REQUEST_INFO_EVENT          112
    
    /*
     * Game event type when a player clicks on a square of the game BOARD
     */
    #define REQUEST_BOARD_EVENT         113
    
    /*
     * Game event type when opponent sends message to initialise a new game
     */
    #define MESSAGE_INIT_EVENT          221
    
    /*
     * Game event type when opponent sends message to start playing a new game after initialisation is done
     */
    #define MESSAGE_PLAY_EVENT          222
    
    /*
     * Game event type when opponent sends message to place a new disc on the game board
     */
    #define MESSAGE_BOARD_EVENT         223
    
    /*
     * Game event type when opponent sends message to confirm that player's new disc has been placed on board
     */
    #define MESSAGE_RESPONSE_EVENT      224
    
    /*
     * Game event type when opponent sends message to indicate end of game
     */
    #define MESSAGE_END_EVENT           225
    
    
    
    /*
     * Status value when a game square is empty
     */
    #define EMPTY_SQUARE                331
    
    /*
     * Status value when a game square contains a black disc
     */
    #define BLACK_SQUARE                332
    
    /*
     * Status value when a game square contains a white disc
     */
    #define WHITE_SQUARE                333
    
    
    
    /*
     * gameEventsDispatcher : Main function to handle game events
     *
     * Parameters           : Unique ID number to identify events and help with debugging
     *                        Type of game event
     *                        The row identifier of the active board square
     *                        The column identifier of the active board square
     *                        The IP address and port number of the opponent
     *
     */
    void gameEventsDispatcher( long, int, int, char, char* );
    
    
    /*
     * updateBoard : Show status of game square at specific location on the board
     *
     * Parameters  : Row label
     *               Column label
     *               Game square status
     *
     */
    void updateBoard( int, char, int );
    
    
    /*
     * resetBoard : Reset all game squares on the board to be all empty
     *
     */
    void resetBoard();
    
    
    /*
     * displayMessage : Appends message to game message console
     *
     * Parameters     : Text message to display
     *
     */
    void displayMessage( char* );
    
    
    /*
     * notifyInit : Send message to opponent initiating a game
     *
     * Parameters : String of opponent host IP address
     *
     */
    int notifyInit( char* );
    
    
    /*
     * notifyPlay : Send message to opponent that you are ready to start playing
     *
     */
    int notifyPlay();
    
    
    /*
     * putNewDisc : Place a new disc on a specific location on the game board
     *
     * Parameters : Row label
     *              Column label
     */
    void putNewDisc( int, char );
    
    
    /*
     * respond    : Send confirmation response to opponent's placement of a new disc
     *
     * Parameters : Row label
     *              Column label
     *
     */
    void respond( int, char );
    
    
    /*
     * getPortNumber : Returns port number of current game program
     *
     * Returns port number if server is connected, else returns NETWORK_SOCKET_CLOSED value
     *
     */
    int getPortNumber( void );
    
    
    /*
     * getIPAddress : Returns IP address of current computer
     *
     * Returns string representing the computer IP address
     *
     */
    char* getIPAddress( void );
    
    
    /*
     * endGame    : Send message to opponent indicating end of game
     *
     * Parameters : TRUE if application should also exit after end of game message has been sent
     *
     */
    void endGame( int );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  2. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM