Write a three-child chess game of the sentiment

Source: Internet
Author: User

Before writing, I have designed the structure of the function, mainly by the following several functions;

1: Initialize the Chess box

2: Players play chess

3: Computer Chess

4: Judging the outcome

5: Update the Chess box

6: Main function

In the process of writing encountered can not be placed under the player position in the Chess box, because the function is defined in the process of not introducing a two-dimensional array,

Cause the box update is not correct, when referencing a two-dimensional array in a function, because the C language compiler does not check the size of the first dimension, as long as the second dimension is the same size, the first dimension of the shape parameter group can be different from the argument, so it is only necessary to write int qk (Arr[][i]) in the function definition, It is only necessary to write QK (arr) when the main function is called, because the array name itself is the address, so it is equivalent to passing the address of arr[0][0] to the function, so the contents of the array can be changed directly in the function. The source code is as follows:

Three-son chess

#define _CRT_SECURE_NO_WORNINGS 1

#include <stdio.h>

#include <stdlib.h>

void qk (int i, int j,char arr[][3])

{

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

{

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

{

ARR[I][J] = ";

}

}

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

{

printf ("%c| %c| %c ", Arr[i][0], arr[i][1], arr[i][2]);

if (I < 2)

{

printf ("\n__|__|__\n");

}


}

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

}

void print (int i, Int J, Char Arr[][3])

{

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

{

printf ("%c| %c| %c ", Arr[i][0], arr[i][1], arr[i][2]);

if (I < 2)

{

printf ("\n__|__|__\n");

}


}

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

}

void Play (int i,int J,char arr[][3])

{

printf ("Please enter the coordinates of the pieces");

while (1)

{

int m = 0, n = 0;

scanf_s ("%d%d", &m, &n);

i = m-1;

j = n-1;

if ((arr[i][j]! = ' o ') && (arr[i][j]! = ' x '))

{

ARR[I][J] = ' o ';

Break

}

Else

printf ("Please re-enter");

}

}

int comp (int i,int J,char arr[][3])

{

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

{

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

{

if (arr[i][j]! = ' o ' &&arr[i][j]! = ' x ')

{

ARR[I][J] = ' x ';

return 0;

}

}

printf ("************************");

}

return 1;

}

int judge (int i, int j,char arr[][3])

{

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

{

if (arr[i][0] = = ' O ' &&arr[i][1] = = ' O ' && arr[i][2] = = ' O ')

{

printf ("You won \ nthe");

return 0;

}

if (arr[i][0] = = ' x ' &&arr[i][1] = = ' x ' && arr[i][2] = = ' x ')

{

printf ("You lost \ nthe");

return 0;

}

if (arr[0][i] = = ' O ' &&arr[1][i] = = ' O ' && arr[2][i] = = ' O ')

{

printf ("You won \ nthe");

return 0;

}

if (arr[0][i] = = ' x ' &&arr[1][i] = = ' x ' && arr[2][i] = = ' x ')

{

printf ("You lost \ nthe");

return 0;

}

}

if ((arr[0][0] = = ' O ' &&arr[1][1] = = ' O ' && arr[2][2] = = ' o ') | | (arr[0][2] = = ' O ' &&arr[1][1] && arr[2][0]))

{

printf ("You won \ nthe");

return 0;

}

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

{

printf ("You lost \ nthe");

return 0;

}

return 1;

}

int main ()

{

Char arr[3][3];

int i = 3, j = 3;

int ret;

QK (i, J, arr);//Initialize the Chess box

while (1)

{

Play (I, J, arr);

Print (I, J, arr);

Ret=judge (i, J, arr);

if (ret = = 0)

{

printf ("Game Over");

Break

}

Comp (i, J, arr);

Print (I, J, arr);

Ret=judge (i, J, arr);

if (ret = = 0)

{

printf ("Game Over");

Break

}

}

System ("pause");

return 0;

}

In a few days continue to write, today's level is limited, I hope the great God ask questions.

This article is from the "Qin-wang" blog, make sure to keep this source http://10810196.blog.51cto.com/10800196/1709661

Write a three-child chess game of the sentiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.