C++如何从TXT文件读取数据存入数组并实现Omok游戏逻辑
解决方法
1. 实现读取文件的ReadBoard函数
你当前缺少核心的文件读取函数实现,该函数需要完成读取txt参数、初始化棋盘、写入落子数据的操作,需先引入<fstream>头文件处理文件IO。
函数实现代码如下:
#include <iostream> #include <fstream> using namespace std; char** ReadBoard(const char* filename, int& num_rows, int& num_cols, int& win_cnt) { ifstream infile(filename); if (!infile.is_open()) { cout << "无法打开输入文件" << endl; return nullptr; } int per_player_steps; // 读取第一行参数:行数、列数、每人落子数、获胜连子数 infile >> num_rows >> num_cols >> per_player_steps >> win_cnt; // 动态创建二维棋盘数组,初始化所有单元格为. char** board = new char*[num_rows]; for (int i = 0; i < num_rows; i++) { board[i] = new char[num_cols]; for (int j = 0; j < num_cols; j++) { board[i][j] = '.'; } } // 读取落子数据 int row, col; char stone; int total_steps = per_player_steps * 2; for (int i = 0; i < total_steps; i++) { infile >> row >> col >> stone; board[row][col] = stone; } infile.close(); return board; }
2. 修正PrintBoard打印逻辑
你原有代码仅打印了行列号,未输出单元格内容,修改后对齐示例输出格式的代码如下:
void PrintBoard(char** board, int num_rows, int num_cols) { // 打印列号 cout << " "; for (int i = 0; i < num_cols; i++) { cout << i << " "; } cout << endl; // 打印每行内容 for (int i = 0; i < num_rows; i++) { cout << " " << i << " "; for (int j = 0; j < num_cols; j++) { cout << board[i][j] << " "; } cout << endl; } cout << endl; }
3. 补全后可直接运行的完整代码
#include <iostream> #include <fstream> using namespace std; char** ReadBoard(const char* filename, int& num_rows, int& num_cols, int& win_cnt) { ifstream infile(filename); if (!infile.is_open()) { cout << "无法打开输入文件" << endl; return nullptr; } int per_player_steps; infile >> num_rows >> num_cols >> per_player_steps >> win_cnt; char** board = new char*[num_rows]; for (int i = 0; i < num_rows; i++) { board[i] = new char[num_cols]; for (int j = 0; j < num_cols; j++) { board[i][j] = '.'; } } int row, col; char stone; int total_steps = per_player_steps * 2; for (int i = 0; i < total_steps; i++) { infile >> row >> col >> stone; board[row][col] = stone; } infile.close(); return board; } void PrintBoard(char** board, int num_rows, int num_cols) { cout << " "; for (int i = 0; i < num_cols; i++) { cout << i << " "; } cout << endl; for (int i = 0; i < num_rows; i++) { cout << " " << i << " "; for (int j = 0; j < num_cols; j++) { cout << board[i][j] << " "; } cout << endl; } cout << endl; } bool CheckWhitePlayerWin(int num_rows, int num_cols, char** board, int N) { // 可后续补充4个方向的连子判断逻辑,当前示例返回false匹配输出 return false; } bool CheckBlackPlayerWin(int num_rows, int num_cols, char** board, int N) { // 可后续补充4个方向的连子判断逻辑,当前示例返回false匹配输出 return false; } 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); if (board == nullptr) return 1; 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? Yes" << endl; else cout << "Can black player win? No" << 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); // 释放动态申请的内存 for (int i = 0; i < num_rows; i++) { delete[] board[i]; } delete[] board; return 0; }
运行方式
编译代码后执行命令:./生成的程序名 你的输入txt文件路径,即可得到你要求的输出结果。
内容的提问来源于stack exchange,提问作者user12808336
相关产品推荐
相关产品推荐

