C ++ learning footprint-(DOS only) wuziqi

Source: Internet
Author: User

The source code is as follows:

---------------------------------------------------------

# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Class chess
{
Public:
Chess ();
Void setstep (bool & ipjudge); // This function is used to take turns playing chess for two players.
Void setstepc (bool & ipjudge); // the man-machine playing chess Function
Void coutchess (); // outputs the chessboard
Void coutpw (); // output weight table
Bool getturn () {flag =! Flag; return flag;} // arms
Void flushchess (); // refresh the Board information function
Void judgewin (); // determines whether to win the game.
Void winer (); // winner output function
Int getans () {return result;} // return result (winner's judgment)
Static int count; // The number of moves.
PRIVATE:
Bool flag; // determines the variable in turn
Int PW [16] [16], tpw [4]; // weight variable, maximum weight variable
Int result, num [2]; // result (winner judgment). The player enters the coordinate of the pawn to judge
Char input [2], temp [2]; // gamer input data, converting temporary data
Char cboard [16] [16]; // Board Data
Int judgeawin (int A, int B); // determines whether a is the Winner Function
Int judgebwin (int A, int B); // determines whether B is the Winner Function
Void csetstep (); // The computer playing chess Function
Void setpower (); // initialize the Weight Function
Int adddepth (INT depth); // fill in the Weight Function
Void judgepw (int x, int y, int direction, int depth, char test); // determine the [x coordinate, Y coordinate, direction (clockwise 0-none, 1-left, 2-top left, 3-top, 4-top right), depth (depth), identifier (a/B)]
Void getfinalpw (); // weight judgment Function
};

Int chess: Count = 0;

Void vscomputer (); // Man-Machine combat
Void vsplayer (); // battle for two

 

Int main ()
{
Int choose;
Chess newp;
Do
{
Choose = 0;
System ("CLS ");
Cout <"Happy wuziqi" <Endl;
Cout <"select:" <Endl;
Cout <"1: Man-Machine combat mode" <Endl;
Cout <"2: Dual combat mode" <Endl;
Cout <"3: withdraw from the game" <Endl;
Cout <"****************" <Endl;
Cout <"****************" <Endl;
Cout <"Enter your choice :";
Cin> choose;
If (choose = 2)
Vsplayer ();
Else if (choose = 1)
Vscomputer ();
Else if (choose = 3)
Exit (0 );
Else
{
Cout <"input error. Please enter it again! "<Endl;
System ("pause ");
}
} While (choose! = 3 );
Return 0;
}

 

 

Void vscomputer ()
{
Bool ipjudge;
Chess newp;
Do
{
Newp. coutchess ();
Newp. setstepc (ipjudge); // Man-Machine combat Function
If (! Ipjudge)
Continue;
If (! Newp. getturn ())
Newp. flushchess ();
Newp. coutchess ();
Newp. judgewin ();
Chess: Count ++;
} While (newp. getans () = 0 & chess: Count <256 );
Newp. winer ();
}

Void chess: setstepc (bool & ipjudge)
{
Int I;
If (FLAG)
{
Cout <"Go chess :";
Cin> input;
For (I = 0; I <= 1; I ++)
If (input [I] <'0' | (input [I]> '9' & input [I] <'O ') | (input [I]> 'F' & input [I] <'O') | input [I]> 'F ')
{
Ipjudge = false;
Cout <"the input is out of bounds. Please enter it again! ";
System ("pause ");
Break;
}
}
Else
Csetstep (); // It's the PC's turn to play
}

