Ejemplo n.º 1
0
// start()
// ... 게임 플레이 전 여러가지를 처리한다.
//
ReturnCode SnakeGame::start()
{
    this->initialize();     // 점수와 먹이위치를 초기화한다.
    snake_.initialize();    // 뱀의 길이 및 위치를 초기화한다.

    display_.clear();
    display_.print_prompt("Do you want to play? (y/n) ");

    int key;
    do
    {
        key = getch();
    }
    while (key != 'y' && key != 'Y' && key != 'n' && key != 'N');

    switch (key)
    {
    case 'y':
    case 'Y':
        make_food();
        display_.clear();
        display_.print_wall();
        display_.print_snake(snake_);
        display_.print_food(food_position_);
        return RETURN_SUCCESS;
    case 'n':
    case 'N':
        return RETURN_EXIT;
    default:
        return RETURN_UNKNOWN;
    }
}
Ejemplo n.º 2
0
/* make a random new object, returning pointer */
Object* create_object(int itemlevel)
{
    Object* newObject;
    int  r;
    int ok = false;

    while (! ok) {
        newObject = ((Object*) checkmalloc(sizeof(Object)));
        r= random_range(135);
        if (r < 20) make_thing(newObject,-1);
        else if (r < 40) make_food(newObject,-1);
        else if (r < 50) make_scroll(newObject,-1);
        else if (r < 60) make_potion(newObject,-1);
        else if (r < 70) make_weapon(newObject,-1);
        else if (r < 80) make_armor(newObject,-1);
        else if (r < 90) make_shield(newObject,-1);
        else if (r < 100) make_stick(newObject,-1);
        else if (r < 110) make_boots(newObject,-1);
        else if (r < 120) make_cloak(newObject,-1);
        else if (r < 130) make_ring(newObject,-1);
        else make_artifact(newObject,-1);
        /* not ok if object is too good for level, or if unique and already made */
        /* 1/100 chance of finding object if too good for level */
        ok = ((newObject->uniqueness < UNIQUE_MADE) &&
              ((newObject->level < itemlevel+random_range(3))
               || (random_range(100)==23)));
        if (!ok)
        {
            free_obj( newObject, true );
        }
    }
    if (newObject->uniqueness == UNIQUE_UNMADE)
        Objects[newObject->id].uniqueness=UNIQUE_MADE;
    return(newObject);
}
Ejemplo n.º 3
0
/* gain for an item */
void acquire(int blessing)
{
    char otype;
    int index,id = ABORT;
    pob newthing;

    if (blessing < 0) {
        index = random_item();
        if (index == ABORT)
            mprint("You feel fortunate.");
        else {
            print1("Smoke drifts out of your pack.... ");
            print2("Destroyed: ");
            nprint2(itemid(Player.possessions[index]));
            morewait();
            dispose_lost_objects(1,Player.possessions[index]);
        }
    }
    else {
        newthing = ((pob) checkmalloc(sizeof(objtype)));
        /* DAG this assignment looks unneccessary */
        newthing->id = -1;
        if (gamestatusp(CHEATED))
            print1("Acquire which kind of item: !?][}{)/=%%\\& ");
        else
            print1("Acquire which kind of item: !?][}{)/=%%\\ ");
        otype = mgetc();
        switch (otype) {
        case (POTION&0xff):
            if (blessing > 0)
                id = itemlist(POTIONID,NUMPOTIONS);
            else
                id = random_range(NUMPOTIONS);
            if (id < 0) print2("You feel stupid.");
            else make_potion(newthing,id);
            break;
        case (SCROLL&0xff):
            if (blessing > 0)
                id = itemlist(SCROLLID,NUMSCROLLS);
            else
                id = random_range(NUMSCROLLS);
            if (id < 0) print2("You feel stupid.");
            else make_scroll(newthing,id);
            break;
        case (RING&0xff):
            if (blessing > 0)
                id = itemlist(RINGID,NUMRINGS);
            else
                id = random_range(NUMRINGS);
            if (id < 0) print2("You feel stupid.");
            else make_ring(newthing,id);
            break;
        case (STICK&0xff):
            if (blessing > 0)
                id = itemlist(STICKID,NUMSTICKS);
            else
                id = random_range(NUMSTICKS);
            if (id < 0) print2("You feel stupid.");
            else make_stick(newthing,id);
            break;
        case (ARMOR&0xff):
            if (blessing > 0)
                id = itemlist(ARMORID,NUMARMOR);
            else
                id = random_range(NUMARMOR);
            if (id < 0) print2("You feel stupid.");
            else make_armor(newthing,id);
            break;
        case (SHIELD&0xff):
            if (blessing > 0)
                id = itemlist(SHIELDID,NUMSHIELDS);
            else
                id = random_range(NUMSHIELDS);
            if (id < 0) print2("You feel stupid.");
            else make_shield(newthing,id);
            break;
        case (WEAPON&0xff):
            if (blessing > 0)
                id = itemlist(WEAPONID,NUMWEAPONS);
            else
                id = random_range(NUMWEAPONS);
            if (id < 0) print2("You feel stupid.");
            else make_weapon(newthing,id);
            break;
        case (BOOTS&0xff):
            if (blessing > 0)
                id = itemlist(BOOTID,NUMBOOTS);
            else
                id = random_range(NUMBOOTS);
            if (id < 0) print2("You feel stupid.");
            else make_boots(newthing,id);
            break;
        case (CLOAK&0xff):
            if (blessing > 0)
                id = itemlist(CLOAKID,NUMCLOAKS);
            else
                id = random_range(NUMCLOAKS);
            if (id < 0) print2("You feel stupid.");
            else make_cloak(newthing,id);
            break;
        case (FOOD&0xff):
            if (blessing > 0)
                id = itemlist(FOODID,NUMFOODS);
            else
                id = random_range(NUMFOODS);
            if (id < 0) print2("You feel stupid.");
            else make_food(newthing,id);
            break;
        case (THING&0xff):
            if (blessing > 0)
                id = itemlist(THINGID,NUMTHINGS);
            else
                id = random_range(NUMTHINGS);
            if (id < 0) print2("You feel stupid.");
            else make_thing(newthing,id);
            break;
        case (ARTIFACT&0xff):
            if (gamestatusp(CHEATED))
                id = itemlist(ARTIFACTID,NUMARTIFACTS);
            else
                id = -1;
            if (id < 0) print2("You feel stupid.");
            else make_artifact(newthing,id);
            break;
        default:
            print2("You feel stupid.");
        }
        xredraw();
        if (id != ABORT) {
            if (blessing > 0) {
                newthing->known = 2;
                Objects[id].known = 1;
            }
            newthing->used = FALSE;
            gain_item(newthing);
        }
        else
        {
            /* DAG newthing allocated but was not freed... was YA memory leak */
            /* use free() rather than free_obj() since newthing not initialized */
            free( (char *) newthing );
        }
    }
}
Ejemplo n.º 4
0
// play()
// ... 실제로 게임을 실행시키고 이벤트를 처리한다.
//
ReturnCode SnakeGame::play()
{
    int key;
    while (FOREVER)
    {
        // 키보드 입력이 있는 경우를 처리한다.
        if (kbhit())
        {
            key = getch();
            if (key == 224)
            {
                // 방향키를 인식한 후 방향을 설정한다.
                key = getch();
                switch (key)
                {
                case UP: case DOWN: case LEFT: case RIGHT:
                    snake_.set_direction((Keyboard)key);
                    break;
                default:
                    return RETURN_FATAL;
                }
            }
            else if (key == ESC)
            {
                // 일시정지 후 메뉴를 출력한다.
                switch (pause())
                {
                case RETURN_RESUME:
                    display_.clear();
                    display_.print_wall();
                    display_.print_snake(snake_);
                    display_.print_food(food_position_);
                    break;
                case RETURN_NEWGAME:
                    return RETURN_NEWGAME;
                case RETURN_STOP:
                    return RETURN_SUCCESS;
                case RETURN_EXIT:
                    return RETURN_EXIT;
                default:
                    return RETURN_UNKNOWN;
                }
            }
        }

        Sleep(GAME_SPEED);

        snake_.move();

        // 뱀이 벽에 부딪히면 게임이 끝난다.
        if (snake_.head().X == 0 || snake_.head().X == 79 ||
            snake_.head().Y == 0 || snake_.head().Y == 24)
        {
            return RETURN_STOP;
        }

        // 뱀이 자기 몸을 물었으면 게임이 끝난다.
        else if (snake_.is_bitten() == true)
        {
            return RETURN_STOP;
        }

        // 뱀이 먹이를 먹었다면 뱀의 길이를 하나 증가시키고 점수를 올린다.
        // 또 화면상의 먹이가 사라졌음을 표시한다.
        else if (snake_.head().X == food_position_.X &&
                 snake_.head().Y == food_position_.Y)
        {
            putchar_at_xy(snake_.head().X, snake_.head().Y, 'O');
            score_++;
            food_position_.X = 0;
            food_position_.Y = 0;
        }

        // 뱀이 먹이를 먹은 것이 아니라면 일반적으로 진행한다.
        else
        {
            putchar_at_xy(snake_.head().X, snake_.head().Y, 'O');
            putchar_at_xy(snake_.tail().X, snake_.tail().Y, ' ');
            snake_.body().pop_back();
        }
        // 뱀이 먹이를 먹어 없앴다면 다시 만든다.
        if (is_no_food() == true)
        {
            make_food();
            display_.print_food(food_position_);
        }
    }

    return RETURN_SUCCESS;
}