Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 200
- #define LESS -1
- // This function creates the matrix
- int** create(int n) {
- int i, j ;
- int **matrix ;
- matrix = (int **) malloc(n * sizeof(int *)) ;
- for(i = 0; i < n; i++)
- matrix[i] = (int *) malloc(n * sizeof(int));
- for(i = 0; i < n; i++) {
- for(j = 0; j < n; j++)
- matrix[i][j] = 0 ;
- }
- return matrix ;
- }
- // This function prints the matrix
- void print(int **matrix, int n) {
- int i, j ;
- for(i = 0; i < n ; i++) {
- for(j = 0; j < n; j++)
- printf("%d ", matrix[i][j]);
- printf("\n");
- }
- }
- int finish(int **matrix, int n) {
- int i, j ;
- int row, col ;
- // check rows
- for(i = 0; i < n; i++) {
- row = 0;
- for(j = 0; j < n; j++) {
- if(row == matrix[i][j]) {
- if(!row) {
- row = LESS ;
- break ;
- }
- } else if(j) {
- row = LESS ;
- break ;
- }
- row = matrix[i][j] ;
- }
- if(row == LESS)
- return 0 ;
- }
- // check cols
- return 1 ;
- }
- int main() {
- int i, j, k, n ;
- int choose = 0 ;
- printf("Welcome new user\n\nHELP SYSTEM: The computer is 2, you is 1 and the pattern is 0.\n\n");
- printf("1 - Start the game\n2-Exit\n");
- back:
- printf("Please, insert your option: ");
- scanf("%d", &choose);
- switch(choose) {
- case 1: {
- printf("Please, insert the order of matrix: ");
- scanf("%d", &n);
- break ;
- }
- case 2: {
- printf("Oh :( Bye bye");
- break ;
- }
- default: {
- goto back ;
- }
- }
- int **matrix = create(n), cond = 0;
- print(matrix, n);
- while(!cond) {
- int row, col ;
- printf("Insert the row and col: ");
- scanf("%d %d", &row, &col);
- matrix[row-1][col-1] = 1 ;
- printf("\n");
- print(matrix, n);
- if(finish(matrix, n))
- cond = 1 ;
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement