cindy

cindy

  • NA
  • 10
  • 0

request for help converting from c

Jan 24 2004 1:13 PM
Another girl in my sorority and I are working on a "simple" vb.net tic-tac-toe program. We have written the program in C, but are having a heck of a time converting it to vb.net. Our main problem is the display_board function. Please help us. Thank you for your help guys! This is the C script: #include #define NOUGHTS 'O' #define CROSSES 'X' #define O_WINS 79 /* ASCII Value "O" */ #define X_WINS 88 /* ASCII Value "X" */ #define NO_WINNER 2 /* winner initial value */ #define SIZE 3 #define MAX_MOVES 9 /*func. prototype*/ void display_board ( char ttt[SIZE][SIZE] ); int get_move ( char player, char ttt[SIZE][SIZE] ); int space_taken ( int mov_num, char ttt[SIZE][SIZE]); void update_board ( int mov_num, char player, char ttt[SIZE][SIZE] ); int check_for_winner ( char ttt[][SIZE], char player ); int main(void) { char ttt[SIZE][SIZE] = {{' ',' ',' '}, {' ',' ',' '}, {' ',' ',' '}}, player = CROSSES; int winner = 2, move, move_num = 1; display_board( ttt ); while ( (move_num <= MAX_MOVES) && (winner == NO_WINNER) ) { move = get_move(player,ttt); update_board( move, player, ttt ); display_board( ttt ); winner = check_for_winner( ttt, player ); if (player == CROSSES) player = NOUGHTS; else player = CROSSES; move_num++; } printf("Game Over..."); if ( winner == X_WINS ) printf("Crosses Win in %d Moves.\n", move_num-1); else if ( winner == O_WINS ) printf("Noughts Win in %d Moves.\n", move_num-1); else printf("No Winner.\n"); return (0); } /* Shows boardgame before every move to see which spots * are free/occupied */ void display_board( char board[][SIZE] ) { printf("\n %c | %c | %c \n", board[0][0], board[0][1], board[0][2]); printf(" ---+---+---\n"); printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]); printf(" ---+---+---\n"); printf(" %c | %c | %c \n\n", board[2][0], board[2][1], board[2][2]); } int check_for_winner( char ttt[][SIZE], char player ) { if(((ttt[0][0] == player) && (ttt[0][1] == player) && (ttt[0][2] == player)) || ((ttt[1][0] == player) && (ttt[1][1] == player) && (ttt[1][2] == player)) || ((ttt[2][0] == player) && (ttt[2][1] == player) && (ttt[2][2] == player)) || ((ttt[0][0] == player) && (ttt[1][0] == player) && (ttt[2][0] == player)) || ((ttt[0][1] == player) && (ttt[1][1] == player) && (ttt[2][1] == player)) || ((ttt[0][2] == player) && (ttt[1][2] == player) && (ttt[2][2] == player)) || ((ttt[0][0] == player) && (ttt[1][1] == player) && (ttt[2][2] == player)) || ((ttt[0][2] == player) && (ttt[1][1] == player) && (ttt[2][0] == player))) { return( (int)player ); } else { return( NO_WINNER ); } } int get_move(char player, char ttt[SIZE][SIZE]) { int mov_num; if(player == NOUGHTS) printf("Turn: NOUGHTS...Choose a field (1-9): "); else if(player == CROSSES) { printf("Turn: CROSSES...Choose a field (1-9): "); } scanf("%d", &mov_num); while (mov_num < 1 || mov_num > 9 || space_taken(mov_num,ttt)) { printf("Invalid Move...\n\n"); if(player == NOUGHTS) printf("Turn: NOUGHTS...Choose a field (1-9): "); else if(player == CROSSES) { printf("Turn: CROSSES...Choose a field (1-9): "); } scanf("%d", &mov_num); } return(mov_num); } /*Finds which spots are occupied */ int space_taken(int mov_num, char ttt[SIZE][SIZE]) { int row, col; row = (mov_num - 1) / 3; col = (mov_num - 1) % 3; if (ttt[row][col] == ' ') return ( 0 ); /* Space Available */ else return ( 1 ); /* Space Taken */ } /*Shows status quo of board. */ void update_board ( int mov_num, char player, char ttt[SIZE][SIZE] ) { int row, col; row = (mov_num - 1) / 3; col = (mov_num - 1) % 3; ttt[row][col] = player; }

Answers (7)