Last active
August 29, 2015 14:17
-
-
Save willxinc/19a29b3cc685f7431e82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
char check_board(char board[3][3]){ | |
for (int i = 0; i < 3; ++i){ | |
if (board[i][0] == board[i][1] && board[i][1] == board[i][2]){ | |
if (board[i][0] != '?'){ | |
return board[i][0]; | |
} | |
} | |
if (board[0][i] == board[1][i] && board[1][i] == board[2][i]){ | |
if (board[0][i] != '?'){ | |
return board[0][i]; | |
} | |
} | |
} | |
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]){ | |
if (board[0][0] != '?'){ | |
return board[0][0]; | |
} | |
} | |
if (board[0][2] == board[1][1] && board[1][1] == board[2][0]){ | |
if (board[0][2] != '?'){ | |
return board[0][2]; | |
} | |
} | |
return '?'; | |
} | |
int main(){ | |
char board[3][3] = | |
{ | |
{'?', '?', '?'}, | |
{'?', '?', '?'}, | |
{'?', '?', '?'} | |
}; | |
char winner = ' '; | |
char current = 'X'; | |
int moves = 0; | |
int target = -1; | |
while (moves < 9){ | |
system("cls"); | |
printf("%c's turn! Please enter 1-9 to make your move.\n\n", current); | |
for (int i = 0; i < 3; i++){ | |
printf("%c %c %c\n", board[i][0], board[i][1], board[i][2]); | |
} | |
winner = check_board(board); | |
if (winner != '?'){ | |
break; | |
} | |
printf("\n"); | |
scanf("%d", &target); | |
--target; | |
if (target < 0 || target > 9 || board[target / 3][target % 3] != '?'){ | |
continue; | |
} | |
board[target / 3][target % 3] = current; | |
current = (current == 'X') ? 'O' : 'X'; | |
++moves; | |
} | |
switch (winner){ | |
case '?': | |
printf("Tie!\n"); | |
break; | |
case 'X': | |
printf("X Wins!\n"); | |
break; | |
case 'O': | |
printf("O Wins!\n"); | |
break; | |
} | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment