生成所有独特的井字板名单 [英] Generate a list of all unique Tic Tac Toe boards
问题描述
我想生成一个包含所有 19683 井字一个文本文件-Toe板布局在0结构=空,1 = X,2 = O。不幸的是数学不是我的强项,我似乎无法找到这个任何地方的任何例子。
I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts in the structure of 0 = Blank, 1 = X, and 2 = O. Unfortunately math is not my strong suit and I cannot seem to find any examples of this anywhere.
这是不是功课我向你保证。我打算通过一个极小的计算器,以生成包含RGB值重新presenting基于板的设置优化移动图像运行这个数据。我开发井字棋的不支持的功能平台(它的事件驱动),所以我会在董事会中我的游戏指示转换为数字,然后查找图像中的像素的RGB什么是最好的此举是。这是一个厚脸皮的解决办法,而是需要不超过一个145x145像素的图像更多的内存(145x145 = 21025因此每个像素再presents根据董事会有效建议的举动)。这也意味着我不会咀嚼的CPU时间,这是另外一个优点。
This isn't for homework I assure you. I intend to run this data through a Minimax calculator in order to generate an image that contains RGB values representing the optimal move based on the board setup. I am developing Tic-Tac-Toe for a platform that does not support functions (it's event-driven) so I will convert the board to a number in my game and then lookup the RGB of a pixel in an image which indicates what the best move is. It's a cheeky workaround, but one that requires no more RAM than an 145x145 pixel image (145x145 = 21,025 so each pixel represents the recommended move based on the board effectively). This also means I won't have to chew CPU time which is another plus.
推荐答案
由于您希望电路板布局,这里只有少数人(19683)。
Since you want board layouts, there's only a small number of them (19683).
您可以只暴力生成所有的这些。每盒只有3个可能性。而且有9盒,只是通过他们都运行。
You can just brute-force generate all of these. Each box only has 3 possibilities. And there are 9 boxes, just run through all of them.
编辑:
int c = 0;
while (c < 262144){
bool valid = (c & 3) < 3;
valid &= ((c >> 2) & 3) < 3;
valid &= ((c >> 4) & 3) < 3;
valid &= ((c >> 6) & 3) < 3;
valid &= ((c >> 8) & 3) < 3;
valid &= ((c >> 10) & 3) < 3;
valid &= ((c >> 12) & 3) < 3;
valid &= ((c >> 14) & 3) < 3;
valid &= ((c >> 16) & 3) < 3;
if (valid){
int i = c;
int j = 0;
while (j < 9){
cout << (i & 3) << " ";
i >>= 2;
j++;
}
cout << endl;
}
c++;
}
这将打印出所有19683电路板布局。我不知道你想要什么样的格式,但它应该是相当容易提取从输出。
This will print out all 19,683 board layouts. I'm not sure what format you want, but it should be fairly easy to extract that from the output.
这篇关于生成所有独特的井字板名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!