Uploaded by muhammaddawoodsaeed27

Tic Tac Toe Game Group J

advertisement
Computer Systems
and Programming
TIC-TAC-TOE
Game
End Semester Project
Submitted to:
Sir Affan
By:
Group J
Muhammad Gulzaib (460917)
Jonathan Sharif (474228)
Muhammad Dawood Saeed (465231)
Introduction:
The given code represents a C++ program that introduces a text-based implementation of the
classic Tic-Tac-Toe game. This interactive game involves a human player, marked as 'X,'
competing against a computer opponent, marked as 'O,' on a 3x3 grid. The primary objective for
the player is to create a winning combination of their symbol or strategically block the computer
from achieving victory.
The program employs a nested loop structure to manage various aspects of the game, including
initializing the grid, handling user input, and implementing the core game logic. This structured
approach ensures a systematic flow of the game, providing an engaging and interactive experience
for the player.
Explanation of Code:
1. The initial step of the program involves the creation of a 3x3 character array named 'arr,' which
serves as the representation of the Tic-Tac-Toe grid. This array is pre-filled with empty spaces (' ')
to establish the initial state of the game board.
2. The flow of the game is governed by a game loop, wherein the variable 'again' controls whether
the player chooses to initiate another round after completing a game.
3. Within the confines of the game loop, the player and the computer engage in alternating turns to
make their moves. The player specifies the position to place their 'X,' while the computer
generates a random move to position an 'O' on the grid.
4. The progression of the game persists until a conclusive outcome is reached—either a clear
winner or a tied match. The determination of a winner involves scrutinizing the grid for three
consecutive 'X's or 'O's in either a row, column, or diagonal configuration.
5. Throughout the game, the program regularly displays the current state of the game board
following each move. Once the game concludes, it reports the victorious party or declares a tie,
ensuring the player is informed of the outcome in a clear and informative manner.
Code:
1. #include<bits/stdc++.h>
2. using namespace std;
3. int main(){
4. int y;
5. int ace=1;
6. cout<<"Hello welcome to tic tac toe. \n";
7. cout<<"You are player and you are going to play with computer. This game is for
your life. Be careful. \n";
8. cout<<"| 1 | 2 | 3 | \n";
9. cout<<"|_|_|_|\n";
10. cout<<"| 4 | 5 | 6 | \n";
11. cout<<"|_|_|_|\n";
12. cout<<"| 7 | 8 | 9 | \n";
13. cout<<"|_|_|_|\n";
14. cout<<"Press the number where you want to place 'X' \n";
15. char again='y';
16. while (again=='y'){
17. char arr[3][3]={};
18. char n=' ';
19. for (int i=0;i<3;i++){
20.
for(int j=0;j<3;j++){
21.
arr[i][j]=n;
22.
23.
}
24. }
25. cout<<endl;
26. char place;
27. for (int j=0;j<9;j++){
28. cout<<"Enter your choice \n";
29. cin>>place;
30. if(place=='1'&&arr[0][0]==' '){
31.
arr[0][0]='X';
32. }
33. else if(place=='2'&&arr[0][1]==' '){
34.
arr[0][1]='X';
35. }
36. else if(place=='3'&&arr[0][2]==' '){
37.
38. }
39.
arr[0][2]='X';
else if(place=='4'&&arr[1][0]==' '){
arr[1][0]='X';
}
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
else if(place=='5'&&arr[1][1]==' '){
arr[1][1]='X';
}
else if(place=='6'&&arr[1][2]==' '){
arr[1][2]='X';
}
else if(place=='7'&&arr[2][0]==' '){
arr[2][0]='X';
}
else if(place=='8'&&arr[2][1]==' '){
arr[2][1]='X';
}
else if(place=='9'&&arr[2][2]==' '){
arr[2][2]='X';
}
else
{
cout<<" Invalid or place already taken \n";
continue;
}
if (arr[0][0] ==arr[0][1] && arr[0][1]=='X'&&arr[0][2]==' '){
arr[0][2]='O';
}
else if(arr[0][1] ==arr[0][2] && arr[0][2]=='X'&&arr[0][0]==' '){
arr[0][0]='O';
}
else if(arr[0][0] ==arr[0][2] && arr[0][2]=='X'&&arr[0][1]==' '){
arr[0][1]='O';
}
else if (arr[1][0] == arr[1][1] && arr[1][1]=='X'&&arr[1][2]==' '){
75.
76.
77.
arr[1][2]='O';
}
else if(arr[1][1] ==arr[1][2] && arr[1][2]=='X'&&arr[1][0]==' '){
arr[1][0]='O';
78.
79.
}
else if(arr[1][0] ==arr[1][2]&&arr[1][2]=='X'&&arr[1][1]==' '){
arr[1][1]='O';
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
}
else if (arr[2][0] == arr[2][1] && arr[2][1]=='X'&&arr[2][2]==' '){
arr[2][2]='O';
}
else if(arr[2][1] ==arr[2][2] && arr[2][2]=='X'&&arr[2][0]==' '){
arr[2][0]='O';
}
else if(arr[2][0] == arr[2][2] && arr[2][2]=='X'&&arr[2][1]==' '){
arr[2][1]='O';
}
else if (arr[0][0] == arr[1][0] && arr[1][0]=='X'&&arr[2][0]==' '){
arr[2][0]='O';
}
else if(arr[0][0] == arr[2][0] && arr[2][0]=='X'&&arr[1][0]==' '){
arr[1][0]='O';
}
else if(arr[1][0] == arr[2][0] && arr[2][0]=='X'&&arr[0][0]==' '){
arr[0][0]='O';
}
else if (arr[0][1] ==arr[1][1] && arr[1][1]=='X'&&arr[2][1]==' '){
arr[2][1]='O';
}
else if(arr[0][1] == arr[2][1] && arr[2][1]=='X'&&arr[1][1]==' '){
arr[1][1]='O';
}
else if(arr[2][1] == arr[1][1] && arr[1][1]=='X'&&arr[0][1]==' '){
arr[0][1]='O';
113.
114.
115.
}
else if (arr[0][2] == arr[1][2] && arr[1][2]=='X'&&arr[2][2]==' '){
arr[2][2]='O';
116.
117.
118.
}
else if(arr[1][2] ==arr[2][2] && arr[2][2]=='X'&&arr[0][2]==' '){
arr[0][2]='O';
}
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
else if(arr[0][2] == arr[2][2] && arr[2][2]=='X'&&arr[1][2]==' '){
arr[1][2]='O';
}
else if (arr[0][0] == arr[1][1] && arr[1][1]=='X'&&arr[2][2]==' '){
arr[2][2]='O';
}
else if(arr[0][0] ==arr[2][2] && arr[2][2]=='X'&&arr[1][1]==' '){
arr[1][1]='O';
}
else if(arr[1][1] == arr[2][2] && arr[2][2]=='X'&&arr[0][0]==' '){
arr[0][0]='O';
}
else if (arr[0][2] == arr[1][1] && arr[1][1]=='X'&&arr[2][0]==' '){
arr[2][0]='O';
}
else if(arr[1][1] == arr[2][0] && arr[2][0]=='X'&&arr[0][2]==' '){
arr[0][2]='O';
}
else if(arr[0][2] == arr[2][0] && arr[2][0]=='X'&&arr[1][1]==' '){
arr[1][1]='O';
}
else
{
for(int u=0;u<100;u++){
ace=(rand()%9)+1;
148.
149.
150.
//ace= ((ace * 37 + 13) % 100)%9+1;
if(place==1&&arr[0][0]==' '){
arr[0][0]='O';
break;
151.
152.
153.
154.
155.
156.
157.
158.
}
else if(ace==2&&arr[0][1]==' '){
arr[0][1]='O';
break;
}
else if(ace==3&&arr[0][2]==' '){
arr[0][2]='O';
break;
}
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
else if(ace==4&&arr[1][0]==' '){
arr[1][0]='O';
break;
}
else if(ace==5&&arr[1][1]==' '){
arr[1][1]='O';
break;
}
else if(ace==6&&arr[1][2]==' '){
arr[1][2]='O';
break;
}
else if(ace==7&&arr[2][0]==' '){
arr[2][0]='O';
break;
}
else if(ace==8&&arr[2][1]==' '){
arr[2][1]='O';
break;
}
else if(ace==9&&arr[2][2]==' '){
arr[2][2]='O';
break;
}
else
{
continue;
186.
187. }
188.
}
}
for (int z=0;z<3;z++){
189.
190.
191.
192.
193.
194.
195.
196.
197.
for(int k=0;k<3;k++){
cout<<"| "<<arr[z][k];
if(k==2){
cout<<"|";
}
}
cout<<endl;
cout<<"|_||_|"<<endl;
}
198.
if ((arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2] && arr[0][0] ==
'X') ||
199.
(arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2] && arr[1][0] == 'X') ||
200.
(arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2] && arr[2][0] == 'X') ||
201.
(arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0] && arr[0][0] == 'X') ||
202.
(arr[0][1] == arr[1][1] && arr[1][1] == arr[2][1] && arr[0][1] == 'X') ||
203.
(arr[0][2] == arr[1][2] && arr[1][2] == arr[2][2] && arr[0][2] == 'X') ||
204.
(arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] == 'X') ||
205.
(arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[0][2] == 'X')) {
206.
cout << "Player wins" << endl;
207.
break;
208.
}
209.
else if ((arr[0][0] == arr[0][1] && arr[0][1] == arr[0][2] && arr[0][0] == 'O') ||
210.
(arr[1][0] == arr[1][1] && arr[1][1] == arr[1][2] && arr[1][0] == 'O') ||
211.
(arr[2][0] == arr[2][1] && arr[2][1] == arr[2][2] && arr[2][0] == 'O') ||
212.
(arr[0][0] == arr[1][0] && arr[1][0] == arr[2][0] && arr[0][0] == 'O') ||
213.
(arr[0][1] == arr[1][1] && arr[1][1] == arr[2][1] && arr[0][1] == 'O') ||
214.
(arr[0][2] == arr[1][2] && arr[1][2] == arr[2][2] && arr[0][2] == 'O') ||
215.
(arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[0][0] == 'O') ||
216.
(arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[0][2] == 'O')) {
217.
cout << "computer wins" << endl;
218.
break;
219.
}
220.
else
if((arr[0][0]=='X'||arr[0][0]=='O')&&(arr[0][1]=='X'||arr[0][1]=='O')&&(arr[0][2]=='X'||
arr[0][2]=='O')&&
(arr[1][0]=='X'||arr[1][0]=='O')&&(arr[1][1]=='X'||arr[1][1]=='O')&&(arr[1][2]=='X'||arr[1]
[2]=='O')&&
221.
(arr[2][0]=='X'||arr[2][0]=='O')&&(arr[2][1]=='X'||arr[2][1]=='O')&&(arr[2][2]=='X'||
arr[2][2]=='O')){
222.
cout<<"Its a tie "<<endl;
223.
}
224.
225.
226.
}
227.
cout<<"Would You like to play it again. press 'y' for yes or 'n' for no and press
enter ";
228.
cin>>again;
229. }
230.
231.
return 0;
232. }
Outputs:
The program meticulously communicates the evolving status of the Tic-Tac-Toe board by
providing detailed output after every player moves. Additionally, it ensures that the conclusive
outcome of each game is effectively conveyed to the user, whether it be the announcement of a
winner or the declaration of a tie. This informative feedback is prominently displayed in the
console, enhancing the user experience by offering clear and comprehensive insights into the
results of each played game.
Explanation of Outputs:
1. Following every move, a comprehensive display of the Tic-Tac-Toe board is presented,
revealing the precise positions of both 'X' and 'O' symbols. This continuous update ensures that
players have a detailed visual representation of the evolving state of the game.
2. At the conclusion of each game, the program delivers a concise and informative announcement,
clearly specifying whether the victory belongs to the player, the computer, or if the game
concludes in a tie. This detailed feedback allows players to fully comprehend the outcomes of their
strategic moves and decisions throughout the course of the game.
Future work and applications:
1. Elevating the code's capabilities can be achieved through the integration of advanced strategies
for the computer opponent. Implementing a sophisticated algorithm like minimax can significantly
enhance the gameplay, ensuring optimal decision-making and strategic depth.
2. To create a more engaging and user-friendly experience, the inclusion of Graphical User
Interfaces (GUIs) stands as a viable enhancement. This addition not only streamlines interaction
but also adds a visually intuitive layer to the gameplay, making it more accessible and enjoyable
for users.
3. The introduction of multiplayer functionality represents another valuable avenue for
improvement. By allowing two human players to engage in head-to-head competition, the code
can cater to a broader audience and provide an enriched gaming experience, fostering friendly
rivalry and interactive gameplay.
4. Beyond immediate enhancements, the code holds potential as an educational tool for grasping
fundamental game development concepts and algorithms. It serves as a robust foundation for
learners, offering insights into the intricacies of creating and optimizing games, thus facilitating a
deeper understanding of programming principles within the context of game development.
Conclusion:
The presented C++ code provides a foundational framework for a straightforward Tic-Tac-Toe
game featuring rudimentary player-computer interaction. While the current implementation
functions as intended, there exists ample space for refinement and augmentation, aiming to
transform it into a more immersive and feature-laden gaming experience. This code not only
serves as a functional game but also serves as an entry point for individuals eager to delve into the
realms of game development and gain insights into elementary artificial intelligence concepts. Its
simplicity makes it an ideal starting point for those keen on acquiring a fundamental
understanding of the principles underlying both game creation and basic AI integration.
Download