Void chess: csetstep ()
{
Int I, j, depth = 0;
Setpower ();
For (I = 0; I <16; I ++)
{
For (j = 0; j <16; j ++)
{
If (cboard [I] [J] = '+') // optimization: exclude the situation where the surrounding areas are all +
{
If (cboard [I-1] [J] = 'O' | cboard [I-1] [J-1] = 'O' | cboard [I] [J-1] =' o' | cboard [I + 1] [J-1] = 'O' | cboard [I + 1] [J] = 'O' | cboard [I + 1] [J + 1] = 'O' | cboard [I] [J + 1] = 'O' | cboard [I-1] [J + 1] = = 'O ')
{
Judgepw (I, j, 0, depth, 'O ');
Judgepw (I, j, 0, depth, 'x ');
}
Else if (cboard [I-1] [J] = 'X' | cboard [I-1] [J-1] = 'X' | cboard [I] [J-1] = 'X' | cboard [I + 1] [J-1] = 'X' | cboard [I + 1] [J] = 'X' | cboard [I + 1] [J + 1] = 'X' | cboard [I] [J + 1] = 'X' | cboard [I-1] [J + 1] = 'X ')
{
Judgepw (I, j, 0, depth, 'O ');
Judgepw (I, j, 0, depth, 'x ');
}
}
}
}
Getfinalpw ();
// Coutpw ();
// System ("pause ");
If (tpw [0]> 0)
Cboard [tpw [1] [tpw [2] = 'X ';
/* Else if (tpw [0]> 0 & tpw [3]> 1)
{
For (I = 0; I <16; I ++)
{
For (j = 0; j <16; j ++)
{
If (tpw [0] = pw [I] [J])
If ()
}
}
}*/
Else
{
Cout <"Incorrect weight function! ";
System ("pause ");
Exit (1 );
}
}

 

Void chess: judgepw (int x, int y, int direction, int depth, char test)
{
If (depth> = 0 & depth <4)
{
If (Direction = 1) // left
{
If (cboard [x-depth-1] [Y] = test)
Judgepw (X, Y, 1, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );
}
Else if (Direction = 2) // upper left corner
{
If (cboard [x-depth-1] [y-depth-1] = test)
Judgepw (X, Y, 2, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );

}
Else if (Direction = 3) // forward
{
If (cboard [x] [y-depth-1] = test)
Judgepw (X, Y, 3, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );

}
Else if (Direction = 4) // upper right corner
{
If (cboard [x + depth + 1] [y-depth-1] = test)
Judgepw (X, Y, 4, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );

}
Else if (Direction = 5) // right side
{
If (cboard [x + depth + 1] [Y] = test)
Judgepw (X, Y, 5, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );

}
Else if (Direction = 6) // lower right
{
If (cboard [x + depth + 1] [Y + depth + 1] = test)
Judgepw (X, Y, 6, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );
}
Else if (Direction = 7) // bottom
{
If (cboard [x] [Y + depth + 1] = test)
Judgepw (X, Y, 7, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );
}
Else if (Direction = 8) // lower left
{
If (cboard [x-depth-1] [Y + depth + 1] = test)
Judgepw (X, Y, 6, depth + 1, test );
Else
PW [x] [Y] + = adddepth (depth );
}
Else if (Direction = 0)
{
If (cboard [x-depth-1] [Y] = test) // left side
Judgepw (X, Y, 1, depth + 1, test );
If (cboard [x-depth-1] [y-depth-1] = test) // top left
Judgepw (X, Y, 2, depth + 1, test );
If (cboard [x] [y-depth-1] = test) // forward
Judgepw (X, Y, 3, depth + 1, test );
If (cboard [x + depth + 1] [y-depth-1] = test) // upper right corner
Judgepw (X, Y, 4, depth + 1, test );
If (cboard [x + depth + 1] [Y] = test) // right side
Judgepw (X, Y, 5, depth + 1, test );
If (cboard [x + depth + 1] [Y + depth + 1] = test) // lower right
Judgepw (X, Y, 6, depth + 1, test );
If (cboard [x] [Y + depth + 1] = test) // bottom
Judgepw (X, Y, 7, depth + 1, test );
If (cboard [x-depth-1] [Y + depth + 1] = test) // bottom left
Judgepw (X, Y, 6, depth + 1, test );
}
}
Else if (depth = 4)
PW [x] [Y] + = adddepth (depth );
Else
{
Cout <"depth function error! ";
System ("pause ");
Exit (1 );
}
}

 

Int chess: adddepth (INT depth)
{
Switch (depth)
{
Case 0: Return 0; break;
Case 1: return 1; break;
Case 2: return 2; break;
Case 3: return 4; break;
Case 4: return 6; break;
Default:
{
Cout <"depth function error! ";
System ("pause ");
Exit (1 );
}
}
}

 

Void chess: getfinalpw ()
{
Int I, J;
For (I = 0; I <= 15; I ++)
For (j = 0; j <= 15; J ++)
{
If (PW [I] [J]> tpw [0])
{
Tpw [0] = pw [I] [J];
Tpw [1] = I;
Tpw [2] = J;
Tpw [3] = 1;
}
Else if (PW [I] [J] = tpw [0] & PW [I] [J]! = 0)
Tpw [3] + = 1;
}
}

 

//////////////////////////////////////// ////////////////////////////////
// Double match
//////////////////////////////////////// ////////////////////////////////

Void vsplayer ()
{
Bool ipjudge;
Chess newp;
Do
{
Newp. coutchess ();
Newp. setstep (ipjudge );
If (! Ipjudge)
Continue;
Newp. getturn ();
Newp. flushchess ();
Newp. coutchess ();
Newp. judgewin ();
Chess: Count ++;
} While (newp. getans () = 0 & chess: Count <256 );
Newp. winer ();
}

Void chess: winer ()
{
If (chess: Count = 256)
{
Cout <"| draw! This Council is over! "<Endl;
System ("pause ");
Exit (1 );
}
If (result = 1)
Cout <"| congratulations! Player A wins this council! "<Endl;
Else if (result = 2)
Cout <"| congratulations! Chess Player B wins this game! "<Endl;
System ("pause ");
}

 

// Default constructor
Chess: Chess ()
{
Int I, J;
Count = 0;
Result = 0;
Flag = true;
For (I = 0; I <= 15; I ++)
{
If (I <10)
Cboard [I] [0] = I + 48;
Else
Cboard [I] [0] = I + 55;
}
For (I = 0; I <= 15; I ++)
For (j = 0; j <= 15; J ++)
Cboard [I] [J] = '+ ';
}

// Fill in the Weight Function
Void chess: setpower ()
{
Int I, J;
For (I = 0; I <= 15; I ++)
For (j = 0; j <= 15; J ++)
PW [I] [J] = 0;
Tpw [0] = 0;
Tpw [1] = 0;
Tpw [2] = 0;
Tpw [3] = 0;
}

// Output the checkerboard Function
Void chess: coutchess ()
{
Int I, J;
System ("CLS ");
Cout <"Happy wuziqi" <Endl;
Cout <"0 1 2 3 4 5 6 7 8 9 a B C D E F" <Endl;
For (I = 0; I <= 15; I ++)
{
If (I> = 0 & I <10)
Cout <I <'';
Else if (I> = 10)
Cout <static_cast <char> (I + 55) <'';
Else
Cout <"An error occurred while outputting the sequence number of the Board column! ";
For (j = 0; j <= 15; J ++)
Cout <cboard [I] [J] <'';
Cout <Endl;
}
Cout <Endl;
}

// Two-player combat, chess player playing in turn Function
Void chess: setstep (bool & ipjudge)
{
If (FLAG)
Cout <"Player :";
Else
Cout <"Player B :";
Cin> input;
For (INT I = 0; I <= 1; I ++)
{
If (input [I] <'0' | (input [I]> '9' & input [I] <'O ') | (input [I]> 'F' & input [I] <'O') | input [I]> 'F ')
{
Ipjudge = false;
Cout <"the input is out of bounds. Please enter it again! ";
System ("pause ");
Break;
}
}
}
// Refresh the Board
Void chess: flushchess ()
{
Int I;
Temp [0] = input [0];
Temp [1] = input [1];
For (I = 0; I <= 1; I ++)
{
If (temp [I]> = '0' & temp [I] <= '9 ')
Num [I] = static_cast <int> (temp [I]-48 );
Else if (temp [I]> = 'O' & temp [I] <= 'F ')
Num [I] = static_cast <int> (temp [I]-55 );
Else if (temp [I]> = 'O' & temp [I] <= 'F ')
Num [I] = static_cast <int> (temp [I]-87 );
Else
{
Cout <"Unknown error 1 entered by the user. Please enter it again! ";
System ("pause ");
Exit (1 );
}
}
If (cboard [num [0] [num [1] = '+ '&&! Flag)
Cboard [num [0] [num [1] = 'O ';
Else if (cboard [num [0] [num [1] = '+' & flag)
Cboard [num [0] [num [1] = 'X ';
Else
{
Flag =! Flag;
Cout <"user input error. Please try again! ";
System ("pause ");
}
}

 

 

// Determine the new winning Algorithm
Void chess: judgewin ()
{
Int I = 0, J;
Do
{
J = 0;
Do
{
If (cboard [I] [J] = 'O ')
Result = judgeawin (I, j );
Else if (cboard [I] [J] = 'X ')
Result = judgebwin (I, j );
Else if (cboard [I] [J] = '+ ');
Else
{
Cout <" [" <I <"] [" <j <"] Location Information error! "<Endl;
System ("pause ");
Exit (1 );
}
If (result = 1 | result = 2)
Break;
J ++;
} While (j <= 15 );
If (result = 1 | result = 2)
Break;
I ++;
} While (I <= 15 );
}

 

// Checks the chess pieces of Player
Int chess: judgeawin (int A, int B)
{
If (cboard [a] [b-1] = 'O' & cboard [a] [B-2] = 'O' & cboard [a] [B + 1] = = 'O' & cboard [a] [B + 2] = 'O ') // horizontal five child
Return 1;
Else if (cboard [A + 1] [B] = 'O' & cboard [A + 2] [B] = 'O' & cboard [A-1] [b] = 'O' & cboard [A-2] [B] = 'O ') // vertically
Return 1;
Else if (cboard [A + 1] [B + 1] = 'O' & cboard [A + 2] [B + 2] = 'O' & cboard [A-1] [b-1] = 'O' & cboard [A-2] [B-2] = 'O ') // top left and bottom right
{
Return 1;
}
Else if (cboard [A + 1] [b-1] = 'O' & cboard [A + 2] [B-2] = 'O' & cboard [A-1] [B + 1] = 'O' & cboard [A-2] [B + 2] = 'O ') // top five in the lower left and upper right
Return 1;
Else
Return 0;
}

// Checks the chess pieces of Player B
Int chess: judgebwin (int A, int B)
{
If (cboard [a] [b-1] = 'X' & cboard [a] [B-2] = 'X' & cboard [a] [B + 1] = = 'X' & cboard [a] [B + 2] = 'X ') // horizontal five child
Return 2;
Else if (cboard [A + 1] [B] = 'X' & cboard [A + 2] [B] = 'X' & cboard [A-1] [b] = 'X' & cboard [A-2] [B] = 'X ') // vertically
Return 2;
Else if (cboard [A + 1] [B + 1] = 'X' & cboard [A + 2] [B + 2] = 'X' & cboard [A-1] [b-1] = 'X' & cboard [A-2] [B-2] = 'X ') // top left and bottom right
Return 2;
Else if (cboard [A + 1] [b-1] = 'X' & cboard [A + 2] [B-2] = 'X' & cboard [A-1] [B + 1] = 'X' & cboard [A-2] [B + 2] = 'X ') // top five in the lower left and upper right
Return 2;
Else
Return 0;
}

 

// Output weight table
Void chess: coutpw ()
{
Int I, J;
System ("CLS ");
Cout <"Happy wuziqi (weight table)" <Endl;
Cout <"0 1 2 3 4 5 6 7 8 9 a B C D E F" <Endl;
For (I = 0; I <= 15; I ++)
{
If (I> = 0 & I <10)
Cout <I <'';
Else if (I> = 10)
Cout <static_cast <char> (I + 55) <'';
Else
Cout <"An error occurred while outputting the sequence number of the Board column! ";
For (j = 0; j <= 15; J ++)
Cout <PW [I] [J] <'';
Cout <Endl;
}
Cout <Endl;
}

 

 

 

// During the summer vacation, it's boring to look at the things produced by C ++. I don't know the data structure, the algorithms, and so on ...... I just want to leave something for myself and I will remember it later. Please let me see the prawns .......

This program has poor portability, and the program AI is super stupid. It will be suspended for a period of time and remains unsolved ......

Related Article

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.