-5

Input:

Text File Values:  1 1 A 2 2 B 3 3 C 0 0 X

Output:

3 TILES on Scrabble Board

ROW COL LETTER
=== === ======
 1   1    A
 2   2    B
 3   3    C
=== === ======


4 x 4 SCRABBLE BOARD 


         1 2 3 4
       + - - - - +
row1=> | A       |
row2=> |   B     |
row3=> |     C   |
row4=> |         |
       + - - - - +
         1 2 3 4

(1) Store all board characters into array

char Board[9][9];   // Capacity is 9x9 = 81.

RULE: A board cell can contain at most one value; Display error message when: - the cell already contains a mark - the mark is not a letter - the row is outside the range [1,boardsize] - the col is outside the range [1,boardsize]

ERROR:  REJECTED CELL <row> <col> <symbol> CELL MARKED
ERROR:  REJECTED CELL <row> <col> <symbol> NOT A LETTER
ERROR:  REJECTED CELL <row> <col> <symbol> BAD ROW
ERROR:  REJECTED CELL <row> <col> <symbol> BAD COL

(2) After the input file has been read, display the Board array.

(3) Display the words on the scrable board (illustration only):

HORIZONTAL: xxxx yyyyyyy zzzzz 3 WORDS
VERTICAL:   aaa bbb ccc ddd  4 WORDS
7 SCRABBLE WORDS

QUESTION: How do I input the file values ex: 1 1 A into char Board[9][9]? I want 1 1 to represent row and col and the symbol A to be associated with 1 1.

Example:

Board[1][1] = A
Board[2][2] = B

New Code:

 int main()
{
   //-------------------------------------------- --------------------------
   //  Declare variables
   //----------------------------------------------------------------------
   char filename[80];
   int boardsize, row, col;
   char symbol;
   char Board[9][9];
   ifstream inF;

   //-| ----------------------------------------------------------------------
   //-| Print the copyright notice declaring authorship.
   //-| ----------------------------------------------------------------------
   cout << endl << "(c) 2017, twilson" << endl << endl; 


   //-| ----------------------------------------------------------------------
   //-| 1. Get file name and open file.
   //-| ---------------------------------------------------------------------- 
   cout << "Enter name of input file: "; 
   cin >> filename;
   cout << endl;

   inF.open(filename);
   if (inF.fail())
   {
      cout << "FATAL ERROR: Can not open file " << "'" << filename << "'" << endl;
      exit(1);
   }

   //-| ----------------------------------------------------------------------
   //-| 2. Get board size.
   //-| ----------------------------------------------------------------------
   cout << "Enter board size [1-9]: ";
   cin >> boardsize;
   cout << endl;

   //-| ----------------------------------------------------------------------
   //-3. Read in file values and output ROW, COL, and LETTER on scrabble board.
   //-| ----------------------------------------------------------------------
    int T = 0;
    int nextLine = 0;
    int Tiles = 0;


    // Read in file and count each tile

    int a = 0;
    while(inF >> row >> col >> symbol)
    {   
        if(row > 0 && col > 0)
        {
            if( row == row && col == col)
            {
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " CELL MARKED" << endl;
            }
            else if(!isalpha(symbol))
            {
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " NOT A LETTER" << endl;
            }
            else if(row > boardsize)
            {
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " BAD ROW" << endl;
            }
            else if(col > boardsize)
            {
                cout << "REJECTED CELL " << row << " " << col << " "
                     << symbol << " BAD COL" << endl;
            }

            else
                Tiles++;
        }

        }
  • you read `int row, int col, char Val`, validate row and col, check that cell is free, check Val is a letter. If everything is good you assign `Board[row-1][col-1] = Val` – Artemy Vysotsky Sep 17 '17 at 04:33
  • My code so far: while(!inF.eof()) { inF >> row >> col >> symbol; if(row > 0 && col > 0) { if(!isalpha(symbol)) { cout << "REJECTED CELL " << row << " " << col << " " << symbol << " NOT A LETTER" << endl; } else if(row > boardsize) { cout << "REJECTED CELL " << row << " " << col << " " << symbol << " BAD ROW" << endl; } else if(col > boardsize) { cout << "REJECTED CELL " << row << " " << col << " " << symbol << " BAD COL" << endl; } else Tiles++; } – Tishauna Wilson Sep 17 '17 at 04:55
  • Put your code into your question and read https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong or this https://stackoverflow.com/a/5605159/8491726 – Artemy Vysotsky Sep 17 '17 at 04:56
  • Thank you. Also, I added the code to the question and changed the while loop. – Tishauna Wilson Sep 17 '17 at 05:04

1 Answers1

0

Between item 2 and 3 you have to init your board (so you can later check if the place is marked or not

    for (int i = 0; i < boardsize; i++)
        for (int j = 0; j < boardsize; j++)
            Board[i][j] = ' ';

Your while loop should look like this (actual error printing removed - only confditions left)

    while (inF >> row >> col >> symbol)
    {
        if(row == 0 && col == 0 && symbol == 'X'){
            //end condition? - not clear from your explanation
            break;
        }
        if (row < 1 || row >  boardsize)
        {
            // BAD ROW error
        }
        else if (col <1 || col > boardsize)
        {
            // BAD COL error
        }
        else if (!isalpha(symbol)) {
            //NOT A LETTER error
        }
        else if (Board[row - 1][col - 1] != ' ')  //This check requires the Board to be properly initialized
        {
            //CELL MARKED error
        }
        else {
            //All is good
            Board[row - 1][col - 1] = symbol;
            Tiles++;
        }
    }
Artemy Vysotsky
  • 2,374
  • 8
  • 18