Home > Back-end >  The novice consult
The novice consult

Time:10-24

# include
using namespace std;
Void init (char b [20] [20]);//initialize the chessboard
Void the draw (char c [20] [20]);//designated board
Int test (char d [20] [20], char);//determine whether chess end
Bool check (int, int);//check if player input coordinate legal
Int main ()//set the board size, you can set a variable x, and y
{
Char a [20] [20].//initialization data
Init (a);
For (int x=0; x<400;)//the board can take down a total of x * y pieces
{
int i,j;
If (x % 2==0)
{
cout<" Please party A way: ";
cin> i;
cin> j;
While (a [I] [j]=='X' | | a [I] [j]=='O')//whether the input position have pieces
{
coutcout<" There are already pieces here! Please walk again: ";
cin> i;
cin> j;
}
If (check (I, j)==false)
{
coutcout<" Input coordinates illegal! Please input again: ";
cin> i;
cin> j;
}
A [I] [j]='X';//the specified location on pieces
The draw (a);
If (test (a, 'X'))//determine whether win (a)
{
Char replay.
cout<" A party victory! Whether another game? Y/N: ";
cin> Replay.
If (replay=='Y' | | replay=='Y')
{
The main ();
} the else
{
break;
}

}
X +=1;
} else {
cout<" Please party B way: ";
cin> i;
cin> j;
While (a [I] [j]=='X' | | a [I] [j]=='O')
{
coutcout<" There are already pieces here! Please walk again: ";
cin> i;
cin> j;
}
If (check (I, j)==false)
{
coutcout<" Input coordinates illegal! Please input again: ";
cin> i;
cin> j;
}
A [I] [j]='O'.
The draw (a);
If (test (a, 'O'))//determine whether win (B)
{
Char replay.
cout<" Party B victory! Whether another game? Y/N: ";
cin> Replay.
If (replay=='Y' | | replay=='Y')
{
The main ();
} the else
{
break;
}

}
X +=1;
}
}
return 0;
}

Void init (char [b] [20])
{
for(int i=0; i<20; I++)
{
for(int j=0; j<20; J++)
{
B [I] [j]='|';
cout<" - ";
cout}
cout}
}

Int test (char [d] [20], char f)
{
Int mark=0;
for(int i=0; i<15. I++)
{
for(int j=0; j<15. J++)
{
If ((f==d [I] [j] & amp; & F==d + 1] [I [j] & amp; & [I f==d + 2] [j] & amp; & F==d + 3 [I] [j] & amp; & F==d [I + 4] [j])
| | (f==d [I] [j] & amp; & F==d [I] [m + 1] & amp; & F==d [I] [m + 2] & amp; & F==d + 3 [j] [I] & amp; & F==d + 4 [j] [I])
| | (f==d [I] [j] & amp; & F==d + 1] [I [m + 1] & amp; & F==d/I + 2 + 2 [j] & amp; & F==d/I + 3, + 3 [j] & amp; & F==d + 4 [j] [I + 4]))//sideways, number, Angle of the three established
{
coutMark=1;
}
}
}
Return mark;
}
Void the draw (char [c] [20])
{
for(int i=0; i<20; I++)
{
for(int j=0; j<20; J++)
{
cout<" - ";
cout}
cout}
}
Bool check (int x, int y)
{
if(x> 20 | | y> 20 | | x<0 | | y<0)
return false;
The else
return true;
}
Teachers can give explain
If (test (a, 'X'))//determine whether win (a)

Int test (char [d] [20], char f)
{
Int mark=0;
for(int i=0; i<15. I++)
{
for(int j=0; j<15. J++)
{
If ((f==d [I] [j] & amp; & F==d + 1] [I [j] & amp; & [I f==d + 2] [j] & amp; & F==d + 3 [I] [j] & amp; & F==d [I + 4] [j])
| | (f==d [I] [j] & amp; & F==d [I] [m + 1] & amp; & F==d [I] [m + 2] & amp; & F==d + 3 [j] [I] & amp; & F==d + 4 [j] [I])
| | (f==d [I] [j] & amp; & F==d + 1] [I [m + 1] & amp; & F==d/I + 2 + 2 [j] & amp; & F==d/I + 3, + 3 [j] & amp; & F==d + 4 [j] [I + 4]))//sideways, number, Angle of the three established
{
coutMark=1;
}
}
}
Return mark;
}
Why did you return is mark? Return is how to determine after the execution of the if statement? The novice consult

CodePudding user response:

Cross-examine: why do you want to define mark???

CodePudding user response:

Mark is a sign of 0 said there is no win situation (three mongols, etc.), 1 win, said the test function is used to determine whether the board game,
In fact this is a typical problems in software engineering, the function name and variable names are optional, let people scratching their head, if so write this function estimates will be more easy to understand:
 
Bool IsWin (... ) {
Bool bIsWin=false;
If (... ) {
BIsWin=true;
.
}

Return bIsWin;
}

CodePudding user response:

Mark used to return to your results

CodePudding user response:

Mark used to judge the value of the victory or failure, the feeling is int test (char [] [20] d, char f) defined as bool types such as easy to understand

CodePudding user response:

You get the code?
  • Related