hello,

im just learning c++ and was wondering if any one could spare the time to explain to me what each line of the ollowing code does,

this is no my code i was just hoping to see what each lines does to see if it wil make it clearer.

thank you

#include <iostream>
#include <vector>
using namespace std;
int const rows=6;
int const cols=7;
int const connect=4;

void Who_Moves( char cur_val)
{
 
  if (cur_val=='-')
    cout << " "; // val is 0, so draw a space
  else if (cur_val=='O')
    cout << "O"; // val is 1, so draw a 'O'
  else if (cur_val=='*')
    cout << "*"; // val is 2, so draw a '*'
}
 
bool Is_Legal(const vector<vector<char> >Board, int cur_col)
{
  if(Board[0][cur_col] != '-')
        return false;
}

bool Tie_Check(const vector<vector<char> >& Board)
{
    for(int i = 0; i < rows; i++){
      for (int j = 0; j < cols; j++){
        if(Board[i][j] == '-')
        {
        return false; 
        }
            else 
           {
            return true;
           }
        }
    }
}
 
//function that checks to see if the game is over
bool End_Game_Check(const vector<vector<char> >Board, int row, int cur_col, char cur_move){
    //check for horizontal win in the current row (4 cases)
    if(Board[row][0]== cur_move && Board [row][1]== cur_move && Board [row][2]== cur_move && Board[row][3]== cur_move)
       {
        return true; 
        }
       
    if(Board[row][1]== cur_move && Board [row][2]== cur_move && Board [row][3]== cur_move && Board[row][4]== cur_move)
         {
        return true; 
        }
        
    if(Board[row][2]== cur_move && Board [row][3]== cur_move && Board [row][4]== cur_move && Board[row][5]== cur_move)
         {
        return true; 
        }
        
    if(Board[row][3]== cur_move && Board [row][4]== cur_move && Board [row][5]== cur_move && Board[row][6]== cur_move)
         {
        return true; 
        }
        
    //check for vertical win in the current column (3 cases)
    if(Board[5][cur_col] == cur_move && Board[4][cur_col] == cur_move && Board[3][cur_col] && Board[2][cur_col] == cur_move)
        {
        return true; 
        }
        
    if(Board[4][cur_col] == cur_move && Board[3][cur_col] == cur_move && Board[2][cur_col] && Board[1][cur_col] == cur_move)
        {
        return true; 
        }
       
    if(Board[3][cur_col] == cur_move && Board[2][cur_col] == cur_move && Board[1][cur_col] && Board[0][cur_col] == cur_move)
        {
        return true; 
        }
      
    //check for diagonal win that contains the current position (6 cases)
    if(Board[row][cur_col] == Board[row + 1][cur_col- 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col - 2] && Board[row + 2][cur_col - 2] == Board[row + 3][cur_col - 3])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col- 2] && Board[row + 2][cur_col- 2] == Board[row - 1][cur_col + 1])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row - 1][cur_col + 1] && Board[row - 1][cur_col + 1] == Board[row - 2][cur_col + 2])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col + 2] && Board[row][cur_col] == Board[row + 3][cur_col + 3])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col+ 2])
         {
        return true; 
        }
        
    if(Board[row][cur_col] == Board[row - 2][cur_col - 2] && Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1])
        {
        return true; 
        }
        else
        {
         return false;
        }
}
 
 

 void Print_Board(const vector<vector<char> >&Board)
{
 for (int i=0; i<rows; i++)
 { cout << endl;
  for(int j=0; j<cols; j++)
  {
   cout << Board[i][j];
  }
 cout << endl;
 }
}

 
int main() {
int cur_row; 
int cur_col;
vector<vector<char> >Board;
Board.resize(rows);
for (int i=0; i<rows; i++) { 
Board[i].resize(cols);
for (int j=0; j<cols; j++) {
   Board[i][j] = '-';
    }
}
 
 
 
 Print_Board(Board);

 bool Game_Over=false;
 char cur_move='O';//o goes first

  while (!Game_Over)
 {
  cout<<"Player" << cur_move <<" please enter the column in which you would like your marker placed"<<endl;
  cin >> cur_col;
   while(Is_Legal(Board,cur_col - 1)==false){
   cout << "illegal column please try enter column again"<<endl;
   cin >> cur_col; 
  }
  bool Place_Marker=false;
 int i=5;
    
 do{
   if (Board[i][cur_col-1]=='-')
 {
  Board[i][cur_col-1]=cur_move;
   Place_Marker=true;
 Print_Board(Board);}
 else
{
i--;
}
 }while(Place_Marker=false && i>=0);
Game_Over=End_Game_Check(Board,i , cur_col-1, cur_move);
        if(Game_Over == true)
            cout << endl << cur_move << " has won!" << endl; 
        
        if(Game_Over == false){
            bool Draw = Tie_Check(Board);
            if(Draw == true){
                cout << "There's a DRAW - GAME OVER";
                Game_Over = true;
            }
        }
 
   
   
   
  
  if(cur_move=='O')
  {
   cur_move='*';
  }
 else
 {
  cur_move='O';
 }

 
 }
 

 system("pause");
 return 0;
} 
#include <iostream>
#include <vector>
using namespace std;
int const rows=6;
int const cols=7;
int const <strong class="highlight">connect</strong>=4;

void Who_Moves( char cur_val)
{
 
  if (cur_val=='-')
    cout << " "; // val is 0, so draw a space
  else if (cur_val=='O')
    cout << "O"; // val is 1, so draw a 'O'
  else if (cur_val=='*')
    cout << "*"; // val is 2, so draw a '*'
}
 
