I finally finished making 3 by 3 matrix which have 1 ~ 9 numbers in it.
These are results. There is no repeated number but 1 ~ 9 numbers with random arrangement
And this is the code
////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <random>
using namespace std;
int num[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int arrNum[9];
int arr[3][3];
void createSudoku();
void printSudoku();
void bubbleSortArr();
void swap(double* a, double* b);
void printArrNum();
bool IsSame();
void main()
{
createSudoku();
printSudoku();
}
void createSudoku()
{
while (1)
{
random_device rd;
mt19937_64 rng(rd());
uniform_int_distribution<__int64> dist1(1, 9);
int k = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i][j] = dist1(rng);
arrNum[k] = arr[i][j];
k++;
}
}
bubbleSortArr();
if (IsSame())
{
break;
}
}
}
void printSudoku()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
void printArrNum()
{
for (int i = 0; i < 9; i++)
{
cout << arrNum[i] << " ";
}
}
void bubbleSortArr()
{
for (int j = 0; j < 9; j++)
{
for (int k = 0; k < 9 - j - 1; k++)
{
if (arrNum[k] > arrNum[k + 1])
swap(arrNum[k], arrNum[k + 1]);
}
}
}
void swap(double* a, double* b)
{
double * temp;
temp = a;
a = b;
b = temp;
}
//day1
bool IsSame()
{
bool same = true;
for (int i = 0; i < sizeof(num) / sizeof(int); i++)
{
if (num[i] != arrNum[i])
same = false;
}
return same;
}
////////////////////////////////////////////////////////////////////////////////////
Now, I will extend this to 9 by 9 Sudoku matrix
'공부(Study)' 카테고리의 다른 글
First Win32 Project (0) | 2017.02.15 |
---|---|
Start studying MFC (0) | 2017.02.15 |
Sudoku Game with C++ - day 3 (0) | 2016.07.21 |
Sudoku Game with C++ - day1 (0) | 2016.07.06 |
Java Study - day1 (0) | 2016.07.06 |