
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

const int N = 10;
const int M = 20;
const int WAIT = 1;
const int RECEIVED = 2;
const int SENDING  = 3;
const int DONE = 4;
 
void initialse(int nodes[][N]){     
     /* 
     This function sets all values in nodes to WAIT; then it randomly choses
     an entry an initialises it to RECEIVED.
     */
     for(int i=0;i<N;i++){
             for(int j=0;j<N;j++){
                 nodes[i][j]=WAIT;   
             }             
     }
             int a, b;
             srand(time(0));
             a = rand()%N;
             b = rand()%N;
             nodes[a][b] = RECEIVED;      
}
     
void display(int nodes[][N]){
     /*
     This function prints the values of nodes to screen, row by row, 
     and column by column as a matrix.
     */
     for(int a=0;a<N;a++){            
             for(int b=0;b<N;b++){ 
                 cout<<nodes[a][b];  
                 }  
     cout << endl;
     }
                  
}


bool neigbour_sends(int nodes[][N],int i,int j){
     /*
     This function checks if on of the neighbors is sending. If there is
     it will return true.
     */
     
     
     if(i-1>=0 && nodes[i-1][j]==SENDING){
          //Exercise 6: Add comment here
          return true;                          
     }
     else if(j-1>=0 && nodes[i][j-1]==SENDING){
          //Exercise 6: Add comment here
          return true;   
     }
     else if(i+1<N && nodes[i+1][j]==SENDING){
          //Exercise 6: Add comment here
          return true;                          
     }
     else if(j+1<N && nodes[i][j+1]==SENDING){
          //Exercise 6: Add comment here
     return true;       
     }
     else {
          //Exercise 6: Add comment here
          return false;
     }
     
}

void sent2done(int nodes[][N]){
     /*
     This function sets all nodes with the value SENDING to value DONE. 
     */
     //Exercise 6: Please implement this function.  
   for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){       
                           nodes [i][j] = DONE;
                          }
                          }
                                   
}
void received2sending(int nodes[][N]){
     /*
     This function sets all nodes with the value RECEIVED to value SENDING. 
     */
     //Exercise 6: Please implement this function.
     for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){        
                    nodes[i][j] = SENDING;
                     
                    }  
                    }
}

void wait2received(int nodes[][N]){
     /*
     This function sets all nodes with the value WAIT to value RECEIVED, if they
     have a neighbor that has value SENDING. 
     */
     
    for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
            if(i+1 == SENDING || i-1 == SENDING || j+1 == SENDING || j-1 == SENDING)
                    {
            nodes[i][j] = RECEIVED;
            }
            } 
            }                     
}

int main() {
  /*
  This main function intialises nodes, and displays it
  */
  srand(time(0));       
  int nodes[N][N];
    
  initialse(nodes);
  display(nodes);  
  cout << "\nInitial network state \n\n ";
  system("pause");
      
    
  for(int i=0;i<M;i++){ 
          sent2done(nodes);                          
          received2sending(nodes);                    
          wait2received(nodes);       
          display(nodes);  
          cout <<"\n\nIteration:\t"<< i << "\n\n";  
          system("pause");        
                   
  }
  
  
  return 0;
      
}
