Thread: Multi-Dimensional Array Question

  1. #1
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31

    Multi-Dimensional Array Question

    Hello, I am making a simple tic tac toe game, with no AI, it's just for two players. I decided to use a 2 dimensional array for the board. What I want to do is output the board so the player can see it. But would I have to type out each slot so the user can see it, or can I show the whole array to player. LIke this:

    Code:
    void gBoard()
    {
         system("CLS");
         int board[3][3]={1,2,3,4,5,6,7,8,9};
         cout << board[1][1];
         cout << board[1][2];
         cout << board[1][3];
    
    }
    Would I have to keep going like cout << board[1][1], or could I just show the whole board. Please tell me how.

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    With arrays.. you should always try to take advantage of using loops.. many ways you could tackle this problem.. here is one perspective. Take a look at load_grid( ) and see how I used loops to handle loading the bars into the array. I could have used nested for loops... but I decided just to hard code one dimenstion and then use a loop counter to increment through the second dimension (thus loading the grid into the 2D array 'line at a time')

    Code:
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cctype>
    using namespace std;
    
    
    class TicTac
    {
    
    public:
    
    	TicTac();
    	void load_grid();
    	void display_grid();
    	void prompt_player();
    	void update_grid();	
    	void set_turn();
    	bool check_winner();
    	void reset();
    
    private:
    
    	char grid[5][6];
    	char square;
    	int  turn;	
    	int  turn_counter;
    	bool tie;
    };
    
    
    char again();
    
    int main()
    {
    	TicTac Toe;
    
    	Toe.load_grid();
    
    	do{
    		Toe.reset();		
    
    		Toe.display_grid();
    
    		do{	
    			Toe.prompt_player();			
    	
    			Toe.update_grid();	
    			
    			Toe.set_turn();
    
    			Toe.display_grid();			
    
    		}while(!Toe.check_winner());	 
    
    	}while(again()!='N');
    
    	return 0;
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    TicTac::TicTac()
    {
    	for(int row=0; row<5; row++)
    		for(int column=0; column<5; column++)
    
    			grid[row][column]='0';
    
    	turn	= 1;
    	tie     = 0;
    }
    
    
    void TicTac::load_grid()
    {
    
    	for(int row=0;row<5;row+=2)
    	
    		grid[row][1]=grid[row][3]=(char)179;
    
    	for(row=1;row<4;row+=2)
    
    		grid[row][0]=grid[row][1]=grid[row][2]=grid[row][3]=grid[row][4]=(char)196;
    
    	for(row=0;row<6;row++)
    
    		grid[row][5]='\0';
    }
    
    	
    void TicTac::display_grid()
    {
    	int spacer;
    
    	if(turn==1)
    		spacer = 20;	
    	else 
    		spacer = 39;
    
    
    	system("cls");
    
    	if(tie)
    
    		cout << "\a\n\n\t   *** Tie!!  No Winners!!  ***\n";
    
    	else if(check_winner())
    
    		cout << "\a\n\n\t   *** Player " << turn << " weeens! ***\n";
    
    	else 
    	
    		cout << "\n\n\tPlayer 1 = X       Player 2 = O\n" << setw(spacer) << "------------";	
    		
    
    	cout << endl << endl << endl << endl;
    
    	for(int row=0;row<5;row++)
    	
    		cout << setw(25) << grid[row] << endl;
    }
    
    
    void TicTac::prompt_player()
    {
    	cout << endl << endl << endl; 
    
    	cout << "\tPlayer " << turn << ", Enter a Square: ";	
    	cin  >> square;
    	cin.ignore(1000,'\n');
    
    	square=toupper(square);	
    	turn_counter++;
    }
    
    
    void TicTac::update_grid()
    {
    	bool found = false;
    
    	for(int row=0;!found && row<5; row+=2)
    		for(int column=0;!found && column<5;column+=2)
    		{
    			if(grid[row][column]==square)
    			{
    				if(turn==1)				
    					grid[row][column]='X';
    
    				else 
    					grid[row][column]='O';
    
    				found=true;
    			}
    		}
    	
    }
    
    
    void TicTac::set_turn()
    {
    	if(!check_winner())
    	{
    		if(turn==1)
    		   turn= 2;	
    		else
    		   turn= 1;
    	}
    }
    
    
    bool TicTac::check_winner()
    {
    
    
    	//Row Check
    	for(int row=0; row<5; row+=2)
    	{
    		if(grid[row][0]==grid[row][2]&&grid[row][2]==grid[row][4])
    					
    			return true;		
    	}
    
    	//Column Check
    	for(column=0; column<5; column+=2)
    	{
    		if(grid[0][column]==grid[2][column]&&grid[2][column]==grid[4][column])
    					
    			return true;		
    	}
    
    
    	//Diag Check
    	if(  (grid[0][0]==grid[2][2]&&grid[2][2]==grid[4][4]) ||
    	     (grid[0][4]==grid[2][2]&&grid[2][2]==grid[4][0])   )
    				 
    			return true;
    	
    
    	//Tie Check
    	if(turn_counter==9)
    	{			
    		tie	 = true;
    		return true;
    	}
    
    	return false;
    }
    
    
    char again()
    {
    	char choice;
    	cout << "\n\n\n\tWould ye' like to try again? (Y/N): ";
    	cin  >> choice;
            cin.ignore(1000,'\n');
    	cout << endl << endl;
    
    	return toupper(choice);
    }
    
    
    void TicTac::reset()
    {
    	int square_number = '1';
    
    	for(int row=0;row<5;row+=2)
    		for(int column=0;column<5;column+=2)
    
    			grid[row][column]=square_number++;
    
    	turn   = 1;	
    	tie    = false;
    	turn_counter = 0;
    }
    Last edited by The Brain; 08-07-2005 at 03:40 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    That's a bit too confusing for me, since I've been only learning for a little over a month. I haven't gotten to use classes that much, but I guess I should. I will look at classes, since it seems a lot more organized. Thanks for the code. I didn't expect for you to make the whole game.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A short explanation with nothing additonal:

    First, remember that arrays are zero based (in all dimensions), so the valid indiced for:
    int array[3] are : array[0], array[1], and array[2]
    and for
    int grid[2][2] are : grid[0][0], grid[0][1], grid[1][0], and grid[1][1]

    So, to quickly go through one index of an array, you can use a counter variable in a for loop:
    Code:
    for(int i = 0; i != ARRAY_LEN; ++i)
       array[i] = ...;
    So, for a multidimensional array, you would have to nest loops (one for each dimension):
    Code:
    for(int i = 0; i != NUM_ROWS; ++i)
       for(int j = 0; j != NUM_COLS; ++j)
          grid[i][j] = ...;
    So that, at each value of i in the range [0, NUM_ROWS - 1], the nested loop executes, looping j from 0 to NUM_COLS - 1.

    Hope this clarifies things a little.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User LiNeAr's Avatar
    Join Date
    Aug 2005
    Posts
    31
    Yeah that helps, thanks.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I didn't expect for you to make the whole game.
    i made this game like 3 months ago.. just offering my way of doing things. maybe you could learn something or ask questions or make suggestions or something.

    learn == good
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<iomanip>
    #include<cctype>
    using namespace std;
    
    
    class TicTac
    {
    
    public:
    
    	TicTac();
    	void load_grid();
    	void display_grid();
    	void prompt_player();
    	void update_grid();	
    	void set_turn();
    	bool check_winner();
    	void reset();
    
    private:
    
    	char grid[5][6];
    	char square;
    	int  turn;	
    	int  turn_counter;
    	bool tie;
    };
    
    
    char again();
    
    int main()
    {
    	TicTac Toe;
    
    	Toe.load_grid();
    
    	do{
    		Toe.reset();		
    
    		Toe.display_grid();
    
    		do{	
    			Toe.prompt_player();			
    	
    			Toe.update_grid();	
    			
    			Toe.set_turn();
    
    			Toe.display_grid();			
    
    		}while(!Toe.check_winner());	 
    
    	}while(again()!='N');
    
    	return 0;
    }
    
    
    
    //FUNCTION DEFINITIONS
    
    TicTac::TicTac()
    {
    	for(int row=0; row<5; row++)
    		for(int column=0; column<5; column++)
    
    			grid[row][column]='0';
    
    	turn	= 1;
    	tie     = 0;
    }
    
    
    void TicTac::load_grid()
    {
    
    	for(int row=0;row<5;row+=2)
    	
    		grid[row][1]=grid[row][3]=(char)179;
    
    	for(int row=1;row<4;row+=2) //<--------forgot int
    
    		grid[row][0]=grid[row][1]=grid[row][2]=grid[row][3]=grid[row][4]=(char)196;
    
    	for(int row=0;row<6;row++)  //<----forgot int
    
    		grid[row][5]='\0';
    }
    
    	
    void TicTac::display_grid()
    {
    	int spacer;
    
    	if(turn==1)
    		spacer = 20;	
    	else 
    		spacer = 39;
    
    
    	system("cls");
    
    	if(tie)
    
    		cout << "\a\n\n\t   *** Tie!!  No Winners!!  ***\n";
    
    	else if(check_winner())
    
    		cout << "\a\n\n\t   *** Player " << turn << " weeens! ***\n";
    
    	else 
    	
    		cout << "\n\n\tPlayer 1 = X       Player 2 = O\n" << setw(spacer) << "------------";	
    		
    
    	cout << endl << endl << endl << endl;
    
    	for(int row=0;row<5;row++)
    	
    		cout << setw(25) << grid[row] << endl;
    }
    
    
    void TicTac::prompt_player()
    {
    	cout << endl << endl << endl; 
    
    	cout << "\tPlayer " << turn << ", Enter a Square: ";	
    	cin  >> square;
    	cin.ignore(1000,'\n');
    
    	square=toupper(square);	
    	turn_counter++;
    }
    
    
    void TicTac::update_grid()
    {
    	bool found = false;
    
    	for(int row=0;!found && row<5; row+=2)
    		for(int column=0;!found && column<5;column+=2)
    		{
    			if(grid[row][column]==square)
    			{
    				if(turn==1)				
    					grid[row][column]='X';
    
    				else 
    					grid[row][column]='O';
    
    				found=true;
    			}
    		}
    	
    }
    
    
    void TicTac::set_turn()
    {
    	if(!check_winner())
    	{
    		if(turn==1)
    		   turn= 2;	
    		else
    		   turn= 1;
    	}
    }
    
    
    bool TicTac::check_winner()
    {
    
    
    	//Row Check
    	for(int row=0; row<5; row+=2)
    	{
    		if(grid[row][0]==grid[row][2]&&grid[row][2]==grid[row][4])
    					
    			return true;		
    	}
    
    	//Column Check
    	for(int column=0; column<5; column+=2)  // <-----------forgot int
    	{
    		if(grid[0][column]==grid[2][column]&&grid[2][column]==grid[4][column])
    					
    			return true;		
    	}
    
    
    	//Diag Check
    	if(  (grid[0][0]==grid[2][2]&&grid[2][2]==grid[4][4]) ||
    	     (grid[0][4]==grid[2][2]&&grid[2][2]==grid[4][0])   )
    				 
    			return true;
    	
    
    	//Tie Check
    	if(turn_counter==9)
    	{			
    		tie	 = true;
    		return true;
    	}
    
    	return false;
    }
    
    
    char again()
    {
    	char choice;
    	cout << "\n\n\n\tWould ye' like to try again? (Y/N): ";
    	cin  >> choice;
            cin.ignore(1000,'\n');
    	cout << endl << endl;
    
    	return toupper(choice);
    }
    
    
    void TicTac::reset()
    {
    	int square_number = '1';
    
    	for(int row=0;row<5;row+=2)
    		for(int column=0;column<5;column+=2)
    
    			grid[row][column]=square_number++;
    
    	turn   = 1;	
    	tie    = false;
    	turn_counter = 0;
    }
    Also you're missing a check repeat.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 01:35 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  4. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  5. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM