C语言-三子棋游戏
C語言中用寫頭文件的方式寫了一個三子棋游戲
1.測試函數text.c
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include"chess.h"void menu()
{printf("***********************\n");printf("******  1.play  *******\n");printf("******  0.exit  *******\n");printf("***********************\n");printf("請選擇相應的數字>:");
}int main()
{char board[LINE][LIST];int input = 1;int a = 0;srand((unsigned int)time(NULL));while (input){memset(board, ' ', sizeof(char)*LINE*LIST);menu();scanf("%d", &input);printf("\n");switch (input){case 1:a = game(board);break;case 0:break;default:printf("你的輸入不符合要求,請重新輸入>:"); break;}if (0 == input){break;}	}printf("游戲結束,歡迎再次使用!!!\n");return 0;
}2.頭文件chess.h
#ifndef __CHESS_H__
#define __CHESS_H__#define LINE 3
#define LIST 3int game(char board[LINE][LIST]);
void Player(char board[LINE][LIST]);
void Computer(char board[LINE][LIST]);
void ChessBoard(char board[LINE][LIST]);
int Check(char board[LINE][LIST]);
//void ChessBoard(int LINE,int LIST );       此語句錯誤,不可以直接用宏作為參數,頭文件中不可以這樣寫,LIST和LINE可以作為實參,不可以作為形參
#endif  //__CHESS_H__3.函數文件chess.c
 
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"chess.h"void ChessBoard(char board[LINE][LIST])
{printf("%c|%c|%c\n", board[0][0], board[0][1], board[0][2]);printf("-----\n");printf("%c|%c|%c\n", board[1][0], board[1][1], board[1][2]);printf("-----\n");printf("%c|%c|%c\n", board[2][0], board[2][1], board[2][2]);printf("\n\n");
}int game(char board[LINE][LIST])
{ChessBoard(board);//為了使電腦贏了之后可以顯示出棋盤來while (1){Player(board);ChessBoard(board);if (Check(board)){return 0;}Computer(board);ChessBoard(board);if (Check(board)){return 0;}}
}void Player(char board[LINE][LIST])
{int x = 0, y = 0;int m = 1;//設置m是為了使輸入數據不合法printf("玩家玩\n請輸入坐標>:");while (m){scanf("%d%d", &x, &y);printf("\n");if ((x >= 1 && x <= 3) && (board[x - 1][y - 1] == ' ')){board[x - 1][y - 1] = 'X';m = 0;}else{printf("數據不符合要求\n請重新輸入>:");}}
}void Computer(char board[LINE][LIST])  
{int x = 0, y = 0;int m = 1;printf("電腦玩\n");while (m){x = rand()% 3;y = rand()% 3;if (board[x][y] == ' '){board[x][y] = '#';m = 0;}}
}int Check(char board[LINE][LIST])   //When you pass an array, you must add the brackets in the back of the array.
{int i = 0,j = 0,flag = 0;for (i = 0; i <= 2; i++){if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == 'X')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == 'X'))){printf("The player wins the game!!!\n\n");return 1;}if (((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) && (board[i][0] == '#')) || ((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) && (board[0][i] == '#'))){printf("The computer wins the game!!!\n\n");return 1;}if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'X')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == 'X'))){printf("The player wins the game!!!\n\n");return 1;}if (((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == '#')) || ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[0][2] == '#'))){printf("The computer wins the game!!!\n\n");return 1;}for (j = 0; j <= 2; j++)  //平局{if (' ' != board[i][j]){flag++;}}if (9 == flag){printf("平局\n");return 1;}}return 0;
}
總結
 
                            
                        - 上一篇: Python基础学习3
- 下一篇: 维多利亚一套【除折扇】值多少元?
