Game_TicTacToe.c

// 简介: TicTacToe是一款休闲益智游戏。

// 中文名: 三子棋游戏。

#include <stdio.h>
#include <stdlib.h>

// 简介:  TicTacToe是一款休闲益智游戏。
// 中文名:  三子棋游戏。
const int SIZE = 3;
int CsBd[SIZE][SIZE] = {  // chessboard.
    {0, 0, 0}, 
    {0, 0, 0}, 
    {0, 0, 0}
    };
// 注: valuePsn 和 valueSys 都不能设置为0 。
const int valueSpace = 0;
const int valuePsn = 1;
const int valueSys = 2;
// 移棋的方向。
//const int directionNum = 4;
const int trendNum = 4;
const int trendT = 1;
const int trendB = 2;
const int trendL = 3;
const int trendR = 4;
void Show() {
    int i, j;
    for(i=0; i<SIZE; i++) {
        for(j=0; j<SIZE; j++) {
            if(CsBd[i][j] == valueSpace)
                printf("□");  //空格的棋
            else if(CsBd[i][j] == valuePsn)
                printf("○");  //玩家的棋
            else
                printf("●");  //系统的棋
        }
        printf("\n");
    }
}

int WinValue() {
    int i;
    for(i=0; i<SIZE; i++) {
        if(CsBd[i][0]==CsBd[i][1]&&CsBd[i][1]==CsBd[i][2])  
            return CsBd[i][0];
    }
    for(i=0; i<SIZE; i++) {
        if(CsBd[0][i]==CsBd[1][i]&&CsBd[1][i]==CsBd[2][i])  
            return CsBd[0][i];
    }
    if(CsBd[0][0]==CsBd[1][1]&&CsBd[1][1]==CsBd[2][2])
        return CsBd[0][0];
    if(CsBd[0][2]==CsBd[1][1]&&CsBd[1][1]==CsBd[2][0])
        return CsBd[0][2];
    return 0;
}

int Result() {
    int result = WinValue();
    Show();
    if(valuePsn == result) {
        printf("玩家○胜利:-D:-D 系统●失败\\(^ω^)/\n");
        exit(0);
    }
    else if(valueSys == result) {
        printf("系统●胜利:-D:-D 玩家○失败\\(^ω^)/\n");
        exit(0);
    }
    else
        printf("提示: 胜负不分 大家加油\n");
    return 0;
}

//玩家落棋。
void PsnFallPiece() {
    int x, y;
    while(1) {
        printf("请输入你的落棋位置[x(1-%d) y(1-%d)]:",  SIZE,  SIZE);
        scanf("%d %d", &x, &y);
        if(x<1 || x>SIZE || y<1 || y>SIZE) {
            printf("警告: 你选定的位置不合法。\n");
        } else {
            if(CsBd[x-1][y-1]==0) {
                printf("提示: 你选定的位置落棋成功。\n");
                break;
            } else {
                printf("提醒: 你选定的位置已有棋。\n");
            }
        }
    }
    CsBd[x-1][y-1] = valuePsn;
    Show();
}

//系统落棋。
void SysFallPiece() {
    int m, n;
    while(1) {
        m = rand()%SIZE;
        n = rand()%SIZE;
        if(CsBd[m][n] == 0) {
            break;
        }
    }
    printf("系统的落棋位置(%d %d)\n", m+1, n+1);  
    CsBd[m][n] = valueSys;
    Show();
}

int MoveTrend(int x, int y, int trend, int value) {
    switch (trend) {
      case trendT:
        if(x>0) {
            if(0==CsBd[x-1][y]) {
                CsBd[x][y] = 0;
                CsBd[x-1][y] = value;
                return 1;
            }
        }
        break;
      case trendB:
        if(x<(SIZE-1)) {
            if(0==CsBd[x+1][y]) {
                CsBd[x][y] = 0;
                CsBd[x+1][y] = value;
                return 1;
            }
        }
        break;
      case trendL:
        if(y>0) {
            if(0==CsBd[x][y-1]) {
                CsBd[x][y] = 0;
                CsBd[x][y-1] = value;
                return 1;
            }
        }
        break;
      case trendR:
        if(y<(SIZE-1)) {
            if(0==CsBd[x][y+1]) {
                CsBd[x][y] = 0;
                CsBd[x][y+1] = value;
                return 1;
            }
        }
        break;
      // default:
      //   break;
    }

    return 0;
}

//玩家移棋。
void PsnMovePiece() {
    int x, y, z;
    while(1) {
        printf("请输入你要移的原位置和方向[x(1-%d) y(1-%d) z(1-4)]:",  SIZE,  SIZE);
        scanf("%d %d %d", &x, &y, &z);
        if (x<1 || x>SIZE || y<1 || y>SIZE) {
            printf("警告: 你要移的位置不合法。\n");
        }
        else if (z<1 || y>4) {
            printf("警告: 你要移的方向不合法。\n");
        }
        else if(CsBd[x-1][y-1] == valuePsn) {
            if(1==MoveTrend(x-1, y-1, z, valuePsn)) {
                printf("提示: 你的移棋已成功。\n");
                break;
            }
            else
                printf("警告: 你要移的方向不正确。\n");
        }
        else if(CsBd[x-1][y-1] == 0) {
            printf("提醒: 你要移的位置没有棋。\n");
        }
        else if(CsBd[x-1][y-1] == valueSys) {
            printf("提醒: 你要移的棋不是你的。\n");
        }
    }
    Result();
}

//系统移棋。
void SysMovePiece() { 
    int m, n, s;
    while(1) {
        m = rand()%SIZE;
        n = rand()%SIZE;
        s = rand()%trendNum;
        // printf("m=%d, n=%d, s=%d\n", m, n, s);
        if(0<=m && m<SIZE 
          && 0<=n && n<SIZE 
          && 0<=s && s<trendNum
          && valueSys == CsBd[m][n]) {
            if (1==MoveTrend(m, n, s+1, valueSys)) {
                break;
            }
        }
    }
    printf("系统将位置(%d %d)的棋", (m+1), (n+1));
    switch (s + 1) {
      case trendT:
        printf("向上移\n");
        break;
      case trendB:
        printf("向下移\n");
        break;
      case trendL:
        printf("向左移\n");
        break;
      case trendR:
        printf("向右移\n");
        break;
      default:
        printf("\n提示: 系统异常导致退出游戏\n");
        exit(-1);
    }
    Result();
}

int main(void)  
{
    int FallCPCnt;
    printf("三子棋游戏开始:\n");
    Show();
    
    for(FallCPCnt=0; FallCPCnt<SIZE; ++FallCPCnt) {
        PsnFallPiece();
        SysFallPiece();
    }
    printf("<< 落棋环节已结束\n");
    Result();

    printf("进入移子的环节 [ 移棋提示:1--上 2--下 3--左 4--右 ] >>\n");

    while(1) {
        PsnMovePiece();
        SysMovePiece();
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值