Professional Documents
Culture Documents
Class: XI-A
Tic-Tac-Toe Game
2
INDEX
3
Department of Computer Science
CERTIFICATE
completed the investigatory project on the topic “Tic-Tac-Toe Game” during the
This project consumed huge amount of work, research and dedication. Still,
implementation would not have been possible if we did not have a support of
few individuals. Therefore, I would like to extend my sincere gratitude.
I am grateful to my Computer Science teacher, who gave me all the time needed
for guiding and clearing my doubts in every step.
5
Problem Statement
6
System Requirements
To play the game, one needs to have a working C++ runtime environment.
To view the source code, one can open it in notepad, code editor or an integrated
development environment.
7
Technology Used
Atom
Atom is a free, opensource, cross-platform code editor which can be used to act
as an IDE when paired with a compiler such as MinGW.
Characteristics of C++:
• Object-Oriented Language
• Platform Independent
• Write Once Run Anywhere(WORA)
• Highly Secure
8
Coding
1. #include <iostream>
2. using namespace std;
3. char box[10] = {
4. 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'
5. };
6. int checkwin();
7. void board();
8. int main() {
9. int player = 1, i, choice;
10. char mark;
11. do {
12. board();
13. player = (player % 2) ? 1 : 2;
14. cout << "Player " << player << ", enter your choice: ";
15. cin >> choice;
16. mark = (player == 1) ? 'X' : 'O';
17. if (choice == 1 && box[1] == '1') box[1] = mark;
18. else if (choice == 2 && box[2] == '2') box[2] = mark;
19. else if (choice == 3 && box[3] == '3') box[3] = mark;
20. else if (choice == 4 && box[4] == '4') box[4] = mark;
21. else if (choice == 5 && box[5] == '5') box[5] = mark;
22. else if (choice == 6 && box[6] == '6') box[6] = mark;
23. else if (choice == 7 && box[7] == '7') box[7] = mark;
24. else if (choice == 8 && box[8] == '8') box[8] = mark;
25. else if (choice == 9 && box[9] == '9') box[9] = mark;
26. else {
27. cout << "Wrong move ";
28. player--;
29. cin.ignore();
30. cin.get();
31. }
32. i = checkwin();
33. player++;
34. } while (i == -1);
35. board();
36. if (i == 1) cout << "==>\aPlayer " << --player << " WINS ";
37. else cout << "==>\aDRAW";
38. cin.ignore();
39. cin.get();
40. return 0;
41. }
42. int checkwin() {
43. if (box[1] == box[2] && box[2] == box[3]) return 1;
44. else if (box[4] == box[5] && box[5] == box[6]) return 1;
45. else if (box[7] == box[8] && box[8] == box[9]) return 1;
46. else if (box[1] == box[4] && box[4] == box[7]) return 1;
47. else if (box[2] == box[5] && box[5] == box[8]) return 1;
48. else if (box[3] == box[6] && box[6] == box[9]) return 1;
49. else if (box[1] == box[5] && box[5] == box[9]) return 1;
9
50. else if (box[3] == box[5] && box[5] == box[7]) return 1;
51. 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;
52. else return -1;
53. }
54. void board() {
55. system("cls");
56. cout << "\n\n\tTic Tac Toe\n\n";
57. cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
58. cout << endl;
59. cout << " | | " << endl;
60. cout << " " << box[1] << " | " << box[2] << " | " << box[3] << endl;
61. cout << "_____|_____|_____" << endl;
62. cout << " | | " << endl;
63. cout << " " << box[4] << " | " << box[5] << " | " << box[6] << endl;
64. cout << "_____|_____|_____" << endl;
65. cout << " | | " << endl;
66. cout << " " << box[7] << " | " << box[8] << " | " << box[9] << endl;
67. cout << " | | " << endl << endl;
68. }
10
Implementation
11
Conclusion
Our game successfully demonstrates the use of functions and audio in C++.
This game is based on a simple design using functions. The grid is updated as soon
as a player makes a move.
As a player makes a move, that block in the grid is blocked so that no one else can
put their mark in it.
After each move, conditions for winning(vertical, horizontal, diagonal) and draw
are checked.
If a player wins, the name of the winning player is declared and the game exits.
Bibliography
12