Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++

Модерирует : ShIvADeSt

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

Lyrik

Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору

Код:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
 
#define MENTION    printf("Enter 'j', 'i', 'l' or 'k' to move the empty cell left, up, right or down correspondingly, or 'q' to exit: \n\n");
 
int** field;
int emptyCell_x, emptyCell_y;
 
//------------------
void init()
{
    int x,y, i,j;
    srand(time(NULL));
    for(i=1; i<=15;)
    {
        x=rand()%4; y=rand()%4;
        if(field[x][y] == 0) field[x][y] = i++;        
    }
 
    for(i=0; i<4; i++) //to find the empty cell
        for(j=0; j<4; j++)
            if(field[j][i] == 0)  
            {
                emptyCell_x=j; emptyCell_y = i; return;
            }
 
}
 
//---------------------
void printField()
{
    int i,j;
    for(i=0; i<4; i++)
    {
        for(j=0; j<4; j++)
            if(field[j][i]) printf("%3d", field[j][i]);
            else printf("  _");
        printf("\n\n");
    }
    printf("\n\n");
}
 
//-----------------------
int notEndYet()
{
    int i,j;
    for(i=0; i<3; i++)
        for(j=0; j<4; j++)
            if(field[j][i] != 4*i+j+1) return 0; //go on play
    if(field[0][3] != 13) return 0;
 
    return 1; //victory!
}
 
//---------------   allows move the emply cell
int move()
{
    char c;
    
    while(1)
    {
        c = getch();  
        switch(c)
        {
            case 'j':
                if(emptyCell_x==0)
                {
                    printf("Not valid move.\n\n");
                    break;
                }
                else
                {
                    field[emptyCell_x][emptyCell_y] = field[emptyCell_x-1][emptyCell_y];
                    field[emptyCell_x-1][emptyCell_y] = 0;
                    emptyCell_x--;
                    return 1;
                }
            case 'i':
                if(emptyCell_y==0)
                {
                    printf("Not valid move.\n\n");
                    break;
                }
                else
                {
                    field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y-1];
                    field[emptyCell_x][emptyCell_y-1] = 0;
                    emptyCell_y--;
                    return 2;
                }
            case 'l':
                if(emptyCell_x==3)
                {
                    printf("Not valid move.\n\n");
                    break;
                }
                else
                {
                    field[emptyCell_x][emptyCell_y] = field[emptyCell_x+1][emptyCell_y];
                    field[emptyCell_x+1][emptyCell_y] = 0;
                    emptyCell_x++;
                    return 3;
                }
            case 'k':
                if(emptyCell_y==3)
                {
                    printf("Not valid move.\n\n");
                    break;
                }
                else
                {
                    field[emptyCell_x][emptyCell_y] = field[emptyCell_x][emptyCell_y+1];
                    field[emptyCell_x][emptyCell_y+1] = 0;
                    emptyCell_y++;
                    return 4;
                }
            case 'q': exit(1);
            default: MENTION
        }
    }
}
 
//----------------- main function
void main()
{
    field = (int**)calloc(4, sizeof(int*));
    for(int i=0; i<4; i++)
        field[i] = (int*)calloc(4, sizeof(int));
 
    do    init(); while(notEndYet());
    MENTION
    printField();
    
    while(!notEndYet())
    {
        move();
        printField();
    }
 
    printf("\nYou win!\n"); getch();
}

Всего записей: 388 | Зарегистр. 04-04-2006 | Отправлено: 23:33 24-10-2007
Открыть новую тему     Написать ответ в эту тему

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Задачи по C/С++


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru