Home > Back-end >  Tic-tac-toe game in the calculation of weight don't understand, ask for help!
Tic-tac-toe game in the calculation of weight don't understand, ask for help!

Time:09-29

#include
#include
using namespace std;
Typedef char chess [10].//character array
Typedef int temparr [10].//integer array
Chess arr.//character array variables
Temparr BRR.//integer array variables
Int number, suc, n3, c3, n2, c2, n1, c1;
Void inarrdata chess (a)//initializes the board no.
{
A [1]='1'; A, [2]='2'. A, [3]='3'.
A [4]='4'; A [5]='5'; A [6]='6';
A [7]='7'; A [8]='8'. A [9]='9'.
}
Void the display (chess a)//output board state
{
CoutCout<& lt;" "& lt; Cout<& lt;" -- -- -- -- -- -- -- -- -- -- - "& lt; Cout<& lt;" "& lt; Cout<& lt;" -- -- -- -- -- -- -- -- -- -- - "& lt; Cout<& lt;" "& lt; Cout}
Int arrfull ()//the location of the judge is there to play chess
{
int i;
Int arrf=0;
for(i=1; i<=9; I++)
If (I==arr [I] - 48)//if the character arr [I] - 48 equals I
Arrf=1;//so arrf=1, i.e., can turn
Return arrf.
}
Void cn (int line)//judge state
{
The switch (line)
{
Case 0: c3=c3 + 1; break;//computer win
Case 1: n=(n + 1; break;
Case 2: c2=c2 + 1; break;//2 x
Case 3: n1 n1 +=1; break;
Case 4: c1=c1 + 1; break;//an x
Case 5: n3=n3 + 1; break;
}
}
Int linenum (char a, b char, char c)//judge state
{
Int ln=6;
If (a=='X') & amp; & (b=='X') & amp; & (c=='X'))
Ln=0;
If (((a=='O') & amp; & (b=='O') & amp; & (c! )='O') | | ((a=='O') & amp; & (b!='O') & amp; & (c=='O') | | ((a!='O') & amp; & (b=='O') & amp; & (c=='O')))
Ln=1;
If (((a=='X') & amp; & (b=='X') & amp; & (c!='X') | | ((a=='X') & amp; & (b!='X') & amp; & (c=='X') | | ((a!='X') & amp; & (b=='X') & amp; & (c=='X')))
Ln=2;
If (((a=='O') & amp; & (b!='O') & amp; & (c! )='O') | | ((a!='O') & amp; & (b=='O') & amp; & (c! )='O') | | ((a!='O') & amp; & (b!='O') & amp; & (c=='O')))
Ln=3;
If (((a=='X') & amp; & (b!='X') & amp; & (c!='x') | | ((a!='X') & amp; & (b=='X') & amp; & (c!='X') | | ((a!='X') & amp; & (b!='X') & amp; & (c=='X')))
Ln=4;
If (a=='O') & amp; & (b=='O') & amp; & (c=='O'))
Ln=5;
Return ln.
}
Int maxbrr (int * br)//judge the power value
{
Int temp, I, MB;
Temp=888;
for(i=1; i<=9; I++)
{
If (temp<=br [I])
{
Temp=br [I];
MB=I;
}
}
Return MB;
}
Void manstep ()//turn processing module is
{
int j;
The display (arr);
If (arrfull ())//if the board and the location of the chess, people move
{
Cout<& lt;" Which one would you like to go? Please enter a number (1-9), ";
Cin> j;
While ((j<1) | | (j> 9) | | (j! [j] - 48)=arr)
{
Cout<& lt;" Sorry, you input the wrong number, please input again (1-9) : ";
Cin> j;
}
Arr [j]='O';
N3=0; C3=0; N2=0; C2=0; N1=0; C1=0;
Number=linenum (arr [1], arr [2], arr [3]). Cn (number);
Number=linenum (arr [4], arr [5], arr [6]); Cn (number);
Number=linenum (arr [7], arr [8], arr [9]); Cn (number);
Number=linenum (arr [1], arr [4], arr [7]); Cn (number);
Number=linenum (arr [2], arr [5], arr [8]). Cn (number);
Number=linenum (arr [3], arr [6], arr [9]); Cn (number);
Number=linenum (arr [1], arr [5], arr [9]); Cn (number);
Number=linenum (arr [3], arr [5], arr [7]); Cn (number);
If (n3!=0)//you win
{
The display (arr);
CoutCout<& lt;" Congratulations you win!!!!!! "& lt; Suc=0;
}
}
}
Void computerstep ()/moves/computer processing module
{
int i;
If (arrfull ())//if the board and the location of the chess, then the computer's turn
{
for(i=1; i<=9; I++)//is calculated for each step can go
{
If (I==arr [I] - 48)
{
C3=0; N2=0; C2=0; N1=0; C1=0;
Arr=[I] 'X';
Number=linenum (arr [1], arr [2], arr [3]). Cn (number);
Number=linenum (arr [4], arr [5], arr [6]); Cn (number);
Number=linenum (arr [7], arr [8], arr [9]); Cn (number);
Number=linenum (arr [1], arr [4], arr [7]); Cn (number);
Number=linenum (arr [2], arr [5], arr [8]). Cn (number);
Number=linenum (arr [3], arr [6], arr [9]); Cn (number);
Number=linenum (arr [1], arr [5], arr [9]); Cn (number);
Number=linenum (arr [3], arr [5], arr [7]); Cn (number);
BRR [I]=(c3-128 * 63 * 31 15 * * c2 - n1 + n2 + 7 * c1);//step calculate the weights of
Arr=I + 48 [I];
}
The else
BRR [I]=999;
}
Arr [maxbrr (BRR)]='X';//determine which step computer go, weight the biggest step
C3=0; N2=0; C2=0; N1=0; C1=0;
Number=linenum (arr [1], arr [2], arr [3]). Cn (number);
Number=linenum (arr [4], arr [5], arr [6]); Cn (number);
Number=linenum (arr [7], arr [8], arr [9]); Cn (number);
Number=linenum (arr [1], arr [4], arr [7]); Cn (number);
Number=linenum (arr [2], arr [5], arr [8]). Cn (number);
Number=linenum (arr [3], arr [6], arr [9]); Cn (number);
Number=linenum (arr [1], arr [5], arr [9]); Cn (number);
Number=linenum (arr [3], arr [5], arr [7]); Cn (number);
If (c3!=0)//computer has won
{
The display (arr);
CoutCout<& lt;" Computer won!!!!!! "& lt; Suc=0;
}
}
The else
Suc=0;
}
Int main ()
{
Cout
  • Related