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!
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!
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.
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.
_ X _
O _ X
_ O _
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.
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.
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...
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.
Reminds me of a limerick: