Project Report: Alphabetic Sudoku Verifier

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

PROJECT REPORT

Alphabetic sudoku verifier

Your Name/Group Details

Name & Student Id


1) Irene Sultana – 183014035
2) Faisal Hossain- 182014017
3) Anika Nusrat Ridme- 171014068
Project Overview:

[Here write a short summary about your selected project]

Normally a sudoku puzzle uses 9 by 9 grid in which each row and column and each of the 3 by 3
sub grids must contain 1 to 9 numbers. But we modify the puzzle to 4 by 4 grids and instead of
numbers we use alphabets. This project consists of designing a multi-threaded application that
determine whether a solution of the sudoku is valid or not.

OS Concepts

[Here write in detail mention which OS features/concepts/modules are used in your project and
how did you apply in your code.

You need to write details with example codes.]

This program reads in a sudoku board and incorporates it into a 4 by 4 matrix. The program then
creates variations of the struct for row and column information. Next the program creates pthread
and calls the pthread functions. The functions check if row, column and 2 by 2 sub grid is valid
that means each row and column contains A to D alphabets. If it is valid the function updates the
global array valid to read 1. If any row, col or grid doesn’t contain all alphabets (A-D) then it
will read 0. Once all threads are finished and joined, a loop iterates through valid to check for 1s.
If all elements are 1s, it will print out “solved” and exit. If there is a zero the it will print out” not
solved” and program ends.
1) Create the threads
pthread_t thread_rows, thread_cols, thread1, thread2, thread3,
thread4;

2) Create the return values for the threads


void * all_rows;
void * all_cols;
void * square1;
void * square2;
void * square3;
void * square4;

3)) Initialize the threads ======


pthread_create(&thread_rows, NULL, walk_rows, (void *) param0);

1
pthread_create(&thread_cols, NULL, walk_cols, (void *) param0);
pthread_create(&thread1, NULL, check_square, (void *) param1);
pthread_create(&thread2, NULL, check_square, (void *) param2);
pthread_create(&thread3, NULL, check_square, (void *) param3);
pthread_create(&thread4, NULL, check_square, (void *) param4);

4) Wait for all threads to finish their tasks =======


pthread_join(thread_rows, &all_rows);
pthread_join(thread_cols, &all_cols);
pthread_join(thread1, &square1);
pthread_join(thread2, &square2);
pthread_join(thread3, &square3);
pthread_join(thread4, &square4);

Contribution

[If you are work in a group, please mention each member’s contribution to your project work.]

[If you are alone no need to mention the contribution]


Irene Sultana – 183014035
i) Main function
ii) Creating threads
iii) Wait for all threads to finish their tasks
iv) Check whether the sudoku puzzle is solved
v) Walk rows function

Faisal Hossain- 182014017


i) Walk_cols and check_squares functions

Anika Nusrat Ridme- 171014068


i)Parameter creating.

Repl Link: [if any repl]

https://replit.com/join/fjpozfrf-irenesultana

2
Detailed Source Code:

Copy paste your source code


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

/**
* Structure that holds the parameters passed to a thread.
* This specifies where the thread should start verifying.
*/
typedef struct
{
// The starting row.
int row;
// The starting column.
int col;
// The pointer to the board.
char (* board)[4];
} parameters;

// Prototype for the walk_rows function.


void * walk_rows(void * params);

// Prototype for the walk_cols function.


void * walk_cols(void * params);

// Prototype for 2x2 square function.


void * check_square(void * params);

/***************
* ENTRY POINT
**************/
int main(void)
{
// ====== Create the board =======
char board[4][4] = {
{'A','B','C','D'},
{'B','C','D','A'},
{'C','D','A','B'},
{'D','A','B','C'},

};

// ====== Create the parameter for the columns and rows check =======

3
parameters * param0 = (parameters *) malloc(sizeof(parameters));
param0->row = 0;
param0->col = 0;
param0->board = board;

// ====== Create the parameters for the 2x2 threads ======

// First 2x2
parameters * param1 = (parameters *) malloc(sizeof(parameters));
param1->row = 0;
param1->col = 0;
param1->board = board;

// Second 2x2
parameters * param2 = (parameters *) malloc(sizeof(parameters));
param2->row = 0;
param2->col = 2;
param2->board = board;

// Third 2x2
parameters * param3 = (parameters *) malloc(sizeof(parameters));
param3->row = 2;
param3->col = 0;
param3->board = board;

// Fourth 2x2
parameters * param4 = (parameters *) malloc(sizeof(parameters));
param4->row = 2;
param4->col = 2;
param4->board = board;

// ====== Create the threads ======


pthread_t thread_rows, thread_cols, thread1, thread2, thread3, thread4;

// ====== Create the return values for the threads ======


void * all_rows;
void * all_cols;
void * square1;
void * square2;
void * square3;
void * square4;

// ====== Initialize the threads ======

4
pthread_create(&thread_rows, NULL, walk_rows, (void *) param0);
pthread_create(&thread_cols, NULL, walk_cols, (void *) param0);
pthread_create(&thread1, NULL, check_square, (void *) param1);
pthread_create(&thread2, NULL, check_square, (void *) param2);
pthread_create(&thread3, NULL, check_square, (void *) param3);
pthread_create(&thread4, NULL, check_square, (void *) param4);

// ======= Wait for all threads to finish their tasks =======


pthread_join(thread_rows, &all_rows);
pthread_join(thread_cols, &all_cols);
pthread_join(thread1, &square1);
pthread_join(thread2, &square2);
pthread_join(thread3, &square3);
pthread_join(thread4, &square4);

// ====== Check whether the Sudoku Puzzle was solved ======


if ( (int) all_rows == 1 &&
(int) all_cols == 1 &&
(int) square1 == 1 &&
(int) square2 == 1 &&
(int) square3 == 1 &&
(int) square4 == 1)
{
printf("The Sudoku Puzzle is solved!\n");
}
else {
printf("The Sudoku Puzzle is NOT solved.\n");
}

return 0;
}

/**
* Checks each row if it contains all alphabets A-D
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * walk_rows(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startRow; i < 4; ++i) {
int row[5] = {0};
for (int j = startCol; j < 4; ++j) {
int val = data->board[i][j];

5
if (row[val] == 0) {
return (void *) 1;
}
else{
row[val] = 0;
}
}
}
return (void *) 1;
}

/**
* Checks each column if it contains all alphabets A-D.
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * walk_cols(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
for (int i = startCol; i < 4; ++i) {
int col[5] = {0};
for (int j = startRow; j < 4; ++j) {
int val = data->board[j][i];
if (col[val] == 0) {
return (void *) 1;
}
else{
col[val] = 0;
}
}
}
return (void *) 1;
}

/**
* Checks if a square of size 2x2 contains all alphabets A-D.
* @param void * The parameters (pointer).
* @return void * 1 if all rows contain all alphabets A-D, 0 otherwise.
*/
void * check_square(void * params) {
parameters * data = (parameters *) params;
int startRow = data->row;
int startCol = data->col;
int saved[5] = {0};

6
for (int i = startRow; i < startRow + 2; ++i) {
for (int j = startCol; j < startCol + 2; ++j) {
int val = data->board[i][j];
if (saved[val] == 0) {
return (void *) 1;
}
else{
saved[val] = 0;
}
}
}
return (void *) 1;
}

You might also like