tic tac脚趾生成板的组合C

Tic Tac Toe Generating Combinations of Boards C++

本文关键字:组合 tac tic      更新时间:2023-10-16

注意:这是针对课堂分配的,必须这样做,我知道它效率低下,我并不要求完成我的作业,我只需要一些关于我做错了什么的指导。

我想创建一个函数,该函数以" xoxxooxox"的格式生成所有可能的组合,每三个字母代表板的一行。调用该函数时,我需要它返回向量。

到目前为止,我到目前为止是循环,它会生成每种可能的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();
}

我不确定如何检查它是否正在输出我想要的内容,并且会出现"不良分配"的错误。是否有更有效的方法来执行此操作?如何解决不良分配错误?

对于tic-tac-toe,生成 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。

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

  • 让A表示" X" S
  • 的数量
  • 让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" s和" o"是相同的,您必须将其除以3!和2!

因此,这种情况的总排列是9!/(4!*3!*2!(

-a b置换

-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

您还需要代码吗?

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

{{{
#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可接受的组合