Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int turn; /* flag for the player's turn. 0 = player X, 1 = player O */
- char* board[9]; /* cells in the tic-tac-toe board */
- /* get input from the user for what cell to play in */
- int get_input() {
- /* get user input */
- printf("Position to play: ");
- int pos;
- scanf("%d", &pos);
- pos--;
- /* error check the input */
- if (pos < 0 || pos > 8) {
- printf("Invalid position (choose between 1 and 9)\n");
- return get_input();
- }
- /* make sure nobody has played in the cell before */
- if (board[pos] != "-") {
- printf("Somebody has already played there. Try again...\n");
- return get_input();
- }
- return pos;
- }
- /* display game board to the console */
- void display_board() {
- for (int x=0; x<9; x++) {
- /* new line after every 3 cells to form the grid output */
- if (x > 0 && x % 3 == 0) {
- printf("\n");
- }
- /* print either X or O depending whats in the cell. */
- /* print the cell number if the cell is empty */
- if (board[x] == "-") {
- printf("%d ", x+1);
- } else {
- printf("%s ", board[x]);
- }
- }
- printf("\n\n");
- }
- /* make a player move (place X or O in a cell) */
- void make_move(int pos) {
- board[pos] = turn ? "O" : "X";
- turn = !turn;
- }
- /* check if the game is over */
- int game_over() {
- /* horizontal wins */
- if (board[0] != "-" && board[0] == board[1] && board[1] == board[2]) {
- printf("Player %s has won!\n", board[0]);
- return 1;
- }
- if (board[3] != "-" && board[3] == board[4] && board[4] == board[5]) {
- printf("Player %s has won!\n", board[3]);
- return 1;
- }
- if (board[5] != "-" && board[5] == board[6] && board[6] == board[7]) {
- printf("Player %s has won!\n", board[5]);
- return 1;
- }
- /* vertical wins */
- if (board[0] != "-" && board[0] == board[3] && board[3] == board[6]) {
- printf("Player %s has won!\n", board[0]);
- return 1;
- }
- if (board[1] != "-" && board[1] == board[4] && board[4] == board[7]) {
- printf("Player %s has won!\n", board[1]);
- return 1;
- }
- if (board[2] != "-" && board[2] == board[5] && board[5] == board[8]) {
- printf("Player %s has won!\n", board[2]);
- return 1;
- }
- /* diagonal wins */
- if (board[0] != "-" && board[0] == board[4] && board[4] == board[8]) {
- printf("Player %s has won!\n", board[0]);
- return 1;
- }
- if (board[2] != "-" && board[2] == board[4] && board[4] == board[6]) {
- printf("Player %s has won!\n", board[2]);
- return 1;
- }
- /* its a tie when all cells are filled */
- for (int i=0; i<9; i++) {
- if (board[i] == "-") {
- return 0;
- }
- }
- printf("It's a tie!\n");
- return 1;
- }
- int main() {
- /* setup default game board */
- for (int i=0; i<9; i++) {
- board[i] = "-";
- }
- printf("Welcome to Tie-Tac-Toe\n");
- printf("Player X will go first.\n\n");
- display_board();
- /* game loop until somebody wins or you both tie */
- while (!game_over()) {
- int position = get_input();
- make_move(position);
- display_board();
- }
- printf("The game has ended.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement