1. 程式人生 > >c語言編程三子棋(井字棋)

c語言編程三子棋(井字棋)

客戶 def mem isa memset time 判斷函數 layer 聲明

頭文件(test.h):

#ifndef _THREE_CHESS_H_//防止頭文件被重復,包含[也可以用#pragma once]
#define _THREE_CHESS_H_

#include <stdio.h>
#include <windows.h>
#include <time.h>
#pragma warning(disable:4996)//VS中解決scanf函數不能使用問題

#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

源文件:(test.c)

#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;//調用隨機函數,確保橫豎坐標小於3
        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){
            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];返回‘X’‘O’判斷誰贏
        }
    }
    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)

#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;
        }
    }
    return 0;
}

c語言編程三子棋(井字棋)