快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C程序三子棋

#ifndef_THREE_CHESS_H_
#define_THREE_CHESS_H_
#include 
#include 
#include 
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
void ShowUI();//显示
void Game();//游戏操作
void ComputerMove(char board[][COL], int row, int col);//电脑出琪
void ShowBoard(char board[][COL], int row, int col);//显示棋盘
char Judge(char board[][COL], int row, int col);//判定
void PlayerMove(char board[][COL], int row, int col);//玩家出琪
#endif
#define  _CRT_SECURE_NO_WARNINGS 1
#include"three_chess.h"

int main()
{
    int select = 0;
    int quit = 0;
    while (!quit){
        ShowUI();
        scanf("%d", &select);
        switch (select){
        case 1:
            Game();
            break;
        case 2:
            quit = 1;
            printf("Bye,Bye!\n");
            break;
        default:
            printf("Please Enter Again!\n");
            break;
        }
    }

    system("pause");
    return 0;``
#define  _CRT_SECURE_NO_WARNINGS 1
#include "three_chess.h"

void ShowUI()
{
    printf("##################################\n");
    printf("## 1. Play              2. Exit ##\n");
    printf("##################################\n");
    printf("Please Select:> ");
}
void ComputerMove(char board[][COL], int row, int col)
{
    while (1){
        int x = rand() % row;//0 1 2
        int y = rand() % col;
        if (board[x][y] == ' '){
            board[x][y] = 'O';//如果有空格,电脑可以放棋子
            break;
        }
    }
}
void PlayerMove(char board[][COL], int row, int col)
{``
int x, y;
while (1){
    printf("Please Enter Your Pos(x,y):>  ");
    scanf("%d %d", &x, &y);
    if (x >= 1 && x <= row && y >= 1 && y <= col){//输入1 2 3 
        if (board[x - 1][y - 1] == ' '){//对应电脑上 0 1 2,空格处玩家放棋子
            board[x - 1][y - 1] = 'X';
            break;
        }
        else{
            printf("Enter Pos Is Not OK, Try Again!\n");//选的地方已被占据
        }
    }
    else{
        printf("Enter Error, Try Again!\n");
    }
}

}
char Judge(char board[][COL], int row, int col)
{
int i = 0;
int j = 0;
for (; i < row; i++){
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && \
board[i][0] != ' '){``
return board[i][0];
}
}
for (i = 0; i < col; i++){
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && \
board[0][i] != ' '){
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && \
board[1][1] != ' '){
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && \
board[1][1] != ' '){
return board[1][1];
}

我们提供的服务有:成都网站制作、成都做网站、微信公众号开发、网站优化、网站认证、龙港ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的龙港网站制作公司


    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            if (board[i][j] == ' '){//仍有空格。继续
                return 'N';
            }
        }
    }
    return 'E';//满格
}
void ShowBoard(char board[][COL], int row, int col)
{
    printf("    1   2   3\n");
    printf("-----------------\n");
    int i = 0;
    int j = 0;
    for (; i < row; i++){
        printf("%d |", i + 1);
        for (j = 0; j < col; j++){
            printf(" %c |", board[i][j]);
        }
    printf("\n-----------------\n");

}

    printf("\n");
}
void Game()
{
    char board[ROW][COL];
    memset(board, ' ', sizeof(board)); 
    char result = 'N';
    srand((unsigned long)time(NULL));

    while (1){
        system("cls");//清屏
        ComputerMove(board, ROW, COL);
        ShowBoard(board, ROW, COL);
        result = Judge(board, ROW, COL);
        if (result != 'N'){//'X' 'O' 'E' 'N'
            break;
        }
        PlayerMove(board, ROW, COL);
        ShowBoard(board, ROW, COL);
        result = Judge(board, ROW, COL);
        if (result != 'N'){//'X' 'O' 'E' 'N'
            break;
        }
    }
    switch (result){
    case 'X':
        printf("You Win! :)\n");
        break;
    case 'O':
        printf("You Lose, Computer Win! :(\n");
        break;
    case 'E':
        printf("平局,恭喜!\n");
        break;
    default:
        break;
    }
}

新闻名称:C程序三子棋
标题链接:http://6mz.cn/article/jjdjoi.html

其他资讯