arrays - random move tic tac toe in c -
alright, i'm new @ coding in c , have lot of copy , paste in code. (it's final project, don't have time optimize , shorten until end if time permits). anyway, i'm gonna avoid posting code reason. i'm gonna post relevant part i'm trying do.
i need game pick random number 1-9 , use number select move on board. added printf in part of code make sure number being picked, when go scanf, game gives blinking prompt , not continue.
does see wrong or need post other section of code? appreciated!! have included 1 player portion of game , when computer supposed move. removed win checks , re printing of board. let me know if guys need see else didn't want have huge block of code.
if (player == 1) { for(k=0;k<9;k++) // 9 moves max. { printf("\n\n"); // print board again. printf(" %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", board[2][0], board[2][1], board[2][2]); { printf("player 1, move? enter 1-9\n"); scanf("%d", &move); if (move == 1) board[0][0] = '1'; if (move == 2) board[0][1] = '1'; if (move == 3) board[0][2] = '1'; if (move == 4) board[1][0] = '1'; if (move == 5) board[1][1] = '1'; if (move == 6) board[1][2] = '1'; if (move == 7) board[2][0] = '1'; if (move == 8) board[2][1] = '1'; if (move == 9) board[2][2] = '1'; }while(move>9 && move <1); { printf("computer moves..."); move = rand() % 9 + 1; printf("%d", move); scanf("%d", &move); if (move == 1) board[0][0] = '2'; if (move == 2) board[0][1] = '2'; if (move == 3) board[0][2] = '2'; if (move == 4) board[1][0] = '2'; if (move == 5) board[1][1] = '2'; if (move == 6) board[1][2] = '2'; if (move == 7) board[2][0] = '2'; if (move == 8) board[2][1] = '2'; if (move == 9) board[2][2] = '2'; }while(move>9 && move <1); }
it appears need reamove scanf()
code, scanf()
blocks until input given program, returns number of items matched format string if any, input.
if generating number rand()
don't need input @ all.
please read this, understand scanf()
for.
Comments
Post a Comment