目錄
- 兩個源檔案與一個頭檔案代碼
- 游戲主干:test.c
- 頭檔案與函式宣告:game.h
- 函式實作:game.c
- 運行截圖
兩個源檔案與一個頭檔案代碼
游戲主干:test.c
#include"game.h"
void menu();//選單
void game();//游戲
int main(){
srand((unsigned int)time(NULL));//初始化隨機種子
int input = 0;
do{
menu();//選單
printf("請選擇:");
scanf("%d", &input);
switch (input){
case 1:
system("cls");
game();//進入游戲
break;
case 0:
printf("退出\n");
break;
default:
printf("無效選項,重新選擇\n");
break;
}
system("cls");
} while (input);
return 0;
}
void menu() {//游戲選單
printf("|----------------------|\n");
printf("| 1.玩 |\n");
printf("| 0.摸 |\n");
printf("|----------------------|\n");
printf("輸入選項:\n");
}
void game() {//游戲
char res = 0;
char board[ROW][COL];
Init(board, ROW, COL);
ShowBoard(board, ROW, COL);
while (1) {
PlayerAct(board, ROW, COL);//玩家行動
system("cls");
ShowBoard(board, ROW, COL);
res = Check(board, ROW, COL);
if (res != 'c')
break;
ComputerAct(board, ROW, COL);//電腦行動
system("cls");
ShowBoard(board, ROW, COL);
Sleep(1000);
res = Check(board, ROW, COL);
if (res != 'c')
break;
}
if (res == '#')
printf("玩家勝\n");
else if (res == '*')
printf("電腦勝\n");
else if (res == 'q')
printf("平局\n");
Sleep(1500);
}
頭檔案與函式宣告:game.h
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 3
#define COL 3
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<Windows.h>
void Init(char board[ROW][COL], int row, int col);//初始化
void ShowBoard(char board[ROW][COL], int row, int col);//列印棋盤
void PlayerAct(char board[ROW][COL], int row, int col);//玩家行動
void ComputerAct(char board[ROW][COL], int row, int col);//電腦行動
char Check(char board[ROW][COL], int row, int col);//勝負判斷
函式實作:game.c
#include"game.h"
//初始化
void Init(char board[ROW][COL], int row, int col){
int i = 0,j = 0;
for (i = 0; i < row; i++){
for (j = 0; j < col; j++){
board[i][j] = ' ';
}
}
}
//列印棋盤
void ShowBoard(char board[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 ", board[i][j]);
if (j < col - 1)
printf("|");
}
printf("\n");
if (i < row - 1){
for (j = 0; j < col; j++){
printf("---");
if (j < col - 1)
printf("|");
}
printf("\n");
}
}
}
//玩家行動
void PlayerAct(char board[ROW][COL], int row, int col){
int x = 0,y = 0;
printf("#玩家回合:\n");
while (1){
printf("輸入一個坐標,用空格隔開:");
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] = '#';
break;
}
else
printf("坐標被占用,重新輸入\n");
}
else{
printf("無效坐標,重新輸入\n");
}
}
}
//電腦行動
void ComputerAct(char board[ROW][COL], int row, int col){
printf("*電腦回合:>\n");
Sleep(500);
while (1){
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' '){
board[x][y] = '*';
break;
}
}
}
//判斷平局
int Full(char board[ROW][COL], int row, int col){
int i = 0;
for (i = 0; i < row; i++){
int j = 0;
for (j = 0; j < col; j++){
if (board[i][j] == ' ')
return 0;
}
}
return 1;
}
//判定
char Check(char board[ROW][COL], int row, int col){
int i = 0;
//檢查行
for (i = 0; i < row; i++){
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//檢查列
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[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//判斷平局
if (Full(board, row, col) == 1)
return 'q';
//勝負未定,繼續游戲
return 'c';
}
運行截圖


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290747.html
標籤:其他
上一篇:使用android studio實作阿里云物聯網平臺數字簽名(Signature)(附源代碼)
下一篇:C語言實作簡易三子棋
