1. 程式人生 > >用C語言實現三子棋遊戲

用C語言實現三子棋遊戲

    game.h
    #ifndef __GAME_H__
    #define __GAME_H__
    
    #define _CRT_SECURE_NO_WARNINGS
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    #define ROW 3
    #define COL 3
    
    void InitBoard(char board[ROW]
[COL], int row, int col);// 棋盤初始化 void DisplayBoard(char board[ROW][COL], int row, int col);// 顯示棋盤 void PlayerMove(char board[ROW][COL], int row, int col); //玩家下棋 void ComputerMove(char board[ROW][COL], int row, int col); //電腦下棋 char JudgeWin(char board[ROW][COL], int row, int col); //判斷是否遊戲結束
#endif //__GAME_H__ game.c #define _CRT_SECURE_NO_WARNINGS #include"game.h" void InitBoard(char board[ROW][COL] ,int row ,int col) { memset(board, ' ', ROW*COL); } void DisplayBoard(char board[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 (j < COL - 1) printf(" %c |", board[i][j]); else printf(" %c ", board[i][j]); } printf("\n"); for (j = 0; j < COL; j++) { if (i < ROW - 1) printf("--- "); } printf("\n"); } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("玩家走,請輸入座標:>"); scanf("%d%d", &x, &y); if (x >= 0 && x <= ROW && y >= 0 && y <= COL) { if (' ' == board[x - 1][y - 1]) { board[x - 1][y - 1] = 'X'; break; } else printf("輸入錯誤,請重試\n"); } else printf("輸入錯誤,請重試\n"); } } void ComputerMove(char board[ROW][COL] ,int row, int col) { while (1) { int x = rand() % 3; int y = rand() % 3; if (' ' == board[x][y]) { board[x][y] = '*'; break; } } } static int JudgeFull(char board[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 (' ' == board[i][j]) return 0; } } return 1; } char JudgeWin(char board[ROW][COL] ,int row ,int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ') return board[i][0]; } for (i = 0; i < row; i++) { if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[i][0] != ' ') return board[0][i]; } if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ') return board[1][1]; else if (board[0][2] == board[1][1] == board[2][0] && board[1][1] != ' ') return board[1][1]; else if (JudgeFull(board, row ,col)) return 'R'; } test.c #define _CRT_SECURE_NO_WARNINGS #include "game.h" void menu() { printf("**************************\n"); printf("******** 1.play ********\n"); printf("******** 0.quit ********\n"); printf("**************************\n"); } void game() { char board[ROW][COL]; char key = '0'; InitBoard(board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); key = JudgeWin(board, ROW, COL); printf("key = %c\n", key); if (key == '*') { printf("電腦贏\n"); break; } if (key == 'X') { printf("玩家贏\n"); break; } if (key == 'R') { printf("平局\n"); break; } } } void test() { int choose; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇:>"); scanf("%d", &choose); switch (choose) { case 1: game(); break; case 0: printf("即將退出遊戲\n"); break; default: printf("輸入錯誤,請重試\n"); break; } } while (choose); } int main() { test(); system("pause"); return 0; }