// hex3.cpp    P. Conrad, for CISC181, Fall 2004
// generate an output file with a 3x3 board for playing hex
// have n blank lines first, 5 spaces for the left margin
// Put the output file in a file referred to by the first command
// line argument, and let the second command line argument be the 
// value of n.

// Here's what the board looks like for n = 4  Note the four blank lines
// before and after, and the 5 spaces at the left margin
//
//
//
//     
//      __       
//     /  \__    
//     \__/  \__ 
//     /  \__/  \
//     \__/  \__/
//     /  \__/  \
//     \__/  \__/
//        \__/  \
//           \__/
//
//
//


#include <iostream>
#include <fstream>
#include <cstdlib>
using std::cout;
using std::cerr;
using std::endl;
using std::ios;
using std::ofstream;
using std::atoi;

void writeBlanks(int n, ostream &outf)
{
  for (int i=0; i<n; i++)
    outf << ' ';
  return; 
}


int main(int argc, char *argv[])
{
  
  if (argc!=3)
    {
      cerr << "Usage: " << argv[0] << " outfilename blanklines " << endl;
      exit (-1);
    }

  int i;


  // Note: In your script for this assignment, do a "chmod u-w ." on the
  // directory where you run the program, so that temporarily, you can't
  // create the output file.  Show that this code works.  Then do a 
  // "chmod u+w ." to change it back, and show that creating the output file
  // also works. @@@

  ofstream outf(argv[1],ios::out);

  if (!outf)
    {
      cerr << "Can't open output file " << argv[1] << endl;

    }

  // Add some error checking so that if the result of this conversion is
  // less than or equal to zero, you print an error message and halt the
  // program.   Consider too: should this check be done before or after
  // open the output file?  Hmmm.... What are the pros/cons? @@@

  int numBlankLines = atoi(argv[2]);


  // @@@ Add code to take this value from a command line argument.
  // You'll need to change the "usage" message and also change the
  // check to argc.

  int numBlanks = 5; 

  // The following two lines of code should be factored out into a function
  // shouldn't they!?!? So do it. :-)  @@@

  for (i=0; i<numBlankLines; i++)
    outf << "\n";
  
  // @@@ The following is a cheat.  Your task is to 
  // make it so that the first command line argument
  // is the size of the board (currently hardcoded at 3x3).
  // That means of course, that the other command line arguments,
  // argv[1], argv[2], etc. need to shift over by one.
  // Some clues at the bottom...
  
  writeBlanks(numBlanks, outf);  outf << " __       \n"; 
  writeBlanks(numBlanks, outf);  outf << "/  \\__    \n";
  writeBlanks(numBlanks, outf);  outf << "\\__/  \\__ \n";
  writeBlanks(numBlanks, outf);  outf << "/  \\__/  \\\n";
  writeBlanks(numBlanks, outf);  outf << "\\__/  \\__/\n";
  writeBlanks(numBlanks, outf);  outf << "/  \\__/  \\\n";
  writeBlanks(numBlanks, outf);  outf << "\\__/  \\__/\n";
  writeBlanks(numBlanks, outf);  outf << "   \\__/  \\\n";
  writeBlanks(numBlanks, outf);  outf << "      \\__/\n";
    

  for (i=0; i<numBlankLines; i++)
    outf << "\n";

  outf.close();

  return 0;

}


  // How to approach the problem:
  // There are several three character components:
  // " __"   
  //
  // "/  "   
  // 
  // "\\__"  
  // 
  // "/\n" 
  // 
  // "   " 
  // 
  // etc.... see if you can determine the pattern, and also the
  // special cases...

