c - TicTacToe print winner -
i trying make tic tac toe game 2 players. pretty far , works part. can't figure out how print out string stored in array. have seen lots of loops examples. please let me know whats goin on.
enter code here
int main() { time_t t; char player1 [23]; char player2 [23]; int let; int turns = 0; printf("\n welcome galactic tic tac toe:\n"); printf("\n please enter player 1's name"); fgets(player1, 22, stdin); printf("\nplayer 2's name?\n"); fgets(player2, 22, stdin); void winner (char board [][9], char player1 [][23], char player2 [][23]){ if (board [0][0] && board [0][1] && board [0][2] == 'x'){printf("\nplayer 1 has won\n congratulations : %s ", player1);} if (board [0][3] && board [0][4] && board [0][5] == 'x'){printf("\nplayer 1 has won\n congratulations : %s ", player1);} if (board [0][6] && board [0][7] && board [0][8] == 'x'){printf("\nplayer 1 has won\n congratulations : %s ", player1);} if (board [0][0] && board [0][1] && board [0][2] == 'o'){printf("\nplayer 2 has won\n congratulations : %s ", player2);} if (board [0][3] && board [0][4] && board [0][5] == 'o'){printf("\nplayer 2 has won\n congratulations : %s ", player2);} if (board [0][6] && board [0][7] && board [0][8] == 'o'){printf("\nplayer 2 has won\n congratulations : %s ", player2);} if (board [0][0] && board [0][5] && board [0][8] == 'x'){printf("\nplayer 1 has won\n congratulations : %s ", player1);} if (board [0][2] && board [0][5] && board [0][7] == 'x'){printf("\nplayer 1 has won\n congratulations : %s ", player1);} if (board [0][0] && board [0][5] && board [0][8] == 'o'){printf("\nplayer 1 has won\n congratulations : %s ", player2);} if (board [0][2] && board [0][5] && board [0][7] == 'o'){printf("\nplayer 1 has won\n congratulations : %s ", player2);} }
your biggest problem:
if (board [0][0] && board [0][1] && board [0][2] == 'x')
this does not think does. assume checks see if 3 of spaces marked 'x'
. incorrect.
&&
boolean , operator, means left- , right-side operators evaluated (independently) booleans. you've written means:
if ( // if board[0][0] // board[0][0] non-zero && // , board[0][1] // board[0][1] non-zero && // , board[0][2] == 'x' // board[0][2] equal 'x' )
you haven't shown how initialize board (presumably space ' '
), whatever (printable) character store there evaluated boolean true.
so expression should be:
if ((board[0][0] == 'x') && (board[0][1] == 'x') && (board[0][2] == 'x'))
on next problem:
i have not understood printing loops , why typically used arrays.
well there's no such thing printing loop. there loops. can call printf
in loop if want to.
to understand why need loops, consider different version of tic-tac-toe board 100 x 100. "did win?" logic then?
it should easy see coding if( board[0][0] .... board[99][99]
, possible combinations become exhausting human code. computers on other hand enjoy doing same (or similar) tasks repetitively. it's human (you) ensure energy spent on useful.
Comments
Post a Comment