Thread: Tic Tac Toe with enum. Assigning a char to an enum value.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40

    Post Tic Tac Toe with enum. Assigning a char to an enum value.

    Code:
    #include <iostream>
    
    
    
    
    using namespace std;
    
    
    
    
    enum signBoard {up_left, up_middle, up_right, left_middle, middle, right_middle, down_left, down_middle, down_right};
    
    
    void playerXturn();
    void playerOturn();
    int cellNumber;
    
    
    int main()
    {
        //Draws the board and shows what number each cell represents.
        cout << up_left << " | " << up_middle << " | " << up_right << endl;
        cout << "---------" << endl;
        cout << left_middle << " | " << middle << " | " << right_middle << endl;
        cout << "---------" << endl;
        cout << down_left << " | " << down_middle << " | " << down_right << endl;
        cout << "Write the number of the cell of which you would like to mark." << endl;
        cout << "Player X turn: ";
    
    
        int i = 0;
        while (i < 4)
        {
            playerXturn();
            cout << up_left << " | " << up_middle << " | " << up_right << endl;
            cout << "---------" << endl;
            cout << left_middle << " | " << middle << " | " << right_middle << endl;
            cout << "---------" << endl;
            cout << down_left << " | " << down_middle << " | " << down_right << endl;
            cout << "Player O turn: ";
            playerOturn();
            cout << up_left << " | " << up_middle << " | " << up_right << endl;
            cout << "---------" << endl;
            cout << left_middle << " | " << middle << " | " << right_middle << endl;
            cout << "---------" << endl;
            cout << down_left << " | " << down_middle << " | " << down_right << endl;
            cout << "Player X turn: ";
            i++;
        }
    
    
        return 0;
    }
    
    
    
    
    
    
    void playerXturn ()
    {
        //Takes input and is supposed to assign a char to the enumerated data type.
        //It doesnt however.
        cin >> cellNumber;
        switch (cellNumber)
        {
        case 0:
            up_left = 'X';
            break;
        case 1:
            up_middle = 'X';
            break;
        case 2:
            up_right = 'X';
            break;
        case 3:
            left_middle = 'X';
            break;
        case 4:
            middle = 'X';
            break;
        case 5:
            right_middle = 'X';
            break;
        case 6:
            down_left = 'X';
            break;
        case 7:
            down_middle = 'X';
            break;
        case 8:
            down_left = 'X';
            break;
    
    
        }
    
    
    }
    void playerOturn ()
    {
        cin >> cellNumber;
        switch (cellNumber)
        {
        case 0:
            up_left = 'O';
            break;
        case 1:
            up_middle = 'O';
            break;
        case 2:
            up_right = 'O';
            break;
        case 3:
            left_middle = 'O';
            break;
        case 4:
            middle = 'O';
            break;
        case 5:
            right_middle = 'O';
            break;
        case 6:
            down_left = 'O';
            break;
        case 7:
            down_middle = 'O';
            break;
        case 8:
            down_left = 'O';
            break;
    
    
        }
    
    
    }
    So the problem I am experiencing is that I dont know how to assign a char to one of my enumerated data types. I kind of know why, ?because it is an integer and I cant just set the enum equal to a char?. So my question is: How do I assign a char to an enumerated type? Any other suggestions of improvement to my other code is also greatly appreciated. There are alot of functions missing, like win condition etc, but I have a basic idea of making those, so I dont necessary need input on those.
    Thank you.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    You need to clearly separate the logical representation of the board in your program, from the output that is shown to the player. Ideally you would only generate the output in one spot, just before it is written to the screen, and in all other parts of the program you would have your board represented in the most logical way, not with char's. What you need to do is to write a function that will accept a game state, and then generate some appropriate output so the player can see what is going on, everywhere else in the program you forget about char's and work with the representation that you have chosen for the game board, in this case enumerations.

    But before you start thinking about all that, you need to learn about arrays, specifically 2 dimensional arrays (ideally, 2D vectors or 2D std::array). Representing the board with 9 differently named variables leads to some horrible code, and i would go so far as to say that it is wrong, this is not the best way to model a tic-tac-toe board, it is not even a moderately acceptable way.

    Also, you don't need to use OOP to solve this, it can be done just fine using a procedural approach. But if you're learning C++ you should learn about OOP at some point, and tic-tac-toe would not be a bad task to try and solve using classes.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Well, I am doing the practice problem in chapter 7 in "Jumping into C++". Where it specifically says that I should try to use enumerations. (I forgot to mention this, sorry.) I can definetly see how using Arrays would be much easier, but that is some chapters away from where I am right now. About your "accepting a game state and generating appropriate output", thank you, it seems much more logical than what I am doing right now, but it seems a little out of my league atm. I dont really know how to do that. Would it be something like creating a board and assigning values to represent what sign should be on that position? That would be easy with an array I guess, but alas, I dont know much about them.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Iceboxes View Post
    Well, I am doing the practice problem in chapter 7 in "Jumping into C++". Where it specifically says that I should try to use enumerations. (I forgot to mention this, sorry.) I can definetly see how using Arrays would be much easier, but that is some chapters away from where I am right now. About your "accepting a game state and generating appropriate output", thank you, it seems much more logical than what I am doing right now, but it seems a little out of my league atm. I dont really know how to do that. Would it be something like creating a board and assigning values to represent what sign should be on that position? That would be easy with an array I guess, but alas, I dont know much about them.
    Quote Originally Posted by Jumping into C++
    Write a two-player tic-tac-toe game; use enums when possible to represent the values of the board
    I'm guessing you mean this assignment? If you re-read the couple of pages preceding this assignment you will see what the author actually intended:

    Quote Originally Posted by Jumping into C++
    Code:
    enum TicTacToeSquare { TTTS_BLANK, TTTS_O, TTTS_X };
    if ( board_position == TTTS_O )
    {
    /* some code */
    }
    ...So the idea is to use an enum to represent the value of a cell in a TTT game; it can either be X, O or empty, what you're doing is using an enum to represent the cells of the board itself, which isn't a good idea. I have no idea why the author suggests TTT as an exercise for the reader before introducing arrays, no sane C++ programmer would attempt to implement TTT using no form of arrays or other data structures. My suggestion is to ditch this book and get a better one, for example "Accelerated C++" which is a great book that is often suggested by members of this board to newcomers. I own a copy myself and it is not a bad way to start out. I realize this might not be what you want to hear after investing in your first C++ book, but if this is indicative of the rest of your current book then you will be spending lots of time with pointless exercises like this one.

    Edit:

    The following chapter on random numbers will have you writing a poker game, still without using arrays. Get out while you can :-)
    Last edited by Neo1; 11-29-2013 at 04:42 PM.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Neo1: email your feedback to Alex.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    I will look up "Accelerated C++". Is it do-able however with the method I was trying to use? How do I keep track of the board if I dont know how to use arrays? Maybe using 9 different variables instead of enums? Anyways, thanks for the help!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An enum is not a variable. An enum declares a type whose only valid* values are those declared inside the enum. A variable stores some data and must be of some type. Hence, it is possible to declare a variable to be of an enum type, but an enum is not a variable.

    *) Simplification, but you can think of it as that.
    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. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  2. enum
    By mouse666666 in forum C Programming
    Replies: 2
    Last Post: 03-30-2010, 12:17 AM
  3. Enum
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2010, 03:59 AM
  4. i fail to convert char * to enum
    By mrprogrmr in forum C Programming
    Replies: 1
    Last Post: 01-07-2005, 06:57 AM
  5. Enum example, plz?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2002, 09:08 PM

Tags for this Thread