Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 mini-contest: No selection-structure Tic-Tac-Toe
Index -> Contests
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: 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
Sponsor
sponsor
saltpro15




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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 in range (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 in self.p:
                foo = ""
                for y in x:
                    foo += str (y)
                print foo
            for ii in range (self.sent == 9 and self.winner == 0):
                self.again = 0
                print "Cat's game"
                del "Th-th-th-th-that's all folks"
            for ii in range (self.again == 0):
                return
            self.add (i + 1)
            self.check (i + 1)
            self.sent += 1
        for i in range (self.again):
            self.update ()
       
b = Board ()


also Saad caught me cheating so this doesn't count, oh well
Brightguy




PostPosted: 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]);
}

function mark(n)
{       marks[turn][n] = 1;
        document.getElementById("b"+n).innerHTML = String.fromCharCode(88-9*turn);
        document.getElementById("b"+n).disabled = 1;
        for(var i=0; i<9; i++)
                document.getElementById("b"+i).disabled |= won();
        document.getElementById("won"+won()).innerHTML = document.getElementById("p"+turn).value+" (Player "+turn+") wins!";
        turn = (turn+1) % 2;
}

function start()
{       marks = [[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]];
        turn = 0;
        for(var i=0; i<9; i++)
        {       document.getElementById("b"+i).disabled = 0;
                document.getElementById("b"+i).innerHTML = "&#160;";
        }
        document.getElementById("won1").innerHTML = "";
}
Insectoid




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
Nick




PostPosted: 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




PostPosted: Mon Apr 20, 2009 5:23 pm   Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe

Python:
import os
os.startfile("http://www.prongo.com/tictac/")



Laughing Laughing Laughing
Insectoid




PostPosted: Mon Apr 20, 2009 5:24 pm   Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe

no.
richcash




PostPosted: 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 : array 0 .. 2, 0 .. 2 of int := init (0, 0, 0, 0, 0, 0, 0, 0, 0)
var names : array - 1 .. 1 of string := 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))
        end for
    end for
    put names (turn), " enter a number from 1-9."
    View.Update
    sel := strint (getchar) - 1
    board (sel div 3, sel mod 3) := sign (board (sel div 3, sel mod 3) * 2 + turn)
    turn := 0
    for i : 0 .. 2
        for j : 0 .. 2
            turn += board (i, j)
        end for
    end for
    turn := turn * -2 - 1
    winner := 0
    w := 0
    for i : 0 .. 2
        for j : 0 .. 2
            w += board (i, j)
        end for
        winner += w div 3
        w := 0
    end for
    for i : 0 .. 2
        for j : 0 .. 2
            w += board (j, i)
        end for
        winner += w div 3
        w := 0
    end for
    for i : 0 .. 2
        w += board (i, i)
    end for
    winner += w div 3
    w := 0
    for i : 0 .. 2
        w += board (i, 2 - i)
    end for
    winner += w div 3
end loop
The_Bean




PostPosted: Mon Apr 20, 2009 7:34 pm   Post subject: Re: mini-contest: No selection-structure Tic-Tac-Toe

Python 2.5.4:
Python:

def getNothing(person):
    return 10

def board():
    print '\n\n',grid[0],'|',grid[1],'|',grid[2],'\n','- + - + -','\n',grid[3],'|',grid[4],'|',grid[5],'\n','- + - + -','\n',grid[6],'|',grid[7],'|',grid[8],'\n\n'

def getInput(message):
    return raw_input(message)

def noMatch(num):
    return 0

def threeInARow(num):
    return num

winCases=['Cats Game','X Wins','O wins']
inputCase=[getInput,getNothing,getNothing]
matching=[noMatch,noMatch,noMatch,threeInARow]

grid=['1','2','3','4','5','6','7','8','9','10']
win=0

for turn in range(9):
    board()
    grid[int(inputCase[win](chr(88-(turn%2)*9)+' Pick a position:'))-1]=chr(88-(turn%2)*9)
    for i in range (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




PostPosted: 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




PostPosted: 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).
Display posts from previous:   
   Index -> Contests
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 38 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: