1. 程式人生 > >井字棋遊戲C語言簡單思路人人對戰版(無圖形化介面))

井字棋遊戲C語言簡單思路人人對戰版(無圖形化介面))

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#defineROW 3

#defineLINE 3

intsymbol1, symbol2, step;

char a[10];

voidinitialize()     //將棋盤元素初始化為空格

{

    step= 0;

    int i;

    for (i = 1; i <= 9;i++)

    {

         a[i]= ' ';

    }

 

}

voidstart_game()     //列印開始介面並讓P1,P2選擇所持棋子

{

    printf("                             ||************************************************************||\n"

);

    printf("                             ||************************************************************||\n");

    printf(

"                             ||***********       歡迎來到大型博弈遊戲          ***********||\n");

    printf("                             ||***********                                     ***********||\n");

    printf("                             ||************         傳統井字棋PVP版            ************||\n");

    printf("                             ||************                                   ************||\n");

    printf("                             ||**************       按任意鍵開始遊戲        **************||\n");

    printf("                             ||************************************************************||\n");

    printf("                             ||************************************************************||\n");

    system("PAUSE");

    printf("                                    請選擇P1要使用的符號(輸入X或O)(大寫)\n\n");

    while (1)

    {

         symbol1=getch();

         if (symbol1 == 'X' || symbol1 == 'O')                         //P1,P2選擇棋子

         {

             if (symbol1 == 'X')

                  symbol2= 'O';

             else symbol2 = 'X';

             printf("                                       P1使用%c,P2使用%c\n\n", symbol1, symbol2);

             break;

         }       

         printf("輸入錯誤,請重新輸入:\n");

    }

}

void print()      //列印棋盤

{

    printf("                                              |       |         \n");

    printf("                                           %c  |   %c   |   %c   \n", a[1], a[2], a[3]);

    printf("                                       -------| ------| ------- \n");

    printf("                                              |       |         \n");

    printf("                                           %c  |   %c   |  %c    \n", a[4], a[5], a[6]);

    printf("                                              |       |         \n");

    printf("                                       -------|-------| ------- \n");

    printf("                                           %c   |  %c   |   %c   \n", a[7], a[8], a[9]);

    printf("                                              |       |         \n");

}

 

voidplayer1_step()       //P1的回合


 

{

    int location;

    printf("請P1輸入要放的位置:\n");

    while (1)

    {

         scanf("%d", &location);

         if ((char)(location + 48)<'1' || (char)(location + 48)>'9')                                 //防止輸入1~9以外的數字

         {

             printf("輸入錯誤,請重新輸入:\n");

             continue;

         }

         if (a[location] !=symbol1&&a[location] != symbol2)           //若此位置為空則可以放置棋子

         {

             a[location]= symbol1;

             print();                                                 //列印放置後的棋盤

             break;

         }

         else printf("該位置被佔了,請重新輸入:\n");

    }

}

voidplayer2_step()       //P2的回合

{

    int location;

    printf("請P2輸入要放的位置:\n");

    while (1)

    {

         scanf("%d", &location);

         if ((char)(location + 48)<'1' || (char)(location + 48)>'9')                                 //防止輸入1~9以外的數字

         {

             printf("輸入錯誤,請重新輸入:\n");

             continue;

         }

         if (a[location] !=symbol1&&a[location] != symbol2)           //若此位置為空則可以放置棋子

         {

             a[location]= symbol2;

             print();                                                 //列印放置後的棋盤

             break;

         }

         else printf("該位置被佔了,請重新輸入:\n");

    }

}

int row(intn, charsymbol)                     //通過判斷每一行三個元素是否都是symbol來決定是否是winner

{

    int i, flag = 0;

    for (i = (1 + ROW*(n - 1)); i <= ROW*n; i++)

    {

         if (a[i] == symbol)

             flag++;                            //累計迴圈次數用於下面判斷

 

    }

    if (flag == ROW)

         return 1;

    else

         return 0;

}

int line(intn, charsymbol)                    //通過判斷每一列三個元素是否都是symbol來決定是否是winner

{

    int i, flag = 0;

    for (i = n; i <= (n + LINE*(ROW - 1)); i = i + ROW)

    {

         if (a[i] == symbol)

             flag++;                        //累計迴圈次數用於下面判斷

    }

    if (flag == LINE)

         return 1;

    else

         return 0;

}

intdigonal(charsymbol)

{

    int i, flag = 0;

    for (i = 1; i <= ROW*LINE; i = i + ROW + 1)

    {

         if (a[i] == symbol)

             flag++;                        //累計迴圈次數用於下面判斷

    }

 

    if (flag == LINE)

         return 1;

    flag= 0;

    for (i = ROW; i <= 1+ ROW*(LINE - 1); i = i + ROW - 1)

    {

         if (a[i] == symbol)

             flag++;                        //累計迴圈次數用於下面判斷

    }

    if (flag == LINE)

         return 1;

    return 0;

}

int judge(charsymbol)                               //判斷遊戲是否出現勝利者

{

    int i;

    if (digonal(symbol) == 1)

         return 1;

    for (i = 1; i <= ROW; i++)

    {

         if (row(i, symbol) == 1)

             return 1;

    }

    for (i = 1; i <= LINE; i++)

    {

         if (line(i, symbol) == 1)

             return 1;

    }

    return 0;

 

}

intchoose_continue()                  //選擇是否繼續,返回一則表示繼續

{

    printf("是否要繼續玩?(輸入1:繼續玩,2:退出)\n");

    int choice;

    while (1)

    {

         scanf("%d", &choice);

         if (choice == 1)                   //繼續

         {

             initialize();

             start_game();

             return 1;

         }

         elseif (choice == 2)

         {

             printf("                                               遊戲結束\n");

             exit(0);

         }                         //退出遊戲

         else

             printf("                                           沒有該指令,重新輸入:\n");

    }

    return 0;

}

voidchoose()                                   //選擇要下棋的玩家並列印判斷資訊

{

    int t;

    while (1)

    {

         player1_step();                            //從P1開始走

         step++;                                    //累計步數判斷是否達到平局,下同

         if (t = judge(symbol1)> 0)                 //判斷P1是否贏了

         {

             printf("\n\n                                          恭喜P1獲得勝利\n");

             if (choose_continue()== 1)

                  choose;                            //呼叫choose繼續下一局,因不能呼叫自身後面的函式所以不能放此語句在choose中

         }

         if (step >= 9)                             //平局條件,因最後一步一定由P1走出,所以放在p1_step之後

         {

             printf("\n\n                                         兩人打平,沒有winner\n");

             if (choose_continue()== 1)

                  choose;

         }

         player2_step();

         step++;

         if (judge(symbol2)> 0)

         {

             printf("\n\n                                           恭喜P2獲得勝利\n");

             if (choose_continue()== 1)

                  choose;

         }

 

    }

 

}

 

int main()                         //主函式

{

    initialize();             //初始化陣列為空

    start_game();             //開始遊戲

    choose();

}