Tic Tic Toe in 1 line...well, sort of.
Author |
Message |
bass_maniac
|
Posted: Thu Apr 27, 2006 3:41 pm Post subject: Tic Tic Toe in 1 line...well, sort of. |
|
|
I'm not sure what forum this belongs in, but I was trying to program tic tac toe in the smallest number of lines possible. I got it down to 11 and then, on a whim, tried seperating two commands on the same line with a semicolon. Like,
and it worked! I figure this has something to do with Turing being written in C++, but I wasn't aware it was available.
Anyway, here it is: Tic Tac Toe in 1 line.
code: | var board : array 1 .. 19 of string := init (" _", "|_", "|_", " _", "|_", "|_", " _", "|_", "|_", "", "", "", "", "", "", "", "", "", ""); for a : 1 .. 9; put repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 215), board (1), board (2), board (3), chr (10), board (4), board (5), board (6), chr (10), board (7), board (8), board (9), chr (10), "X is first. Type the number of the box you want to take. Upper-left = 1, bottom-right = 9."; get board (10); exit when board (10) not= "1" and board (10) not= "2" and board (10) not= "3" and board (10) not= "4" and board (10) not= "5" and board (10) not= "6" and board (10) not= "7" and board (10) not= "8" and board (10) not= "9"; if a = 1 or a = 3 or a = 5 or a = 7 or a = 9 and index(board (strint(board(10))), "X") = 0 and index (board (strint(board(10))), "O") = 0 then; board (strint (board (10))) := board (strint (board (10))) (1) + "X"; board (10 + strint (board (10))) := "X"; elsif index (board (strint(board(10))), "X") = 0 and index (board (strint(board(10))), "O") = 0 then; board (strint (board (10))) := board (strint (board (10))) (1) + "O"; board (10 + strint (board (10))) := "O"; end if; end for
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
do_pete
|
Posted: Thu Apr 27, 2006 3:56 pm Post subject: (No subject) |
|
|
It's very bad form to make a program fit on one line unless it's supposed to (ie: put "hello")
EDIT: You don't need semicolons |
|
|
|
|
|
bass_maniac
|
Posted: Thu Apr 27, 2006 4:04 pm Post subject: (No subject) |
|
|
I hadn't thought to try it without the semicolon, but that does appear to work. And, yes, of course it looks better spread out. |
|
|
|
|
|
md
|
Posted: Thu Apr 27, 2006 4:33 pm Post subject: (No subject) |
|
|
Ooo... you put manly lines of code on one line...
Guess what, your code is still just as long! You didn't improve it or make it any smaller! If you want to make it less lines then fine; change the code so it requires less lines. Don't just string them all together. |
|
|
|
|
|
bass_maniac
|
Posted: Thu Apr 27, 2006 4:41 pm Post subject: (No subject) |
|
|
Cornflake wrote: If you want to make it less lines then fine; change the code so it requires less lines. Don't just string them all together.
Oh, geez. That's what I WAS doing. Read the post! 11 lines. I just strung them together to explain what I meant. |
|
|
|
|
|
md
|
Posted: Thu Apr 27, 2006 5:23 pm Post subject: (No subject) |
|
|
11 lines is Good! But you then condenced it into the cesspool that is the quoted code above.
My point is that optimizing to few lines (which isn't nessessarily optimized either...) is good; making everything a single line is worthy of death. |
|
|
|
|
|
Delos
|
Posted: Thu Apr 27, 2006 9:12 pm Post subject: (No subject) |
|
|
Moved to [Source]. This is not a tutorial. Also, the title was changed to be a little more, uh, reflective of the content .
I believe beard0 holds the record for the shortest Tic-Tac-Toe game? I forget how long it was. |
|
|
|
|
|
Clayton
|
Posted: Thu Apr 27, 2006 9:29 pm Post subject: (No subject) |
|
|
um, it may be in "one line" but, it still doesnt work properly, i had a win going down the left side and it was still letting me put X's and O's in(argh) short progs can be good, but short isnt necessarily the best, efficient is however. I remember Pong in 20 lines, and it was good too, nice try with tic-tac-toe though, and i think beard0's was like 5 or 6 lines maybe a bit more, but not much |
|
|
|
|
|
Sponsor Sponsor
|
|
|
War_Caymore
|
Posted: Thu May 04, 2006 1:14 pm Post subject: Um.... Blah??? |
|
|
just a question. isnt this supposed to be TURING source code, not C++. just wondering if you guys every figured that out.... |
|
|
|
|
|
[Gandalf]
|
Posted: Thu May 04, 2006 2:45 pm Post subject: Re: Um.... Blah??? |
|
|
War_Caymore wrote: just a question. isnt this supposed to be TURING source code, not C++. just wondering if you guys every figured that out....
Eh?
The only other place I see C++ mentioned in this thread is the first post, where he stated that Turing was written in C++, which is quite close to the truth. The code he posted was valid Turing code |
|
|
|
|
|
ultramancool
|
Posted: Thu May 04, 2006 7:51 pm Post subject: (No subject) |
|
|
Ok, while I'm a big perl coder and I live in the world of really short perl 1 liners. I really think this is a great idea, and if you're just going to say crap about it then get out of here. Ok, while maybe this is a little excessive for a one-liner, but I mean, the lightweight and fast are better than bloated. |
|
|
|
|
|
[Gandalf]
|
Posted: Thu May 04, 2006 8:06 pm Post subject: (No subject) |
|
|
The point was that it's not truly one line. The code should be written as:
code: | var board : array 1 .. 19 of string := init (" _", "|_", "|_", " _", "|_", "|_", " _", "|_", "|_", "", "", "", "", "", "", "", "", "", "");
for a : 1 .. 9;
put repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 255), repeat (" ", 215), board (1), board (2), board (3), chr (10), board (4), board (5), board (6), chr (10), board (7), board (8), board (9), chr (10), "X is first. Type the number of the box you want to take. Upper-left = 1, bottom-right = 9.";
get board (10);
exit when board (10) not= "1" and board (10) not= "2" and board (10) not= "3" and board (10) not= "4" and board (10) not= "5" and board (10) not= "6" and board (10) not= "7" and board (10) not= "8" and board (10) not= "9";
if a = 1 or a = 3 or a = 5 or a = 7 or a = 9 and index(board (strint(board(10))), "X") = 0 and index (board (strint(board(10))), "O") = 0 then;
board (strint (board (10))) := board (strint (board (10))) (1) + "X";
board (10 + strint (board (10))) := "X";
elsif index (board (strint(board(10))), "X") = 0 and index (board (strint(board(10))), "O") = 0 then;
board (strint (board (10))) := board (strint (board (10))) (1) + "O";
board (10 + strint (board (10))) := "O";
end if;
end for |
Which is 13 lines of code, not one.
If you want to see some truly impressive short code in Turing, check the old contests out. |
|
|
|
|
|
|
|