i made a code of [X-O]
i made a 2D array of type character in game level 1 only
i wanted to show what is saved in the memory of the array
CODE
// FCI - Year 1 - Programming 1 - Course Project
// Team Name: The Champions
// Program Name: X-O game.cpp
// Last Modification Date: xx/xx/xxxx
// Author: Mohamed Ahmed Mostafa ID: 20070244
// Author: Mohamed Hossam Eldeen ID: 20070252
// Author: Omar Mohsen Elgohary ID: 20070410
// Author: Assim abdelmenaem Tolba ID: 20070408
// Purpose: A game to be played either by 2 players
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct highscore // structure to have the name and score of each player
{
string name;
int score;
};
// End of the Structure
// Function to add the name and score for player in the level played
void append (string file, string target , int s)
{
highscore y;
y.name=target;
y.score=s;
ofstream x;
x.open((file.c_str()),ios::app);
x<<y.name<<y.score;
x.close();
}
// End of the function
// Function to check the player's name wheter found before or not
// If found update his score
// If not found create a new score for him
void check_player_name(string file, string target , int s)
{
int i=0;
ofstream q;
ifstream r;
r.open((file.c_str()));
highscore b[10];
while (r.is_open() && !r.eof())
{
r>> b[i].name >> b[i].score;
i++;
}
r.close();
for(i=0;i<10;i++)
{
if (target.compare(b[i].name) == 0)
break;
}
if ( i == 10)
append (file , target , s);
else
{
q.open((file.c_str()));
b[i].score<<s;
for (int j=0; j<10; j++)
{
q << b[j].name <<b[j].score;
}
q.close();
}
}
// End of the function
void game_level_2(int &check2)
{
string high_scores_level_2 = "high_scores_level_2";
string name1;
string name2;
int score1=0;
int score2=0;
int check =0;
int r , c,w,z;
cout<<" Player 1 will sign ' x ' while player 2 will sign ' o '"<<endl<<endl;
int x[4][4];
for (int j=0; j<4; j++)
{
cout<< setw(6)<<"l"<<setw(6)<<"l"<<setw(6)<<"l";
cout<<endl;
for (int i=0; i<4; i++)
cout<<setw(5)<<" ____";
cout<<endl;
}
for (z=0; z<16; z++)
{
cout<<" Player 1: enter first the number of the row then number of cloumb"<<endl;
cout<<" to mark your sign";
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=1;
if (x[0][0] ==1 && x[0][1] ==1 && x[0][2] == 1 && x[0][3]== 1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[1][0] ==1 && x[1][1] ==1 && x[1][2] == 1 && x[1][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[2][0] ==1 && x[2][1] ==1 && x[2][2] == 1 && x[2][3]==1 )
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[3][0] ==1 && x[3][1] ==1 && x[3][2] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][1] ==1 && x[2][2] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==1 && x[1][2] ==1 && x[2][1] == 1 && x[3][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][0] ==1 && x[2][0] == 1 && x[3][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][1] ==1 && x[1][1] ==1 && x[2][1] == 1 && x[3][1]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][2] ==1 && x[2][2] == 1 && x[3][2]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==1 && x[1][3] ==1 && x[2][3] == 1 && x[3][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
}
cout<< " Now player 2 enter the number of row then coloumb"<<endl;
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=2;
if (x[0][0] ==2 && x[0][1] ==2 && x[0][2] == 2 && x[0][3] ==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[1][0] ==2 && x[1][1] ==2 && x[1][2] == 2 && x[1][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[2][0] ==2 && x[2][1] ==2 && x[2][2] == 2 && x[2][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[3][0] ==2 && x[3][1] ==2 && x[3][2] == 2 && x[3][3]== 2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][1] ==2 && x[2][2] == 2 && x[3][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][3] ==2 && x[1][2] ==2 && x[2][1] == 2 && x[3][0]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][0] ==2 && x[2][0] == 2 && x[3][0]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][1] ==2 && x[1][1] ==2 && x[2][1] ==2 && x[3][1]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][2] ==2 && x[2][2] ==2 && x[3][2]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][3] ==2 && x[1][3] ==2 && x[2][3] ==2 && x[3][3]==2)
{
cout<<"player 2 wins"<<endl;
score2+=1;
check = 2;
break;
}
}
}
if (check == 0)
{
cout<<" drow !!!"<<endl;
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==1)
{
cout<<" Player 1 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name1;
check_player_name(high_scores_level_2 ,name1 ,score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==2)
{
cout<<" Player 2 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name2;
check_player_name(high_scores_level_2 , name2 , score2);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
}
void game_level_1(int &check2, string &name1, string &name2)
{
string high_scores_level_1 = "high_scores_level_1";
int score1=0;
int score2=0;
int check =0;
int r , c,w,z;
cout<<name1<<" your sign is ' x ' while "<<name2<<" your sign is ' o '"<<endl<<endl;
char x[3][3];
for (int j=0; j<3; j++)
{
cout<< setw(5)<<"I"<<setw(5)<<"I"<<setw(5)<<"l";
cout<<endl;
for (int i=0; i<3; i++)
cout<<setw(4)<<" ____";
cout<<endl;
}
for (z=0; z<9; z++)
{
cout<<name1<<": enter first the number of the row then number of cloumb"<<endl;
cout<<" to mark your sign"<<endl;
cin>>r>>c;
if (x[r][c] == 'X' || x[r][c] == 'X')
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]='X';
for (int g=0; g<3; g++)
{
for (int h=0; h<3; h++)
cout<<x[g][h];
}
if (x[0][0] =='X' && x[0][1] =='X' && x[0][2] =='X')
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[1][0] ==1 && x[1][1] ==1 && x[1][2] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[2][0] ==1 && x[2][1] ==1 && x[2][2] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][1] ==1 && x[2][2] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][1] ==1 && x[2][0] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][0] ==1 && x[2][0] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][1] ==1 && x[1][1] ==1 && x[2][1] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][2] ==1 && x[2][2] == 1)
{
cout<<name1<<" wins"<<endl;
score1++;
check = 1;
break;
}
}
cout<<name2<<" : enter the number of row then coloumb"<<endl;
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]='O';
for (int g=0; g<3; g++)
{
for (int h=0; h<3; h++)
cout<<x[g][h];
}
if (x[0][0] ==2 && x[0][1] ==2 && x[0][2] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[1][0] ==2 && x[1][1] ==2 && x[1][2] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[2][0] ==2 && x[2][1] ==2 && x[2][2] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][1] ==2 && x[2][2] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][1] ==2 && x[2][0] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][0] ==2 && x[1][0] ==2 && x[2][0] == 2)
{
cout<<name2<<" wins"<<endl;
score2+=1;
check = 2;
break;
}
else if (x[0][1] ==2 && x[1][1] ==2 && x[2][1] ==2)
{
cout<<name2<<" wins"<<endl;
score2++;
check = 2;
break;
}
else if (x[0][2] ==2 && x[1][2] ==2 && x[2][2] ==2)
{
cout<<name2<<" wins"<<endl;
score2++;
check = 2;
break;
}
}
}
if (check == 0)
{
cout<<" drow !!!"<<endl;
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==1)
{
cout<<name1<<" your score increased"<<endl;
check_player_name(high_scores_level_1 ,name1 ,score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==2)
{
cout<<name2<<" your score increased"<<endl;
check_player_name(high_scores_level_1 , name2 , score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
}
void game_level_3(int &check2)
{
string high_scores_level_3 = "high_scores_level_3";
string name1;
string name2;
int score1=0;
int score2=0;
int check =0;
int r , c,w,z;
cout<<" Player 1 will sign ' x ' while player 2 will sign ' o '"<<endl<<endl;
int x[5][5];
for (int j=0; j<5; j++)
{
cout<< setw(7)<<"l"<<setw(7)<<"l"<<setw(7)<<"l";
cout<<endl;
for (int i=0; i<5; i++)
cout<<setw(6)<<" ____";
cout<<endl;
}
for (z=0; z<25; z++)
{
cout<<" Player 1: enter first the number of the row then number of cloumb"<<endl;
cout<<" to mark your sign";
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=1;
if (x[0][0] ==1 && x[0][1] ==1 && x[0][2] == 1 && x[0][3]== 1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[1][0] ==1 && x[1][1] ==1 && x[1][2] == 1 && x[1][3]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[2][0] ==1 && x[2][1] ==1 && x[2][2] == 1 && x[2][3]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[3][0] ==1 && x[3][1] ==1 && x[3][2] == 1 && x[3][3]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[4][0] ==1 && x[4][1] ==1 && x[4][2] == 1 && x[4][3]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][1] ==1 && x[2][2] == 1 && x[3][3]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][4] ==1 && x[1][3] ==1 && x[2][2] == 1 && x[3][1]==1 && x[4][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==1 && x[1][0] ==1 && x[2][0] == 1 && x[3][0]==1 && x[4][0]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][1] ==1 && x[1][1] ==1 && x[2][1] == 1 && x[3][1]==1 && x[4][1]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][2] ==1 && x[1][2] ==1 && x[2][2] == 1 && x[3][2]==1 && x[4][2]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==1 && x[1][3] ==1 && x[2][3] == 1 && x[3][3]==1 && x[4][3]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][4] ==1 && x[1][4] ==1 && x[2][4] == 1 && x[3][4]==1 && x[4][4]==1)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
}
cout<< " Now player 2 enter the number of row then coloumb"<<endl;
cin>>r>>c;
if (x[r][c] == 1 || x[r][c] == 2)
cout<<" Wrong cell to put your sign please enter in another cell"<<endl;
else
{
x[r][c]=2;
if (x[0][0] ==2 && x[0][1] ==2 && x[0][2] == 2 && x[0][3]== 2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[1][0] ==2 && x[1][1] ==2 && x[1][2] == 2 && x[1][3]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[2][0] ==2 && x[2][1] ==2 && x[2][2] ==2 && x[2][3]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[3][0] ==2 && x[3][1] ==2 && x[3][2] == 2 && x[3][3]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[4][0] ==2 && x[4][1] ==2 && x[4][2] ==2 && x[4][3]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==2 && x[1][1] ==2 && x[2][2] ==2 && x[3][3]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][4] ==2 && x[1][3] ==2 && x[2][2] ==2 && x[3][1]==2 && x[4][0]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][0] ==2 && x[1][0] ==2 && x[2][0] ==2 && x[3][0]==2 && x[4][0]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][1] ==2 && x[1][1] ==2 && x[2][1] ==2 && x[3][1]==2 && x[4][1]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][2] ==2 && x[1][2] ==2 && x[2][2] ==2 && x[3][2]==2 && x[4][2]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][3] ==2 && x[1][3] ==2 && x[2][3] ==2 && x[3][3]==2 && x[4][3]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
else if (x[0][4] ==2 && x[1][4] ==2 && x[2][4] ==2 && x[3][4]==2 && x[4][4]==2)
{
cout<<"player 1 wins"<<endl;
score1+=1;
check = 1;
break;
}
}
}
if (check == 0)
{
cout<<" drow !!!"<<endl;
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==1)
{
cout<<" Player 1 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name1;
check_player_name(high_scores_level_3 ,name1 ,score1);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2"<<endl;
cout<<" if you want to exit press 3"<<endl;
cin>> check2;
}
else if (check ==2)
{
cout<<" Player 2 your score increased"<<endl;
cout<<" Please enter your name"<<endl;
cin>> name2;
check_player_name(high_scores_level_3 , name2 , score2);
cout<<" if you want to play again press 1"<<endl;
cout<<" if you want to display high score press 2
This post has been edited by quaresma: 18 May, 2008 - 08:40 AM