Printing 2d array problem

This is a discussion on Printing 2d array problem within the C Programming forums, part of the General Programming Boards category; Hi, This an odd problem, I am making a connect 5 game with a 15x15 array but after a player ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Printing 2d array problem

    Hi, This an odd problem, I am making a connect 5 game with a 15x15 array but after a player enters a row and column the board doesn't print [13][14] or [14][0-6] for some reason. Run this code and you will see. The initial printBoard() works fine but after 1 turn this happens, I can't figure out the problem. Thanks

    Code:
    #include <stdio.h>
    
    void printBoard();
    void initBoard();
    void addStone();
    
    char board[14][14];
    int row, col;
    int turn = 1;  //1 = player 1 turn, 2 = player 2 turn
    
    int main()
    {
    	initBoard();
    
    	while(1)
    	{
    	    printBoard();
    	addStone();
    	}
    
        return(0);
    }
    
    void addStone()
    {
    	if(turn == 1)
    		printf("%s", "\nPlayer 1: ");
    	else
    		printf("%s", "\nPlayer 2: ");
    
    	scanf("%d %d", &row, &col);
    
    	if(row == 99 && col == 99)  //To end game early enter 99 99
    		exit(0);
    
    	if(board[row][col] == '-' && row < 15 && row >= 0 && col < 15 && col >= 0)
        	if(turn == 1)
        	{
        		board[row][col] = 'O';
        		turn = 2;
        	}
        	else
        	{
        		board[row][col] = 'X';
        		turn = 1;
        	}
    
        else
        	printf("%s", "Invalid space, go again.\n");
    }
    
    void initBoard()
    {
    	int i,j;
    	for(i=0; i<15; i++)
    		for(j=0; j<15; j++)
    			board[i][j] = '-';
    }
    
    void printBoard()
    {
    	int i,j;
    	int rowColNumbers[15] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4};
    
    	printf("%s", " ");  //Apply one space to align numbers correctly
    
    	for(i=0; i<15; i++)
    		printf("%d", rowColNumbers[i]);
    
    
    	for(i=0; i<15; i++)
    	{
    		printf("%s%d", "\n", rowColNumbers[i]);
    		for(j =0; j<15; j++)
    			printf("%c", board[i][j]);
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    None of those elements exist; your array's last element is [13][13].

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    ahh, thank you. It was throwing me off that the first time the board printed fully but not after the first turn. Thanks again.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Macros becomes very handy on these sutitations. You define a macro to the size of the array and use the macros everywhere in your code, while refering the row and columns. Thats would make it much easier for you.

    Code:
    #define ROW       15
    #define CCOLUMN   15
    
    int array[ROW][COLUMS];
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a 2D array
    By Suchy in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2007, 03:53 PM
  2. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  3. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 07:42 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21