亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言實現三子棋

發布時間:2020-07-03 22:13:16 來源:網絡 閱讀:256 作者:ZJL2019 欄目:編程語言

在學習了,一段時間的C語言后,這次程序可能更讓我看到了一個學習的結果
//three_chess

//three_chess.h//創立頭文件,用來存儲函數聲聲明
#ifndef _THREE_CHESS_H_
#define _THREE_CHESS_H_

#include <stdio.h>
#include <windows.h>
#include <time.h>
#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

//three_chess.c//用來實現函數功能的源代碼
#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;
        int y = rand() % col;
        if (board[x][y] == ' '){
            board[x][y] = '0';
            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){
            if (board[x - 1][y - 1] == ' '){
                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];
    }

    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;
    }
}

//main.c//主函數用來通過調用函數來實現使用功能
#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;
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

浦县| 罗定市| 九龙城区| 娱乐| 普安县| 蕉岭县| 东宁县| 宽城| 云和县| 富川| 鸡东县| 钦州市| 新乐市| 元阳县| 清苑县| 东阳市| 东丽区| 乐陵市| 东兴市| 利川市| 华坪县| 巫溪县| 攀枝花市| 大埔区| 景谷| 广西| 班玛县| 抚顺市| 蒙城县| 肇源县| 镇赉县| 上虞市| 衡水市| 阳东县| 桐城市| 元氏县| 四会市| 寿光市| 新营市| 黎川县| 横山县|