Tic Tac Toe生成板C ++的组合

时间:2018-02-19 01:48:46

标签: c++ c++11 vector tic-tac-toe

注意:这是课堂作业,必须以这种方式完成,我知道这样做效率低,而且我没有要求完成作业,我我需要一些关于我做错的指导。

我想创建一个功能,以" xoxxooxox"格式生成所有可能的tic tac toe board组合。每三个字母代表一排董事会。当我调用函数时,我需要它来返回一个向量。

到目前为止我所拥有的是一个循环,它可以生成0,1和2之间的每个可能的9位数组合,直到19683,这只是3 ^ 9组合的电路板。然后我将它转换为#(表示空格),1到" o"和2到" x"。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> get_all_boards() {

  vector<int> numboard;
  std::string board;
  for(int i = 0; i < 19683; ++i) {
    int c = i;
    for (int j = 0; j < 9; ++j) {

      int playnum = c % 3;
        if (playnum == 0) {
        playnum = 35;
        numboard.push_back(playnum);
      } else if (playnum == 1) {
        playnum = 111;
        numboard.push_back(playnum);
      } else if (playnum == 2) {
        playnum = 120;
        numboard.push_back(playnum);
      }
      c /= 3;
    }
    for (auto x : numboard) {
      board += static_cast<char>(x);
    }
    std::copy( board.begin(), board.end(), std::back_inserter(numboard));
  }
}
int main() {
  get_all_boards();
}

我不确定如何查看它是否输出了我想要的内容,并且我收到错误的错误分配。&#34;有没有更有效的方法来执行此操作以及如何修复错误的分配错误?

3 个答案:

答案 0 :(得分:0)

对于井字游戏,生成每个可能的X和O的组合是过度的。由于球员交替,一个完全填充的板将具有正好5个X和4个O.因此,要生成一堆电路板,只需从一个简单的电路板开始,然后生成所有可能的排列:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <string>

int main() {
    std::string board = "XXXXXOOOO";
    std::sort(std::begin(board), std::end(board));
    do {
        std::cout << board << '\n';
    } while (std::next_permutation(std::begin(board), std::end(board));
    return 0;
}

但请注意,许多这些电路板代表不可能的位置。例如,最初的一个是不可能的,因为它连续有三个X,连续有三个O.

答案 1 :(得分:0)

假设玩家首先输入“x”,组合数量比您想象的要低很多,

  • 设A表示“x”的数量
  • 设B表示“o”s
  • 的数量

比A = B + 1或A = B

所以你必须考虑以下排列:

  • A = 0,B = 0
  • A = 1,B = 0
  • A = 1,B = 1
  • A = 2,B = 1
  • A = 2,B = 2

等等,

并且对于每种情况,排列的数量可以如下计算:

9!/((9-A-B)!* A!* B!)

例如,如果A = 3且B = 2 对于第一个X:9选择 对于第二个X:8选择 对于第三个X:7选择 对于第一个O:6选择 对于第二个O:5选择

是9 * 8 * 7 * 6 * 5,即9!/(9-A-B)!

但我们没有完成,因为“X”和“O”是相同的,你必须将它除以3!和2!

因此这个案例的总排列数为9!/(4!* 3!* 2!)

-A B Permutations

-0 0 1

-1 0 9

-1 1 72

-2 1 252

-2 2 756

-3 2 1260

-3 3 1680

-4 3 1260

-4 4 630

-5 4 126

总计= 6046

你还需要代码吗?

答案 2 :(得分:0)

此代码将为您提供所有排列,请注意其中许多排列是不可接受的,因为Os的数量和X的数量可能差异不超过一个

{{{
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;

std::string ConvertPermutationToString( int permutation )
{
    std::string board = "#########";

    int counter = 0;

    while ( permutation > 0 )
    {
        switch ( permutation %3 )
        {
        case 0: 
            //do nothing
            break;

        case 1:
            board[counter] = 'O';
            break;

        case 2:
            board[counter] = 'X';
            break;
        }

        counter++;
        permutation = permutation/3;
    }

    return board;
}
vector<string> get_all_boards() {


  std::string board;
  vector<string> allBoards;
  for(int i = 0; i < 19683; ++i) {
      allBoards.push_back( ConvertPermutationToString( i ) );
  }

  return allBoards;
}

int main() {
  get_all_boards();
}

}}}

现在,如果您想过滤掉坏组合,请使用以下代码

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<math.h>
using namespace std;

std::string ConvertPermutationToString( int permutation )
{
    std::string board = "#########";

    int counter = 0;
    int xCount = 0;
    int oCount = 0;

    while ( permutation > 0 )
    {
        switch ( permutation %3 )
        {
        case 0: 
            //do nothing
            break;

        case 1:
            board[counter] = 'O';
            oCount++;
            break;

        case 2:
            board[counter] = 'X';
            xCount++;
            break;
        }

        counter++;
        permutation = permutation/3;
    }

    if ( abs( xCount - oCount ) > 1 ) 
    {
        board = "";
    }

    return board;
}
vector<string> get_all_boards() {


  std::string board;
  vector<string> allBoards;
  for(int i = 0; i < 19683; ++i) {

      board = ConvertPermutationToString( i );

      if ( board.length() )
      {
        allBoards.push_back( ConvertPermutationToString( i ) );
      }
  }

  return allBoards;
}

int main() {
  get_all_boards();
}

你有8953个可接受的组合