mini-contest: No selection-structure Tic-Tac-Toe
Author |
Message |
matt271
|
Posted: 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 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Nick
|
Posted: 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): |
|
|
|
|
|
|
matt271
|
Posted: Tue Apr 21, 2009 4:21 pm Post subject: RE:mini-contest: No selection-structure Tic-Tac-Toe |
|
|
no comment on my solution? |
|
|
|
|
|
richcash
|
Posted: 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? |
|
|
|
|
|
matt271
|
Posted: 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 ]);
}
} |
|
|
|
|
|
|
Nick
|
Posted: 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? |
|
|
|
|
|
matt271
|
Posted: 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 )
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 :] |
|
|
|
|
|
matt271
|
Posted: 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, ¬valid, ¬valid };
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 _
_ _ _ |
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: 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, ¬valid, ¬valid};
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? |
|
|
|
|
|
Insectoid
|
Posted: 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. |
|
|
|
|
|
richcash
|
Posted: 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 )
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. |
|
|
|
|
|
matt271
|
Posted: 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
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 its not a conditional statement its just a very clever way to call functions
its no dif then when i said printf(msg[blah]); |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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). |
|
|
|
|
|
matt271
|
Posted: 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 |
|
|
|
|
|
OneOffDriveByPoster
|
Posted: 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);
}
} |
|
|
|
|
|
|
|
|