I am new to c++ and fstream (infile) and wanted to know how data is read from a txt file and how can I put that data in an array. Basically I am trying to make this korean game called omok. where the number of times a player gets their move 4 times simultaneously either vertically, horizontally or diagonally.
Input from the txt file
4 4 2 4
2 0 B
0 1 W
3 2 B
1 3 W
Data from the first line means that its is a 4x4 array / table. 2 means that there have been 2 moves from each player and 4 moves in total.
the second line onwards where the number coresponds to the location of the move for eg 2 0 means row 2 col 0.
Output should be:
0 1 2 3
0 . W . .
1 . . . W
2 B . . .
3 . . B .
Can white player win? No
Can black player win? No
This is what I've got so far, what am I supposed to do? I am logically stuck. Could you first explain how the data is read an how I can display it like this?
void PrintBoard(char** board, int num_rows, int num_cols) {
cout << "Print board:" << endl << endl;
for (int i = 0; i < num_cols; i++) {
cout<<" "<<i;
}
cout << endl;
for (int i = 0; i < num_rows; i++) {
cout << " " << i << endl;
}
cout << endl;
/////////////////////////////////////////////////////////////////
// Print
// * " W" to print a white stone, and
// * " B" to print a black stone, and
// * " ." to print an empty cell.
/////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////
// Add your own functions if you need
bool CheckWhitePlayerWin(int num_rows, int num_cols, char** board, int N) {
return true;
}
bool CheckBlackPlayerWin(int num_rows, int num_cols, char** board, int N) {
return true;
}
void PrintPlayerWinCondition(int num_rows, int num_cols, char** board, int N) {
}
/////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
if (argc <= 1) {
cout << "Need the input file containing board size, N, and stones." << endl;
return 0;
}
int num_rows;
int num_cols;
int N;
char** board = ReadBoard(argv[1], num_rows, num_cols, N);
PrintBoard(board, num_rows, num_cols);
if(CheckWhitePlayerWin(num_rows, num_cols, board, N))
cout << "Can white player win? Yes" << endl;
else
cout << "Can white player win? No" << endl;
if (CheckBlackPlayerWin(num_rows, num_cols, board, N))
cout << "Can black player win?" << endl;
else
cout << "Can black player win?" << endl;
cout << endl;
cout << "List of winning conditions sorted by the index of top-left stone location:" << endl;
PrintPlayerWinCondition(num_rows, num_cols, board, N);
Thanks in advance!
[cpp] string array from text file.