You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 9
void Print(int board[9][9])
{
int i,j;
printf("|");
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d",board[i][j]);
if(j==8)
{
printf("|\n");
}
if(i==8&&j==8)
{
break;
}
if((i+1)%3==0&&j==8&&i!=8)
{
printf("|-----------|\n");
}
if((j+1)%3==0)
{
printf("|");
}
}
}
}
void printMatrix(int matrix[SIZE][SIZE]) {
int i,j;
for (i = 0; i < SIZE; i++) {
for ( j = 0; j < SIZE; j++) {
if (matrix[i][j] == 0) {
printf(".");
} else {
printf("%d ", matrix[i][j]);
}
}
printf("\n");
}
}
int main() {
srand(time(NULL));
int matrix[SIZE][SIZE] = {0};
int i, j, k, num, positions[3];
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
do {
positions[j] = rand() % SIZE;
} while (positions[j] == positions[(j + 1) % 3] || positions[j] == positions[(j + 2) % 3]);
}
for (j = 0; j < 3; j++) {
matrix[i][positions[j]] = rand() % 9 + 1;
}
}
for (i = 3; i < 6; i++) {
for (j = 0; j < 3; j++) {
do {
positions[j] = rand() % SIZE;
} while (positions[j] == positions[(j + 1) % 3] || positions[j] == positions[(j + 2) % 3]);
}
for (j = 0; j < 3; j++) {
matrix[i][positions[j]] = rand() % 9 + 1;
}
}
for (i = 6; i < 9; i++) {
for (j = 0; j < 3; j++) {
do {
positions[j] = rand() % SIZE;
} while (positions[j] == positions[(j + 1) % 3] || positions[j] == positions[(j + 2) % 3]);
}
for (j = 0; j < 3; j++) {
matrix[i][positions[j]] = rand() % 9 + 1;
}
}
printMatrix(matrix);
Print(matrix) ;
return 0;
}