Загрузил(а) файлы в 'final'
This commit is contained in:
		
							parent
							
								
									6c115f087d
								
							
						
					
					
						commit
						e8b6c13023
					
				
							
								
								
									
										136
									
								
								final/game.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								final/game.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,136 @@
 | 
			
		||||
#include "curses.h"
 | 
			
		||||
#include "time.h"
 | 
			
		||||
#include "ballsortpuzzle.h"
 | 
			
		||||
#include "stdlib.h"
 | 
			
		||||
#include "stdbool.h"
 | 
			
		||||
#include "stdio.h"
 | 
			
		||||
 | 
			
		||||
void generator(const int rows, const int columns, char field[rows][columns]) {
 | 
			
		||||
    srand(time(NULL));
 | 
			
		||||
    int column_1 = rand() % columns, column_2 = rand() % columns;
 | 
			
		||||
    while(column_1 == column_2)
 | 
			
		||||
        column_2 = rand() % columns;
 | 
			
		||||
    char characters[6] = "@+*^$&";
 | 
			
		||||
    int k[columns - 2];
 | 
			
		||||
    for(int i = 0; i < columns - 2; i++)
 | 
			
		||||
        k[i] = rows;
 | 
			
		||||
    for(int i = 0; i < rows; i++)
 | 
			
		||||
        for(int j = 0; j < columns; j++)
 | 
			
		||||
        {
 | 
			
		||||
            if(j == column_1 || j == column_2){
 | 
			
		||||
                field[i][j] = ' ';
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            int ind = rand() % (columns - 2);
 | 
			
		||||
            while(k[ind] == 0)
 | 
			
		||||
                ind = rand() % (columns - 2);
 | 
			
		||||
            k[ind]--;
 | 
			
		||||
            field[i][j] = characters[ind];
 | 
			
		||||
        }
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void game_field(const int rows, const int columns, char field[rows][columns]) {
 | 
			
		||||
    for(int i = 0; i < rows; i++)
 | 
			
		||||
    {
 | 
			
		||||
        printw("%d ", i + 1);
 | 
			
		||||
        for(int j = 0; j < columns; j++)
 | 
			
		||||
        {
 | 
			
		||||
            printw("| ");
 | 
			
		||||
            printw("%c ", field[i][j]);
 | 
			
		||||
        }
 | 
			
		||||
        printw("|\n");
 | 
			
		||||
    }
 | 
			
		||||
    printw("  ");
 | 
			
		||||
    for(int i = 0; i < columns; i++)
 | 
			
		||||
    {
 | 
			
		||||
        printw(" ---");
 | 
			
		||||
    }
 | 
			
		||||
    printw("\n    ");
 | 
			
		||||
    for(int i = 0; i < columns; i++)
 | 
			
		||||
        printw("%d   ", i + 1);
 | 
			
		||||
    printw("\n");
 | 
			
		||||
    refresh();
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void down_possible(const int rows, const int columns, char field[rows][columns], int x, int y) {
 | 
			
		||||
    int x_up = 0;
 | 
			
		||||
    for (int i = 0; i < rows; i++) {
 | 
			
		||||
        if (field[i][x - 1] != ' ') {
 | 
			
		||||
            x_up = i;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    int tmp = 0;
 | 
			
		||||
    x--;
 | 
			
		||||
    y--;
 | 
			
		||||
    for(int i = 0; i < rows; i++) {
 | 
			
		||||
        if(field[i][y] != ' ') {
 | 
			
		||||
            if(i && field[i][y] == field[x_up][x]) {
 | 
			
		||||
                field[i - 1][y] = field[x_up][x];
 | 
			
		||||
                mvprintw(i - 1, 4 + y * 4, "%c", field[i - 1][y]);
 | 
			
		||||
                field[x_up][x] = ' ';
 | 
			
		||||
                mvprintw(x_up, 4 + x * 4, "%c", field[x_up][x]);
 | 
			
		||||
            }
 | 
			
		||||
            else tmp = 1;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            if (rows == i + 1) {
 | 
			
		||||
                field[i][y] = field[x_up][x];
 | 
			
		||||
                mvprintw(i, 4 + y * 4, "%c", field[i][y]);
 | 
			
		||||
                field[x_up][x] = ' ';
 | 
			
		||||
                mvprintw(x_up, 4 + x * 4, " ");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if(tmp == 1)mvprintw(rows + 3, 0, "CAN'T MOVE");
 | 
			
		||||
    refresh();
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool check(const int rows, const int columns, char field[rows][columns]) {
 | 
			
		||||
    bool check_tmp;
 | 
			
		||||
    check_tmp = true;
 | 
			
		||||
    for(int i = 0; i < columns; i++) {
 | 
			
		||||
        for(int j = 1; j < rows; j++){
 | 
			
		||||
            if(field[j][i] != field[j - 1][i])
 | 
			
		||||
                check_tmp = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if(check_tmp == true)
 | 
			
		||||
        return true;
 | 
			
		||||
    else
 | 
			
		||||
        return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ball_sort_puzzle() {
 | 
			
		||||
    initscr();
 | 
			
		||||
    int rows = 4;
 | 
			
		||||
    int columns = 6;
 | 
			
		||||
    char field[rows][columns];
 | 
			
		||||
    generator(rows, columns, field);
 | 
			
		||||
    game_field(rows, columns, field);
 | 
			
		||||
    while(check(rows, columns, field) == false) {
 | 
			
		||||
        mvprintw(rows + 2, 0, "WHAT:       ");
 | 
			
		||||
        move(rows + 2, 6);
 | 
			
		||||
        refresh();
 | 
			
		||||
        int x;
 | 
			
		||||
        scanw("%d", &x);
 | 
			
		||||
        mvprintw(rows + 2, 0, "WHERE:       ");
 | 
			
		||||
        mvprintw(rows + 3, 0, "                          ");
 | 
			
		||||
        move(rows + 2, 7 );
 | 
			
		||||
        refresh();
 | 
			
		||||
        int y;
 | 
			
		||||
        scanw("%d", &y);
 | 
			
		||||
        if(x == y){mvprintw(rows + 3, 0, "NUMBERS MUST BE DIFFERENT"); continue;}
 | 
			
		||||
        if(x > columns || y > columns){mvprintw(rows + 3, 0, "TOO BIG NUMBERS"); continue;}
 | 
			
		||||
        down_possible(rows, columns, field, x, y);
 | 
			
		||||
    }
 | 
			
		||||
    mvprintw(rows + 2, 0, "YOU WON!\nPRESS ENTER TO EXIT");
 | 
			
		||||
    refresh();
 | 
			
		||||
    getch();
 | 
			
		||||
    endwin();
 | 
			
		||||
    return;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user