Brainless writing of tic tac toe chess game in c language

Posted by BAM1979 on Wed, 05 Jan 2022 14:29:23 +0100

At the end of freshman year, I had nothing to do, so I had a whim about whether I could create a simple symbolic game without char character type. Also because I am not a computer major, one of the c language lab assignments in the first semester was to let us play tic tac toe chess by ourselves. At that time, many functions were used for splicing, and char character type was also used, so it was not difficult to make Sanzi game. So in order to pass the time, I wrote a complete tic tac toe chess of int type.

The result of this program is that the player must lose or draw, and the player will never win the computer

*Splice the codes in the following text in order to run!

1. These are header files #include < conio h> Is to call the subsequent getch () function
#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. Start the initialization of the main function and variables! A large number of arrays are used in the main function, which is mainly used for recording and subsequent judgment

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 goes first and computer goes second, where a [] array is used to record the number corresponding to the position each time

Each point you want to print corresponds to 123456789 of the keyboard, which obviously does not correspond to the normal array printing order. Therefore, to do this step, you need to change the corresponding number in the function later!

 
	//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. This code is just to use count [] and b [] arrays to record the chess positions of player and computer respectively, and a [] is to record the whole. This is only for subsequent inspection and AI can selectively block players

	//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. Print out the image of the chess pieces played by the player and present it on the computer screen! Where there is no brain, it uses a function containing nine parameters, in fact, corresponds to the most of the nine pieces (also can be said to be nine figures), and then call into function, print the chessboard in array order. Such a practice is very brainless. It is conceivable that it can not be applied in Gobang. The advantage of this is to record the position of each walk, so that when the next new digital input, it can ensure that the previous chess pieces are also in the newly printed chessboard!

	//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. This is a check module. The purpose is to judge whether one party wins. The whole function is contained in a large while loop. Once a win occurs, it will jump out of the loop, otherwise it will continue to cycle. Therefore, the efficiency of this program is very low, so it is also why it is written in a mindless way. (extremely inefficient. It may take five or six seconds to wait for the computer while the game is running)

//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. A clear screen system("cls") is used here to have a better sense of experience when the program is executed. The previously used getch() function can directly read the keyboard, which is also for a better sense of experience of the game! After that, computer plays chess, generates random numbers, and then filters whether there are existing numbers in the array, so as to achieve the goal.

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. This large piece of code is that I have nothing to do in my spare time and cite all the situations, instead of letting the computer randomly generate numbers that have not appeared, otherwise the computer will be very retarded!! So in the game, even if the player takes the lead, it is only a draw at most!

//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. As before, the presentation module after computer chess!

	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. Computer judgment module

//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 . Determination of final situation after large cycle

	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. Main content of drawing Function: so many if in the front is to correspond to the conversion of numeric keyboard and chessboard position on the computer

The last side is the source of printing the chessboard

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. This is a function judgment corresponding to getch()! Because getch returns the ACILL value corresponding to this character

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. Splice all the previous 12 codes to run!

Conclusion: the whole program is written down. It is really mindless, wordy, cumbersome and repetitive. I do use these more than 1000 lines of code to kill time. In fact, many modules can be simplified and omitted in the form of functions, because a large number of the same modules are reused. But as a freshman learning c language, I feel the charm of this simple computer language close to the bottom. There are many ways to create the same object. No matter how good or bad the methods are, when you solve one bug after another and create something that meets your expectations, this sense of joy is self-evident!

Topics: C Game Development