输入:
Text File Values: 1 1 A 2 2 B 3 3 C 0 0 X
输出:
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)将所有电路板字符存储到数组
char Board[9][9]; // Capacity is 9x9 = 81.
规则:单元格单元最多可包含一个值; 显示以下错误消息: - 单元格已包含标记 - 商标不是信件 - 行超出范围[1,boardsize] - col超出范围[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)读取输入文件后,显示Board数组。
(3)显示scrable board上的文字(仅供参考):
HORIZONTAL: xxxx yyyyyyy zzzzz 3 WORDS
VERTICAL: aaa bbb ccc ddd 4 WORDS
7 SCRABBLE WORDS
问题:如何将文件值ex:1 1 A输入char board [9] [9]?我希望1 1代表行和col,符号A代表1 1。
示例:
Board[1][1] = A
Board[2][2] = B
新守则:
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++;
}
}
答案 0 :(得分:0)
在第2项和第3项之间,您必须初始化您的电路板(以便稍后检查地点是否标记
for (int i = 0; i < boardsize; i++)
for (int j = 0; j < boardsize; j++)
Board[i][j] = ' ';
你的while循环应该是这样的(删除了实际的错误打印 - 只剩下配置了)
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++;
}
}