Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 29, 2011, 03:52 PM   #1
gigemzach
macrumors 6502
 
Join Date: Dec 2009
Location: San Antonio, Texas
Tic Tac Toe Game in Command Prompt

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!
gigemzach is offline   0 Reply With Quote
Old Apr 29, 2011, 04:08 PM   #2
ehoui
macrumors regular
 
Join Date: Jan 2011
Quote:
Originally Posted by gigemzach View Post
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.
ehoui is offline   0 Reply With Quote
Old Apr 29, 2011, 04:18 PM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
I suspect the OP's teacher has already done that.
jiminaus is offline   0 Reply With Quote
Old Apr 29, 2011, 04:50 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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 by lee1210; Apr 29, 2011 at 05:02 PM.
lee1210 is offline   0 Reply With Quote
Old Apr 29, 2011, 06:55 PM   #5
SidBala
macrumors 6502a
 
Join Date: Jun 2010
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.
SidBala is offline   0 Reply With Quote
Old Apr 29, 2011, 07:02 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by SidBala View Post
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.
chown33 is offline   1 Reply With Quote
Old Apr 29, 2011, 08:29 PM   #7
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Quote:
Originally Posted by lee1210 View Post
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.
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote
Old Apr 29, 2011, 08:43 PM   #8
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
I'm pretty sure Lee made that up. I took it be a portmanteau of ternary digit. Trigit does sound good.
jiminaus is offline   0 Reply With Quote
Old Apr 29, 2011, 08:52 PM   #9
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by chrono1081 View Post
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 by lee1210; Apr 29, 2011 at 08:58 PM.
lee1210 is offline   0 Reply With Quote
Old Apr 29, 2011, 09:18 PM   #10
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by lee1210 View Post
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.
I've always used trit. Wikipedia agrees:
http://en.wikipedia.org/wiki/Ternary_numeral_system

The question is: How many trits in a tryte? And is a trillion trits a trilobite?
http://en.wikipedia.org/wiki/Trilobite
chown33 is offline   0 Reply With Quote
Old Apr 29, 2011, 10:18 PM   #11
RiskyMr
macrumors newbie
 
Join Date: Apr 2009
Reminds me of a limerick:
Quote:
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.
RiskyMr is offline   1 Reply With Quote
Old Apr 29, 2011, 11:33 PM   #12
peregin55
macrumors newbie
 
Join Date: Feb 2008
Quote:
Originally Posted by lee1210 View Post
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...
peregin55 is offline   0 Reply With Quote
Old Apr 29, 2011, 11:59 PM   #13
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by peregin55 View Post
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
lee1210 is offline   0 Reply With Quote
Old Apr 30, 2011, 02:17 PM   #14
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Quote:
Originally Posted by lee1210 View Post
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.

Quote:
Originally Posted by RiskyMr View Post
Reminds me of a limerick:
LOL!
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Tic Tac Toe Edition - Another Game For All Age markiphonedev iPhone and iPod touch Apps 0 May 27, 2014 11:26 AM
(New Free Puzzler) Tip Tap Overlap: Brain Teaser meets Tic Tac Toe! [Universal] ElectroBunny iPhone and iPod touch Apps 0 Nov 24, 2013 05:38 PM
Tic Tac Pop! A new very competitive free puzzle game. kaiji81 iPhone and iPod touch Apps 1 Dec 17, 2012 01:27 AM

Forum Jump

All times are GMT -5. The time now is 03:59 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC