Thread: Problems with my arrays in this program

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Problems with my arrays in this program

    Please look at this and tell me what I'm doing wrong. I fear that I am not seeing the right things and 'fixing things' that aren't the problem. I need to stop before I confuse myself out of all the things I think I've learned.

    TIA

    Code:
    #pragma once
    
    //Header file - this is where all of the declarations are made.
    
    class TicTacToe
    {
    public:
    	TicTacToe(void);
    	~TicTacToe(void);
    	void InitBoard(char board[][]);
    	void PrintBoard(char board[][]);
    	void DrawBoard();
    	void CheckHoriz(int board[][]);
    	void CheckVert(int board[][]);
    	void CheckDiag(int board[][]);
    	void CheckDraw(int turn);
    	void EnterMove();
    	void PlayTurn(int turn);
    
    private:
    	//Private variables cannot be directly accessed
    	//by the outside world.
    	char X;
    	char O;
    	char board[][];
    	int board[][]
    	int turn;
    	int x;
    	int y;
    	int col;
    	int row;
    };
    
    -------------------------------------------------------------------------------------
    
    #include "StdAfx.h"
    #include ".\TicTacToe.h"
    #using <mscorlib.dll>
    using namespace std;
    #include<iostream>
    
    //Implementation file where the methods are actually coded.
    //---------------------------------------------------------------
    void TicTacToe::InitBoard(char board[][])
    {
    	
    	for(row=0;row<3;row++)
    		{
    			for(col=0;col<3;col++)
    			{
    				board[row][col] = '+';
    			}
    		}
    }
    //-----------------------------------------------------------------
    void TicTacToe::PrintBoard(char board[][])
    {
    		for(row=0;row<3;row++)
    		{
    			for(col=0;col<3;col++)
    			{
    				cout<< board[row][col];
    			}
    			cout<<endl;
    		}
    }
    //----------------------------------------------------------------------------------
    char TicTacToe::CheckHoriz(int board[][])
    {
    
     if ((board[0][0]=='X' &&board[0][1]=='X' && board[0][2]=='X')  ||
          (board[1][0]=='X' &&board[1][1]=='X' && board[1][2]=='X') || 
          (board[2][0]=='X' &&board[2][1]=='X' && board[2][2]=='X')) 
    
      {
    	return'X';
      } 
     else
    	 if ((board[0][0]=='O' &&board[0][1]=='O' && board[0][2]=='O')  ||
          (board[1][0]=='O' &&board[1][1]=='O' && board[1][2]=='O') || 
          (board[2][0]=='O' &&board[2][1]=='O' && board[2][2]=='O')) 
    
    	 {
    		return'O';
    	 }
    	return '\O';
    } 
    //-----------------------------------------------------------------------------------------
    
    char TicTacToe::CheckVert(int board[][])
    {
    
     if ((board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='X')  ||
          (board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='X') || 
          (board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='X')) 
      {
    	 return 'X';
      }
     else
    		if	( (board[0][0]=='O' &&board[1][0]=='O' && board[2][0]=='O')  ||
    		(board[0][1]=='O' &&board[1][1]=='O' && board[2][1]=='O') || 
    		(board[0][2]=='O' &&board[1][2]=='O' && board[2][2]=='O'))
      {
    
       return 'O';
      } 
    
    	return'\0';
     
    }
    //-------------------------------------------------------------------------------------
     
    void TicTacToe::CheckDiag(char player)
    {
    
     if ( ((board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X')  ||
          (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X')) 
    
      {
       return 'X';
      } 
     else
    	 if ((board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O')  ||
          (board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O')) 
      {
         return 'O';
      }
      return '\0';
    }
    //------------------------------------------------------------------------------------------
    void TicTacToe::CheckDraw(int turn)
    {
    	if(turn<9)
    	{
    		PlayTurn(turn);
    	}
    	else
    	{
    		cout<<"Draw";
    	}
    }
    //------------------------------------------------------------------------------------------
    
    void TicTacToe::PlayTurn(int turn)
    {
    	
    	if((turn%2) == 0)
    	{
    		cout<<"Player 1's turn";
    		EnterMove();
    	}
    	else
    	{
    		cout<<"Player 2's turn";
    		EnterMove();
    	}
    }
    //---------------------------------------------------------------------------------
    
    void TicTacToe::EnterMove(char player)
    {
    	cout<<"Enter the 2 coords where you want to place your move: "
    	cin<<x<<y;
    		
    	board[x][y]= player;
    	PrintBoard();
    }
    //------------------------------------------------------------------------------------
    
    
     void TicTacToe::PlayTurn(int turn, char p1, char p2)
     {
     
    	if((turn % 2) == 0)
    	{
    		cout<<"Player 1's turn";
    		EnterMove(p1);
    	}
    	else
            {
    		cout<<"Player 2's turn";
    		EnterMove(p2);
    	}
     }
    
    //----------------------------------------------------------------------------------
    //----------------------Test Game File-----------------------------------------
    
    #include "stdafx.h"
    #include "TicTacToe.h"
    #include <iostream>
    #using <mscorlib.dll>
     
    using namespace System;
    using namespace std;
     
    #define _tmain main
    void getPlayersLetters(char &p1, char &p2);
     
    int _tmain()
    {
    	//This is how you declare an instance of the class.
        TicTacToe mainTac;
     
        //Our player letters!
        char p1, p2;
     
        //Call our get letters function.
        getPlayersLetters(p1,p2);
     
        //This is how you call them
        mainTac.InitBoard();
    	mainTac.PrintBoard();
     
    	for (int turn = 1; turn < 9; turn++)
    	{   
            mainTac.PlayTurn(turn);
    		mainTac.EnterMove();
    		mainTac.PrintBoard();
    		mainTac.CheckHoriz();
    	}	
    	return 0;
    }
    
    //-------------------------------------------------------------------
     
    void getPlayersLetters(char &p1, char &p2)
    {
        int num;
        cout<<"Please enter 1 for X or 2 to be O: ";
        cin>>num;
     
        if(num==1)
        {
    	   cout <<"player One will be X"<<endl;
    	   p1 = 'X';
    	   p2 = 'O';
        }
        else
        {
    	   cout <<"player Two will be X"<<endl;
    	   p2 = 'X';
    	   p1 = 'O';
        }
    }
    
    //----------------------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the problem?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void InitBoard(char board[][]);
    Specify your array sizes

    > char board[][];
    > int board[][]
    Choose 1 - int or char

    > cin<<x<<y;
    Wrong way round, use >>

    > EnterMove();
    According to your code, this takes parameters.


    General tips
    1. Use spaces (not tabs) for indenting code. It's the only way to make sure your code looks the same to you and all the people who would look at your code.

    2. Write less and compile often. Don't write any more until the code you have is free of compile errors. Then you don't get into the state of having lots of errors to fix all at once.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Thank you for the response.

    As far as I know - I have gone back and tried to change everything that you said.

    it doesn't see my .h file...no matter what I try. I'm posting the code below.

    I'm trying to get it to compile and run and be correct - I am very lost at the moment.

    I will keep in mind the tab suggestion and the future idea of writing less and compiling more often. I cannot help but think there's some fundamental gap Im not getting...

    Code:
    #include "stdafx.h"
    #include ".\TicTacToe.h"
    #include <iostream>
    #using <mscorlib.dll>
     
    using namespace System;
    using namespace std;
     
    #define _tmain main
    void getPlayersLetters(char &p1, char &p2);
     
    int main()
    {
        //This is how you declare an instance of the class.
        TicTacToe mainTac;
     
        //Our player letters!
        char p1, p2;
     
        //Call our get letters function.
        getPlayersLetters(p1,p2);
     
        //This is how you call them
        mainTac.InitBoard();
        mainTac.PrintBoard();
     
        for (int turn = 1; turn < 9; turn++)
       {   
                    mainTac.PlayTurn(turn);
                    mainTac.EnterMove();
                    mainTac.PrintBoard();
                    mainTac.CheckHoriz();
       }	
             return 0;
    }
     
    void getPlayersLetters(char &p1, char &p2)
    {
        int num;
        cout<<"Please enter 1 for X or 2 to be O: ";
        cin>>num;
     
        if(num==1)
        {
               cout <<"player One will be X"<<endl;
               p1 = 'X';
               p2 = 'O';
        }
        else
        {
               cout <<"player Two will be X"<<endl;
               p2 = 'X';
               p1 = 'O';
        }
    }
    
    //==============================================
    //                                       Methods                                                  
    //==============================================
    
    #include "stdafx.h"
    #include ".\TicTacToe.h"
    #using <mscorlib.dll>
    using namespace std;
    #include<iostream>
    
    //Implementation file where the methods are actually coded.
    //---------------------------------------------------------------
    void TicTacToe::InitBoard(char board[3][3])
    {
    	
                    for(row=0;row<3;row++)
                    {
                             for(col=0;col<3;col++)
                            {
                                  board[row][col] = '+';
                            }
                    }
    }
    //-----------------------------------------------------------------
    void TicTacToe::PrintBoard(char board[3][3])
    {
                     for(row=0;row<3;row++)
    		{
                             for(col=0;col<3;col++)
                             {
                                   cout<< board[row][col];
                             }
                        cout<<endl;
    		}
    }
    //----------------------------------------------------------------------------------
    char TicTacToe::CheckHoriz(char player)
    {
     if ((board[0][0]=='X' &&board[0][1]=='X' && board[0][2]=='X')  ||
          (board[1][0]=='X' &&board[1][1]=='X' && board[1][2]=='X') || 
          (board[2][0]=='X' &&board[2][1]=='X' && board[2][2]=='X')) 
    
             {
    	      return'X';
             } 
     else
    	 if ((board[0][0]=='O' &&board[0][1]=='O' && board[0][2]=='O')  ||
          (board[1][0]=='O' &&board[1][1]=='O' && board[1][2]=='O') || 
          (board[2][0]=='O' &&board[2][1]=='O' && board[2][2]=='O')) 
    
    	 {
                   return'O';
    	 }
         return '\0';
    } 
    //-----------------------------------------------------------------------------------------
    
    char TicTacToe::CheckVert(char player)
    {
    
     if ((board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='X')  ||
          (board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='X') || 
          (board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='X')) 
      {
             return 'X';
      }
     else
    		if	( (board[0][0]=='O' &&board[1][0]=='O' && board[2][0]=='O')  ||
    		(board[0][1]=='O' &&board[1][1]=='O' && board[2][1]=='O') || 
    		(board[0][2]=='O' &&board[1][2]=='O' && board[2][2]=='O'))
      {
    
       return 'O';
      } 
    
    return'\0';
     
    }
    //-------------------------------------------------------------------------------------
     
    char TicTacToe::CheckDiag(char player)
    {
    
     if ((board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X')  ||
          (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X')) 
    
      {
       return 'X';
      } 
     else
    	 if ((board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O')  ||
          (board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O')) 
      {
         return 'O';
      }
       return '\0';
    }
    //------------------------------------------------------------------------------------------
    void TicTacToe::CheckDraw(int turn)
    {
            char p1, p2;
            if(turn<9)
    	{
             PlayTurn(turn, p1, p2);
            }
            else
            {
                   cout<<"Draw";
            }
    }
    //------------------------------------------------------------------------------------------
    
    void TicTacToe::PlayTurn(int turn, char p1, char p2)
    {
              if((turn%2) == 0)
             {
                    cout<<"Player 1's turn";
                    EnterMove(p1);
             }
             else
            {
                    cout<<"Player 2's turn";
                    EnterMove(p2);
            }
    }
    //---------------------------------------------------------------------------------
    
    void TicTacToe::EnterMove(char player)
    {
            cout<<"Enter the 2 coords where you want to place your move: ";
            cin>>x>>y;
    		
            board[x][y]= player;
            PrintBoard(char board[][]);
    }
    //------------------------------------------------------------------------------------
    
    
     void TicTacToe::PlayTurn(int turn, char p1, char p2)
     {
     
             if((turn % 2) == 0)
            {
                    cout<<"Player 1's turn";
                    EnterMove(p1);
    	}
            else
        {
                    cout<<"Player 2's turn";
                    EnterMove(p2);
            }
     }
    //===============================================
    //                                             Header
    //===============================================
    
    #pragma once
    
    //Header file - this is where all of the declarations are made.
    
    class TicTacToe
    {
    public:
            TicTacToe(void);
            ~TicTacToe(void);
            void InitBoard(char board[3][3]);
            void PrintBoard(char board[3][3]);
            void DrawBoard();
            char CheckHoriz(char player);
            char CheckVert(char player);
            char CheckDiag(char player);
            void CheckDraw(int turn);
            void EnterMove(char player);
            void PlayTurn(int turn, char p1, char p2);
    
    private:
    	//Private variables are encapsulated because they cannot be directly accessed
    	//by the outside world.
            char X;
            char O;
            char board[3][3];
            char player;
            int turn;
            int x;
            int y;
            int col;
            int row;
    };
    Last edited by shadowctr; 11-20-2005 at 05:41 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your EnterMove in main
    Code:
    mainTac.EnterMove();
    still has no arguments.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>it doesn't see my .h file...no matter what I try. I'm posting the code below.

    Is TicTacToe.h in the same directory as your project? If it's in the directory above it, then you probably want "..\TicTacToe.h"

    If board is a member variable, then you shouldn't have to pass an array to PrintBoard or InitBoard.

    Also, in your loops you need to specify the type for the index:
    Code:
    void TicTacToe::InitBoard(char board[3][3])
    {
      for(int row=0;row<3;row++)
    //...
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (int turn = 1; turn < 9; turn++)
    That may not loop the number of times you think it does.

    Code:
    if ((board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X')  ||
          (board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X'))
    You might want to consider a for loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Are you aware you can comment out sections of code with /* and */? Learn it, love it, use it. You need to comment out your whole program, and then uncomment one function and one line at a time, compile what you have and correct any errors. Then uncomment some more code.

    What inspired you to write all that code without doing any testing along the way?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blackjack program, having problems with the ace
    By yigster in forum C Programming
    Replies: 6
    Last Post: 05-07-2009, 06:41 AM
  2. Have a few minor problems trying to finish this program
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 07:00 PM
  3. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  4. Having problems with arrays
    By pokiepo in forum C Programming
    Replies: 4
    Last Post: 06-18-2008, 01:27 AM
  5. problems reading input file into two arrays
    By quasigreat in forum C Programming
    Replies: 2
    Last Post: 05-18-2008, 03:53 PM