C語言——三子棋

game.h
#ifndef _GAME_H_
#define _GAME_H_

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

#define ROWS 3
#define COLS 3

void InitBoard(char board[ROWS][COLS], int row, int col);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void PlayMove(char board[ROWS][COLS], int row, int col);
void ComputerMove(char board[ROWS][COLS], int row, int col);
static int IsWin(char board[ROWS][COLS], int row, int col);
char IsWiner(char board[ROWS][COLS], int row, int col);

#endif
test.c
#include<stdio.h>
#include<time.h>
#include "game.h"
#include<stdlib.h>

void menu()
{
    printf("**********************************************************\n");
    printf("**********  1-play  ****************  0-exit  ************\n");
    printf("**********************************************************\n");
}

void game()
{
    char board[ROWS][COLS] = { 0 };
    char ret = 0;
    InitBoard(board, ROWS, COLS);//初始化棋盤
    DisplayBoard(board, ROWS, COLS);//打印棋盤
    srand((unsigned int)rand(NULL));
    while (1)
    {
        PlayMove(board, ROWS, COLS);//玩家走
        if ((ret = IsWiner(board, ROWS, COLS)) != ' ')
            break;
        DisplayBoard(board, ROWS, COLS);
        printf("-----------------------------\n");
        printf("-----------------------------\n");
        ComputerMove(board, ROWS, COLS);//電腦走
        if ((ret = IsWiner(board, ROWS, COLS)) != ' ')
            break;
        DisplayBoard(board, ROWS, COLS);
    }
    //結果評定
    if (ret == 'X')
    {
        DisplayBoard(board, ROWS, COLS);
        printf("\n");
        printf("哇!你贏啦~~真棒!!\n");
    }
    if (ret == '0')
    {
        DisplayBoard(board, ROWS, COLS);
        printf("你好笨哦~電腦贏了你耶= =\n");
    }
    if (ret == 'q')
    {
        DisplayBoard(board, ROWS, COLS);
        printf("你和電腦半斤八兩,打了個平手啊~\n");
    }

}

int  main()
{
    int input = 0;
    do
    {
        menu();
        printf("請選擇:> ");
        scanf_s("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出遊戲!\n");
            break;
        }
    } while (input);
    return 0;
}
game.c
#include"game.h"

void InitBoard(char board[ROWS][COLS], int row, int col)
{
    memset(board, ' ', col*row*sizeof(char));
}

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    for (i = 0; i < row; i++)
    {
        printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
        if (i != 2)
            printf("---|---|---\n");
    }
}

void PlayMove(char board[ROWS][COLS], int row, int col)
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("請輸入你要下位置的座標:>   ");
        scanf_s("%d%d", &x, &y);
        x--;
        y--;
        if (((x >= 0) && (x <= 2)) && ((y >= 0) && (y <= 2)))
        {
            if (board[x][y] == ' ')
            {
                board[x][y] = 'X';
                break;
            }
            else
            {
                printf("你輸入的座標有誤,請重新輸入!\n");
                //break;
            }
        }
        else
        {
            printf("你輸入的座標有誤,請重新輸入!\n");
            //break;
        }
    }
}

void ComputerMove(char board[ROWS][COLS], int row, int col)
{
    while (1)
    {
        int x = rand() % 3;
        int y = rand() % 3;
        if (board[x][y] == ' ')
        {
            board[x][y] = '0';
            break;
        }
    }
}

static int IsWin(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    for (i = 0; i <= row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (board[i][j] = ' ')
                return 0;
        }
    }
    return 1;
}

char IsWiner(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    //橫3排
    for (i = 0; i < row; i++)
    {
        if ((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] != '0'))
            return board[i][0];
    }
    //豎3排
    for (i = 0; i < col; i++)
    {
        if ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] != '0'))
            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];

    if (IsWin(board, row, col))
    {
        return 'q';
    }
    return ' ';

}

圖:

這裏寫圖片描述

發佈了33 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章