bool Is_Legal(const vector<vector<char> >Board, int cur_col)
{
  if(Board[0][cur_col] != '-')
        return false;
}

bool Tie_Check(const vector<vector<char> >& Board)
{
    for(int i = 0; i < rows; i++){
      for (int j = 0; j < cols; j++){
        if(Board[i][j] == '-')
        {
        return false; 
        }
            else 
           {
            return true;
           }
        }
    }
}
 
//function that checks to see if the game is over
bool End_Game_Check(const vector<vector<char> >Board, int row, int cur_col, char cur_move){
    //check for horizontal win in the current row (<strong class="highlight">4</strong> cases)
    if(Board[row][0]== cur_move && Board [row][1]== cur_move && Board [row][2]== cur_move && Board[row][3]== cur_move)
       {
        return true; 
        }
       
    if(Board[row][1]== cur_move && Board [row][2]== cur_move && Board [row][3]== cur_move && Board[row][4]== cur_move)
         {
        return true; 
        }
        
    if(Board[row][2]== cur_move && Board [row][3]== cur_move && Board [row][4]== cur_move && Board[row][5]== cur_move)
         {
        return true; 
        }
        
    if(Board[row][3]== cur_move && Board [row][4]== cur_move && Board [row][5]== cur_move && Board[row][6]== cur_move)
         {
        return true; 
        }
        
    //check for vertical win in the current column (3 cases)
    if(Board[5][cur_col] == cur_move && Board[4][cur_col] == cur_move && Board[3][cur_col] && Board[2][cur_col] == cur_move)
        {
        return true; 
        }
        
    if(Board[4][cur_col] == cur_move && Board[3][cur_col] == cur_move && Board[2][cur_col] && Board[1][cur_col] == cur_move)
        {
        return true; 
        }
       
    if(Board[3][cur_col] == cur_move && Board[2][cur_col] == cur_move && Board[1][cur_col] && Board[0][cur_col] == cur_move)
        {
        return true; 
        }
      
    //check for diagonal win that contains the current position (6 cases)
    if(Board[row][cur_col] == Board[row + 1][cur_col- 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col - 2] && Board[row + 2][cur_col - 2] == Board[row + 3][cur_col - 3])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row + 2][cur_col- 2] && Board[row + 2][cur_col- 2] == Board[row - 1][cur_col + 1])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col - 1] && Board[row + 1][cur_col - 1] == Board[row - 1][cur_col + 1] && Board[row - 1][cur_col + 1] == Board[row - 2][cur_col + 2])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col + 2] && Board[row][cur_col] == Board[row + 3][cur_col + 3])
         {
        return true; 
        }
    if(Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1] && Board[row][cur_col] == Board[row + 2][cur_col+ 2])
         {
        return true; 
        }
        
    if(Board[row][cur_col] == Board[row - 2][cur_col - 2] && Board[row][cur_col] == Board[row - 1][cur_col - 1] && Board[row][cur_col] == Board[row + 1][cur_col + 1])
        {
        return true; 
        }
        else
        {
         return false;
        }
}
 
 

 void Print_Board(const vector<vector<char> >&Board)
{
 for (int i=0; i<rows; i++)
 { cout << endl;
  for(int j=0; j<cols; j++)
  {
   cout << Board[i][j];
  }
 cout << endl;
 }
}

 
int main() {
int cur_row; 
int cur_col;
vector<vector<char> >Board;
Board.resize(rows);
for (int i=0; i<rows; i++) { 
Board[i].resize(cols);
for (int j=0; j<cols; j++) {
   Board[i][j] = '-';
    }
}
 
 
 
 Print_Board(Board);

 bool Game_Over=false;
 char cur_move='O';//o goes first

  while (!Game_Over)
 {
  cout<<"Player" << cur_move <<" please enter the column in which you would like your marker placed"<<endl;
  cin >> cur_col;
   while(Is_Legal(Board,cur_col - 1)==false){
   cout << "illegal column please try enter column again"<<endl;
   cin >> cur_col; 
  }
  bool Place_Marker=false;
 int i=5;
    
 do{
   if (Board[i][cur_col-1]=='-')
 {
  Board[i][cur_col-1]=cur_move;
   Place_Marker=true;
 Print_Board(Board);}
 else
{
i--;
}
 }while(Place_Marker=false && i>=0);
Game_Over=End_Game_Check(Board,i , cur_col-1, cur_move);
        if(Game_Over == true)
            cout << endl << cur_move << " has won!" << endl; 
        
        if(Game_Over == false){
            bool Draw = Tie_Check(Board);
            if(Draw == true){
                cout << "There's a DRAW - GAME OVER";
                Game_Over = true;
            }
        }
 
   
   
   
  
  if(cur_move=='O')
  {
   cur_move='*';
  }
 else
 {
  cur_move='O';
 }

 
 }
 

 system("pause");
 return 0;
}

It gives me a headache... thats what it does. But seriously, I think its some kind of tic-tac-toe or connect four game.

If you could throw some delicious code tags around it, that would be helpful.

Under your post, click "Edit this post"
Then add [*code=c++] before your code and [*/code] after but without the *'s.

If you are just learning C++ I suggest you start like everyone else : figure out how "Hello world!" works in C++

Under your post, click "Edit this post"
Then add [*code=c++] before your code and [*/code] after but without the *'s.

[noparse][/noparse] tags are also brilliant!

I agree with ddanbe, you should start simple. Don't just dive into someone elses code and expect to be able to understand it all from the go.

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.