C語言程式碼實現三子棋遊戲
阿新 • • 發佈:2020-11-02
三子棋小遊戲,供大家參考,具體內容如下
一、基本框架
1、初始介面:這個介面是讓使用者選擇玩或者不玩。
2、遊戲介面:進入遊戲之後,需要顯示棋盤。
二、遊戲的基本邏輯
首先,使用者下一個X。然後判斷,是使用者贏了,還是電腦贏了,還是平局,還是繼續下。然後,電腦下一個O。接著判斷,判斷形式和使用者下完棋之後的判斷形式一樣。只有在一方贏了之後,或者平局才跳出遊戲。
三、遊戲的基本步驟
(1)畫棋盤
(2)使用者下棋
(3)判斷
(4)電腦下棋
(5)判斷
(6)畫棋盤
上面的前五個步驟是迴圈,判斷就是判斷誰贏了,平局兩種情況下再退出。
四、程式
標頭檔案game.h:
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include<windows.h> #include<time.h> #define ROW 3 #define COL 3 #define P_COLOR 'X' #define C_COLOR 'O' #pragma warning (disable:4996) void Menu(); void Game(); #endif
遊戲模組game.c:
#include "game.h" void Menu(){ printf("####################\n"); printf("###1.Play 2.Exit####\n"); printf("####################\n"); printf("please enter#"); }//遊戲初始介面 void DrawBoard(char board[][COL],int row,int col){ printf(" 1 | 2 | 3 |\n"); printf("--------------\n"); for (int i = 0; i < row; i++){ printf("%d|",i+1); for (int j = 0; j < col; j++){ printf(" %c |",board[i][j]); } printf("\n--------------\n"); } }//畫出一個棋盤,並且棋盤上有下好的棋子 void PlayMove(char board[][COL],int col){ int x = 0; int y = 0; while (1){ printf("Please Enter Your pos#"); scanf("%d %d",&x,&y); if (x < 1 || x>3 || y < 1 || y>3){ printf("Pos Error\n"); continue; }//判斷使用者輸入的位置是否越界 if (board[x - 1][y - 1]!=' '){ printf("Chess Exist\n"); continue; }//判斷使用者輸入的位置上是否已經有棋子 board[x - 1][y - 1] = P_COLOR; return;//如果沒有棋子,則在這個位置放置‘X' } } char Judge(char board[][COL],int col){ for (int 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]; } }//判斷每一行,是否已經都是一樣的了,如果是,輸出對應的字元,如果是X,就輸出X,如果是O就輸出O。注意:排除掉都是空格的情況。 for (int j = 0; j < col; j++){ if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != ' '){ return board[0][j]; } }//判斷每一列,是否已經都是一樣的了,如果是,輸出對應的字元,如果是X,就輸出X,如果是O就輸出O。注意:排除掉都是空格的情況。 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]; }//判斷兩條對角線,是否已經都是一樣的了,如果是,輸出對應的字元,如果是X,就輸出X,如果是O就輸出O。注意:排除掉都是空格的情況。 for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (board[i][j] != 0){ return 'N'; } } }//判斷是否平均,如果每一個位置都不是空格,那麼就是平局,否則下一個人接著下。 return 'P';//輸出平均。 } void ComputerMove(char board[][COL],int col){ while (1){ int x = rand() % row; int y = rand() % row; if (board[x][y] == ' '){ board[x][y] = C_COLOR; break;//如果這個位置是空格,電腦就在這裡下一個O。 } } } void Game(){ srand((unsigned long)time(NULL)); char win = '\0'; char board[ROW][COL]; memset(board,' ',sizeof(board)); do{ DrawBoard(board,ROW,COL); PlayMove(board,COL); win=Judge(board,COL);//這裡判斷程式返回一個字元。X代表使用者贏了,O代表使用者輸了,P代表平局,N代表繼續。 if (win != 'N'){ break; } ComputerMove(board,COL); if (win != 'N'){ break; } } while (1); switch (win){ case P_COLOR: printf("you win!\n"); break; case C_COLOR: printf("you lose!\n"); break; case 'P': printf("平局\n"); break; default: break; } DrawBoard(board,COL); }
主程式main.c:
#include "game.h" int main(){ int quit = 0; while (!quit){ Menu();//遊戲初始介面 int select = 0; scanf("%d",&select);//讓使用者選擇是不是玩 switch (select){ case 1: Game(); break; case 2: printf("quit!"); quit = 1; break; default: printf("Enter Error!Try Again!\n"); break; } } printf("Byebye!"); system("pause"); return 0; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。