Console - 바둑소스
Computer_IT/C++2006. 2. 22. 13:53
반응형
Console로 프로그래밍된 바둑 소스이다.
출처는 불분명... 어디더라??
출처는 불분명... 어디더라??
- #include <stdio.h>
- #include <windows.h>
- #include <stdlib.h>
- #include <conio.h>
- #define LEFT '4'
- #define RIGHT '6'
- #define UP '8'
- #define DOWN '2'
- #define ENTER '5'
- #define CLEAR 0
- #define NONE 0
- #define MAPSIZE 19
- #define PLAYER1 1
- #define PLAYER2 2
- #define PLAY 1
- #define START 1
- #define END 0
- #define NO 0
- #define OK 1
- #define YES 1
- #define OKTOTALCOUNT 4
- #define FLAGTRUE 1
- #define FLAGFALSE 0
- void gotoxy(int x, int y);
- int xy[MAPSIZE][MAPSIZE];
- int map[MAPSIZE][MAPSIZE];
- int x, y, x2 = 2;
- int centerX=MAPSIZE/2, centerY=MAPSIZE/2;
- int displayX=0, displayLineY=21, displayY=22;
- char mapChar[]="┌┬┐├┼┤└┴┘";
- void clearValue() {
- int x,y;
- for(y=0;y<MAPSIZE;y++) {
- for(x=0;x<MAPSIZE;x++) {
- map[y][x]=CLEAR;
- }
- }
- }
- int IsKill(int cnt) {
- return cnt==OKTOTALCOUNT;
- }
- int TestFlag(int x,int y) {
- if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return FLAGTRUE;
- if(map[y][x]==OK) return FLAGTRUE;
- return FLAGFALSE;
- }
- int SefeDeleteTest(int x,int y,int OtherPlayer) {
- int result;
- int cnt;
- if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return OK;
- if(xy[y][x]==OtherPlayer) return OK;
- if(xy[y][x]==NONE) return NO;
- cnt=0;
- result=NO;
- map[y][x]=OK;
- if(TestFlag(x,y-1)==FLAGFALSE) {
- if(SefeDeleteTest(x,y-1,OtherPlayer)) {
- cnt=cnt + 1;
- }
- } else {
- cnt=cnt+1;
- }
- if(TestFlag(x-1,y)==FLAGFALSE) {
- if(SefeDeleteTest(x-1,y,OtherPlayer)) {
- cnt=cnt + 1;
- }
- } else {
- cnt=cnt+1;
- }
- if(TestFlag(x+1,y)==FLAGFALSE) {
- if(SefeDeleteTest(x+1,y,OtherPlayer)) {
- cnt=cnt + 1;
- }
- } else {
- cnt=cnt+1;
- }
- if(TestFlag(x,y+1)==FLAGFALSE) {
- if(SefeDeleteTest(x,y+1,OtherPlayer)) {
- cnt=cnt + 1;
- }
- } else {
- cnt=cnt+1;
- }
- if(IsKill(cnt)){
- result=OK;
- }
- return result;
- }
- void DolDelete(int x,int y,int OtherPlayer) {
- if(x<0 x>=MAPSIZE y<0 y>=MAPSIZE) return;
- if(xy[y][x]==OtherPlayer) return;
- if(xy[y][x]==NONE) return;
- xy[y][x]=CLEAR;
- if(x+1<MAPSIZE) DolDelete(x+1,y,OtherPlayer);
- if(x-1>=0) DolDelete(x-1,y,OtherPlayer);
- if(y+1<MAPSIZE) DolDelete(x,y+1,OtherPlayer);
- if(y-1>=0) DolDelete(x,y-1,OtherPlayer);
- }
- void RalDeleteTest(int x,int y,int OtherPlayer) {
- clearValue();
- if(SefeDeleteTest(x+1,y,OtherPlayer)==YES)
- DolDelete(x+1,y,OtherPlayer);
- clearValue();
- if(SefeDeleteTest(x-1,y,OtherPlayer)==YES)
- DolDelete(x-1,y,OtherPlayer);
- clearValue();
- if(SefeDeleteTest(x,y+1,OtherPlayer)==YES)
- DolDelete(x,y+1,OtherPlayer);
- clearValue();
- if(SefeDeleteTest(x,y-1,OtherPlayer)==YES)
- DolDelete(x,y-1,OtherPlayer);
- }
- void input(int Player) {
- char c;
- int LOOP=START;
- int OtherPlayer;
- if(Player == PLAYER1) {
- OtherPlayer = PLAYER2;
- } else {
- OtherPlayer = PLAYER1;
- }
- while(LOOP!=END){
- c = getch();
- switch(c)
- {
- case RIGHT:
- x=x+1;
- if(x >= MAPSIZE) x=x-1;
- gotoxy(x,y);
- break;
- case LEFT:
- x=x-1;
- if(x < 0 ) x=x+1;
- gotoxy(x,y);
- break;
- case UP:
- y=y-1;
- if(y < 0 ) y=y+1;
- gotoxy(x,y);
- break;
- case DOWN:
- y=y+1;
- if(y >= MAPSIZE ) y=y-1;
- gotoxy(x,y);
- break;
- case ENTER:
- if(xy[y][x] != NONE ) break;
- xy[y][x]=Player;
- RalDeleteTest(x,y,Player);
- clearValue();
- if(SefeDeleteTest(x,y,OtherPlayer)==YES){
- xy[y][x]=CLEAR;
- } else {
- LOOP = END;
- }
- }
- }
- }
- void printScreen(){
- system("cls");
- int x,y,n;
- char c[3];
- for(y=0;y<MAPSIZE;y++) {
- for(x=0;x<MAPSIZE;x++) {
- if(xy[y][x] == PLAYER1) {
- } else if(xy[y][x] == PLAYER2 ) {
- } else if(xy[y][x] == NONE ) {
- n=(x+16)/17+(y+16)/17*3;
- c[0]=mapChar[n*2];
- c[1]=mapChar[n*2+1];
- c[2]=0;
- if((x-3)%6==0 && (y-3)%6==0) {
- strcpy(c,"×");
- }
- }
- }
- }
- gotoxy(displayX,displayLineY);
- }
- int gamePlayCheck(int Player)
- {
- //승패를 가른다.
- return 1;
- }
- void InitMapData() {
- int x,y;
- for(y=0;y<MAPSIZE;y++) {
- for(x=0;x<MAPSIZE;x++) {
- xy[y][x]=CLEAR;
- }
- }
- }
- // 커서를 나타냅니다.
- void SetCursorShow(void)
- {
- HANDLE hConsole;
- hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO ci;
- GetConsoleCursorInfo( hConsole, &ci );
- ci.dwSize=100;
- ci.bVisible = TRUE;
- SetConsoleCursorInfo( hConsole , &ci );
- }
- void main() {
- int Player=PLAYER1,GamePlayFlag=PLAY;
- InitMapData();
- SetCursorShow();
- clearValue();
- printScreen();
- x=centerX;
- y=centerY;
- while(GamePlayFlag!=END){
- gotoxy(displayX,displayY);
- gotoxy(x,y);
- input(Player);
- printScreen();
- if(Player==PLAYER1) {
- Player=PLAYER2;
- } else {
- Player=PLAYER1;
- }
- GamePlayFlag = gamePlayCheck(Player);
- }
- gotoxy(displayX,displayY);
- }
- void gotoxy(int x , int y)
- {
- COORD Pos = { x*2 , y};
- SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE), Pos);
- }
반응형
'Computer_IT > C++' 카테고리의 다른 글
LinkedList 자료 (0) | 2006.04.06 |
---|---|
VC++ 단축키 모음 (2) | 2006.03.19 |
Console Codepage 설정. (0) | 2006.02.15 |
[펌] Visual C++ Console - 사격게임 (0) | 2006.02.15 |
Visual C++ Console 에서 TurboC clrscr 구현 (0) | 2006.02.15 |
[펌] Visual C++ Console - 사격게임
Computer_IT/C++2006. 2. 15. 00:13
반응형
출처... 잊어먹었음.
- #include < stdio.h >
- #include < time.h >
- #include < conio.h >
- #include < stdlib.h >
- #include < windows.h >
- /*
- * < 사격게임 >
- *
- * 호환성
- * 이 프로그램은 Borland C++ 5.5 에서 컴파일하고 시험되었습니다.
- *
- * 게임설명
- * 사격장은 각각 하나씩의 화살을 포함하고 있는 아홉개의 레인으로 만들어져 있습니다.
- * 프로그램이 시작되면 레인의 오른쪽에 한개의 타겟(X)이 나타납니다.
- * 당신의 과제는 화살들 중의 하나로 타겟을 맞추는 것 입니다.
- * 화살을 쏘려면 레인 번호를 누르면 됩니다.
- * 25개의 타겟이 나타난 후에, 게임은 끝나고, 최종점수가 출력됩니다.
- *
- * 난이도 변경하기
- * 난이도를 변경하려면 ShootArrow(int)함수의 두번째 for루프를 변경해서 화살의 속도를 변경하면 됩니다.
- * 화살이 천천히 나갈수록 맞추기까지의 시간을 소비하므로 난이도는 높아집니다.
- * PlayGame(void)함수의 TIME_LIMIT값을 2초로 맞추어 놓았기 때문에
- * 화살이 발사되는데 2초가 걸린다면 게임이 안되죠. 역시 제한시간도 마음대로 수정하세요.
- */
- #define true 1
- #define false 0
- typedef int bool;
- void ShowScore(int);
- int PlayGame(void);
- void EraseTarget(int);
- void ShootArrow(int);
- int ShowTarget(void);
- void DrawScreen(void);
- void gotoxy(int x, int y);
- void clrscr(void);
- int main(void) {
- int score;
- srand((unsigned) time(NULL));
- DrawScreen();
- score = PlayGame();
- ShowScore(score);
- return 0;
- }
- void DrawScreen(void) {
- int x;
- clrscr();
- gotoxy(20, 11);
- getch();
- clrscr();
- for (x = 1; x <= 10; x++) { // 루프는 한번에 한 개의 헤인벽과 한개의 화살을 그린다.
- gotoxy(1, x * 2 + 2);
- if (x < 10) {
- gotoxy(1, x * 2 + 3);
- }
- }
- }
- void EraseTarget(int target_position) {
- gotoxy(60, target_position * 2 + 3);
- }
- int PlayGame(void) {
- int score = 0;
- int target_position;
- long start_time;
- bool shot_fired;
- int num; // 값으로 저장하는 숫자키
- const int TIME_LIMIT = 2; // 한 타겟당 제한시간 2초
- int x;
- for (x = 1; x <= 20; x++) { // 이 루프는 25개의 타겟을 부여한다.
- target_position = ShowTarget();
- start_time = time(NULL); // 여기에서 시작시간이 저장된다.
- shot_fired = false;
- // 제한시간과 남은 화살개수를 알려줌
- gotoxy(44, 2);
- gotoxy(10, 2);
- do { // 선수가 사격을 할 때까지 키 입력을 기다린다.
- num = getch() - '0';
- if (num >= 1 && num <= 9) {
- ShootArrow(num);
- shot_fired = true;
- }
- } while (!shot_fired);
- // 시간 안에(2초) 타겟을 맞추었을 때 실행된다.
- if ((time(NULL) < start_time + TIME_LIMIT) && num == target_position) {
- putchar('\a');
- ++score;
- }
- EraseTarget(target_position);
- }
- return score;
- }
- void ShootArrow(int a) { // 파라미터 a는 발사한 화살의 번호
- int x;
- long delay;
- for (x = 4; x <= 60; x++) {
- gotoxy(x, a * 2 + 3); // 루프의 매 회마다 화살을 1문자씩 오른쪽으로 움직인다.
- for (delay = 0; delay < 3999999; delay++) // 이 코드로 화살의 속도조절을 한다. 시스템의 성능에 따라 다르다.
- continue;
- gotoxy(x, a * 2 + 3);
- }
- gotoxy(4, a * 2 + 3);
- }
- void ShowScore(int score) {
- gotoxy(60, 20);
- gotoxy(60, 21);
- gotoxy(60, 22);
- }
- int ShowTarget(void) {
- int p = rand() % 9 + 1; // 이 난수는 타겟이 나타날 레인번호이다. 1 ~ 9
- gotoxy(60, p * 2 + 3);
- return p;
- }
- void gotoxy(int x, int y)
- {
- COORD pos={x,y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
- }
- void clrscr(void)
- {
- COORD Cur= {0,0 };
- unsigned long dwLen;
- FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE) , ' ', 80*25, Cur, &dwLen);
- }
반응형
'Computer_IT > C++' 카테고리의 다른 글
Console - 바둑소스 (0) | 2006.02.22 |
---|---|
Console Codepage 설정. (0) | 2006.02.15 |
Visual C++ Console 에서 TurboC clrscr 구현 (0) | 2006.02.15 |
Visual C++ Console에서 TurboC gotoxy구현 (0) | 2006.02.15 |
Compiler ... (0) | 2006.02.14 |