zoukankan      html  css  js  c++  java
  • C语言实现五子棋简单功能

    /********************************************************************
            C-4.29-1:
    	实现五子棋游戏
    	操作说明:用方向键或者"w","s","a","d"控制棋子放置位置,
    			使用空格键放置棋子,使用“ESC”键退出游戏
         测试环境:Redhat5.5 ********************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #define ROW 30 #define COL 36 #define MOVECOLOR 36 #define MOVECHAR '*' //初始化五子棋表格 void initbox(char p[][COL]) { int i = 0, j = 0; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { p[i][j] = '+'; } } } //打印显示五子棋表格 void show(char p[][COL]) { int i = 0, j = 0; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { printf("%c ", p[i][j]); } putchar(10); } } //将当前光标定位于指定的(x, y)处 void gotoxy(int x, int y) { printf("33[%d;%dH", x, y); } //显示光标所在位置的坐标值(x, y) void showxy(int x, int y) { int i = 0; printf("33[s"); gotoxy(ROW+1, COL-6); for (i = 0; i < 14; i++) { printf(" "); } //printf("33[K33[10;10m"); printf("33[14D"); printf("x = %d y = %d ", x, y); printf("33[u"); } //移动光标到要下棋点 void move(char rect[][COL], char *buf, int *x, int *y, int player) { char ch = 0; if (0 == player % 2) { ch = '@'; } else { ch = '#'; } //方向键控制区按键识别,并处理 if (buf[0] == 27 && buf[1] == 91) { switch (buf[2]) { case 65: if (*x > 1 && *x <= ROW) //上移处理 { if (rect[*x-1][*y-1] == '+') //下棋点位置在移动时要判断该位置是否为空,为空则将空的颜色恢复黑色 printf("33[10;10m+33[D33[0m"); printf("33[A"); //上移 if (rect[*x-2][*y-1] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); //将下棋点位置标明为特殊字符,以便于识别 *x -= 1; } break; case 66: if (*x >= 1 && *x < ROW) //下移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[B"); if (rect[*x][*y-1] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *x += 1; } break; case 67: if (*y >= 1 && *y < COL) //右移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[2C"); if (rect[*x-1][*y] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *y += 1; } break; case 68: if (*y > 1 && *y <= COL) //左移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[2D"); if (rect[*x-1][*y-2] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *y -= 1; } break; default: break; } } //左侧控制键"w","s","a","d"识别,并处理 if (0 == buf[1]) { switch (buf[0]) { case 119: if (*x > 1 && *x <= ROW) //上移处理 { if (rect[*x-1][*y-1] == '+') //下棋点位置在移动时要判断该位置是否为空,为空则将空的颜色恢复 printf("33[10;10m+33[D33[0m"); printf("33[A"); //上移 if (rect[*x-2][*y-1] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); //将下棋点位置标明为特殊字符,以便于识别 *x -= 1; } break; case 115: if (*x >= 1 && *x < ROW) //下移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[B"); if (rect[*x][*y-1] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *x += 1; } break; case 100: if (*y >= 1 && *y < COL) //右移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[2C"); if (rect[*x-1][*y] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *y += 1; } break; case 97: if (*y > 1 && *y <= COL) //左移处理 { if (rect[*x-1][*y-1] == '+') printf("33[10;10m+33[D33[0m"); printf("33[2D"); if (rect[*x-1][*y-2] == '+') printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); *y -= 1; } break; default: break; } } showxy(*x, *y); } void playerchk(int *player) { if (0 == *player % 2) { printf("33[s"); gotoxy(ROW+1, 0); printf("33[K"); gotoxy(ROW+1, 2); printf("33[10;10mPlayerA33[K33[0m"); printf("33[u"); } else if (1 == *player % 2) { printf("33[s"); gotoxy(ROW+1, 0); printf("33[K"); gotoxy(ROW+1, 32); printf("33[10;10mPlayerB33[K33[0m"); printf("33[u"); } } //检查是否有五个连续并且一样的字符,有则游戏结束 int resultchk(char rect[][COL]) { int i = 0, j = 0, k = 0; int count = 0; char cha = '@'; char chb = '#'; char ch = 0; //检查各行是否有五个连续字符 for (i = 0; i < ROW; i++) { for (j = 0; j < COL-4; j++) { if ((rect[i][j] != '+') && (rect[i][j] == rect[i][j+1]) && (rect[i][j] == rect[i][j+2]) && (rect[i][j] == rect[i][j+3]) && (rect[i][j] == rect[i][j+4])) { if (cha == rect[i][j]) { ch = cha; gotoxy (ROW+2, COL-11); printf("Game over! PlayerA win! "); } else if (chb == rect[i][j]) { ch = chb; gotoxy (ROW+2, COL-11); printf("Game over! PlayerB win! "); } for (k = 0; k < 5; k++) { gotoxy(i+1, (j+k+1) * 2-1); printf("33[10;43m%c33[0m", ch); } return 1; } } } //检查各列是否有五个连续字符 for (j = 0; j < ROW; j++) { for (i = 0; i < COL-4; i++) { if ((rect[i][j] != '+') && (rect[i][j] == rect[i+1][j]) && (rect[i][j] == rect[i+2][j]) && (rect[i][j] == rect[i+3][j]) && (rect[i][j] == rect[i+4][j])) { if (cha == rect[i][j]) { ch = cha; gotoxy (ROW+2, COL-11); printf("Game over! PlayerA win! "); } else if (chb == rect[i][j]) { ch = chb; gotoxy (ROW+2, COL-11); printf("Game over! PlayerB win! "); } for (k = 0; k < 5; k++) { gotoxy(i+k+1, (j+1) * 2-1); printf("33[10;43m%c33[0m", ch); } return 1; } } } //检查左上-右下斜线上是否有五个连续字符相同 for (i = 0; i < ROW-4; i++) { for (j = 0; j < COL-4; j++) { if ((rect[i][j] != '+') && (rect[i][j] == rect[i+1][j+1]) && (rect[i][j] == rect[i+2][j+2]) && (rect[i][j] == rect[i+3][j+3]) && (rect[i][j] == rect[i+4][j+4])) { if (cha == rect[i][j]) { ch = cha; gotoxy (ROW+2, COL-11); printf("Game over! PlayerA win! "); } else if ('#' == rect[i][j]) { ch = chb; gotoxy (ROW+2, COL-11); printf("Game over! PlayerB win! "); } for (k = 0; k < 5; k++) { gotoxy(i+k+1, (j+k+1) * 2-1); printf("33[10;43m%c33[0m", ch); } return 1; } } } //检查左下-右上斜线上是否有五个连续字符相同 for (i = 0; i < ROW-4; i++) { for (j = 4; j < COL; j++) { if ((rect[i][j] != '+') && (rect[i][j] == rect[i+1][j-1]) && (rect[i][j] == rect[i+2][j-2]) && (rect[i][j] == rect[i+3][j-3]) && (rect[i][j] == rect[i+4][j-4])) { if (cha == rect[i][j]) { ch = cha; gotoxy (ROW+2, COL-11); printf("Game over! PlayerA win! "); } else if (ch == rect[i][j]) { ch = chb; gotoxy (ROW+2, COL-11); printf("Game over! PlayerB win! "); } for (k = 0; k < 5; k++) { gotoxy(i+k+1, (j-k+1) * 2-1); printf("33[10;43m%c33[0m", ch); } return 1; } } } } int main(void) { int i = 0, j = 0; char rect[ROW][COL] = { 0 }; int ret = 0; int x = ROW / 2, y = COL / 2; int player = 0; system("clear"); system("stty -echo"); system("stty -icanon"); initbox(rect); show(rect); printf("33[s"); gotoxy(ROW+1, 2); printf("33[10;10mPlayerA(@)33[K33[0m"); printf("33[u"); gotoxy(x, y * 2 - 1); showxy(x, y); printf("33[?25l"); if (rect[x-1][y-1] == '+') { printf("33[%d;10m%c33[D33[0m", MOVECOLOR, MOVECHAR); } fflush(NULL); while (1) { int i = 0; char buf[10] = { 0 }; ret = read(0, buf, 10); move(rect, buf, &x, &y, player); //if (buf[0] == 119) //for (i = 0; i < ret; i++) //{ // printf("buf[%d] = %d ", i, buf[i]); //} if (buf[0] == 32 && buf[1] == 0) //按下空格键,在当前位置放下棋子 { if ((0 == player % 2) && (rect[x-1][y-1] == '+')) //玩家一回合内,按下空格,放置'@' { rect[x-1][y-1] = '@'; printf("33[31;10m@33[D33[0m"); printf("33[s"); gotoxy(ROW+1, 2); printf("33[K"); printf("33[u"); showxy(x, y); printf("33[s"); gotoxy(ROW+1, COL * 2 - 10); printf("33[K"); printf("33[10;10mPlayerB(#)33[K33[0m"); printf("33[u"); player++; } else if ((1 == player % 2) && (rect[x-1][y-1] == '+')) //玩家二回合内,按下空格,放置'#' { rect[x-1][y-1] = '#'; printf("33[32;10m#33[D33[0m"); printf("33[s"); gotoxy(ROW+1, 2); printf("33[K"); printf("33[10;10mPlayerA(@)33[K33[0m"); printf("33[u"); showxy(x, y); player++; } } if (buf[0] == 27 && buf[1] == 0) //按下ESC键退出游戏 { if ('+' == rect[x-1][y-1]) printf("33[10;10m+"); gotoxy(ROW+2, 0); //show(rect); break; } if (resultchk(rect) == 1) { break; } fflush(NULL); } gotoxy(ROW+3, 0); printf("33[?25h"); system("stty echo"); system("stty icanon"); return 0; }

      

  • 相关阅读:
    如何在Ubuntu上安装Wine 2.6
    51nod 1012 最小公倍数LCM
    二次urldecode注入
    CTF中的变量覆盖问题
    redis的bind误区
    宽字节注入原理
    PHP靶场-bWAPP环境搭建
    xxe-lab学习
    PHP代码审计之create_function()函数
    SSRF打认证的redis
  • 原文地址:https://www.cnblogs.com/microxiami/p/5516303.html
Copyright © 2011-2022 走看看