1. 程式人生 > >編寫程式—實現三子棋(五子棋)遊戲

編寫程式—實現三子棋(五子棋)遊戲


編寫程式,實現三子棋(五子棋)遊戲


將define定義的Row、Col改為5即可實現5*5棋盤的五子棋遊戲


程式程式碼如下:


game.h

#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#define Row 3
#define Col 3

void ChessBoard(char arr[Row][Col], int row, int col);
void DisplayBoard(char arr[Row][Col], int row, int col);
void PlayerMove(char arr[Row
][Col], int row, int col); void ComputerMove(char arr[Row][Col], int row, int col); char CheckWin(char arr[Row][Col], int row, int col); int IsFull(char arr[Row][Col], int row, int col); #endif//__GAME_H__

game.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "game.h"
#include <string.h>
void ChessBoard(char arr[Row][Col], int row, int col) { memset(arr, ' ', row*col*sizeof(arr[0][0])); } void DisplayBoard(char arr[Row][Col], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { printf
(" %c ",arr[i][j]); if (j != (col - 1)) { printf("|"); } } printf("\n"); if (i != (row - 1)) { for (j = 0; j < col; j++) { printf("___", arr[i][j]); if (j != (col - 1)) { printf("|"); } } printf("\n"); } else { for (j = 0; j < col; j++) { printf(" "); if (j != (col - 1)) { printf("|"); } } printf("\n"); } } } void PlayerMove(char arr[Row][Col], int row, int col) { int x = 0; int y = 0; while (1) { printf("玩家走:\n"); printf("請輸入座標:\n"); scanf("%d%d", &x, &y); if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col)) { if (arr[x - 1][y - 1] == ' ') { arr[x - 1][y - 1] = 'X'; break; } else { printf("座標被佔用\n"); } } else { printf("座標非法\n"); } } } void ComputerMove(char arr[Row][Col], int row, int col) { printf("電腦走\n"); while (1) { int x = rand() % row; int y = rand() % col; if (arr[x][y] == ' ') { arr[x][y] = '0'; break; } } } char CheckWin(char arr[Row][Col], int row, int col) { int i = 0; int j = 0; int flag = 0; for (i = 0; i < row; i++) { for (j = 0; j < col-1; j++) { if ((arr[i][j] == arr[i][j+1]) && (arr[i][0]!=' ')) { flag = 1; } else { flag = 0; break; } } if (flag == 1) { return arr[i][0]; } } for (j = 0; j < col; j++) { for (i = 0; i < row-1; i++) { if ((arr[i][j]==arr[i+1][j]) && (arr[0][j]!=' ')) { flag = 1; } else { flag = 0; break; } } if (flag == 1) { return arr[0][j]; } } for (i = 0; i < row-1; i++) { if ((arr[i][i]== arr[i+1][i+1]) && (arr[0][0]!=' ')) { flag = 1; } else { flag = 0; break; } } if (flag == 1) { return arr[0][0]; } for (i = 0; i < row-1; i++) { if ((arr[i][row-i-1]==arr[i+1][row-i-2]) && (arr[0][row-1]!=' ')) { flag = 1; } else { flag = 0; break; } } if (flag == 1) { return arr[0][row-1]; } if (IsFull(arr, row, col) == 1) { return 'q'; } return ' '; } int IsFull(char arr[Row][Col], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (arr[i][j] == ' ') { return 0; } } } return 1; }

test.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"

void menu()
{
    printf("********************\n");
    printf("*****  1.Play  *****\n");
    printf("*****  0.Exit  *****\n");
    printf("********************\n");
}

void game()
{
    char arr[Row][Col] = {0};
    char ret = 0;
    ChessBoard(arr, Row, Col);//初始化棋盤
    DisplayBoard(arr, Row, Col);//在螢幕上列印棋盤
    do
    {
        PlayerMove(arr, Row, Col);//玩家走
        ret = CheckWin(arr, Row, Col);//判斷輸贏
        DisplayBoard(arr, Row, Col);//列印棋盤
        if (ret != ' ')
        {
            break;
        }
        ComputerMove(arr, Row, Col);//電腦走
        ret = CheckWin(arr, Row, Col);
        DisplayBoard(arr, Row, Col);//列印棋盤
    } while (ret == ' ');
    if (ret == 'X')
    {
        printf("玩家贏\n");
    }
    if (ret == '0')
    {
        printf("電腦贏\n");
    }
    if (ret == 'q')
    {
        printf("平局\n");
    }
}
void test()
{
    int input = 0;
    do
    {
        menu();
        printf("請選擇:\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出遊戲:\n");
            break;
        default:
            printf("選擇錯誤,請重新選擇:\n");
            break;
        }
    } while (input);
}
int main()
{
    srand((unsigned int)time(NULL));//生成隨機數
    test();
    system("pause");
    return 0;
}

程式執行結果如下:


這裡寫圖片描述