Tic-Tac Toe: C++ Programming Language
Tic-Tac Toe: C++ Programming Language
Tic-Tac Toe
Made by: Murtaza Khambati Under the guidance of: Ms. Shraddha
Acknowledgement
We would like to express our greatest gratitude to the people who have helped & supported us throughout our project. We are grateful to Ms. shraddha for her continuous support for the project, from initial advice & encouragement to this day.
Special thanks of ours goes to our colleagues who helped us in completing the project by giving interesting ideas, thoughts & made this project easy and accurate. we wish to thank our parents for their undivided support &
interest who inspired & encouraged us to go our own way, without which we would be unable to complete our project. At last but not the least we want to thank our friends who appreciated us for our work & motivated us and finally to God who made all the things possible...
Index Page No.
4
1.
1 5 17 19
2.
3.
4.
Project synopsis
Project title: Software used: Turbo C++ Language used : C++ programming (object oriented programming language) Use of project:
Tic-tac-toe is not a very challenging game for human beings. If youre an enthusiast, youve probably moved from the basic game to some variant like three-dimensional tic-tac-toe on a larger grid. If you sit down right now to play ordinary three-by-three tic-tac-toe with a friend, what will probably happen is that every game will come out a tie. Both you and your friend can probably play perfectly, never making a mistake that would allow your opponent to win. you probably arent even aware of alternative possibilities; you just look at the board and instantly know where you want to move. That kind of instant knowledge is great for human beings, because it makes you a fast player. But it isnt much help in writing a computer program. For that, you have to know very explicitly what your strategy is. By the way, although the example of tic-tac-toe strategy is a relatively trivial one, this issue of instant knowledge versus explicit rules is a hot one in modern psychology. This game is really good for entertainment in your leisure time!!
Coding
6
#include <iostream> #include <conio.h> using namespace std; int gameover= 0; int pos[9]={1,2,3,4,5,6,7,8,9}; char set[9]={'1','2','3','4','5','6','7','8','9'}; int taken[9]={0,0,0,0,0,0,0,0,0}; char cPlayer1 = 'X'; char cPlayer2 = 'O'; void print_board_start() //Prints board at the start of the program { cout<<"Welcome to Tic Tac Toe."<<endl; cout<<"|"<<pos[0]<<"|"<<pos[1]<<"|"<<pos[ 2]<<"|"<<endl; cout<<"-------"<<endl; cout<<"|"<<pos[3]<<"|"<<pos[4]<<"|"<<pos[ 5]<<"|"<<endl; cout<<"-------"<<endl;
cout<<"|"<<pos[6]<<"|"<<pos[7]<<"|"<<pos[ 8]<<"|"<<endl; cout<<"-------"<<endl; } void input_board_x(int user_pos_x) //Sets the value for Player 1(X). { set[user_pos_x-1]= 'X'; } void input_board_y(int user_pos_y) //Sets position for Player 2(O) { set[user_pos_y-1]= 'O'; } void final_result_board() //Prints board after the game is won. { cout<<"|"<<set[0]<<"|"<<set[1]<<"|"<<set[2] <<endl; cout<<"-------"<<endl; cout<<"|"<<set[3]<<"|"<<set[4]<<"|"<<set[5] <<endl;
8
int check_gameover() // Checks if the game is over { //***---Checking if game is over.***---// //First possibilty if(set[0] == set[1] && set[2] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl;
9
} //Second possiblity if(set[0] == set[3] && set[6] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Third possibility if(set[0] == set[4] && set[8] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl;
10
} else cout<<"Player 2 wins!"<<endl; } //Fourth possibility if(set[1] == set[4] && set[7] == set[1]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[1] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Fifth possibility if(set[2] == set[5] && set[2] == set[8]) { cout<<"GAME OVER!"<<endl; gameover = 1;
11
if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Sixth possibilty if(set[2] == set[4] && set[4] == set[6]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Seventh possibilty if(set[6] == set[7] && set[8] == set[6])
12
{ cout<<"GAME OVER!"<<endl; gameover = 1; if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } return gameover; } int check_duplicate(int i) //Checks if any of the two players add a duplicate data. { while(taken[i-1] == 1) { cout<<i<<" is already taken, please select another spot "; cin>>i; }
13
return i; } int main() { int count = 0; int x_pos,y_pos; print_board_start(); cout<<"Player 1 : X"<<endl<<"Player 2 : 0"<<endl; while(gameover==0 && count<=8) { count++; cout<<"Enter the position for X :"<<endl; cin>>x_pos; while(x_pos>9 || x_pos <1 ) { cout<<"Invalid Move! Please select position from 1 to 9: "; cin>>x_pos; }
14
if(taken[x_pos-1] == 1) { x_pos=check_duplicate(x_pos); } input_board_x(x_pos); //Setting the position of X. taken[x_pos -1] = 1; ////Setting the user entered position to taken. check_gameover(); game is over. //Check if the
if(gameover == 1) //If game ends, this part of code will terminate the while loop { break; } if(count == 9) //If there have been 9 moves till now, loops stop executing { break; } count++;
15
final_result_board(); //Prints current game progress cout<<"Enter the position for 0 : "<<endl; cin>>y_pos; while(y_pos>9 || y_pos <1 ) //If user enters invalid move. That is more than 9 and less than 0. { cout<<"Invalid Move! Please select position from 1 to 9: "; cin>>y_pos; } if(taken[y_pos-1] == 1) { y_pos=check_duplicate(y_pos); } input_board_y(y_pos); position of Y. //Setting the
taken[y_pos -1] = 1; //Setting the user entered position to taken. check_gameover(); game is over // Checks if the
16
final_result_board(); //Prints current game progress if(gameover == 1) //If game ends, this part of code will terminate the while loop { break; } } if(gameover == 0) //If there is no result, i.e gameover stays at 0 after 9 iteration. { cout<<"Draw"<<endl; } final_result_board(); getch(); }
OUTPUT
17
18
19
Bibliography
20