- # include <stdio.h>
- # include <windows.h>
- # include <conio.h>
- # include <stdlib.h>
- # define Map 20
- short x,y,aorb;
- char ch;
- short target[Map][Map];
- void gotoxy(short x, short y){
- COORD pos = {x,y};
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
- }
- void draw(){
- short i,j;
- for(i=0;i<Map;i++){
- for(j=0;j<Map;j++){
- gotoxy(i*2,j),target[i][j] = 0;
- if(j==0&&i==0) puts("©³");
- else if(j==Map-1&&i==Map-1) puts("©¿");
- else if(j==Map-1&&i==0) puts("©»");
- else if(j==0&&i==Map-1) puts("©·");
- else if(i==0) puts("©Ç");
- else if(i==Map-1) puts("©Ï");
- else if(j==0) puts("©×");
- else if(j==Map-1) puts("©ß");
- else puts("©ï");
- }
- }
- aorb = 0,gotoxy(0,Map),puts("ºÚ·½¡ð");
- }
- void win(short aorb){
- short i,j,f = 0,c;
- for(i=x,j=y-4,c=1;c<=5;j++,c++){
- if(target[i][j]==1&&target[i][j+1]==1&&target[i][j+2]==1&&target[i][j+3]==1&&target[i][j+4]==1
- ||target[i][j]==2&&target[i][j+1]==2&&target[i][j+2]==2&&target[i][j+3]==2&&target[i][j+4]==2)
- f = 1;
- }
- for(i=x-4,j=y,c=1;c<=5;i++,c++){
- if(target[i][j]==1&&target[i+1][j]==1&&target[i+2][j]==1&&target[i+3][j]==1&&target[i+4][j]==1
- ||target[i][j]==2&&target[i+1][j]==2&&target[i+2][j]==2&&target[i+3][j]==2&&target[i+4][j]==2)
- f = 1;
- }
- for(i=x-4,j=y-4,c=1;c<=5;i++,j++,c++){
- if(target[i][j]==1&&target[i+1][j+1]==1&&target[i+2][j+2]==1&&target[i+3][j+3]==1&&target[i+4][j+4]==1
- ||target[i][j]==2&&target[i+1][j+1]==2&&target[i+2][j+2]==2&&target[i+3][j+3]==2&&target[i+4][j+4]==2)
- f = 1;
- }
- for(i=x+4,j=y-4,c=1;c<=5;i--,j++,c++){
- if(target[i][j]==1&&target[i-1][j+1]==1&&target[i-2][j+2]==1&&target[i-3][j+3]==1&&target[i-4][j+4]==1
- ||target[i][j]==2&&target[i-1][j+1]==2&&target[i-2][j+2]==2&&target[i-3][j+3]==2&&target[i-4][j+4]==2)
- f = 1;
- }
- gotoxy(0,Map+1);
- if(aorb==1&&f) puts("ºÚ·½Ó®"),gotoxy(6,Map+1),getch(),exit(1);
- else if(aorb==0&&f) puts("°×·½Ó®"),gotoxy(6,Map+1),getch(),exit(2);
- }
- void move(char ch){
- if(ch=='w' && y-1>=0)y--;
- else if(ch=='s' && y+1<=Map-1)y++;
- else if(ch=='a' && x-1>=0)x--;
- else if(ch=='d' && x+1<=Map-1)x++;
- else if(ch==' '){
- if(target[x][y]!=0) return;
- if(aorb == 0){
- gotoxy(0,Map),puts("°×"),gotoxy(4,Map),puts("¡ñ");
- aorb = 1,target[x][y] = 1,gotoxy(x*2,y),puts("¡ð");
- }
- else{
- gotoxy(0,Map),puts("ºÚ"),gotoxy(4,Map),puts("¡ð");
- aorb = 0,target[x][y] = 2,gotoxy(x*2,y),puts("¡ñ");
- }
- win(aorb);
- }
- gotoxy(x*2,y);
- }
- int main(){
- short target[Map][Map];
- draw(),gotoxy(Map,x = y = Map/2);
- while(1)
- if(kbhit())
- ch = getch(),move(ch);
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201513812.html
来源: http://www.codesnippet.cn/detail/1510201513812.html