Computer Science Canada

mini-contest: No selection-structure Tic-Tac-Toe

Author:  Insectoid [ 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)

Author:  saltpro15 [ 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?

Author:  Insectoid [ 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.

Author:  saltpro15 [ 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

Author:  Nick [ 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

Author:  Brightguy [ 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 = "";
}

Author:  Insectoid [ 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.

Author:  Insectoid [ 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)...'?

Author:  Nick [ 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....

Author:  Alexmula [ 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

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

no.

Author:  richcash [ 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

Author:  The_Bean [ 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...')

Author:  CodeMonkey2000 [ 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.

Author:  richcash [ 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).

Author:  matt271 [ Tue Apr 21, 2009 5:31 am ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

c++:
/*
 * File:   newmain.cpp
 * Author: matt
 *
 * Created on April 21, 2009, 7:06 AM
 */


#include <stdlib.h>
#include <iostream>
using namespace std;

int board[3][3] = {0, 0, 0,
                   0, 0, 0,
                   0, 0, 0};

char play[3] = {'_', 'X', 'O'};

char msg[3][10] = {"", "X wins\n", "O Wins\n"};

int player = 1;

void showboard();
void move(int i, int j);
void test();

int main(int argc, char** argv) {
    int i, j;
    while (true) {
        cin >> i;
        cin >> j;
        move(i, j);
        showboard();
        test();
    }
    return (EXIT_SUCCESS);
}

void move(int i, int j) {
    player++;

    board[i][j] = player % 2 + 1;
}

void showboard() {
    printf("\n");
   
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            printf("%c ", play[board[i][j]]);
        printf("\n");
    }

    printf("\n");
}

void test() {
    int t;
    t = board[0][0] & board[0][1] & board[0][2] |
        board[1][0] & board[1][1] & board[1][2] |
        board[2][0] & board[2][1] & board[2][2] |
        board[0][0] & board[1][1] & board[2][2] |
        board[2][2] & board[1][1] & board[0][0];

    printf("%s", msg[t]);
}


enter the quardenents from 0 to 2 separated by spaces.

should look like:

Quote:
0 0

X _ _
_ _ _
_ _ _

0 1

X O _
_ _ _
_ _ _

1 1

X O _
_ X _
_ _ _

0 2

X O O
_ X _
_ _ _

2 2

X O O
_ X _
_ _ X

X wins

Author:  Nick [ Tue Apr 21, 2009 1:36 pm ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

richcash @ Mon Apr 20, 2009 9:38 pm wrote:
@Nick, do you mind telling us why your code is supposedly invalid (I don't read python well).


I used a for i in range (x): loop as an if x: statement

ex:
code:
for i in range (self.p [x][y] != 0):

Author:  matt271 [ Tue Apr 21, 2009 4:21 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

no comment on my solution? Sad

Author:  richcash [ Tue Apr 21, 2009 4:59 pm ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

Nick wrote:
I used a for i in range (x): loop as an if x: statement

ex:
code:
for i in range (self.p [x][y] != 0):
Ah, I see, I somehow missed that.


matt271 wrote:
no comment on my solution? (
Looks good to me, though I don't have a compiler at the moment to test it. However :
c++:
t = board[0][0] & board[0][1] & board[0][2] |
        board[1][0] & board[1][1] & board[1][2] |
        board[2][0] & board[2][1] & board[2][2] |
        board[0][0] & board[1][1] & board[2][2] |
        board[2][2] & board[1][1] & board[0][0]
I think you forgot to check for vertical wins, if I'm not mistaken. Am I right?

Author:  matt271 [ Tue Apr 21, 2009 6:00 pm ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

my bad :] i fixed it ty

c++:
/*
 * File:   newmain.cpp
 * Author: matt
 *
 * Created on April 21, 2009, 7:06 AM
 */


#include <stdlib.h>
#include <iostream>
using namespace std;

int board[3][3] = {0, 0, 0,
                   0, 0, 0,
                   0, 0, 0};

char play[3] = {'_', 'X', 'O'};

char msg[3][10] = {"", "X wins\n", "O Wins\n"};

int player = 1;

void showboard();
void move(int i, int j);
void test();

int main(int argc, char** argv) {
    int i, j;
    while (true) {
        cin >> i;
        cin >> j;
        move(i, j);
        showboard();
        test();
    }
    return (EXIT_SUCCESS);
}

void move(int i, int j) {
    player++;

    board[i][j] = player % 2 + 1;
}

void showboard() {
    printf("\n");
   
    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            printf("%c ", play[board[i][j]]);
        printf("\n");
    }

    printf("\n");
}

void test() {
    int t;
    t = board[0][0] & board[0][1] & board[0][2] |
        board[1][0] & board[1][1] & board[1][2] |
        board[2][0] & board[2][1] & board[2][2] |
           
        board[0][0] & board[1][1] & board[2][2] |
        board[0][2] & board[1][1] & board[2][0] |
           
        board[0][0] & board[1][0] & board[2][0] |
        board[0][1] & board[1][1] & board[2][1] |
        board[0][2] & board[1][2] & board[2][2];

    printf("%s", msg[t]);
}


and in java!

Java:

import java.util.Scanner;


/**
 *
 * @author matt
 */

public class Main {

    static int[][] board = { {0, 0, 0},
                             {0, 0, 0},
                             {0, 0, 0} };

    static char[] play = {'_', 'X', 'O'};

    static String[] msg = {"", "X wins\n", "O Wins\n", "Game is over\n"};

    static int player = 1;

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i, j;
        while (true) {
            i = in.nextInt();
            j = in.nextInt();
            move(i, j);
            showboard();
            test();
        }
    }

    static void move(int i, int j) {
        player++;

        board[i][j] = player % 2 + 1;
    }

    static void showboard() {
        System.out.println();

        int i, j;
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++)
                System.out.print(play[board[i][j]] + " ");
            System.out.println();
        }

        System.out.println();
    }

    static void test() {
        int t;
        t = board[0][0] & board[0][1] & board[0][2] |
            board[1][0] & board[1][1] & board[1][2] |
            board[2][0] & board[2][1] & board[2][2] |

            board[0][0] & board[1][1] & board[2][2] |
            board[0][2] & board[1][1] & board[2][0] |

            board[0][0] & board[1][0] & board[2][0] |
            board[0][1] & board[1][1] & board[2][1] |
            board[0][2] & board[1][2] & board[2][2];

        System.out.println(msg[t]);
    }

}

Author:  Nick [ Wed Apr 22, 2009 2:54 am ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

here's a comment, where's cat's game and where's the code that stops you from playing where one's already played?

Author:  matt271 [ Wed Apr 22, 2009 3:46 am ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

what is cats game?

and nothing stops u from playing where some1 already played.

also nothing stops u from giving a point off the 3x3 grid

and nothing stops u from playing after u already won (but the java one will tell u the game is already over Very Happy)

i guess i could come up w/ some real clever way to do those

but i still think i won, i want the bits (what r bits anyways?)

its all for fun :]

Author:  matt271 [ Wed Apr 22, 2009 4:22 am ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

there i did it in c this time:

c:
/*
 * File:   newmain.c
 * Author: matt
 *
 * Created on April 22, 2009, 5:54 AM
 */


#include <stdio.h>
#include <stdlib.h>

void valid();
void notvalid();
void showboard();
int test();
void none();
void win();
void clearboard();

int board[3][3] = {0, 0, 0,
                   0, 0, 0,
                   0, 0, 0};

char play[3] = {'_', 'X', 'O'};

int player = 2;

int i, j;

/*
 * main function
 */

int main(int argc, char** argv) {
    void (*fun[3])() = {&valid, &notvalid, &notvalid};
    void (*msg[3])() = {&none, &win, &win};

    while (1) {
        scanf("%i", &i);
        scanf("%i", &j);

        i &= 3;
        j &= 3;

        fun[board[i][j]]();

        showboard();

        msg[test()]();
    }

    return (EXIT_SUCCESS);
}

void valid() {
    board[i][j] = player ^= 3;
}

void notvalid() {
    printf("Sorry not valid move my friend\n");
}

void showboard() {
    printf("\n");

    int i, j;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            printf("%c ", play[board[i][j]]);
        printf("\n");
    }

    printf("\n");
}

int test() {
    return board[0][0] & board[0][1] & board[0][2] |
           board[1][0] & board[1][1] & board[1][2] |
           board[2][0] & board[2][1] & board[2][2] |

           board[0][0] & board[1][1] & board[2][2] |
           board[0][0] & board[1][0] & board[2][0] |

           board[0][0] & board[1][0] & board[2][0] |
           board[0][1] & board[1][1] & board[2][1] |
           board[0][2] & board[1][2] & board[2][2];
}

void none() {
    // nada
}

void win() {
    printf("%c wins!\n\n", play[player]);

    clearboard();

    player = 2;
}

void clearboard() {
    int i, j;
    for (i = 0; i < 3; i++)
        for (j = 0; j < 3; j++)
            board[i][j] = 0;
}


input ur moves the same

output is liek this

Quote:
1 1

_ _ _
_ X _
_ _ _

0 1

_ O _
_ X _
_ _ _

0 0

X O _
_ X _
_ _ _

0 2

X O O
_ X _
_ _ _

2 2

X O O
_ X _
_ _ X

X wins!

0 0

X _ _
_ _ _
_ _ _

1 0

X _ _
O _ _
_ _ _

0 1

X X _
O _ _
_ _ _

1 1

X X _
O O _
_ _ _

0 2

X X X
O O _
_ _ _

X wins!

2 2

_ _ _
_ _ _
_ _ X

0 0

O _ _
_ _ _
_ _ X

2 1

O _ _
_ _ _
_ X X

0 2

O _ O
_ _ _
_ X X

1 1

O _ O
_ X _
_ X X

0 1

O O O
_ X _
_ X X

O wins!

1 1

_ _ _
_ X _
_ _ _

1 1
Sorry not valid move my friend

_ _ _
_ X _
_ _ _

0 1

_ O _
_ X _
_ _ _

Author:  OneOffDriveByPoster [ Wed Apr 22, 2009 9:19 am ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

matt271 @ Wed Apr 22, 2009 4:22 am wrote:

c:
    void (*fun[3])() = {&valid, &notvalid, &notvalid};
    void (*msg[3])() = {&none, &win, &win};

I don't know about the contest judge, but this looks like cheating.
An array of function pointers? That's not a selection statement? Really?

Author:  Insectoid [ Wed Apr 22, 2009 12:55 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

I dunno how that works (not knowing any C) but if OneOffDriveByPoster thinks it's cheating, I'll trust him.

Anyway, if anyone wants to vote on a submission, PM me. Don't vote for your own, it's not cool.

Author:  richcash [ Wed Apr 22, 2009 1:48 pm ]
Post subject:  Re: RE:mini-contest: No selection-structure Tic-Tac-Toe

matt271 @ Wed Apr 22, 2009 3:46 am wrote:
what is cats game?
When the game ends in a draw.

matt271 @ Wed Apr 22, 2009 3:46 am wrote:
and nothing stops u from playing where some1 already played.

and nothing stops u from playing after u already won (but the java one will tell u the game is already over Very Happy)

i guess i could come up w/ some real clever way to do those
In my opinion you need to fix those things to make it valid. Also, if the person enters an invalid move (or enters a cell that's already filled) it should still be their turn, but the board should not change.



insectoid wrote:
Anyway, if anyone wants to vote on a submission, PM me. Don't vote for your own, it's not cool.
The contest is closed already? The only two completely valid submissions so far are mine and Brightguy's, until matt disallows letting a player enter a cell that's already occupied (since that is a rule of TTT) and Nick, The_Bean get rid of their use of selection.

Author:  matt271 [ Wed Apr 22, 2009 2:41 pm ]
Post subject:  Re: RE:mini-contest: No selection-structure Tic-Tac-Toe

richcash @ Wed Apr 22, 2009 2:48 pm wrote:
In my opinion you need to fix those things to make it valid. Also, if the person enters an invalid move (or enters a cell that's already filled) it should still be their turn, but the board should not change.


i did fix that in my latest submission Razz

insectoid @ Wed Apr 22, 2009 1:55 pm wrote:
I dunno how that works (not knowing any C) but if OneOffDriveByPoster thinks it's cheating, I'll trust him.


its not cheating Crying or Very sad its not a conditional statement its just a very clever way to call functions Crying or Very sad

its no dif then when i said printf(msg[blah]);

Author:  OneOffDriveByPoster [ Wed Apr 22, 2009 2:55 pm ]
Post subject:  Re: RE:mini-contest: No selection-structure Tic-Tac-Toe

matt271 @ Wed Apr 22, 2009 2:41 pm wrote:
its no dif then when i said printf(msg[blah]);
Absolutely not. This can alter control flow (as opposed to the data).

Author:  matt271 [ Wed Apr 22, 2009 4:05 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

but the point was to find a way to build the game w/out condition statements, not w/out flow.

if u say anything that can alter flow is a conditional statement, then any clever way to build the game is a conditional statement.

what if u wrote it in basic and used clever numbers to represent X and O then said GOTO X

blah this was supposed to be a fun lil challenge but now its turning un-fun Crying or Very sad

Author:  OneOffDriveByPoster [ Wed Apr 22, 2009 10:11 pm ]
Post subject:  Re: mini-contest: No selection-structure Tic-Tac-Toe

Cat's game and invalid move checking included.
c++:
#include <iostream>
#include <climits>

using namespace std;

enum { MAX_BITS = CHAR_BIT * sizeof(unsigned) }// compile time constant

unsigned isZero[MAX_BITS] = { ~0u };
unsigned isThree[4] = { 0, 0, 0, ~0u };
unsigned board[2] = { 0, 0 };

void print(void) {

   static const char *const nonZeroSpace[] = { "", " ", " " };
   static const char gridc[] = { '-', 'X', 'O', '!' };

   cout << " |0|1|2" << endl;
   for (int r = 0; r < 3; ++r) {
      cout << r << "|";
      for (int c = 0; c < 3; ++c) {
         unsigned board0 = (board[0] >> (r * 3 + c)) & 0x1u;
         unsigned board1 = (board[1] >> (r * 3 + c)) & 0x1u;
         cout << nonZeroSpace[c] << gridc[board0 | (board1 << 1)];
      }
      cout << endl;
   }
   cout << endl;
}

int numbits(unsigned x) {

   int ans = 0;
   for (int i = 0; i < MAX_BITS; ++i) {

      ans += (x & 1);
      x >>= 1;
   }
   return ans;
}

void doMove(unsigned r, unsigned c, int player) {

   static const char *const movingMsg[] = { "Invalid co-ordinates ", "Making move ", "Cannot make move on occupied cell " };

   const unsigned r3 = r & 0x3u, c3 = c & 0x3u;
   const unsigned moveMask = (isZero[numbits(r & ~0x3u)] &
                              isZero[numbits(c & ~0x3u)] &
                              ~isThree[r3] & ~isThree[c3] &
                              (1 << r3 * 3 + c3));

   const unsigned collisions = numbits(moveMask & (board[0] | board[1]));
   cout << movingMsg[numbits(moveMask) + collisions] << r << " " << c << "\n" << endl;
   board[player] |= moveMask ^ (moveMask * collisions);
}

inline int calcPlayer(void) {
   return numbits(board[0] + board[1]) & 1;
}

void checkWin(const int player) {
   const unsigned someWin = ( isThree[numbits(board[player] & 0444u)] |
                              isThree[numbits(board[player] & 0222u)] |
                              isThree[numbits(board[player] & 0111u)] |
                              isThree[numbits(board[player] & 0700u)] |
                              isThree[numbits(board[player] & 0070u)] |
                              isThree[numbits(board[player] & 0007u)] |
                              isThree[numbits(board[player] & 0421u)] |
                              isThree[numbits(board[player] & 0124u)] );

   const unsigned numFilled = numbits(board[0] | board[1]);
   const unsigned idx = (~isZero[numFilled & 0x8u] & ~isZero[numFilled & 1] & 0x2u) | (someWin & 0x1u);

   print();

   static const char *const msg[] = { "Hope you are having fun ", "You win this game (but there's another one) ", "It's a cat's game anyway ", "You win this game (but there's another one) " };

   cout << msg[idx] << "player " << player + 1 << "\n" << endl;
   board[0] &= isZero[idx];
   board[1] &= isZero[idx];
}

int main(void) {

   print();

   unsigned r, c;
   for (;;) {

      const int player = calcPlayer();

      cout << "Player " << player + 1 << "\n" << endl;
      cout << "Enter row and column in range [0, 3), e.g., 0 2:" << endl;
      cin >> r >> c;

      doMove(r, c, player);
      checkWin(player);
   }
}

Author:  Insectoid [ Thu Apr 23, 2009 4:03 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

No, the contest isn't closed. I'm just accepting votes over PM to help me decide who wins.

When you PM me, include who you're voting for and why their submission is the best.

Author:  matt271 [ Thu Apr 23, 2009 6:51 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

has any1 voted for me? :]

Author:  saltpro15 [ Thu Apr 23, 2009 7:54 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

yeah, sorry insectoid but I really need to get ready for ECOO and do my homework, I don't have the time for this. I wish i did though Sad

Author:  Insectoid [ Thu Apr 23, 2009 7:58 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

Awh, oh well, saltpro. Nobody has voted yet, matt.

Author:  Insectoid [ Sat Apr 25, 2009 6:31 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

All right, looks like all the submissions are in. Contest Closed! Vote on your favorite submission via PM to me! Be sure to include why you voted for that submission!

Author:  bugzpodder [ Sun Apr 26, 2009 8:58 am ]
Post subject:  Re: RE:mini-contest: No selection-structure Tic-Tac-Toe

insectoid @ Mon Apr 20, 2009 3:37 pm wrote:
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.


ROFL

Author:  matt271 [ Thu Apr 30, 2009 12:04 am ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

i vote for myself Very Happy i feel my clever use of an array is in no way even close to a switch statement Very Happy

Author:  matt271 [ Thu May 07, 2009 2:11 pm ]
Post subject:  RE:mini-contest: No selection-structure Tic-Tac-Toe

what happened to this contest?

u got any more interesting contests Very Happy


: