1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Tic Tac Toe Game in Command Prompt

Discussion in 'Mac Programming' started by gigemzach, Apr 29, 2011.

  1. macrumors 6502

    Does anyone have java code to do a tic Tac toe game in the command prompt? It needs to be a two player game. Thanks so much!
  2. macrumors regular

    You will be amazed by this thing called "Google". In fact, I strongly recommend you type in "java tic tac toe console" and see what treasures it unearths for you.
  3. macrumors 65816


    I suspect the OP's teacher has already done that. ;)
  4. lee1210, Apr 29, 2011
    Last edited: Apr 29, 2011

    macrumors 68040


    I don't know if i still have the code, but i wrote a "never lose" java tic-tac-toe that was command-line based in college. It seems perfect for what you're asking for. The very best part of this for you? I'm not going to try to find it and ruin it for you, you're going to get to write it yourself. That was the best part for me, anyway. I learned a lot... finding efficient ways to represent a board... i think we ended up calculating a number in base 3, with one trigit per square... so 9 trigits in base 3 can represent all board layouts (even ones that can never occur). That meant 19683 possibilities, so it could fit in a 15 bit value. Then balancing out the time to stuff a board into that format and unstuff from that format to a string representing the board... optimizing that... good times.

    This problem doesn't sound nearly as interesting. No decision trees or anything. All you need to be able to do is:
    display a board
    accept an input, and check that it is valid
    check a board for victory
    detect that a tie has occurred

    Break it down. Each of these tasks should be fairly straight forward. There are only 6 winning configurations, and only the person that just moved could achieve one. You could know a priori at the start of a move if a win can even occur. You don't even have to start checking until the 5th move of the game. You could get fancy and detect a "checkmate" situation where the game is won no matter how the next move is played, but probably not strictly necessary.

    So good luck!

  5. macrumors 6502a

    I love these one line posts asking you to do something. Especially the ones that end with "thanks so much".

    As if we are really going to do your homework for you.
  6. macrumors 603

    Does anyone have an Automator script, system service, or contextual-menu plugin to make one-click replies to posts that ask for the answer to what are obviously homework problems? It needs to be free because I obviously have no time or inclination to write it myself. Thanks in advance.
  7. macrumors 604


    Your tic tac toe sounds much better than mine was. I thought mine was cool since I used SDL to add graphics but it wasn't optimized and efficient like yours was. I'm curious about the whole "trigits" thing. I've never heard that term before and google doesn't bring much info up. The most I found was a page on fractal functions.

    Oh and to the OP: You will definitely want to write this stuff on your own, you learn so much by doing that. Just write down some pseudo code on what you need to do and keep breaking the project down into little manageable chunks and then code them.
  8. macrumors 65816


    I'm pretty sure Lee made that up. I took it be a portmanteau of ternary digit. Trigit does sound good. :)
  9. lee1210, Apr 29, 2011
    Last edited: Apr 29, 2011

    macrumors 68040


    I just made up that word, or at least this usage. A single binary numeral is a bit, a single octal numeral is an octet/octit, a single decimal numeral is a digit, a single hexadecimal numeral is a hexit. I figure a trinary numeral is a trigit.

    Each square of the board would be assigned a trigit based on its value. 0 for unoccupied, 1 for X, 2 for O. So:
    _ X _
    O _ X
    _ O _
    would be 010201020. I'll leave the trinary to decimal conversion as an exercise for the reader, but this allows for a fairly dense representation. I honestly don't know how many legal boards there are, probably a small fraction of all board states. Storing a mapping of a numerical legal board number to the layout would probably save a few bits (though you'd want a backward map to know what your new states number is). We weren't quite that advanced.


    Edit: it was in java. Almost makes me want to do it again.
  10. macrumors 603

    I've always used trit. Wikipedia agrees:

    The question is: How many trits in a tryte? And is a trillion trits a trilobite?
  11. macrumors newbie

    Reminds me of a limerick:
  12. macrumors newbie

    Really interesting idea, Lee. A decimal representation of the trinary number fits with plenty of room to spare in a java short. Only question is if you end up doing a lot of decimal <=> trinary conversions, are we trading speed for space? As I recall converting a number out of decimal requires a series of divisions which can be a bit of a pain to program...
  13. macrumors 68040


    There is almost always such a tradeoff. The real point of the program was to build a tree scoring each board state based on the distance to a winning board state and losing board state. This really only needs to be done once, then an archive kept, but that wasn't really the point, it was an academic exercise. I think we were limited to 32MB of RAM, and we were gunning for the fastest runtime. Making the trinary <=> decimal function quick was important, and I think minimizing the number of switches back and forth was important. Getting from a board to the decimal was pretty fast, but once the optimal next move was chosen it was a little pokey to go from the decimal representation to the board state to figure out what the move was. That was happening just after the human player entered a choice, though, and it only had to be done once per move.

    That project and a pascal compiler are the primary programming exercises that have stuck with me from school. I remember bits and pieces of other things, but those are probably the only things I'd have any interest in trying again.

  14. macrumors 604


    Thanks for the explanation! I'll have to give it a try sometime. We never did anything like that in the three colleges I went to (and we didn't have programming in my high school since the school decided sports was more important than technology).

    I do remember some fun exercises we had at my most recent school, making a mini data compression program in x86 assembler as well as making a mini tron style game (with the lines as light cycles and such) in x86 as well (these were challenging and I loved it!). In C++ during the console phase we made little games like Blackjack, RPG's, and hangman, easy stuff. I miss the harder things though.


Share This Page