Posted: Sun Apr 19, 2009 6:39 pm Post subject: mini-contest: No selection-structure Tic-Tac-Toe
Well, I've had an idea bouncing around in my head to create a game of Tic-Tac-Toe with no conditionals. That means no if-statements, case-statements, exit-statements, etc. The only selection you can use is in the case of for loops, for example;
for (int i = 0, i>= 5, i++){
}
Being the lazy bum I am, I haven't gotten around to it. However, I believe that clever use of hashes, math, and binary should do the trick. So I'm challenging you guys to do this (mostly so I can see the finished product without actually doing it). The requirements:
-it must have a visual display. Be it ASCII or 3-D, it must have a graphical representation of where the X's and O's are.
-it must follow standard 3x3 tic-tac-toe rules
-it must be multiplatform (Turing is okay, it runs perfectly on Wine and Crossover)
-it must not use and conditionals, except in the case of for loops as shown above
-it must display the name/player number of the winner when a player wins
-it need not have a mouse implementation, you may enter the location of your X or O via coordinates.
Have fun! Winner gets 1000 bits (unless I don't have that many...)
EDIT: The winner is the person who makes the most interesting program, and has the most awesome code (judged by me)
Sponsor Sponsor
saltpro15
Posted: Sun Apr 19, 2009 7:00 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
damn that's a challenge... I'll give it a shot, how long do we have?
Insectoid
Posted: Sun Apr 19, 2009 7:03 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
You have until whenever I say so, aka whenever I get bored of waiting.
And those for loop conditionals can only be used in counted loops. A 'do this X many times' loop. No sneaky rule-bending allowed.
saltpro15
Posted: Sun Apr 19, 2009 7:21 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
hehe very well, I'll be getting ready for ECOO all this week, so I'll have some time to start it hopefully
Nick
Posted: Sun Apr 19, 2009 7:44 pm Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
Done Python style
Python:
class Board:
def__init__(self):
self.p = [[0,0,0],[0,0,0],[0,0,0]] self.again = 1 self.winner = 0 self.sent = 0 self.update() print"You win player " + str(self.winner) + "!" def check (self, player):
foo = max(((self.p[0][0] == player)and(self.p[0][1] == player)and(self.p[1][2] == player)),
((self.p[1][0] == player)and(self.p[1][1] == player)and(self.p[0][2] == player)),
((self.p[0][0] == player)and(self.p[0][1] == player)and(self.p[0][2] == player)), # vertical ((self.p[0][0] == player)and(self.p[1][0] == player)and(self.p[2][0] == player)),
((self.p[0][1] == player)and(self.p[1][1] == player)and(self.p[2][1] == player)),
((self.p[0][2] == player)and(self.p[1][2] == player)and(self.p[2][2] == player)), # horizontal ((self.p[0][0] == player)and(self.p[1][1] == player)and(self.p[2][2] == player)),
((self.p[2][0] == player)and(self.p[1][1] == player)and(self.p[0][2] == player)))# diagonal self.again = min(not(foo), self.again) self.winner = max(foo * player, self.winner) return foo
def assertion (self, player):
y = int(raw_input("Player " + str(player) + " x coord")) - 1
x = int(raw_input("Player " + str(player) + " y coord")) - 1 for i inrange(self.p[x][y] != 0):
print"Invalid selection, choose again (is it cat's game?)"
foo = self.assertion(player)
x = foo [0]
y = foo [1] return[x, y] def add (self, player):
foo = self.assertion(player) self.p[foo [0]][foo [1]] = player
def update (self):
for i in(range(2)):
for x inself.p:
foo = "" for y in x:
foo += str(y) print foo
for ii inrange(self.sent == 9andself.winner == 0):
self.again = 0 print"Cat's game" del"Th-th-th-th-that's all folks" for ii inrange(self.again == 0):
return self.add(i + 1) self.check(i + 1) self.sent += 1 for i inrange(self.again):
self.update()
b = Board ()
also Saad caught me cheating so this doesn't count, oh well
Brightguy
Posted: Mon Apr 20, 2009 8:54 am Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
Done Javascript style. Probably would work in Internet Explorer, but I know it doesn't support XHTML, and I'm sick and tired of working around IE.
Javascript:
function won() {var m = marks[turn];
return(m[0]&m[1]&m[2])|(m[3]&m[4]&m[5])|(m[6]&m[7]&m[8])|(m[0]&m[3]&m[6])|(m[1]&m[4]&m[7])|(m[2]&m[5]&m[8])|(m[0]&m[4]&m[8])|(m[2]&m[4]&m[6]);
}
Posted: Mon Apr 20, 2009 3:37 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
wew, brightguy, that's fancy. I'm gonna have to ask somebody who understands javascript to look it over for cheating (not that I accuse you, but because you may have coded some in by habit). God, javascript's method names are so ridiculously long it's harder to understand than one with short names (ie ruby/python).
EDIT: Neither submissions handles cat's game. Definitely points for that.
Insectoid
Posted: Mon Apr 20, 2009 4:09 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
Just a question;
code:
{ var m = marks[turn];
return (m[0]&m[1]&m[2])|(m[3]&m[4]&m[5])|(m[6]&m[7]&m[8])|(m[0]&m[3]&m[6])|(m[1]&m[4]&m[7])|(m[2]&m[5]&m[8])|(m[0]&m[4]&m[8])|(m[2]&m[4]&m[6]);
Does that big return statement mean 'return (1 and 2 and 3) or (3 and 4 and 5) or (6 and 7 and 8)...'?
Sponsor Sponsor
Nick
Posted: Mon Apr 20, 2009 4:27 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
Quote:
EDIT: Neither submissions handles cat's game. Definitely points for that.
mine does....
Alexmula
Posted: Mon Apr 20, 2009 5:23 pm Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
Posted: Mon Apr 20, 2009 5:24 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
no.
richcash
Posted: Mon Apr 20, 2009 6:10 pm Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
Here is an unspectacular Tic Tac Toe with no conditionals in Turing. I might polish the display if I have more time.
Turing:
View.Set("offscreenonly") var w, winner, turn, sel :=0 var board :array0.. 2, 0.. 2ofint:=init(0, 0, 0, 0, 0, 0, 0, 0, 0) var names :array - 1.. 1ofstring:=init("x", "-", "y")
turn := -1 loop cls put names (winner)," won"
w :=round(sqrt(-abs(winner))) for i :0.. 2 for j :0.. 2 locate(i *2 + 3, j *2 + 1) put names (board (i, j)) endfor endfor put names (turn)," enter a number from 1-9." View.Update
sel :=strint(getchar) - 1
board (sel div3, sel mod3):=sign(board (sel div3, sel mod3)*2 + turn)
turn :=0 for i :0.. 2 for j :0.. 2
turn += board (i, j) endfor endfor
turn := turn * -2 - 1
winner :=0
w :=0 for i :0.. 2 for j :0.. 2
w += board (i, j) endfor
winner += w div3
w :=0 endfor for i :0.. 2 for j :0.. 2
w += board (j, i) endfor
winner += w div3
w :=0 endfor for i :0.. 2
w += board (i, i) endfor
winner += w div3
w :=0 for i :0.. 2
w += board (i, 2 - i) endfor
winner += w div3 endloop
The_Bean
Posted: Mon Apr 20, 2009 7:34 pm Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
for turn inrange(9):
board()
grid[int(inputCase[win](chr(88-(turn%2)*9)+' Pick a position:'))-1]=chr(88-(turn%2)*9) for i inrange(3):
win=max(win,matching[len(filter(lambda x : x==chr(88-(turn%2)*9),grid[i*3:i*3+3]))](turn%2+1))
win=max(win,matching[len(filter(lambda x : x==chr(88-(turn%2)*9),grid[i:9:3]))](turn%2+1))
win=max(win,matching[len(filter(lambda x : x==chr(88-(turn%2)*9),grid[0:9:4]))](turn%2+1))
win=max(win,matching[len(filter(lambda x : x==chr(88-(turn%2)*9),grid[2:7:2]))](turn%2+1))
board() print winCases[win] raw_input('Waiting to Exit...')
CodeMonkey2000
Posted: Mon Apr 20, 2009 8:29 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe
I'm waiting for someone to write this in BF. Hehehe.
richcash
Posted: Mon Apr 20, 2009 9:38 pm Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe
insectoid wrote:
Just a question;
code:
{ var m = marks[turn];
return (m[0]&m[1]&m[2])|(m[3]&m[4]&m[5])|(m[6]&m[7]&m[8])|(m[0]&m[3]&m[6])|(m[1]&m[4]&m[7])|(m[2]&m[5]&m[8])|(m[0]&m[4]&m[8])|(m[2]&m[4]&m[6]);
Does that big return statement mean 'return (1 and 2 and 3) or (3 and 4 and 5) or (6 and 7 and 8)...'?
Yes, but he is using them as bitwise operators not boolean operators, so he is not using selection directly.
@The_Bean, good job but I would consider the function filter a conditional/selection method. In fact, you can probably replace every single if statement by using filter trivially.
@Nick, do you mind telling us why your code is supposedly invalid (I don't read python well).