syntax - Tictactoe c program error with main function -
i'm creating simple tictactoe game in c. keep getting error in main function, don't know expected expression wants before else statement. how program works symbols both players, begin game.
error:
tictac.c: in function ‘main’: tictac.c:31: error: expected expression before ‘else’ tictac.c: @ top level: tictac.c:49: warning: conflicting types ‘print’ tictac.c:30: warning: previous implicit declaration of ‘print’ here tictac.c:63: error: conflicting types ‘check’ tictac.c:63: note: argument type has default promotion can’t match empty parameter name list declaration tictac.c:29: error: previous implicit declaration of ‘check’ here tictac.c:89: warning: conflicting types ‘move’ tictac.c:28: warning: previous implicit declaration of ‘move’ here
code:
char board[3][3]; int main(void) { int first; char player1, player2; printf("player 1: choose symbol: \n"); player1 = getchar(); printf("player 2: choose symbol: \n"); player2 = getchar(); int i=0; int win; char turn; while(win == 0) { if((i%2) == 0) turn = player1; move(player1); win = check(player1); print(); else turn = player2; move(player2); i++; } if (i == 8) printf("its tie"); else printf("the winner %c", turn); return 0; } /*printing board takes in placement int*/ void print(void) { int r; printf("\n"); (r = 0; r < 3; r++){ printf(" %c | %c | %c \n" , board[r][0], board[r][2], board[r][3]); if (r != 2) printf("___________\n"); } printf("\n"); return; } /*check see if won*/ int check(char player) { int r, c; ( r = 0 ; r <3 ; r++) { if ((board[r][0] == player) && (board[r][1] == player) && (board[r][2] == player)) return 1; } ( c = 0 ; c <3 ; c++) { if ((board[0][c] == player) && (board[1][c] == player) && (board[2][c] == player)) return 1; } if((board[0][0] == player) && (board[1][1] == player) && (board[2][2] == player)) return 1; if((board[0][2] == player) && (board[1][1] == player) && (board[2][0] == player)) return 1; return 0; } void move(char player) { int place; printf("player1, enter placement: \n"); scanf("%d", &place); if (place == 1) board[0][0] = player; else if (place == 2) board[0][1] = player; else if (place == 3) board[0][2] = player; else if (place == 4) board[1][0] = player; else if (place == 5) board[1][1] = player; else if (place == 6) board[1][2] = player; else if (place == 7) board[2][0] = player; else if (place == 8) board[2][1] = player; else if (place == 9) board[2][2] = player; }
you can omit curly braces if have 1 line after if/else. error here:
if((i%2) == 0) turn = player1; move(player1); win = check(player1); print(); else turn = player2; move(player2);
you need curly braces. tip, @ bottom instead of 9 if statements, this:
board[(place-1)/3][(place+2) % 3] = player;
and should equivalent 9 if statements in move() function.
Comments
Post a Comment