diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-07-27 08:35:04 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-07-27 08:35:04 +0200 |
| commit | 8172a7f70e35942cf102fb07963687d9ddace818 (patch) | |
| tree | e6cd8039208c1da94e2e10a361cc65406a021195 | |
| parent | 213228e769fe682576c27704981c0b6ff4303aae (diff) | |
| download | piscine-8172a7f70e35942cf102fb07963687d9ddace818.tar.gz piscine-8172a7f70e35942cf102fb07963687d9ddace818.tar.bz2 piscine-8172a7f70e35942cf102fb07963687d9ddace818.zip | |
adding bsq generator, remove rush01 6x6 try
| -rwxr-xr-x | bsq/bsq | bin | 13776 -> 13944 bytes | |||
| -rwxr-xr-x | bsq/gen.pl | 18 | ||||
| -rw-r--r-- | bsq/main.c | 2 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/board.c | 77 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/helper.c | 46 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/include.h | 50 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/main.c | 56 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/solve.c | 146 | ||||
| -rw-r--r-- | rush01_6x6_try/ex00/square.c | 134 |
9 files changed, 18 insertions, 511 deletions
| Binary files differ diff --git a/bsq/gen.pl b/bsq/gen.pl new file mode 100755 index 0000000..bac7219 --- /dev/null +++ b/bsq/gen.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +my ($x, $y, $density) = @ARGV; + +print "$y.ox\n"; +for (my $i = 0; $i < $y; $i++) { + for (my $j = 0; $j < $x; $j++) { + if (int(rand($y) * 2) < $density) { + print "o"; + } + else { + print "."; + } + } + print "\n"; +} + + @@ -17,9 +17,7 @@ int main(int argc, char **argv) { int i; - t_parsed_terrain *pterrain; - pterrain = NULL; if (argc == 1) { parse_stdin_print(); diff --git a/rush01_6x6_try/ex00/board.c b/rush01_6x6_try/ex00/board.c deleted file mode 100644 index 573d459..0000000 --- a/rush01_6x6_try/ex00/board.c +++ /dev/null @@ -1,77 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* board.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 13:23:16 by cacharle #+# #+# */ -/* Updated: 2019/07/14 13:31:15 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "include.h" - -/*void fill_clue1n(t_board board, t_views views)*/ -/*{*/ - /*int i;*/ - /*int j;*/ - - /*i = 0;*/ - /*while (i < 4)*/ - /*{*/ - /*j = 0;*/ - /*while (j < 4)*/ - /*{*/ - /*if (views[i][j] == 1)*/ - /*line_at(i);*/ - - /*}*/ - /*}*/ -/*}*/ - -/*int *line_at(t_board board, t_view_side side, int line_index);*/ -/*{*/ - /*int *line;*/ - /*int i;*/ - - /*line = malloc(sizeof(int) * 4);*/ - /*i = 0*/ - /*if (side == row_left)*/ - /*while (i < 4)*/ - /*line[i] = board[line_index][i++];*/ - /*if (side == row_right)*/ - /*while (i < 4)*/ - /*line[i] = board[line_index][size - i++];*/ - /*if (side == col_up)*/ - /*while (i < 4)*/ - /*line[i] = board[line_index][i++];*/ - - - -/*}*/ - -/*void set_at(t_board board, t_view_side side, int side_index, int line_index, int value)*/ -/*{*/ - /*if (side == row_left)*/ - /*board[side_index][line_index] = value;*/ - /*if (side == row_right)*/ - /*board[side_index][3 - line_index] = value;*/ - /*if (side == col_up)*/ - /*board[line_index][side_index] = value;*/ - /*if (side == col_down)*/ - /*board[3 - line_index][side_index] = value;*/ -/*}*/ - -int get_with_view(t_board board, t_view_point view, int view_line, int line_index) -{ - if (view == row_left) - return board.self[view_line][line_index]; - if (view == row_right) - return board.self[view_line][board.size - 1 - line_index]; - if (view == col_up) - return board.self[line_index][view_line]; - if (view == col_down) - return board.self[board.size - 1 - line_index][view_line]; - return (-1); -} diff --git a/rush01_6x6_try/ex00/helper.c b/rush01_6x6_try/ex00/helper.c deleted file mode 100644 index eb391ea..0000000 --- a/rush01_6x6_try/ex00/helper.c +++ /dev/null @@ -1,46 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* helper.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 12:52:42 by cacharle #+# #+# */ -/* Updated: 2019/07/14 10:36:19 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include <unistd.h> - -void ft_putchar(char c) -{ - write(1, &c, 1); -} - -void ft_putnbr(int nb) -{ - unsigned int p_nb; - - p_nb = nb; - if (nb < 0) - { - write(1, "-", 1); - p_nb = -nb; - } - if (p_nb > 9) - ft_putnbr(p_nb / 10); - ft_putchar(p_nb % 10 + '0'); -} - -int strlen_ignore_space(char *str) -{ - int counter; - - counter = 0; - while (*str) - { - if (*str != ' ') - counter++; - str++; - } -} diff --git a/rush01_6x6_try/ex00/include.h b/rush01_6x6_try/ex00/include.h deleted file mode 100644 index 85f595f..0000000 --- a/rush01_6x6_try/ex00/include.h +++ /dev/null @@ -1,50 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* include.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 12:07:59 by cacharle #+# #+# */ -/* Updated: 2019/07/14 13:22:42 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef INCLUDE_H -# define INCLUDE_H - -//typedef int** t_board; -//typedef int** t_board; - -typedef enum -{ - col_up, - col_down, - row_left, - row_right -} t_view_point; -typedef struct -{ - int size; - int **self; -} t_square; -typedef t_square t_board; -typedef t_square t_views; - -int solve(t_board board, t_views views); -int find_next_unassigned(t_board board, int *row, int *col); -int is_alone(t_board board, int bfloor, int row, int col); -int check_viewpoints(t_board board, t_views views); -int check_line(t_board board, t_view_point view, int view_line, - int building_viewed); -t_views parse_arg(char *arg); -int **init_square(int size); -int **dup_square(int **square); -void destroy_square(int **square); -void print_square(t_board board); -int get_with_view(t_board board, t_view_point view, int view_line, - int line_index); -void ft_putchar(char c); -void ft_putnbr(int nb); - -#endif diff --git a/rush01_6x6_try/ex00/main.c b/rush01_6x6_try/ex00/main.c deleted file mode 100644 index 4e4c239..0000000 --- a/rush01_6x6_try/ex00/main.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 11:30:35 by cacharle #+# #+# */ -/* Updated: 2019/07/14 10:24:10 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include <unistd.h> -#include "include.h" - -#include <string.h> -#include <stdio.h> -int main(int argc, char ** argv) -{ - t_views views; - t_board board; - - if (argc != 2) - { - write(1, "Error\n", 6); - return (0); - } - /*int tb[4][4] = {*/ - /*{1, 2, 3, 4},*/ - /*{2, 3, 4, 1},*/ - /*{3, 4, 1, 2},*/ - /*{4, 1, 2, 3}*/ - /*};*/ - views = parse_arg(argv[1]); - /*print_square(views);*/ - board = init_square(4); - /*memcpy(board[0], tb[0], sizeof(int) * 4);*/ - /*memcpy(board[1], tb[1], sizeof(int) * 4);*/ - /*memcpy(board[2], tb[2], sizeof(int) * 4);*/ - /*memcpy(board[3], tb[3], sizeof(int) * 4);*/ - /*print_square(board);*/ - /*printf("check views %d\n", check_viewpoints(board, views));*/ - /*print_square(board);*/ - /*printf("%d\n", get_with_view(board, col_down, 0, 2));*/ - /*ft_putchar('\n');*/ - solve(board, views); - /*ft_putchar('\n');*/ - /*board = init_square(4);*/ - /*board[0][0] = 2;*/ - /*ft_putchar('\n');*/ - /*print_square(board);*/ - /*solve(board);*/ - /*ft_putchar('\n');*/ - /*print_square(board);*/ - return (0); -} diff --git a/rush01_6x6_try/ex00/solve.c b/rush01_6x6_try/ex00/solve.c deleted file mode 100644 index d08e4aa..0000000 --- a/rush01_6x6_try/ex00/solve.c +++ /dev/null @@ -1,146 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* solve.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 14:25:32 by cacharle #+# #+# */ -/* Updated: 2019/07/14 13:30:12 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "include.h" -#include <stdio.h> - -#define UNASSIGNED 0 -#define TRUE 1 -#define FALSE 0 - -/* -** Find all the sudoku 4x4 grid recursively -** print the one that checks out with the given viewpoints -*/ - -int solve(t_board board, t_views views) -{ - int row; - int col; - int i; - t_board board_clone; - - if (!find_next_unassigned(board, &row, &col)) - return (TRUE); - i = 1; - while (i <= size) - { - board_clone = dup_square(board); - if (is_alone(board_clone, row, col, i)) - { - board_clone.self[row][col] = i; - if (solve(board_clone, views)) - { - if (check_viewpoints(board_clone, views)) - { - print_square(board_clone); - return (TRUE); - } - return (FALSE); - } - } - destroy_square(board_clone); - i++; - } - return (FALSE); -} - -/* -** Move `row` and `col` to the next unassigned(== 0) position -** returns FALSE if the board is already filled with number, TRUE otherwise -*/ - -int find_next_unassigned(t_board board, int *row, int *col) -{ - *row = 0; - while (*row < board.size) - { - *col = 0; - while (*col < board.size) - { - if (board.self[*row][*col] == UNASSIGNED) - return (TRUE); - (*col)++; - } - (*row)++; - } - return (FALSE); -} - -/* -** Check if `building_floor` is already is unique in the row and column -*/ - -int is_alone(t_board board, int row, int col, int building_floor) -{ - int i; - - i = 0; - while (i < board.size) - if (board.self[row][i++] == building_floor) - return (FALSE); - i = 0; - while (i < board.size) - if (board.self[i++][col] == building_floor) - return (FALSE); - return (TRUE); -} - -/* -** Checks if the grid is valid according to the viewpoints -*/ - -int check_viewpoints(t_board board, t_views views) -{ - t_view_point view; - int j; - - view = col_up; - while (view <= row_right) - { - j = 0; - while (j < board.size) - { - if (!check_line(board, view, j, views[view][j])) - return (FALSE); - j++; - } - view++; - } - return (TRUE); -} - -/* -** Returns TRUE if the number buildings viewed -** with some viewpoint on a line are equal to `building_viewed`. -*/ - -int check_line(t_board board, t_view_point view, int view_line, - int building_viewed) -{ - int i; - int tmp_building_floor; - - i = 0; - while (i < board.size) - { - tmp_building_floor = get_with_view(board, view, view_line, i); - if (tmp_building_floor == UNASSIGNED) - return (FALSE); - building_viewed--; - while (i + 1 < board.size && tmp_building_floor > get_with_view( - board, view, view_line, i + 1)) - i++; - i++; - } - return (building_viewed == 0); -} diff --git a/rush01_6x6_try/ex00/square.c b/rush01_6x6_try/ex00/square.c deleted file mode 100644 index 0260f73..0000000 --- a/rush01_6x6_try/ex00/square.c +++ /dev/null @@ -1,134 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* helpers.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/07/13 12:31:20 by cacharle #+# #+# */ -/* Updated: 2019/07/14 13:26:55 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include <stdlib.h> -#include "include.h" - -/* -** Parse the program's first argument where each row is in order -** col up, col down, row left, row right -*/ - -t_views parse_arg(char *arg) -{ - int i; - int j; - t_views views; - - views = init_square(4); - views.size = 4; - i = 0; - j = 0; - while (i < 16) - { - if (arg[i] == ' ') - { - arg++; - continue; - } - views.self[j][i % 4] = arg[i] - '0'; - if (i % 4 == 3) - j++; - i++; - } - return (views); -} - -/* -** Allocate memory for a 2D array(square) of `size` -** initialize each cell to 0. -*/ - -t_square init_square(int size) -{ - int i; - int j; - t_square square; - - square.self = malloc(sizeof(int*) * size); - square.size = size - i = 0; - while (i < size) - { - square.self[i] = malloc(sizeof(int) * size); - j = 0; - while (j < size) - square.self[i][j++] = 0; - i++; - } - return (square); -} - -/* -** Duplicate the square, create an empty clone -** and copy each element of `square` in it. -*/ - -t_square dup_square(t_square square) -{ - t_square clone; - int i; - int j; - - clone = init_square(4); - i = 0; - while (i < 4) - { - j = 0; - while (j < 4) - { - clone.self[i][j] = square.self[i][j]; - j++; - } - i++; - } - return (clone); -} - -/* -** Free each row of the square and the square itself. -*/ - -void destroy_square(int **square) -{ - int i; - - i = 0; - while (i < 4) - free(square.self[i++]); - free(square.self); -} - -/* -** Print each row followed by a line break -** and each element of the row but the last followed by a space. -*/ - -void print_square(t_square square) -{ - int i; - int j; - - i = 0; - while (i < square.size) - { - j = 0; - while (j < square.size) - { - ft_putnbr(square.self[i][j++]); - if (j != 4) - ft_putchar(' '); - } - ft_putchar('\n'); - i++; - } -} |
