Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ehoui

macrumors regular
Jan 27, 2011
217
0
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!

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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!

-Lee
 
Last edited:

SidBala

macrumors 6502a
Jun 27, 2010
533
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green
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.

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.
 

chrono1081

macrumors G3
Jan 26, 2008
8,453
4,159
Isla Nublar
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!

-Lee

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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
I'm pretty sure Lee made that up. I took it be a portmanteau of ternary digit. Trigit does sound good. :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.

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:
Code:
_ 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.

-Lee

Edit: it was in java. Almost makes me want to do it again.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green

RiskyMr

macrumors newbie
Apr 8, 2009
27
0
Reminds me of a limerick:
If binary digits are bits,
Then decimal ones could be dits,
And when things get weary,
Try something less dreary,
Like playing with trinary tits.
 

peregin55

macrumors newbie
Feb 25, 2008
3
1
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:
Code:
_ 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.

-Lee

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

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...
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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...

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.

-Lee
 

chrono1081

macrumors G3
Jan 26, 2008
8,453
4,159
Isla Nublar
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:
Code:
_ 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.

-Lee

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

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.

Reminds me of a limerick:

LOL!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.