Tic tac toe game help

This is a discussion on Tic tac toe game help within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by samwillc ... So I could use the new values to update the board: int boardPos1 = blank; ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by samwillc View Post
    ...

    So I could use the new values to update the board:

    int boardPos1 = blank;
    int boardPos2 = blank;
    int boardPos3 = blank;
    int boardPos4 = blank;
    int boardPos5 = blank;
    int boardPos6 = cross
    int boardPos7 = blank;
    int boardPos8 = blank;
    int boardPos9 = circle;

    But wouldn't this just update the board like this:

    | | | |
    ------
    | | |1|
    ------
    | | |0|
    Now that just seems weird to me. I would have thought it would have updated the board like so:

    | | | |
    ------
    | | |X|
    ------
    | | |O|

    So that seems good. But there are two additional things. First, if you have variables BoardPos1...BoardPos9, what do you do? You merge them into an array, of course, where the index is the offset!
    So, something like:

    std::array<int, 9> BoardPos;

    But it goes farther. An enum is not an int. It is a type where a variable of that type can only take the values stated in that enum.
    (Yes, you can do implicit conversions between enums and ints, but get that out of your mind now.)
    Therefore, this is wrong:

    int boardPos9 = circle;

    It should look something like

    Code:
    enum class BoardPos_t { X, O, Blank };
    std::array<BoardPos_t, 9> BoardPos;
    // Remember that arrays start from 0, so pos 6 = 5 from 0.
    BoardPos.at(5) = BoardPos_t::X;
    BoardPos.at(8) = BoardPos_t::O;
    Read more about arrays: SourceForge.net: Safer arrays in Cpp - cpwiki
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  2. #17
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by Elysia View Post
    Now that just seems weird to me. I would have thought it would have updated the board like so:

    | | | |
    ------
    | | |X|
    ------
    | | |O|
    Yes, this is how I would expect it too, so I need to find a way to show 0,1 or 2 as a 'X', 'O' or blank. I will look at your post in more detail later and practice more with arrays. I haven't actually done any work from my book on arrays so need to do that to get at least some basic understanding. Thanks for the help, I'll get there in the end, not giving up.
    Last edited by samwillc; 04-24-2013 at 05:09 AM.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Why do you need to show 0, 1, 2, etc as X, O or blank? You use the offset inputted as the user to index yourself in the array.
    Anyway, I would suggest you familiarize yourself with arrays before continuing tic-tac-toe. Be sure to read the link to know more about arrays (later), because I'm sure your book won't mention std::array.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #19
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by Elysia View Post
    Why do you need to show 0, 1, 2, etc as X, O or blank? You use the offset inputted as the user to index yourself in the array.
    Anyway, I would suggest you familiarize yourself with arrays before continuing tic-tac-toe. Be sure to read the link to know more about arrays (later), because I'm sure your book won't mention std::array.
    I will look at the link and try to work this out.

    Quote Originally Posted by Elysia View Post
    Why do you need to show 0, 1, 2, etc as X, O or blank?
    1) If a user inputs a 4
    2) int boardPos4 is updated to a circle (which is enum value 2);
    3) If I now std::cout boardPos4 to show a circle on the board, it wont, it'll show a number 2 (the value of the enum)

    That's how I see it. I don't know why this concept is so difficult for me, everything else was going ok so far!
    Last edited by samwillc; 04-24-2013 at 05:35 AM.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    In response to part of your edited out message:
    Again, keep in mind that an enum is not an int! A value in an enum is not an int. It is not implicitly convertible to an int, so get that out of your mind (again, this is C++11 strongly typed enums; forget all other ones if you have a C++11 compiler). That means you cannot output an enum directly (you can't cout it), and an O is O and X is X, nothing else.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #21
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Sorry, I edited before you posted again.

    Code:
    enum TicTacToeSquare {circle, cross, blank}
    Then what is the value of cross, circle, blank here?

    *EDIT*

    Forget that, saw your earlier post:

    Code:
    enum class BoardPos_t { X, O, Blank };
    I will get back to this later.
    Last edited by samwillc; 04-24-2013 at 05:41 AM.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    You are using old enums. The syntax should be:

    Code:
    enum class TicTacToeSquare {circle, cross, blank}
    Whatever "value" these have is irrelevant. It's just a state. How it's stored in computer memory doesn't matter.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #23
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by Elysia View Post
    Code:
    enum class BoardPos_t { X, O, Blank };
    std::array<BoardPos_t, 9> BoardPos;
    // Remember that arrays start from 0, so pos 6 = 5 from 0.
    BoardPos.at(5) = BoardPos_t::X;
    BoardPos.at(8) = BoardPos_t::O;
    Ah, I think I see.

    Code:
    BoardPos.at(5) = BoardPos_t::X;
    would be written instead of:

    Code:
    int boardPos4 = cross;
    Now, I will look at that link later. There is one more thing that confuses me from Jumping Into C++

    Code:
    enum RainbowColor { //I know, should be class, taken from book as is
    RC_RED, RC_ORANGE, RC_YELLOW, RC_GREEN, RC_BLUE, RC_INDIGO, RC_VIOLET
    };
    
    RainbowColor chosen_color = RC_RED;
    If I can use:

    Code:
    //EXAMPLE A
    BoardPos.at(5) = BoardPos_t::X;
    this is using BoardPos_t directly so to speak, not a variable of BoardPos_t.

    In the book, what is the point/s of having a variable of the new enum type (variable 'chosen_color' above) if you can access the enum values directly like in EXAMPLE A?

    In other words, do you ever need a variable of your custom enum type in C++11? I hope this makes sense.

    Thanks.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Quote Originally Posted by samwillc View Post
    If I can use:

    Code:
    //EXAMPLE A
    BoardPos.at(5) = BoardPos_t::X;
    this is using BoardPos_t directly so to speak, not a variable of BoardPos_t.

    In the book, what is the point/s of having a variable of the new enum type (variable 'chosen_color' above) if you can access the enum values directly like in EXAMPLE A?

    In other words, do you ever need a variable of your custom enum type in C++11? I hope this makes sense.

    Thanks.
    No, I think you are misunderstanding. BoardPos is an array of type BoardPos_t. A BoardPos_t can only take values that are available in the enum BoardPos_t. Furthermore, to access these values, you must specify the enum type, so you can't do

    BoardPos.at(5) = X; // <--- compile error: "X" is undeclared.

    Also, know that they are not implicitly convertible to any other type. So BoardPos must be declared as std::array<BoardPos_t, 8>, where the type BoardPos_t is important. Substitute it for anything else and you get a compile error.

    I know this is going to cause confusion, so let me state this, as well. In previous versions of C++ (the old enums), you were allowed to do what I just did above (and also convert enums values to int and vice versa). To use these "old enums", you omit the keyword "class", so instead of "enum class", you write just "enum". Know that I do not recommend you do this. It is always better to use newer features, if possible. The older enums exist for backward compatibility.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #25
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,527
    Quote Originally Posted by samwillc View Post
    In other words, do you ever need a variable of your custom enum type in C++11? I hope this makes sense.
    This has not much to do with the kind of enum you are using. That is to say the answer to the question that you are asking is as true for old enums as the new C++11 ones.

    The short, simplest answer to your question is maybe. Now for the explanation:

    For all intents and purposes BoardPos.at(5) is an expression for a variable of BoardPos_t. In the array data structure, all you have are variables of the same type, stored one after another ("contiguously") so that it is actually possible to refer to them individually by a number called a subscript. In C++ variables are usually objects. Sometimes they are references, but I don't want to talk about reference semantics. So until you learn about them, variables are objects is a true statement.

    When you supply X to that part of the board like this:

    BoardPos.at(5) = BoardPos_t::X;

    You are assigning a value to an object, an array element. And as I explained before, the intent of array element is to be a numbered variable. In C++, understanding the difference between objects and values is important. It is the very foundation for more complex semantics (particularly the difference between r-values and l-values). Importantly, notice that objects are on the left of an assignment. By noting where they are, you get a beginner's perception of the difference between l-values and r-values: what in this expression is the object and where is the value that I am modifying it with.


    [edit] As an aside, BoardPos is a deceptive name in my opinion, the array represents the board itself, not positions. [/edit]
    Last edited by whiteflags; 04-24-2013 at 02:09 PM.

  11. #26
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Thanks for all the input. I have been busy the past few days and not had a chance to look at arrays. I will carry on over this weekend, then eventually get back to that tic tac toe game!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 03:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM

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