主頁 >  其他 > c語言《井字棋游戲》無腦寫法

c語言《井字棋游戲》無腦寫法

2022-01-06 16:36:36 其他

大一期末基本結束,閑來無事,也便突發奇想,想著能否不用char字符型別去創造一個有符號的簡單小游戲,也因為本人非計算機專業,第一學期的c語言lab作業中有一次便是讓我們自己做井字棋游戲,當時用了許多函式進行拼接,也用了char字符型別,三子棋游戲也便不難做出,所以我也就為了打發時間,寫了一個完全int型別完成的井字棋,

此程式三子棋的結果必然是player輸 or 平局,player永遠無法贏computer

*將后文出現的代碼按順序拼接即可運行!

1.這是一些頭檔案 #include<conio.h>是為了呼叫之后的getch()函式
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include<conio.h>

void Function(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9);
int transform(int tran);

2.開始主函式,變數的初始化!主函式中運用了大量的陣列,主要目的是用來記錄和后續的判斷

int main()
{
	printf("  Welcome to play tick tack toe\n  O represents the AI\n  X represents the player\n");
	printf("computer_keyboard\n  7  8  9\n  4  5  6\n  1  2  3\n  .  .  .\n  .  .  .\n  .  .  .\n");
	printf("You first ,please input\n");
	const int size = 3;
	int cnt = 0;
	int count1[3][3] = {0};
	int count[3][3] = {0};
	int a[9] = {0};
	int b[size][size];
	int q = 0;
	int w = 0;
	int toe1 = 0, toe2 = 0, toe3 = 0, toe4 = 0, toe5 = 0, toe6 = 0, toe7 = 0, toe8 = 0, toe9 = 0;
	int numofo;
	int numofx;
	int result = -1;
	for (q = 0; q < 3; q++)
	{
		for (w = 0; w < 3; w++)
		{
			b[q][w] = 0;
		}
	}

3.player先行,computer后行,其中a[ ]陣列用于記錄每次下的位置對應的數字

你想下的每一個點對應鍵盤的123456789,這點與正常陣列列印順序顯然是不對應的,所以要做到這一步需要后期在函式中進行數字對應的更改!


	//player to 1   AI to 2    no to 0
	int i, j;
	int t = 0;
    int tran;
	while (result == -1)
	{

		printf("You turn!\n");
		int x;
		do
		{
			t = 0;
			tran=getch();
			
			x=transform(tran);
			
			for (i = 0; i < 9; i++)
			{
				if (x == a[i])
				{
					printf("error number! input again:\n");
					t = 1;
				}
			}
			if (x < 0 || x > 9)
			{
				printf("error number! input again:\n");
				t = 1;
			}
		} while (t == 1);

4.這段代碼單純只是為了用count[ ]和b[ ]陣列來分別記錄player和computer的下棋位置,a[ ]是記錄整體,這樣做只是為了后續的檢查,和讓AI可以選擇性的堵塞玩家

	//do the smart AI
		if (x == 1)
		{
			count[2][0] = 1;
		}
		else if (x == 2)
		{
			count[2][1] = 1;
		}
		else if (x == 3)
		{
			count[2][2] = 1;
		}
		else if (x == 4)
		{
			count[1][0] = 1;
		}
		else if (x == 5)
		{
			count[1][1] = 1;
		}
		else if (x == 6)
		{
			count[1][2] = 1;
		}
		else if (x == 7)
		{
			count[0][0] = 1;
		}
		else if (x == 8)
		{
			count[0][1] = 1;
		}
		else if (x == 9)
		{
			count[0][2] = 1;
		}

		a[cnt] = x;

		cnt++;
		if (x == 1)
		{
			b[2][0] = 1;
		}
		else if (x == 2)
		{
			b[2][1] = 1;
		}
		else if (x == 3)
		{
			b[2][2] = 1;
		}
		else if (x == 4)
		{
			b[1][0] = 1;
		}
		else if (x == 5)
		{
			b[1][1] = 1;
		}
		else if (x == 6)
		{
			b[1][2] = 1;
		}
		else if (x == 7)
		{
			b[0][0] = 1;
		}
		else if (x == 8)
		{
			b[0][1] = 1;
		}
		else if (x == 9)
		{
			b[0][2] = 1;
		}

5.列印出玩家所下的棋子的影像,呈現在電腦螢屏上!這里無腦之處在于用了一個function里面包含九個引數,其實對應的就是最多下的九個棋子(也可以說是九個數字),然后呼叫進函式,用陣列順序列印棋盤,這樣的做法非常無腦,可想而知在五子棋中便不可能適用,這樣的好處也是記錄下每一次走的位置,以便在下一次新的數字輸入時可以保證之前下的棋子也在新列印的棋盤中!

	//check function,how to draw graph

			if (cnt == 1)
		{
			toe1 = a[0];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 2)
		{
			toe2 = a[1];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 3)
		{
			toe3 = a[2];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 4)
		{
			toe4 = a[3];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 5)
		{
			toe5 = a[4];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 6)
		{
			toe6 = a[5];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 7)
		{
			toe7 = a[6];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 8)
		{
			toe8 = a[7];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 9)
		{
			toe9 = a[8];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}

6.這是一個檢查模塊,目的就是判斷有沒有出現一方獲勝的情況,整個函式都包含在一個大的while回圈中,一旦出現獲勝便會跳出回圈,否則一直進行回圈,所以這個程式的效率非常的低,所以也是為什么說無腦的寫法,(效率極低,游戲運行程序中等待computer下可能需要五六秒

//Check the row
		for (i = 0; i < 3 && result == -1; i++)
		{
			numofo = 0;
			numofx = 0;
			for (j = 0; j < 3; j++)
			{
				if (b[i][j] == 1)
				{
					numofx++;
				}
				else if (b[i][j] == 2)
				{
					numofo++;
				}
				if (numofo == 3)
				{
					result = 1;
					break;
				}
				else if (numofx == 3)
				{
					result = 2;
					break;
				}
			}
		}

		//check the column
		for (j = 0; j < 3 && result == -1; j++)
		{
			numofo = 0;
			numofx = 0;
			for (i = 0; i < 3; i++)
			{
				if (b[i][j] == 1)
				{
					numofx++;
				}
				else if (b[i][j] == 2)
				{
					numofo++;
				}
				if (numofo == 3)
				{
					result = 1;
					break;
				}
				else if (numofx == 3)
				{
					result = 2;
					break;
				}
			}
		}

		//check the diagonal
		numofo = 0;
		numofx = 0;
		for (i = 0; i < 3 && result == -1; i++)
		{
			if (b[i][i] == 1)
			{
				numofx++;
			}
			else if (b[i][i] == 2)
			{
				numofo++;
			}
			if (numofo == 3)
			{
				result = 1;
				break;
			}
			else if (numofx == 3)
			{
				result = 2;
				break;
			}
		}
		numofo = 0;
		numofx = 0;
		for (i = 0; i < 3 && result == -1; i++)
		{
			if (b[i][2 - i] == 1)
			{
				numofx++;
			}
			else if (b[i][2 - i] == 2)
			{
				numofo++;
			}
			if (numofo == 3)
			{
				result = 1;
				break;
			}
			else if (numofx == 3)
			{
				result = 2;
				break;
			}
		}
		if (result == 1 || result == 2)
		{
			break;
		}
		if (cnt == 9)
		{
			result = 0;
			break;
		}
		//check over!

7.這里用到一個清屏system("cls"),目的是程式執行時有更好的體驗感,之前用的getch()函式可以直接讀取鍵盤,也是為了游戲更好的體驗感! 之后就是computer下棋,采用亂數生成,然后進行篩選陣列中是否存在已經有過的數字,從而達到目的,

system("cls");
		printf("AI turn!\n");
		int put = 0;
		int number;
		do
		{
			put = 0;
			srand(time(0));
			number = rand() % 9 + 1;
			int g = number;
			for (i = 0; i < 9; i++)
			{
				if (number == a[i])
				{
					put = 1;
				}
			}

8.這一大段代碼就是我閑來沒事窮舉出所有情況,而不是讓電腦隨機產生沒出現過的數字,不然顯得電腦非常弱智!!所以在進行游戲時,即使玩家先手,最多也只是平局!

//do a smart AI

			if (cnt == 1 && a[0] != 5)
			{
				number = 5;
				break;
			}
			if (cnt == 1 && a[0] == 5)
			{
				number = 7;
				break;
			}

			int op0;
			op0 = number;
			if (count[0][0] == 1 && count[0][1] == 1)
			{
				number = 9;
				op0 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = g;
					}
				}
			}

			//
			int op1;
			op1 = op0;
			if (count[0][2] == 1 && count[0][1] == 1)
			{
				number = 7;
				op1 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = op0;
						op1 = op0;
					}
				}
			}
			//

			int op2;
			op2 = op1;
			if (count[0][0] == 1 && count[1][0] == 1)
			{
				number = 1;
				op2 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = op1;
						op2 = op1;
					}
				}
			}

			//
			int op3;
			op3 = op2;
			if (count[1][0] == 1 && count[2][0] == 1)
			{
				number = 7;
				op3 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = op2;
						op3 = op2;
					}
				}
			}
			//
			int op4;
			op4 = op3;
			if (count[2][0] == 1 && count[2][1] == 1)
			{
				number = 3;
				op4 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = op3;
						op4 = op3;
					}
				}
			}
			//
			int op5;
			op5 = op4;
			if (count[2][2] == 1 && count[2][1] == 1)
			{
				number = 1;
				op5 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = op4;
						op5 = op4;
					}
				}
			}
			//
			int op6;
			op6 = op5;
			if (count[0][2] == 1 && count[1][2] == 1)
			{
				number = 3;
				op6 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = op5;
						op6 = op5;
					}
				}
			}
			//
			int op7;
			op7 = op6;
			if (count[2][2] == 1 && count[1][2] == 1)
			{
				number = 9;
				op7 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = op6;
						op7 = op6;
					}
				}
			}
			//
			int op8;
			op8 = op7;
			if (count[2][0] == 1 && count[1][1] == 1)
			{
				number = 9;
				op8 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = op7;
						op8 = op7;
					}
				}
			}
			//
			int op9;
			op9 = op8;
			if (count[0][2] == 1 && count[1][1] == 1)
			{
				number = 1;
				op9 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = op8;
						op9 = op8;
					}
				}
			}
			//
			int op10;
			op10 = op9;
			if (count[0][0] == 1 && count[1][1] == 1)
			{
				number = 3;
				op10 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = op9;

						op10 = op9;
					}
				}
			}
			//
			int op11;
			op11 = op10;
			if (count[2][2] == 1 && count[1][1] == 1)
			{
				number = 7;
				op11 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = op10;
						op11 = op10;
					}
				}
			}
			//
			int op12;
			op12 = op11;
			if (count[1][1] == 1 && count[1][2] == 1)
			{
				number = 4;
				op12 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 4)
					{
						number = op11;
						op12 = op11;
					}
				}
			}
			//
			int op13;
			op13 = op12;
			if (count[1][0] == 1 && count[1][1] == 1)
			{
				number = 6;
				op13 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 6)
					{
						number = op12;
						op13 = op12;
					}
				}
			}
			//
			int op14;
			op14 = op13;
			if (count[0][1] == 1 && count[1][1] == 1)
			{
				number = 2;
				op14 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 2)
					{
						number = op13;
						op14 = op13;
					}
				}
			}
			//
			int op15;
			op15 = op14;
			if (count[2][1] == 1 && count[1][1] == 1)
			{
				number = 8;
				op15 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 8)
					{
						number = op14;
						op15 = op14;
					}
				}
			}
			//
			int op16;
			op16 = op15;
			if (count[2][0] == 1 && count[2][2] == 1)
			{
				number = 2;
				op16 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 2)
					{
						number = op15;
						op16 = op15;
					}
				}
			}
			//
			int op17;
			op17 = op16;
			if (count[0][0] == 1 && count[2][0] == 1)
			{
				number = 4;
				op17 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 4)
					{
						number = op16;
						op17 = op16;
					}
				}
			}
			//
			int op18;
			op18 = op17;
			if (count[0][0] == 1 && count[0][2] == 1)
			{
				number = 8;
				op18 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 8)
					{
						number = op17;
						op18 = op17;
					}
				}
			}
			//
			int op19;
			op19 = op18;
			if (count[0][2] == 1 && count[2][2] == 1)
			{
				number = 6;
				op19 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 6)
					{
						number = op18;
						op19 = op18;
					}
				}
			}
			for (i = 0; i < 9; i++)
			{
				if (number == a[i])
				{
					put = 1;
				}
			}

			//AI try to win

			int k;
			int od0;
			k = op19;
			od0 = op19;
			if (count1[0][0] == 1 && count1[0][1] == 1)
			{
				number = 9;
				k = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = k;
						od0 = k;
					}
				}
			}

			//
			int od1 = 0;
			od1 = od0;
			if (count1[0][2] == 1 && count1[0][1] == 1)
			{
				number = 7;
				od1 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = od0;
						od1 = od0;
					}
				}
			}
			//

			int od2 = 0;
			od2 = od1;
			if (count1[0][0] == 1 && count1[1][0] == 1)
			{
				number = 1;
				od2 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = od1;
						od2 = od1;
					}
				}
			}

			//
			int od3 = 0;
			od3 = od2;
			if (count1[1][0] == 1 && count1[2][0] == 1)
			{
				number = 7;
				od3 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = od2;
						od3 = od2;
					}
				}
			}
			//
			int od4 = 0;
			od4 = od3;
			if (count1[2][0] == 1 && count1[2][1] == 1)
			{
				number = 3;
				od4 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = od3;
						od4 = od3;
					}
				}
			}
			//
			int od5 = 0;
			od5 = od4;
			if (count1[2][2] == 1 && count1[2][1] == 1)
			{
				number = 1;
				od5 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = od4;
						od5 = od4;
					}
				}
			}
			//
			int od6 = 0;
			od6 = od5;
			if (count1[0][2] == 1 && count1[1][2] == 1)
			{
				number = 3;
				od6 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = od5;
						od6 = od5;
					}
				}
			}
			//
			int od7 = 0;
			od7 = od6;
			if (count1[2][2] == 1 && count1[1][2] == 1)
			{
				number = 9;
				od7 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = od6;
						od7 = od6;
					}
				}
			}
			//
			int od8 = 0;
			od8 = od7;
			if (count1[2][0] == 1 && count1[1][1] == 1)
			{
				number = 9;
				od8 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 9)
					{
						number = od7;
						od8 = od7;
					}
				}
			}
			//
			int od9 = 0;
			od9 = od8;
			if (count1[0][2] == 1 && count1[1][1] == 1)
			{
				number = 1;
				od9 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 1)
					{
						number = od8;
						od9 = od8;
					}
				}
			}
			//
			int od10 = 0;
			od10 = od9;
			if (count1[0][0] == 1 && count1[1][1] == 1)
			{
				number = 3;
				od10 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 3)
					{
						number = od9;
						od10 = od9;
					}
				}
			}
			//
			int od11 = 0;
			od11 = od10;
			if (count1[2][2] == 1 && count1[1][1] == 1)
			{
				number = 7;
				od11 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 7)
					{
						number = od10;
						od11 = od10;
					}
				}
			}
			//
			int od12 = 0;
			od12 = od11;
			if (count1[1][1] == 1 && count1[1][2] == 1)
			{
				number = 4;
				od12 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 4)
					{
						number = od11;
						od12 = od11;
					}
				}
			}
			//
			int od13 = 0;
			od13 = od12;
			if (count1[1][0] == 1 && count1[1][1] == 1)
			{
				number = 6;
				od13 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 6)
					{
						number = od12;
						od13 = od12;
					}
				}
			}
			//
			int od14 = 0;
			od14 = od13;
			if (count1[0][1] == 1 && count1[1][1] == 1)
			{
				number = 2;
				od14 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 2)
					{
						number = od13;
						od14 = od13;
					}
				}
			}
			//
			int od15 = 0;
			od15 = od14;
			if (count1[2][1] == 1 && count1[1][1] == 1)
			{
				number = 8;
				od15 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 8)
					{
						number = od14;
						od15 = od14;
					}
				}
			}
			//
			int od16 = 0;
			od16 = od15;
			if (count1[2][0] == 1 && count1[2][2] == 1)
			{
				number = 2;
				od16 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 2)
					{
						number = od15;
						od16 = od15;
					}
				}
			}
			//
			int od17 = 0;
			od17 = od16;
			if (count1[0][0] == 1 && count1[2][0] == 1)
			{
				number = 4;
				od17 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 4)
					{
						number = od16;
						od17 = od16;
					}
				}
			}
			//
			int od18 = 0;
			od18 = od17;
			if (count1[0][0] == 1 && count1[0][2] == 1)
			{
				number = 8;
				od18 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 8)
					{
						number = od17;
						od18 = od17;
					}
				}
			}
			//
			int od19 = 0;
			od19 = od18;
			if (count1[0][2] == 1 && count1[2][2] == 1)
			{
				number = 6;
				od19 = number;
				for (i = 0; i < 9; i++)
				{
					if (a[i] == 6)
					{
						number = od18;
						od19 = od18;
					}
				}
			}
			for (i = 0; i < 9; i++)
			{
				if (number == a[i])
				{
					put = 1;
				}
			}

		} while (put == 1);

		if (number == 1)
		{
			b[2][0] = 2;
		}
		else if (number == 2)
		{
			b[2][1] = 2;
		}
		else if (number == 3)
		{
			b[2][2] = 2;
		}
		else if (number == 4)
		{
			b[1][0] = 2;
		}
		else if (number == 5)
		{
			b[1][1] = 2;
		}
		else if (number == 6)
		{
			b[1][2] = 2;
		}
		else if (number == 7)
		{
			b[0][0] = 2;
		}
		else if (number == 8)
		{
			b[0][1] = 2;
		}
		else if (number == 9)
		{
			b[0][2] = 2;
		}

		//
		if (number == 1)
		{
			count1[2][0] = 1;
		}
		else if (number == 2)
		{
			count1[2][1] = 1;
		}
		else if (number == 3)
		{
			count1[2][2] = 1;
		}
		else if (number == 4)
		{
			count1[1][0] = 1;
		}
		else if (number == 5)
		{
			count1[1][1] = 1;
		}
		else if (number == 6)
		{
			count1[1][2] = 1;
		}
		else if (number == 7)
		{
			count1[0][0] = 1;
		}
		else if (number == 8)
		{
			count1[0][1] = 1;
		}
		else if (number == 9)
		{
			count1[0][2] = 1;
		}

		a[cnt] = number;
		cnt++;

		if (cnt == 9)
		{
			result = 0;
			break;
		}

8.跟之前一樣,電腦下棋后的呈現模塊!

	if (cnt == 1)
		{
			toe1 = a[0];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 2)
		{
			toe2 = a[1];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 3)
		{
			toe3 = a[2];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 4)
		{
			toe4 = a[3];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 5)
		{
			toe5 = a[4];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 6)
		{
			toe6 = a[5];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 7)
		{
			toe7 = a[6];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 8)
		{
			toe8 = a[7];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}
		else if (cnt == 9)
		{
			toe9 = a[8];
			Function(toe1, toe2, toe3, toe4, toe5, toe6, toe7, toe8, toe9);
		}

9.電腦的判斷模塊

//judge the keyboard
		//Check the row
		for (i = 0; i < 3 && result == -1; i++)
		{
			numofo = 0;
			numofx = 0;
			for (j = 0; j < 3; j++)
			{
				if (b[i][j] == 1)
				{
					numofx++;
				}
				else if (b[i][j] == 2)
				{
					numofo++;
				}
				if (numofo == 3)
				{
					result = 1;
					break;
				}
				else if (numofx == 3)
				{
					result = 2;
					break;
				}
			}
		}

		//check the column
		for (j = 0; j < 3 && result == -1; j++)
		{
			numofo = 0;
			numofx = 0;
			for (i = 0; i < 3; i++)
			{
				if (b[i][j] == 1)
				{
					numofx++;
				}
				else if (b[i][j] == 2)
				{
					numofo++;
				}
				if (numofo == 3)
				{
					result = 1;
					break;
				}
				else if (numofx == 3)
				{
					result = 2;
					break;
				}
			}
		}

		//check the diagonal
		numofo = 0;
		numofx = 0;
		for (i = 0; i < 3 && result == -1; i++)
		{
			if (b[i][i] == 1)
			{
				numofx++;
			}
			else if (b[i][i] == 2)
			{
				numofo++;
			}
			if (numofo == 3)
			{
				result = 1;
				break;
			}
			else if (numofx == 3)
			{
				result = 2;
				break;
			}
		}
		numofo = 0;
		numofx = 0;
		for (i = 0; i < 3 && result == -1; i++)
		{
			if (b[i][2 - i] == 1)
			{
				numofx++;
			}
			else if (b[i][2 - i] == 2)
			{
				numofo++;
			}
			if (numofo == 3)
			{
				result = 1;
				break;
			}
			else if (numofx == 3)
			{
				result = 2;
				break;
			}
		}
		if (result == 1 || result == 2)
		{
			break;
		}
		//check over
	}

10 .大回圈之后最終情況的判定

	if (result == 1)
	{
		printf("AI win the game!\n");
	}
	else if (result == 2)
	{
		printf("You win the game!\n");
	}
	else if (result == 0)
	{
		printf("No one win the game!!");
	}
	system("pause");
	 	printf("time used=%f",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

11.畫圖函式Function的主要內容:前面那么多if 是為了對應電腦上的數字鍵盤和棋盤位置的轉換

最后面則是列印棋盤的源頭

void Function(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
{
	int t1 = -1, t2 = -1, t3 = -1, t4 = -1, t5 = -1, t6 = -1, t7 = -1, t8 = -1, t9 = -1;
	if (x1 == 7)
	{
		t1 = 0;
	}
	else if (x1 == 8)
	{
		t1 = 1;
	}
	else if (x1 == 9)
	{
		t1 = 2;
	}
	else if (x1 == 4)
	{
		t1 = 3;
	}
	else if (x1 == 5)
	{
		t1 = 4;
	}
	else if (x1 == 6)
	{
		t1 = 5;
	}
	else if (x1 == 1)
	{
		t1 = 6;
	}
	else if (x1 == 2)
	{
		t1 = 7;
	}
	else if (x1 == 3)
	{
		t1 = 8;
	}
	if (x2 == 7)
	{
		t2 = 0;
	}
	else if (x2 == 8)
	{
		t2 = 1;
	}
	else if (x2 == 9)
	{
		t2 = 2;
	}
	else if (x2 == 4)
	{
		t2 = 3;
	}
	else if (x2 == 5)
	{
		t2 = 4;
	}
	else if (x2 == 6)
	{
		t2 = 5;
	}
	else if (x2 == 1)
	{
		t2 = 6;
	}
	else if (x2 == 2)
	{
		t2 = 7;
	}
	else if (x2 == 3)
	{
		t2 = 8;
	}
	if (x3 == 7)
	{
		t3 = 0;
	}
	else if (x3 == 8)
	{
		t3 = 1;
	}
	else if (x3 == 9)
	{
		t3 = 2;
	}
	else if (x3 == 4)
	{
		t3 = 3;
	}
	else if (x3 == 5)
	{
		t3 = 4;
	}
	else if (x3 == 6)
	{
		t3 = 5;
	}
	else if (x3 == 1)
	{
		t3 = 6;
	}
	else if (x3 == 2)
	{
		t3 = 7;
	}
	else if (x3 == 3)
	{
		t3 = 8;
	}
	if (x4 == 7)
	{
		t4 = 0;
	}
	else if (x4 == 8)
	{
		t4 = 1;
	}
	else if (x4 == 9)
	{
		t4 = 2;
	}
	else if (x4 == 4)
	{
		t4 = 3;
	}
	else if (x4 == 5)
	{
		t4 = 4;
	}	
	else if (x4 == 6)
	{
		t4 = 5;
	}
	else if (x4 == 1)
	{
		t4 = 6;
	}
	else if (x4 == 2)
	{
		t4 = 7;
	}
	else if (x4 == 3)
	{
		t4 = 8;
	}
	if (x5 == 7)
	{
		t5 = 0;
	}
	else if (x5 == 8)
	{
		t5 = 1;
	}
	else if (x5 == 9)
	{
		t5 = 2;
	}
	else if (x5 == 4)
	{
		t5 = 3;
	}
	else if (x5 == 5)
	{
		t5 = 4;
	}
	else if (x5 == 6)
	{
		t5 = 5;
	}
	else if (x5 == 1)
	{
		t5 = 6;
	}
	else if (x5 == 2)
	{
		t5 = 7;
	}
	else if (x5 == 3)
	{
		t5 = 8;
	}
	if (x6 == 7)
	{
		t6 = 0;
	}
	else if (x6 == 8)
	{
		t6 = 1;
	}
	else if (x6 == 9)
	{
		t6 = 2;
	}
	else if (x6 == 4)
	{
		t6 = 3;
	}
	else if (x6 == 5)
	{
		t6 = 4;
	}
	else if (x6 == 6)
	{
		t6 = 5;
	}
	else if (x6 == 1)
	{
		t6 = 6;
	}
	else if (x6 == 2)
	{
		t6 = 7;
	}
	else if (x6 == 3)
	{
		t6 = 8;
	}
	if (x7 == 7)
	{
		t7 = 0;
	}
	else if (x7 == 8)
	{
		t7 = 1;
	}
	else if (x7 == 9)
	{
		t7 = 2;
	}
	else if (x7 == 4)
	{
		t7 = 3;
	}
	else if (x7 == 5)
	{
		t7 = 4;
	}
	else if (x7 == 6)
	{
		t7 = 5;
	}
	else if (x7 == 1)
	{
		t7 = 6;
	}
	else if (x7 == 2)
	{
		t7 = 7;
	}
	else if (x7 == 3)
	{
		t7 = 8;
	}
	if (x8 == 7)
	{
		t8 = 0;
	}
	else if (x8 == 8)
	{
		t8 = 1;
	}
	else if (x8 == 9)
	{
		t8 = 2;
	}
	else if (x8 == 4)
	{
		t8 = 3;
	}
	else if (x8 == 5)
	{
		t8 = 4;
	}
	else if (x8 == 6)
	{
		t8 = 5;
	}
	else if (x8 == 1)
	{
		t8 = 6;
	}
	else if (x8 == 2)
	{
		t8 = 7;
	}
	else if (x8 == 3)
	{
		t8 = 8;
	}
	if (x9 == 7)
	{
		t9 = 0;
	}
	else if (x9 == 8)
	{
		t9 = 1;
	}
	else if (x9 == 9)
	{
		t9 = 2;
	}
	else if (x9 == 4)
	{
		t9 = 3;
	}
	else if (x9 == 5)
	{
		t9 = 4;
	}
	else if (x9 == 6)
	{
		t9 = 5;
	}
	else if (x9 == 1)
	{
		t9 = 6;
	}
	else if (x9 == 2)
	{
		t9 = 7;
	}
	else if (x9 == 3)
	{
		t9 = 8;
	}
	int bit[9] = {0};
	if (t1 != -1)
	{
		bit[t1] = 1;
	}
	if (t2 != -1)
	{
		bit[t2] = 2;
	}
	if (t3 != -1)
	{
		bit[t3] = 1;
	}
	if (t4 != -1)
	{
		bit[t4] = 2;
	}
	if (t5 != -1)
	{
		bit[t5] = 1;
	}
	if (t6 != -1)
	{
		bit[t6] = 2;
	}
	if (t7 != -1)
	{
		bit[t7] = 1;
	}
	if (t8 != -1)
	{
		bit[t8] = 2;
	}
	if (bit[t9] != 3)
	{
		bit[t9] = 1;
	}
	int i;
	int cnt = 0;
	for (i = 0; i < 9; i++)
	{
		if (bit[i] == 0)
		{
			printf(".  ");
			cnt++;
		}
		if (bit[i] == 1)
		{
			printf("X  ");
			cnt++;
		}
		if (bit[i] == 2)
		{
			printf("O  ");
			cnt++;
		}
		if (cnt % 3 == 0)
		{
			printf("\n");
		}
	}
}

12.這是對應getch()的一個函式判斷!因為getch回傳的是這是字符對應的ACILL值

int transform(int tran)
{
	int tot;
	if(tran==49)tot=1;
	if(tran==50)tot=2;
	if(tran==51)tot=3;
	if(tran==52)tot=4;
	if(tran==53)tot=5;
	if(tran==54)tot=6;
	if(tran==55)tot=7;
	if(tran==56)tot=8;
	if(tran==57)tot=9;
	return tot;
}

13.將前面12段代碼全部拼接即可運行!


總結整個程式寫下來,確實無腦,啰嗦,繁瑣,重復,我也確實用這一千多行的代碼打發時間,其實里面有很多模塊是可以以函式的形式簡化省略的,因為出現了大量的相同模塊的重復使用,但是作為一個初學c語言的大一學生而言,讓我感受到了這個接近底層,樸實無華的計算機語言的魅力,創造一個相同的物體有很多種方法,不論方法的好壞優劣,當你在解決一個又一個bug和error后,創造出一個達到你心中預期的東西時,這種喜悅感是不言而喻的!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/404210.html

標籤:其他

上一篇:Python新年快樂炫酷煙花秀代碼

下一篇:【游戲開發實戰】Unity逆向懷舊經典游戲《尋秦OL》,決議二進制影片檔案生成預設并播放(資源逆向 | 二進制 | C#)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more