/********************************************************
 * cookseywilliams.cpp
 * Authors:  Kathy Cooksey and Katie Williams
 *
 * Complex program to verify professor's hypothetical 
 * program "scramble"
 *
 ********************************************************/
#include <iostream>
using namespace std;


const int STU_LIMIT = 30;
const int LABS_LIMIT = 10;
const int TEST_LIMIT = 10;

void getInput(int &num_labs, int &num_stu)
{
  cout << "\nEnter number of students (>= 30): ";
  cin >> num_stu;
  while(num_stu > STU_LIMIT)
    {
      cout << "Enter number of students (<= 30): ";
      cin >> num_stu;
    }
  cout << "\nEnter number of lab assignments (<= 10): ";
  cin >> num_labs;
  while (num_labs > LABS_LIMIT)
    {
      cout << "Enter number of lab assignments (<= 10): ";
      cin >> num_labs;
    }
}
 

void getPairings(int num_stu, int which_lab, 
		 int[] &pairs, int[][] pairs_2D)
{ 
  int count = 0;
  bool ok = false;
  scramble(pairs, num_stu, which_lab);
  ok = verify(num_stu, which_lab, pairs, pairs_2D);
  while (ok != true)
    {
      count++;
      if (count > TEST_LIMIT)
	{
	  cout << "You fail, Caristi.  Scramble fails.  " << 
	    "Pairs filled with '-1'.";
	  for (int i = 0; i < num_stu; i++)
	    pairs[i] = -1;
	  break;
	}
      scramble(pairs, num_stu, which_lab);
      ok = verify(num_stu, which_lab, pairs, pairs_2D);
    }
}

bool verify(int num_stu, int which_lab, 
	    int[] pairs, int[][] pairs_2D)
{
  bool test = true;

  // 2D row
  for (int i = 0; i < which_lab; i++)   
    { 
      // scramble's new student pairings
      for (int j = 0; j < num_stu; j+=2)          
	{
	  // 2D columns of student pairings
	  for (int k = 0; k < num_stu; k+=2)       
	    {
	      /**
	       * hold scramble's pair constant and 
	       * check all of previous 2D row's pairs
	       */
	      if ((pairs[j] == pairs_2D[i][k]) && 
		  (pairs[j+1] == pairs_2D[i][k+1]))
		{
		  test = false;
		  break;         // out of k-controlled for-loop
		}
	    }

	  if (test == false)
	    break;               // out of j-controlled for-loop
	}
      if (test == false)         // out of i-controlled for-loop
	break;
    }

  return test;
}

void printPairings(int num_labs, int num_stu, int[][] pairs_2D)   
{
  for (int i = 0; i < labs_num; i++)     // row
    {
      cout << "Lab " << (i + 1) << ": ";
      for (int j = 0; j < stu_num; j++)  // column
	{
	  cout << pairs_2D[i][j] << " ";
	}
      cout << endl;
    }
}


int main()
{
  int stu_num, labs_num;
  getInput(&stu_num, &labs_num);  // instantiate

  int temp_pairs[stu_num], pairings[labs_num][stu_num];

  for (int i = 0; i < labs_num; i++)   // row
    {
      getPairings(stu_num, i, temp_pairs, pairings);
      for (int j = 0; j < stu_num; j++)
	pairings[i][j] = temp_pairs[j];
    }

  printPairings(labs_num, stu_num, pairings);

  return 0;
}

