Help me making a tic tac toe program ( and understanding arrays )

This is a discussion on Help me making a tic tac toe program ( and understanding arrays ) within the C++ Programming forums, part of the General Programming Boards category; This is the exercise I'm doing: Write a two-player tic-tac-toe game, allowing two humans to play against each other; use ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77

    Help me making a tic tac toe program ( and understanding arrays )

    This is the exercise I'm doing:

    Write a two-player tic-tac-toe game, allowing two humans to play against each other; use
    enums when possible to represent the values of the board.


    But right now I'm more interested in using arrays in this tic-tac-toe program.

    So here's the code I've came up so far:

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int board[3][3];
    void startGAME();
    
    
    string player1;
    string player2;
    
    
    int main ()
    {   
    	
    
    
        cout << "You're now playing tic tac toe. \n";
    	cout << "Player1 name: ";
    	getline (cin, player1);
    	cout << endl;
    	cout << "Player2 name: ";
    	getline (cin, player2 );
    	cout << endl;
    
    
    	cout << "Start game? y/n ";
    	string yesORno;
    	getline (cin, yesORno );
    	if (yesORno == "yes" )
    	{	startGAME();      }
    	else if (yesORno == "no" )
    	{
    		cout << "Exiting game... \n";
    		system ("pause");
    		return 0;
    	}
    
    
    	else 
    	{	cout << "Invalid choice. \n";   }
    
    
    
    
    
    
    	
    
    
     system("pause");
     return 0;
    
    
    
    
    }
    
    
    
    
    
    
    int board[3][3]
    { 
    	 cout << [0][0] << "|" << [0][1] << "|" << [0][2] << endl;
    	 cout << "_____"<< "|" << "_____"<< "|" << "_____"<< endl;
    	 cout << [1][0] << "|" << [1][1] << "|" << [1][2] << endl;
    	 cout << "_____"<< "|" << "_____"<< "|" << "_____"<< endl;
    	 cout << [2][0] << "|" << [2][1] << "|" << [2][2] << endl;
    
    
    }
    
    
    void startGAME()
    {
    	cout << "Player1 take O and Player2 take X. \n";
    
    
    	int turn, gameOVER(true), gameWIN, gameDRAW, row, column;
    
    
    	
    
    
    	do
    	{
    		cout << "Player1 first. Choose which row and which column. \n";
    
    
    		board[3][3];
    		
    		cout << "Row: ";
    		cin  >> row;
    
    
    		cout << "Column: ";
    		cin >> column;
    
    
    		for ( row = 0 ; row < 3 ; row++)
    		{
    			for ( column = 0 ; column < 3 ; column++ )
    			{
    
    
    				board [row][column] = 'X';
    			}
    
    
    
    
    		}
    	}
    
    
    	while (!gameOVER);
    }



    I got this compiler error:

    1>------ Build started: Project: 42, Configuration: Debug Win32 ------
    1> 424.cpp
    1>c:\users\user\documents\visual studio 2010\projects\42\42\424.cpp(52): error C2470: 'board' : looks like a function definition, but there is no parameter list; skipping apparent body
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Meerul264 View Post
    Code:
    int board[3][3]
    { 
    	 cout << [0][0] << "|" << [0][1] << "|" << [0][2] << endl;
    	 cout << "_____"<< "|" << "_____"<< "|" << "_____"<< endl;
    	 cout << [1][0] << "|" << [1][1] << "|" << [1][2] << endl;
    	 cout << "_____"<< "|" << "_____"<< "|" << "_____"<< endl;
    	 cout << [2][0] << "|" << [2][1] << "|" << [2][2] << endl;
    }
    I don't know why you think you can do that, but you simply cannot. You also seem to be doing several other things that make no sense, like not actually assigning the player name strings with any values.

    Honestly, I'd suggest you get an introductory C++ book and go through it. I can recommend Thinking in C++ (older editions are free and available on the web) which covers design of programs as well as just the language.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Brunei
    Posts
    77
    Alright thanks... I think I'm not through with arrays yet.

    And for the player names I thought I could use them when they change turn. Anyway I really should read more on arrays. Sorry.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I would recommend this.
    Also, make sure you read up on C++ arrays (std::array) as compared to the legacy C arrays you're using now.
    And a hint, maybe. If you want some logic to print out the board you can either a) make a function to do it and call it or b) encapsulate the board into a class and overload the << operator.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help understanding basic arrays
    By jpatrick in forum C Programming
    Replies: 9
    Last Post: 03-15-2012, 03:17 PM
  2. help understanding pointers and arrays
    By cyclone123 in forum C Programming
    Replies: 11
    Last Post: 02-03-2011, 01:11 PM
  3. Need help understanding problem with arrays
    By rob90 in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 05:22 AM
  4. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 03:24 AM
  5. Im Having Problems Understanding The Arrays
    By centaur in forum C Programming
    Replies: 8
    Last Post: 12-19-2005, 09:14 PM

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