我正在玩井字游戏,需要创建一个功能,该功能可根据用户输入创建棋盘。到目前为止,可以是3x3或更大的尺寸,但是运行时它会打印内存位置。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// Array which creates boeard based on user input
int *drawboard(int width, int height)
{
int* board_data = new int[width * height];
int** board = new int* [width];
for (int i = 0; i < width; ++i)
{
board[i] = board_data + height * i;
}
return board_data;
}
void main()
{
int width = 0;
int height = 0;
cout << " Welcome to Tic Tac Toe v1! " << endl;
cout << " Choose your board size, enter width: " << endl;
cin >> width;
cout << " Choose your height: " << endl;
cin >> height;
cout << drawboard << endl;
int *board = drawboard(width, height);
delete[] board;
system("pause");
}
最佳答案
我认为this post对您有很大帮助。看来您只是想声明一个动态2D整数数组,然后将虚拟2D数组用作tic tac toe board,对吧?
不太确定drawboard()
是什么意思,但是这是一种打印网格的简单方法:
void printGrid(int y, int x) {
for (int j = 0; j < y; j++) {
for (int i = 0; i < x; i++) cout << " ---";
cout << endl;
for (int i = 0; i < x; i++) cout << "| ";
cout << "|" << endl;
}
for (int i = 0; i < x; i++) cout << " ---";
cout << endl;
}
关于c++ - 井字游戏板,其大小基于用户输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45315917/