dimanche 18 février 2018

Tic Tac Toe Generating Combinations of Boards C++

very beginner here. I'd like to create a function that generates all possible combinations of tic tac toe boards in the format "xoxxooxox" with every three letters representing a row of the board. I need it to return a vector when I call the function. Here's my problem: It won't let me append to the vector I initialize. It says

no matching function for call to ‘std::vector >::push_back(int&)’

What I have so far is a loop that generates every possible 9 digit combination of 0,1 and 2 up until 19683 which is just 3^9 combination of boards. I know how to do it so it just prints out each board but I need to make the output into one vector so I thought I should just append each number to the vector.

#include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> get_all_boards() {
  std::vector<string> board;
  for(int i = 0; i < 19683; ++i) {
    int c = i;
    for (int j = 0; j < 9; ++j) {
      int place = c % 3;
      board.push_back(place);
      c /= 3;
    }

  }
  return board;
}
int main() {
  get_all_boards();

}

Any help would be greatly appreciated

Aucun commentaire:

Enregistrer un commentaire