Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Writing tic-tac-toe in C++

  1. #1
    Join Date
    Mar 2014
    Beans
    110

    Writing tic-tac-toe in C++

    Hello!

    I decided to get more knowledge about C++ and this seems like a suitable first project.

    I have started from this tutorial: http://www.codeproject.com/Articles/...c-Tac-Toe-Game but then I came to the conclusion that this interface is only build for windows.

    http://www.cplusplus.com/forum/beginner/3041/

    This seems more suitable choice for UNIX-based systems?. Now in this one we are creating tic-tac-toe interface program with computer opponent against the player? The parameters should be simpler than writing a chess engine.

    If someone is interested in teaching this tic-tac-toe program to me it would be cool. We could go through it in parts:
    like first part and what each symbol means:

    #include <iostream>
    using namespace std;

    // This defines who we have playing:
    enum players { draw, player1, player2 };

    int main()
    {
    // This will only display when the program is first run
    cout << "You, are about to witness the program of the century.\n\n";

    // This tracks the number of times each player has won.
    int number_of_wins[ sizeof( players ) ] = {0};

    // And this tells us whether or not to play again.
    bool done = false;

    while (!done)
    {
    // This will display before each game
    cout << "COMMENCE TIC TAC TOE!!!!\n\n"
    "Ladies first, please pick the row then the column (1 through 3).\n\n";

    // Play a game and remember who won (if anyone)
    int winner = playgame();

    // Update it
    number_of_wins[ winner ]++;

    // Display it
    display_winner_stats( number_of_wins, winner );

    done = ask_to_play_again();
    }

    // Finish
    cout << "\n\nThank you for playing!\n";
    return 0;
    }

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Writing tic-tac-toe in C++

    Quote Originally Posted by spacerocket View Post
    Hello!

    We could go through it in parts:[/COLOR] like first part and what each symbol means:
    I do not want to come across as being condescending, but if you require basic assistance for merely understanding the syntax of C++, then perhaps pursuing a Tic-Tac-Toe game should not be your first endeavor. In fact, I would not recommend that you pursue any application that weighs heavily on graphics, much less something that relates to a game. Start with something simple, and then work your way up (and no, you will not be able to learn C++ in 21 days like some people have indicated).

    Here's an online C++ tutorial that might assist you to understand the basics of the language, and more importantly, the syntax.

  3. #3
    Join Date
    Mar 2014
    Beans
    110

    Re: Writing tic-tac-toe in C++

    Hi!

    Thanks for your reply. I thought about buying this book: http://www.cprogramming.com/c++book/?inl=sb but before that it could be useful to learn Structure of a Program and Variables and Types + Basics of C++. I have already written some simple programs like "HELLO WORLD".

    Would it be possible that you assist me on the issue eg. understanding http://www.cplusplus.com/doc/tutorial/variables/ because even if I read it twice I have to say that I don`t understand!

    So step by step guidance by you on this issue Variables and Types so that I understand something (free or not).

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Writing tic-tac-toe in C++

    Quote Originally Posted by spacerocket View Post
    Hi!

    Thanks for your reply. I thought about buying this book: http://www.cprogramming.com/c++book/?inl=sb but before that it could be useful to learn Structure of a Program and Variables and Types + Basics of C++. I have already written some simple programs like "HELLO WORLD".

    Would it be possible that you assist me on the issue eg. understanding http://www.cplusplus.com/doc/tutorial/variables/ because even if I read it twice I have to say that I don`t understand!

    So step by step guidance by you on this issue Variables and Types so that I understand something (free or not).
    A variable is used to store a value so that the value may be referenced (examined) at a later time. Depending on the type of value that needs to be stored, a variable of that appropriate type is declared. It is good practice to name variable using a descriptive name versus using a single-character. For example 'numOfChickens' is better than 'n' or 'noc'. Of course, when it obvious that the variable is a number, the rule can be relaxed (such as when declaring an index in a for-loop).

    Basic (native) types include: char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double and bool.

    In C++, other (complex) types are available, and these are part of the Standard Template Library (STL); for example, string, list, vector, map, etc. These all reside in the std namespace, and hence when using these types, it is good practice to preface the type with the "std::" namespace designation. Optionally, you can employ the use of a "using namespace std" directive, but this not always desirable (especially in header files) and can lead to compiling errors when dealing with large projects in which there might be a type (class) name clash.

    In C++ (and other object-oriented languages), you are also free to define your own classes (types). For example:
    Code:
    struct Person
    {
        std::string name;
        int age;
    };
    
    Person p;
    
    p.name = "Jolly Rancher"
    p.age = 40;
    If you still don't quite understand these basic concepts, you may want to procure a C++ book written by a good author. I cannot recommend any in particular, because frankly I have not held one in my hands in over 12 years.
    Last edited by dwhitney67; April 13th, 2014 at 05:23 PM. Reason: Added bool type; remove char*.

  5. #5
    Join Date
    Mar 2014
    Beans
    110

    Re: Writing tic-tac-toe in C++

    Ok so string is for letters and int for numbers?

    1
    2
    int a;
    float mynumber;


    These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program. ??

    "Each variable needs a name that identifies it and distinguishes it from the others" Ok so in this case the variables were p.name which presents the value "Jolly Rancher" and p.age with value 40. In both cases variable is used to store data.

    If you look at tic-tac-toe code I posted above what are the variables of this function? So in other words all applications eg. tic-tac-toe need to store data to memorize it during the game?

    Basic (native) types include: char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, float, double and char*

    Do you mean this:


    • Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte character. Other types are also provided for wider characters.
    • Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes, and can either be signed or unsigned, depending on whether they support negative values or not.
    • Floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on which of the three floating-point types is used.
    • Boolean type: The boolean type, known in C++ as bool, can only represent one of two states, true or false.


    Here is the complete list of fundamental types in C++:
    Group Type names* Notes on size / precision
    Character types char Exactly one byte in size. At least 8 bits.
    char16_t Not smaller than char. At least 16 bits.
    char32_t Not smaller than char16_t. At least 32 bits.
    wchar_t Can represent the largest supported character set.
    Integer types (signed) signed char Same size as char. At least 8 bits.
    signed short int Not smaller than char. At least 16 bits.
    signed int Not smaller than short. At least 16 bits.
    signed long int Not smaller than int. At least 32 bits.
    signed long long int Not smaller than long. At least 64 bits.
    Integer types (unsigned) unsigned char (same size as their signed counterparts)
    unsigned short int
    unsigned int
    unsigned long int
    unsigned long long int
    Floating-point types float
    double Precision not less than float
    long double Precision not less than double
    Boolean type bool
    Void type void no storage
    Null pointer decltype(nullptr)


    In your example, what is the function of int? how does it differ form char? or is this the answer:

    "Within each of the groups above, the difference between types is only their size (i.e., how much they occupy in memory)"

    How do I know how much each tic-tac-toe needs memory?

    "each compiler implementation may specify the sizes for these types that fit the best the architecture where the program is going to run." I don`t understand.
    Last edited by spacerocket; April 13th, 2014 at 05:13 PM.

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Writing tic-tac-toe in C++

    As a beginner, should not dwell upon how much memory an application will need. If you want to determine the size of a particular type, you can employ the use of the sizeof() macro. For example:
    Code:
    #include <iostream>
    int main()
    {
        std::cout << "The size of an int on my system is: " << sizeof(int) << " bytes." << std::endl;
    }
    Rather than analyze complex applications, you would serve yourself better if you wrote simple applications from the ground up. Do not take someone else's effort to try to comprehend what's going on. Learn on your own, with your own trivial applications.

    If you want to dole around with TTT (Tic-Tac-Toe), then sit back to think about what the game entails, and what variables you will require to keep state of the game. Below are some subtle hints...

    1. You have a 3x3 board, which is initially empty.
    2. You have two players, designated by an X and an O.
    3. Players take turns making moves.
    4. Player moves must be legal (ie. must be within the board space, and must be within an available space on the board).
    5. After each player has moved (or perhaps after a player has moved at least 3 times), check if he is a winner.
    6. Only a total of 9 (3x3) moves are permitted.
    7. After all moves have been played, and there is no winner, then it must be a tie.

    P.S. Earlier I forgot to include 'bool' as one of the system native types in my earlier post. I have amended that post, and also removed the 'char*' type. One can declare a pointer of any type, not just to a char type.

  7. #7
    Join Date
    Mar 2014
    Beans
    110

    Re: Writing tic-tac-toe in C++

    Ok the first step is that I need to write 3x3 board representation...? It should look something like this?

    #include <iostream>
    using namespace std;

    char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
    int checkwin();
    void board();

    int main()
    {
    int player = 1,i,choice;
    char mark;

    do
    {
    board();
    player=(player%2)?1:2;
    cout << "Player " << player << ", enter a number: ";
    cin >> choice;
    mark=(player == 1) ? 'X' : 'O';
    if (choice == 1 && square[1] == '1')
    square[1] = mark;
    else if (choice == 2 && square[2] == '2')
    square[2] = mark;
    else if (choice == 3 && square[3] == '3')
    square[3] = mark;
    else if (choice == 4 && square[4] == '4')
    square[4] = mark;
    else if (choice == 5 && square[5] == '5')
    square[5] = mark;
    else if (choice == 6 && square[6] == '6')
    square[6] = mark;
    else if (choice == 7 && square[7] == '7')
    square[7] = mark;
    else if (choice == 8 && square[8] == '8')
    square[8] = mark;
    else if (choice == 9 && square[9] == '9')
    square[9] = mark;
    else
    {
    cout<<"Invalid move ";
    player--;
    }

    i=checkwin();
    player++;
    }while(i==-1);
    board();
    if(i==1)
    cout<<"==>\aPlayer "<<--player<<" win ";
    else
    cout<<"==>\aGame draw";

    return 0;
    }


    int checkwin()
    {
    if (square[1] == square[2] && square[2] == square[3])
    return 1;
    else if (square[4] == square[5] && square[5] == square[6])
    return 1;
    else if (square[7] == square[8] && square[8] == square[9])
    return 1;
    else if (square[1] == square[4] && square[4] == square[7])
    return 1;
    else if (square[2] == square[5] && square[5] == square[8])
    return 1;
    else if (square[3] == square[6] && square[6] == square[9])
    return 1;
    else if (square[1] == square[5] && square[5] == square[9])
    return 1;
    else if (square[3] == square[5] && square[5] == square[7])
    return 1;
    else if (square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4' && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8' && square[9] != '9')
    return 0;
    else
    return -1;
    }


    void board()
    {
    cout << "\n\n\tTic Tac Toe\n\n";
    cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
    cout << endl;
    cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;
    cout << "_____|_____|" << endl;
    cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;
    cout << "_____|_____|" << endl;
    cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;
    }
    Last edited by spacerocket; April 13th, 2014 at 06:58 PM.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Writing tic-tac-toe in C++

    Place code within CODE tags.

    Also, if you require a board to be 3x3, then why have you declared it as 26x26?

  9. #9
    Join Date
    Mar 2014
    Beans
    110

    Re: Writing tic-tac-toe in C++

    I changed the code above.

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Writing tic-tac-toe in C++

    It seems that you are on the right path. There are a few things you could have done better. For instance, you should employ the use of for-loops when checking for the win. You could also isolate that algorithm that checks for a valid move.

    Here's something I threw together. Compare this with yours.
    Code:
    #include <iostream>
    
    class TicTacToe
    {
    public:
    	enum { PLAYER_X = 'X', PLAYER_O = 'O', AVAILABLE = ' '};
    
    	void newGame()
    	{
    		for (int r = 0; r < 3; ++r)
    		{
    			for (int c = 0; c < 3; ++c)
    			{
    				board[r][c] = AVAILABLE;
    			}
    		}
    	}
    
    	void play()
    	{
    		for (int turns = 0; turns < 9; ++turns)
    		{
    			char player = (turns % 2 == 0 ? PLAYER_X : PLAYER_O);
    			bool legal = false;
    			do
    			{
    				int row = -1;
    				int col = -1;
    
    				std::cout << "\nPlayer " << player << " -- Choose a move..." << std::endl;
    				std::cout << "Enter row and col (e.g. 1 2): ";
    				std::cin  >> row >> col;
    
    				legal = isLegalMove(row-1, col-1);
    
    				if (!legal)
    				{
    					std::cout << "Illegal move!  Try again...\n" << std::endl;
    				}
    				else
    				{
    					board[row-1][col-1] = player;
    
    					displayBoard();
    
    					if (checkForWin(player))
    					{
    						std::cout << "Player " << player << " has won!" << std::endl;
    						exit(0);
    					}
    				}
    			} while (!legal);
    		} 
    		std::cout << "The game ended in a tie!" << std::endl;
    	}
    
    private:
    	void displayBoard()
    	{
    		std::cout << "+-----+-----+-----+\n";
    		for (int r = 0; r < 3; ++r)
    		{
    			std::cout << "|";
    			for (int c = 0; c < 3; ++c)
    			{
    				std::cout << "  " << board[r][c] << "  |";
    			}
    			std::cout << "\n+-----+-----+-----+\n";
    		}
    		std::cout << std::endl;
    	}
    
    	bool isLegalMove(int row, int col)
    	{
    		if (row < 0 || row > 2 || col < 0 || col > 2)
    			return false;
    
    		// return true if cell in board is available, false otherwise
    		return board[row][col] == AVAILABLE;
    	}
    
    	bool checkForWin(char player)
    	{
    		for (int r = 0; r < 3; ++r)
    		{
    			if (board[r][0] == player && board[r][1] == player && board[r][2] == player)
    				return true;
    		}
    		for (int c = 0; c < 3; ++c)
    		{
    			if (board[0][c] == player && board[1][c] == player && board[2][c] == player)
    				return true;
    		}
    		if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
    		    (board[0][2] == player && board[1][1] == player && board[2][0] == player))
    		{
    			return true;
    		}
    		return false;
    	}
    
    	char board[3][3];
    };
    
    int main()
    {
    	TicTacToe ttt;
    
    	ttt.newGame();
    	ttt.play();
    }

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •