Question

Write a Tic-Tac-Toe game in C language. Your project must contain at least one header file,...

Write a Tic-Tac-Toe game in C language.
Your project must contain at least one header file, two C source files.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

It is MANDATORY to save the file names as mentioned:

Save this file as tictactoe.c

 #include <stdio.h> #include <conio.h> #include "tic.h" int start(); int main() {    printf("****Tic Tac Toe - Dual Player Game****\n\n");   start();        return 0; }

Save this file as tic.h

 #include <stdio.h> #include <conio.h> char box[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; int check(); void setBoard(); extern int start() { int pl = 1, i, ch; char mark; do { setBoard(); pl = (pl % 2) ? 1 : 2; printf("Player %d, enter your spot: ", pl); scanf("%d", &ch); mark = (pl == 1) ? 'X' : 'O'; if (ch == 1 && box[1] == '1') box[1] = mark; else if (ch == 2 && box[2] == '2') box[2] = mark; else if (ch == 3 && box[3] == '3') box[3] = mark; else if (ch == 4 && box[4] == '4') box[4] = mark; else if (ch == 5 && box[5] == '5') box[5] = mark; else if (ch == 6 && box[6] == '6') box[6] = mark; else if (ch == 7 && box[7] == '7') box[7] = mark; else if (ch == 8 && box[8] == '8') box[8] = mark; else if (ch == 9 && box[9] == '9') box[9] = mark; else { printf("Invalid move "); pl--; getch(); } i = check(); pl++; }while (i == - 1); setBoard(); if (i == 1) printf("==>\aPlayer %d wins ", --pl); else printf("==>\aThe Game ends in a Draw state. Better luck next time!"); getch(); return 0; } int check() { if (box[1] == box[2] && box[2] == box[3]) return 1; else if (box[4] == box[5] && box[5] == box[6]) return 1; else if (box[7] == box[8] && box[8] == box[9]) return 1; else if (box[1] == box[4] && box[4] == box[7]) return 1; else if (box[2] == box[5] && box[5] == box[8]) return 1; else if (box[3] == box[6] && box[6] == box[9]) return 1; else if (box[1] == box[5] && box[5] == box[9]) return 1; else if (box[3] == box[5] && box[5] == box[7]) return 1; else if (box[1] != '1' && box[2] != '2' && box[3] != '3' && box[4] != '4' && box[5] != '5' && box[6] != '6' && box[7] != '7' && box[8] != '8' && box[9] != '9') return 0; else return - 1; } void setBoard() { printf("\n\n\tGood Luck\n\n"); printf("Player 1 (X) - Player 2 (O)\n\n\n"); printf(" | | \n"); printf(" %c | %c | %c \n", box[1], box[2], box[3]); printf("_____|_____|_____\n"); printf(" | | \n"); printf(" %c | %c | %c \n", box[4], box[5], box[6]); printf("_____|_____|_____\n"); printf(" | | \n"); printf(" %c | %c | %c \n", box[7], box[8], box[9]); printf(" | | \n\n"); } 
  
Add a comment
Know the answer?
Add Answer to:
Write a Tic-Tac-Toe game in C language. Your project must contain at least one header file,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT