게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
간단한 게임 c 로 만들어보고있는데 게속 꼬이네요..
게시물ID : programmer_20221짧은주소 복사하기
작성자 : 뒷북을올려라
추천 : 0
조회수 : 620회
댓글수 : 2개
등록시간 : 2017/04/07 08:47:17
옵션
  • 본인삭제금지
#include <stdio.h>
#include <cs50.h>
#include <string.h>

void draw(int get[]);
bool indexOfRight(int index, int array[]);
bool indexOfLeft(int index, int array[]);
bool indexOfMid(int index, int array[]);
void move(int number, int index[]);
bool check(int array[]);



일단 제가 그 그림맞추기게임같은거있잖아요.. 예를들어 판이있으면

1 3 2
4 7 6
5 8  

이렇게있으면 빈칸으로 숫자를 한번에 하나씩 움직여서 

1 2 3
4 5 6
7 8

이렇게 만들면 이기는 게임을 만들어보려고 계속 하고있는데요... 

대충 알고리등ㅁ을 어떻게 짰냐면요 

index[]라는 array를 만들어서 
value를 입력하면 몇번째 index에 있는지 알려주게 지정했어요. 

예를 들면 
1 2 3
4 5 6
7 8 이렇게 있으면 

index[1] == 0; 
index[7] == 6;
이렇게요. 

그다음에
매 턴마다 
판을 프린트해주고 그다음에 뭔 숫자를 옮기고 싶냐고 물어봤어요 . 
그리고 그 숫자가 빈칸옆에 있는지 체크하고 
빈칸옆에있다면 빈칸의 index포인트랑 숫자의 index포인트를 바꿔치기했어요. move function에서요. 

근데 이게 되게 이상한게 

예를들어 (빈칸을 0이라 할께요)
1 2 3
4 5 6
7 8 0 이렇게 있다면 

6을 입력하면 

1 2 3 
4 5 0
7 8 6 이렇게 바로 바뀝니다

근데 여기서 5를 입력하면   

1 2 3 
4 5 0
7 8 6 이렇게 그대로 있어요. 

1 2 3
4 5 6
7 8 0 여기서 8을 입력해도

1 2 3
4 5 6
7 8 0 이렇게 그대로 있고요. 


consistent 하게 이상하다면 첨부터 다 뜯어고쳐보겠는데 

세로로는 잘 바뀌는데 가로로는 안바뀌니 미치겠네요.. 

몇시간째 혼자 끙끙대다가 올려봐요.. 

스크린샷 2017-04-06 오후 7.43.33.png


중간중간에 printf넣어서 체크해 봤는데요

이렇게 1을 움직이려할때는 인덱스가 move funciton에선 바뀌었다가 다시 돌아와요 draw function이 실행될때는.
근데 3을 움직일때에는 인덱스가 move function에서 바뀌고 draw function에서도 그대로있어요.. 
어떻게된거죠 ㅠㅠ 


int main(void){
    int index[9] = {8, 7, 6, 5, 4, 3, 2, 1, 0};
    
    while(!check(index)){
        draw(index);
        printf("What do you wanna move?");
        int number = get_int();
        move(number, index);
    }
}

// 판을 시작할때마다 프린트해주는 function
void draw(int index[]){
    
    int gridPlane[9];
    for(int n=0; n<9; n++){
        gridPlane[index[n]] = n;
    }
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            printf("%i ",gridPlane[j+(i*3)]);
        }
        printf("\n");
    }
}


// 판이 있으면 왼쪽 3개가 빈 공간 근처인지 체크하는 펑션
bool indexOfLeft(int index, int array[]){
    if(array[index]-1 == array[0] || array[index]+3==array[0] || array[index]-3 ==array[0]){
        return true;
    }
    else
        return false;
}

// 올느쪽 3개가 빈 공간 근처인지 체크하는 펑션
bool indexOfRight(int index, int array[]){
    if(array[index]-1 == array[0] || array[index]+3==array[0] || array[index]-3 == array[0]){
        return true;
    }
    else
        return false;
}

// 중간 3개가 빈공간 근처인지 체크하는 펑션
bool indexOfMid(int index, int array[]){
    if(array[index]-1 == array[0] || array[index]+3==array[0] || array[index]-3 == array[0] || array[index]+1 == array[0])
        return true;
    else
        return false;
}

// 빈 공간이랑 숫자랑 자리 바꿔치기한느 펑션

void move(int number, int index[]){
    if(index[number] == 0 || index[number] == 3 || index[number] == 6)
    {
        if(indexOfLeft(number, index)){
            int temp = index[number];
            index[number] = index[0];
            index[0] = temp;
        }
        else
            printf("you can't do that \n");
    }
    
    if(index[number] == 1 || index[number] == 4 || index[number] == 7)
    {
        if(indexOfMid(number,index)){
            int temp = index[number];
            index[number] = index[0];
            index[0] = temp;
            
            printf("%i index[0] in move \n", index[0]);
            printf("%i index[1] in move \n", index[1]);
        }
        else
            printf("you can't do that\n");
    }
    
    if(index[number] == 2 || index[number] == 5 || index[number] == 8)
    {
        if(indexOfMid(number,index)){
            int temp = index[number];
            index[number] = index[0];
            index[0] = temp;
        }
        else
            printf("you can't do that\n");
    }
}


// order가 다 맞는지 체크하는 펑션

bool check(int array[]){
    for(int i=0; i<8; i++){
        if(array[i] != i+1){
            return false;
        }
    }
    return true;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호