2d Array Shuffle only

I need to shuffle a 2d array 3X3 TicTacToe so all numbers 1-9 show up in the array but none twice on the board to the right. Have managed to randomize it with duplicates, but not randomly shuffle the board any help would be appreciated.

unsigned seed = time(0);
srand(seed);
int bboard [3][3]={1,2,3,4,5,6,7,8,9};

for(int i=0; i<3; i++)
for(int j=0; j<3; j++)

bboard [i][j] = rand () % bboard [i][j];

Player 1: O
Player 2: X
-----------------------
| | | |
1 | 2 | 3 0 | 0 | 0
| | | |
______ _________ ______ ______ _________ ______
| | | |
4 | 5 | 6 0 | 2 | 4
| | | |
______ _________ ______ ______ _________ ______
| | | |
7 | 8 | 9 6 | 6 | 1
| | | |
| | | |
1
2
3
4
5
6
int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::random_shuffle(numbers, numbers + 9);
int board[3][3];
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
        board[i][j] = numbers[i * 3 + j];
This worked on the first run of the program but did not work on the subsequent
runs. The entire code is below. Is there something else missing?

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <iomanip>


using namespace std;

void showgame();
void player();
bool gameover();
int bboard[3][3];


char player1, player2;
char user;
bool tie = false;


char aboard[3][3]= {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};


int main()
{


cout << "Tic Tac Toe\n";
do{
cout << " Player 1 is first select either X or O: ";
cin >> player1;
}while(player1 != 'X' && player1 != 'O');


if(player1 == 'X')
{
player2 = 'O';
user = 'X';
}
else if (player1 == 'O')
{
player2 = 'X';
user = 'O';
}


while(!gameover())

{
showgame();
player();
gameover();
}
if(user == 'X' && !tie)
{
showgame();
cout << "Tic Tac Toe: O Wins!\n";
}
else if(user == 'O' && !tie)
{
showgame();
cout << "Tic Tac Toe: X Wins!";
}
else
{
showgame();
cout << "Tie Game!";
}
}


void showgame()
{
int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::random_shuffle(numbers, numbers + 9);
int bboard[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
bboard[i][j] = numbers[i * 3 + j];

cout<< " "<<" Player 1: "<<player1<<"\n";
cout<< " "<<" Player 2: "<<player2<<"\n";
cout<< " "<<"-----------------------"<<endl;

cout << " | | " " " " | | "<<endl;
cout << " "<<aboard[0][0] << " | " <<aboard[0][1] << " | " <<aboard[0][2] << " " <<bboard[0][0] << " | " <<bboard[0][1] << " | " <<bboard[0][2] <<endl;
cout << " | | " " " " | | "<<endl;
cout << "______ _________ ______" " " "______ _________ ______"<<endl;
cout << " | | " " " " | | "<< endl;
cout << " "<<aboard[1][0] << " | " <<aboard[1][1] << " | " <<aboard[1][2] << " " <<bboard[1][0] << " | " <<bboard[1][1] << " | " <<bboard[1][2]<<endl;
cout << " | | " " " " | | "<< endl;
cout << "______ _________ ______ " " " "______ _________ ______ "<< endl;
cout << " | | " " " " | | "<<endl;
cout << " "<<aboard[2][0] << " | " <<aboard[2][1] << " | " <<aboard[2][2] << " "<<bboard[2][0] << " | " <<bboard[2][1] << " | " <<bboard[2][2]<< endl;
cout << " | | " " " " | | "<<endl;
cout << " | | " " " " | | "<<endl;
}

void player()
{
int selection;
int row=0, column = 0;


if (user == 'X')
{
cout <<"X is up: ";
}
else if (user == 'O')
{
cout <<"O is up: ";
}

cin >> selection;

switch (selection)
{
case 1: row = 0; column = 0;
break;
case 2: row = 0; column = 1;
break;
case 3: row = 0; column = 2;
break;
case 4: row = 1; column = 0;
break;
case 5: row = 1; column = 1;
break;
case 6: row = 1; column = 2;
break;
case 7: row = 2; column = 0;
break;
case 8: row = 2; column = 1;
break;
case 9: row = 2; column = 2;
break;
default:
cout << "Number input is incorrect, input a number from 1-9! Try again.\n";
player();
}

if (user == 'X' && aboard[row][column] != 'X' && aboard[row][column] != 'O')
{
aboard[row][column] = 'X';
bboard[row][column] = 'X';
user='O';

}
else if (user == 'O' && aboard[row][column] != 'X' && aboard[row][column] != 'O')
{
aboard[row][column] = 'O';
bboard[row][column] = 'O';
user='X';

}
else
{
cout << "The space is already taken by the other player, choose an open space.\n";
player();
}
}

bool gameover()
{
for (int i = 0; i < 3; i++)
{
if ((((((((aboard[i][0] == aboard[i][1] && aboard[i][1] == aboard[i][2]) ||

(aboard[0][i] == aboard[1][i] && aboard[1][i] == aboard[2][i])||

(aboard[0][0] == aboard[1][1] && aboard[1][1] == aboard[2][2])||

(aboard[0][2] == aboard[1][1] && aboard[1][1] == aboard[2][0])||

(bboard[i][0] == bboard[i][1] && bboard[i][1] == bboard[i][2])||

(bboard[0][i] == bboard[1][i] && bboard[1][i] == bboard[2][i])||

(bboard[0][0] == bboard[1][1] && bboard[1][1] == bboard[2][2])||

(bboard[0][2] == bboard[1][1] && bboard[1][1] == bboard[2][0]))))))))
{
return true;
}


for (int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
if ((aboard[x][y] != 'X' && aboard[x][y] != 'O') || (bboard[x][y] != 'X' && bboard[x][y] != 'O'))
{
return false;
}
}
tie = true;
return true;
return 0;
}
}
}
Am I supposed to guess what "did not work" means?
Helios,

You are not supposed to guess. It shuffles on the first run with no repeats in the board, however when you run the code again, the same numbers come out in the same order, no shuffle takes place after the first shuffle. The aborad in the code is chars, the bboard is coming back as int and I will have to parse it to chars to compare the arrays so that for example when an X is placed in aboard[0][0] which char 1, it will go into another cell on the bboard as the cells have been shuffled. Essentially giving you the possibility of a second way to win the game.
If you're sure it shuffled on the first run, then it also shuffled on the second run. And the third run. std::random_shuffle implementations often use std::rand and benefit from a call to std::srand for seeding the generator.

std::random_shuffle was deprecated in C++14 and removed in C++17, so using std::shuffle should probably be preferred.

http://en.cppreference.com/w/cpp/algorithm/random_shuffle
Topic archived. No new replies allowed.