C語言實現三子棋

2021-08-14 19:52:57 字數 4192 閱讀 6256

本文是編寫乙個小專案:三子棋

有單人模式(人機)和雙人模式(人人)兩種

主要步驟:

1、首先建立三個檔案

這是我建立的,當熱名字可以隨個人愛好起,main.c中主要實現的時總選單,只需呼叫函式即可;sanziqi.h中實現的

是函式的宣告,當然在該專案中我並沒有分太多的函式;sanziqi.c中實現的是對sanziqi.h中宣告的函式的實現

2、各種函式的實現**

main.c

#define _crt_secure_no_warnings 1

#include#include#include"sanziqi.h"

void menu()

int main()

; int n = 1;

while (n) }

return 0;

}

sanziqi.h

#ifndef __sanziqi_h__

#define __sanziqi_h__

int onegame(int n, char arr[3][3]);

int twogame(int n, char arr[3][3]);

#endif // __sanziqi_h__

sanziqi.c

#define _crt_secure_no_warnings 1

#include#include#include"sanziqi.h"

int twogame(int n,char arr[3][3])

; for (k = 0; k < 6; k++)

if (i>2 || j > 2)

arr[i][j] = '*';

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

printf("|");

printf("\n");

} if ((arr[0][0] == 42 && arr[0][1] == 42 &&(arr[0][2]==42)) || (arr[1][0] == 42 && arr[1][1] == 42 && arr[1][2]==42)

|| (arr[2][0] == 42 && arr[2][1] == 42 && arr[2][2] == 42) || (arr[0][0] == 42 && arr[1][0] == 42 && arr[2][0] == 42)

|| (arr[0][1] == 42 && arr[1][1] == 42 && arr[2][1] == 42) || (arr[0][2] == 42 && arr[1][2] == 42 && arr[2][2] == 42)

|| (arr[0][0] == 42 && arr[1][1] == 42 && arr[2][2] == 42) || (arr[0][2] == 42 && arr[1][1] == 42 && arr[2][0] == 42))

count++;

if (count == 5)

printf("\n玩家二輸入座標:");

scanf("%d%d", &i, &j);

--i, --j;

if (arr[i][j] != 0)

if (i>2 || j > 2)

arr[i][j] = '@';

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

printf("|");

printf("\n");

} if ((arr[0][0] == '@' && arr[0][1] == '@' && (arr[0][2] == '@')) || (arr[1][0] == '@' && arr[1][1] == '@' && arr[1][2] == '@')

|| (arr[2][0] == '@' && arr[2][1] == '@' && arr[2][2] == '@') || (arr[0][0] == '@' && arr[1][0] == '@' && arr[2][0] == '@')

|| (arr[0][1] == '@' && arr[1][1] == '@' && arr[2][1] == '@') || (arr[0][2] == '@' && arr[1][2] == '@' && arr[2][2] == '@')

|| (arr[0][0] == '@' && arr[1][1] == '@' && arr[2][2] == '@') || (arr[0][2] == '@' && arr[1][1] == '@' && arr[2][0] == '@'))

}} return 0;

}int onegame(int n, char arr[3][3])

; for (k = 0; k < 6; k++)

if (i>2 || j > 2)

arr[i][j] = '*';

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

printf("|");

printf("\n");

}if ((arr[0][0] == 42 && arr[0][1] == 42 && (arr[0][2] == 42)) || (arr[1][0] == 42 && arr[1][1] == 42 && arr[1][2] == 42)

|| (arr[2][0] == 42 && arr[2][1] == 42 && arr[2][2] == 42) || (arr[0][0] == 42 && arr[1][0] == 42 && arr[2][0] == 42)

|| (arr[0][1] == 42 && arr[1][1] == 42 && arr[2][1] == 42) || (arr[0][2] == 42 && arr[1][2] == 42 && arr[2][2] == 42)

|| (arr[0][0] == 42 && arr[1][1] == 42 && arr[2][2] == 42) || (arr[0][2] == 42 && arr[1][1] == 42 && arr[2][0] == 42))

count++;

if (count == 5)

printf("\n電腦:");

i = rand() % 3;

j = rand() % 3;

printf("\n");

--i, --j;

while(arr[i][j] != 0)

arr[i][j] = '@';

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

printf("|");

printf("\n");

}if ((arr[0][0] == '@' && arr[0][1] == '@' && (arr[0][2] == '@')) || (arr[1][0] == '@' && arr[1][1] == '@' && arr[1][2] == '@')

|| (arr[2][0] == '@' && arr[2][1] == '@' && arr[2][2] == '@') || (arr[0][0] == '@' && arr[1][0] == '@' && arr[2][0] == '@')

|| (arr[0][1] == '@' && arr[1][1] == '@' && arr[2][1] == '@') || (arr[0][2] == '@' && arr[1][2] == '@' && arr[2][2] == '@')

|| (arr[0][0] == '@' && arr[1][1] == '@' && arr[2][2] == '@') || (arr[0][2] == '@' && arr[1][1] == '@' && arr[2][0] == '@'))

}} return 0;

}

以上就是全部內容,在附幾張結果截圖

C語言實現三子棋

game.h define crt secure no warnings 1 ifndef game h define game h include include include include define rows 3 define cols 3 void init board char bo...

三子棋C語言實現

要寫這個三子棋的程式我們分為三個部分首先是宣告函式的標頭檔案,我們分別宣告了五個函式,初始化棋盤,列印棋盤,玩家走,電腦走,檢查是否贏了。之後我們寫測試 然後分別來實現這五個函式 define crt secure no warnings 1 ifndef game h define game h ...

C語言實現三子棋

實現三子棋程式,只要我們能夠理清楚思路,就可以知道其實它的做法並不難,重點在於實際寫 時需要多關注細節。這裡我們可以寫完一塊就可以立馬執行程式檢查是否如我們所想的效果出現,如若不是便可立即查錯糾錯。如下。test.c include include include include game.h vo...