Форум русскоязычного сообщества Ubuntu


Считаете, что Ubuntu недостаточно дружелюбна к новичкам?
Помогите создать новое Руководство для новичков!

Автор Тема: C - лабиринт до смерть.  (Прочитано 545 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн victor00000

  • Автор темы
  • Старожил
  • *
  • Сообщений: 15568
  • Глухонемой (Deaf)
    • Просмотр профиля
C - лабиринт до смерть.
« : 06 Октября 2018, 11:31:46 »
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
│                ╭╮                 │
│          ╭╾╮   ╽╽                 │
│       ╭╮ ╰╮╽╭╾╮╽╰╾╮               │
│      ╭╯╰╾╾╯╽╽ ╰╯╭╮╰╮              │
│     ╭╯     ╽╽ ╭╾╯╽╭╯              │
│     ╽      ╽╽╭╯╭*╰╯               │
│╭╾╾╾╾╯     ╭╯╽╰╾╯                  │
│╽╭╾╾╮      ╰╾╯                     │
│╽╰╾╮╰╾╾╮                           │
│╰╾╾╯X╾╾╯                           │
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
87 year

C
// test_lab_to.c
// "maze until death, your yo."
// victor00000 (c) 2018
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <locale.h>
#include <sys/ioctl.h>
// ~$ gcc test_lab_to.c -o test_lab_to -lpthread
// ~$ ./test_lab_to
// exit - 'Enter'

int aa[4];
int cnt = 0;
int exit_0 = 0;
const char *cls_c = "\e[1;1H\e[2J";

int cvadr[] = { L'┏', L'━', L'┓', L'│', L'┛',
L'━', L'┗', L'│', 0
};

int *lt = (int *)L"╾╭╮╯╰╽";

typedef struct {
int w, h;
} wht;
int **xy, yy, xx, r = 100;

//===================exit ebter
void *tp1(void *a)
{
char cb;

while (fread(&cb, 1, 1, stdin) != 1) {
}
exit_0 = 1;
return 0;
}

//==============aa null to 4
void aa_null()
{
int i;

for (i = 0; i < 4; i++)
aa[i] = 0;
}

//=================cls
void cls()
{
fwrite(cls_c, 1, strlen(cls_c), stdout);
}

//=====================get w h
int getwhterm(int *wt, int *ht)
{
int e;
struct winsize wl;

e = ioctl(STDOUT_FILENO, TIOCGWINSZ, &wl);
*ht = wl.ws_row;
*wt = wl.ws_col;
return e;
}

//=================xy_rnd
void xy_rnd(wht wh)
{
int i = 0, r1 = r, d, x, y;

aa_null();
x = xx;
y = yy;
while (1) {
// i++;
for (i = 0; i < 4; i++) {
if (aa[i] == 0)
break;
}
if (i == 4) {
// printf("%d year\n", cnt);
exit_0 = 1;
break;
// exit(0);
}
r = rand() % 4;
aa[r] = 1;
if (r == 0)
yy--;
if (r == 1)
xx++;
if (r == 2)
yy++;
if (r == 3)
xx--;

if (xx > wh.w - 2 || xx < 1) {
yy = y;
xx = x; /*wh.w - 2 */ ;
continue;
}
// if (xx<1) { xx = 1; continue; }
if (yy > wh.h - 2 || yy < 0) {
xx = x;
yy = y; /*wh.h - 2 */ ;
continue;
}
// if (yy < 1) { yy = 1; continue; }

if (!(xy[yy][xx] == L'X' || xy[yy][xx] == L' ')) {
xx = x;
yy = y;
continue;
}

break;
}

if (exit_0 == 1)
return;

d = lt[0];

if (r1 == 100)
d = L'*';
else {

if (r1 == 0) {
if (r == 0)
d = lt[5];
if (r == 1)
d = lt[1];
if (r == 3)
d = lt[2];
}

if (r1 == 1) {
if (r == 0)
d = lt[3];
if (r == 1)
d = lt[0];
if (r == 2)
d = lt[2];
}

if (r1 == 2) {
if (r == 2)
d = lt[5];
if (r == 1)
d = lt[4];
if (r == 3)
d = lt[3];
}

if (r1 == 3) {
if (r == 3)
d = lt[0];
if (r == 0)
d = lt[4];
if (r == 2)
d = lt[1];
}

}
xy[y][x] = d;
xy[yy][xx] = L'X';
cnt++;
//==============2
}

//===================xy_down
void xy_down(wht wh)
{

int x, y;

for (x = 1; x < wh.w - 2; x++) {
for (y = 1; y < wh.h - 1; y++) {
//xy [y] [x] = L'.';
xy[y][x] = xy[y][x + 1];

}
}
}

//===================xy_clear
void xy_clear(wht wh)
{
int x, y;

for (y = 0; y < wh.h; y++) {
for (x = 0; x < wh.w; x++) {
if (x == 0 || x == wh.w - 1)
xy[y][x] = cvadr[3];
else
xy[y][x] = L' ';

if (y == 0) {
xy[y][x] = cvadr[1];
if (x == 0)
xy[y][x] = cvadr[0];
if (x == wh.w - 1)
xy[y][x] = cvadr[2];
}
if (y == wh.h - 1) {
xy[y][x] = cvadr[1];
if (x == 0)
xy[y][x] = cvadr[6];
if (x == wh.w - 1)
xy[y][x] = cvadr[4];

//===================1
}
}
}
}

//====================xy_init
int **xy_init(wht wh)
{
int **x_y = 0;

x_y = malloc(sizeof(int) * wh.h);
int i;

for (i = 0; i < wh.h; i++) {
x_y[i] = malloc(sizeof(int) * wh.w);
}
return x_y;
}

//=================tp
void *tp(void *a)
{
wht *wh = (wht *) a;
int x, y;

while (1) {
xy_rnd(*wh);
// xy_down(*wh);
cls();
for (y = 0; y < wh->h; y++) {
for (x = 0; x < wh->w; x++) {
printf("%lc", xy[y][x]);
}
// if (y != wh->h - 1)
puts("");
}
if (exit_0 == 1)
break;
usleep(1000 * 300);
}
return 0;
}

//==================m0
int main()
{
//cls();
//return 0;
srand(time(0));
setlocale(LC_ALL, getenv("LANG"));
int wt, ht, e;

e = getwhterm(&wt, &ht);
if (e != 0) {
perror("Err");
return 1;
}

pthread_t t, t1;
wht wh;

wh.w = wt;
wh.h = ht - 1;

yy = wh.h - 2;

xy = xy_init(wh);
xy_clear(wh);
// xy[wh.h - 2][wh.w - 2] = lt[0];
// xy_down(wh);

xx = wh.w / 2;
yy = wh.h / 2;

pthread_create(&t, 0, tp, &wh);
pthread_create(&t1, 0, tp1, 0);

// char cb;

while (1) {
usleep(1000 * 1000);
if (exit_0 == 1)
break;
//fread(&cb, 1, 1, stdin) != 1) {
}
pthread_cancel(t1);
pthread_join(t1, 0);
// exit_0 = 1;
pthread_join(t, 0);
for (e = 0; e < wh.h; e++)
free(xy[e]);
free(xy);
printf("%d year\n", cnt);
return 0;
}
http://paste.ubuntu.com/p/vH8PsSh9Cn/


Пользователь добавил сообщение 06 Октября 2018, 15:02:21:
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
│        ╭╾╾╾╮╭╮╭╮  ╭╮╭╾╾╮          │
│       ╭╯ ╭╮╰╯╰╯╰╾╾╯╰╯ ╭╯          │
│       ╽ ╭╯╰╾╾╾╮ ╭╾╾╾╮ ╽           │
│       ╽ ╽ ╭╮╭╮╰╾╯╭╾╾╯ ╽           │
│       ╰╮╽╭╯╰╯╽X╭╾╯   ╭╯           │
│        ╽╰╯ ╭╾╯╽╽* ╭╾╾╯            │
│        ╰╮  ╽  ╰╯╽╭╯               │
│         ╰╮╭╯    ╽╰╾╾╮             │
│          ╰╯     ╰╮╭╮╽             │
│                  ╰╯╰╯             │
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
113 year

Пользователь добавил сообщение 06 Октября 2018, 16:41:42:
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
│         ╭╾╾╮          ╭╮   ╭╮     │
│ ╭╾╮     ╰╾╮╽         ╭╯╰╮╭╾╯╽     │
│╭╯X╽       ╽╽         ╽╭╮╽╽  ╰╮    │
│╽╭╯╰╮     ╭╯╰╾╾╾╾╾╾╾╾╮╰╯╽╰╯   ╽    │
│╽╽╭╾╯     ╰╾╾╮╭╾╾╾╮╭╮╽ ╭╯     ╽    │
│╰╯╽╭╾╮       ╰╯ ╭*╰╯╽╰╾╯    ╭╮╽    │
│  ╽╰╮╽  ╭╮╭╮ ╭╾╾╯╭╾╾╯╭╾╮   ╭╯╰╯    │
│  ╰╾╯╰╾╾╯╽╽╰╮╰╾╾╾╯  ╭╯ ╽ ╭╮╽       │
│         ╰╯╭╯    ╭╮ ╽ ╭╯╭╯╽╽       │
│           ╰╾╾╾╾╾╯╰╾╯ ╰╾╯ ╰╯       │
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
170 year


Пользователь добавил сообщение 06 Октября 2018, 20:04:27:
ещё
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
│                ╭╾╾╾╾╮             │
│                ╰╮X╭╾╯             │
│                ╭╯╰╯               │
│                ╽                  │
│                ╽                  │
│                ╰*                 │
│                                   │
│                                   │
│                                   │
│                                   │
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
1538844162 = 19 year
код видите 1538844162, это тогда ваше будет повторно генератора, наберите .команду.
./test_lab_to 1538844162

C
// test_lab_to.c
// "maze until death, your yo."
// victor00000 (c) 2018
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <locale.h>
#include <sys/ioctl.h>
// ~$ gcc test_lab_to.c -o test_lab_to -lpthread
// ~$ ./test_lab_to
// exit - 'Enter'

time_t tt;
int aa[4];
int cnt = 0;
int exit_0 = 0;
const char *cls_c = "\e[1;1H\e[2J";

int cvadr[] = { L'┏', L'━', L'┓', L'│', L'┛',
L'━', L'┗', L'│', 0
};

int *lt = (int *)L"╾╭╮╯╰╽";

typedef struct {
int w, h;
} wht;
int **xy, yy, xx, r = 100;

void xy_clear(wht wh);


//=================xy null
void xy_null (wht wh){
cnt = 0;
xx = wh.w / 2;
yy = wh.h / 2;
xy_clear (wh);
r = 100;
}

//===================exit ebter
void *tp1(void *a)
{
char cb;

while (fread(&cb, 1, 1, stdin) != 1) {
}
exit_0 = 2;
return 0;
}

//==============aa null to 4
void aa_null()
{
int i;

for (i = 0; i < 4; i++)
aa[i] = 0;
}

//=================cls
void cls()
{
fwrite(cls_c, 1, strlen(cls_c), stdout);
}

//=====================get w h
int getwhterm(int *wt, int *ht)
{
int e;
struct winsize wl;

e = ioctl(STDOUT_FILENO, TIOCGWINSZ, &wl);
*ht = wl.ws_row;
*wt = wl.ws_col;
return e;
}

//=================xy_rnd
void xy_rnd(wht wh)
{
int i = 0, r1 = r, d, x, y;

aa_null();
x = xx;
y = yy;
while (1) {
// i++;
for (i = 0; i < 4; i++) {
if (aa[i] == 0)
break;
}
if (i == 4) {
// printf("%d year\n", cnt);
exit_0 = 1;
break;
// exit(0);
}
r = rand() % 4;
aa[r] = 1;
if (r == 0)
yy--;
if (r == 1)
xx++;
if (r == 2)
yy++;
if (r == 3)
xx--;

if (xx > wh.w - 2 || xx < 1) {
yy = y;
xx = x; /*wh.w - 2 */ ;
continue;
}
// if (xx<1) { xx = 1; continue; }
if (yy > wh.h - 2 || yy < 0) {
xx = x;
yy = y; /*wh.h - 2 */ ;
continue;
}
// if (yy < 1) { yy = 1; continue; }

if (!(xy[yy][xx] == L'X' || xy[yy][xx] == L' ')) {
xx = x;
yy = y;
continue;
}

break;
}

if (exit_0 != 0){
if (exit_0 == 2) {exit_0 = 1;return;}

// if (cnt > 200)
return;
exit_0 = 0;
xy_null (wh);
return;
}
d = lt[0];

if (r1 == 100)
d = L'*';
else {

if (r1 == 0) {
if (r == 0)
d = lt[5];
if (r == 1)
d = lt[1];
if (r == 3)
d = lt[2];
}

if (r1 == 1) {
if (r == 0)
d = lt[3];
if (r == 1)
d = lt[0];
if (r == 2)
d = lt[2];
}

if (r1 == 2) {
if (r == 2)
d = lt[5];
if (r == 1)
d = lt[4];
if (r == 3)
d = lt[3];
}

if (r1 == 3) {
if (r == 3)
d = lt[0];
if (r == 0)
d = lt[4];
if (r == 2)
d = lt[1];
}

}
xy[y][x] = d;
xy[yy][xx] = L'X';
cnt++;
//==============2
}

//===================xy_down
void xy_down(wht wh)
{

int x, y;

for (x = 1; x < wh.w - 2; x++) {
for (y = 1; y < wh.h - 1; y++) {
//xy [y] [x] = L'.';
xy[y][x] = xy[y][x + 1];

}
}
}

//===================xy_clear
void xy_clear(wht wh)
{
int x, y;

for (y = 0; y < wh.h; y++) {
for (x = 0; x < wh.w; x++) {
if (x == 0 || x == wh.w - 1)
xy[y][x] = cvadr[3];
else
xy[y][x] = L' ';

if (y == 0) {
xy[y][x] = cvadr[1];
if (x == 0)
xy[y][x] = cvadr[0];
if (x == wh.w - 1)
xy[y][x] = cvadr[2];
}
if (y == wh.h - 1) {
xy[y][x] = cvadr[1];
if (x == 0)
xy[y][x] = cvadr[6];
if (x == wh.w - 1)
xy[y][x] = cvadr[4];

//===================1
}
}
}
}

//====================xy_init
int **xy_init(wht wh)
{
int **x_y = 0;

x_y = malloc(sizeof(int) * wh.h);
int i;

for (i = 0; i < wh.h; i++) {
x_y[i] = malloc(sizeof(int) * wh.w);
}
return x_y;
}

//=================tp
void *tp(void *a)
{
wht *wh = (wht *) a;
int x, y;

while (1) {
xy_rnd(*wh);
// xy_down(*wh);
cls();
for (y = 0; y < wh->h; y++) {
for (x = 0; x < wh->w; x++) {
printf("%lc", xy[y][x]);
}
// if (y != wh->h - 1)
puts("");
}
if (exit_0 == 1)
break;
usleep(1000 * 300 );
}
return 0;
}

//==================m0
int main(int ia, char *ca[])
{
//cls();
//return 0;
tt = time (NULL);
if (ia == 2 )
tt = atoi (ca[1]);
srand(tt);
setlocale(LC_ALL, getenv("LANG"));
int wt, ht, e;

e = getwhterm(&wt, &ht);
if (e != 0) {
perror("Err");
return 1;
}

pthread_t t, t1;
wht wh;

wh.w = wt;
wh.h = ht - 1;

yy = wh.h - 2;

xy = xy_init(wh);
xy_clear(wh);
// xy[wh.h - 2][wh.w - 2] = lt[0];
// xy_down(wh);

xx = wh.w / 2;
yy = wh.h / 2;

pthread_create(&t, 0, tp, &wh);
pthread_create(&t1, 0, tp1, 0);

// char cb;

while (1) {
usleep(1000 * 1000);
if (exit_0 == 1)
break;
//fread(&cb, 1, 1, stdin) != 1) {
}
pthread_cancel(t1);
pthread_join(t1, 0);
// exit_0 = 1;
pthread_join(t, 0);
for (e = 0; e < wh.h; e++)
free(xy[e]);
free(xy);
printf("%d = %d year\n",(int) tt, cnt);
return 0;
}
http://paste.ubuntu.com/p/ty9rVhFTPm/
« Последнее редактирование: 06 Октября 2018, 20:04:27 от victor00000 »
Wars ~.o

 

Страница сгенерирована за 0.072 секунд. Запросов: 25.