#include "stdafx.h" #include "gloab.h"
// definition point struct point {INT X; int y;};
/ / Define the chess pieces nodes, used to registrate chess struct; // chess pieces of chessboard position int status; // 下子 0 is computer, 1 is chess, 2 is blank int WIN [2] [252]; / / Used to store the number of victory combinations of current nodes};
Class Chess {private: // Defines the status of chess pieces on the chessboard // const Int Black = 0; // Black for computer // const Int White = 1; // White chess // const Int blank = 2; //blank
/ / Define chess and computer's chessboard for storage of Bool Playerboard [11] [11] [252]; BOOL Computerboard [11] [11] [252];
// The score of the computer and players in each point is Int Computergrade [11] [11], PLAYERGRADE [11] [11];
// Calculate the computer or player in each combination occupies a few pieces // 0 represent the computer, 1 represents the player Int Win [2] [252];
/ / Define whether it is the first Chess Bool Start;
// Record the player of the computer and chess hand int computercount; int playercount;
// to convert BOOL Player, Computer;
// Used to store the status, position and princes of all chess pieces, used for regrets only use chessnode allchess [122];
// Whether the game ends BOOL GAMEOVER;
// Winner 0 for the computer 1 for the player 2 for the game, the game is not end -1 is the flat int winner; / ************* Function ********************** *************** /
// Put the chessboard to blank int initboard (); // initialize the score void initialmark (); // Initialmark (); // Record the status of the next chess pieces Void RecordChesstatus (int horizontal, int value, INT Player); // Calculates a number of chess pieces that have been occupied by some victory in the number of win [2] [252], the parameters are horizontal, vertical position, and finally record the falling lace is computer. Still player int compute (int.comontal, int value);
Public: / / Define Chessboard INT Board [11] [11]; // Constructor CHESS (); // Computer By calculating the chess position int computerplay (); // Player in the chessboard in the board in the board Int PlayerplayChes (int Horizontal) INT VERTICAL); // Remarks Curse, can only make regrets invasionChess (); // determine if the BOOL GAMEOVER (Int Player) has been played; // Get the winner 0 for the computer 1 for the player 2 The Chess Bureau has not ended -1 is a flat int getwinner ();
/ ************ Function function ********************************** /
/ / Initure the chessboard to blank int check :: initboard () {int i = 0; // initialize the chessboard into space for (i = 0; i <11; i ) {for (int J = 0; J <11 ; J ) Board [i] [j] = 2;} // Initialized chess game for (i = 0; i <122; i ) {Allchess [i] .point.x = -1; allchess [i] .point. y = -1; allchess [i] .status = 2;} computerCount = playercount = 0; GameOver = false; Winner = 2; return 0;} // Initialmark () {for (int i = 0; I <11; i ) for (int J = 0; j <11; j ) {computergrade [i] [j] = 0; PlayerGrade [i] [j] = 0;}} // Initialization win Multiple INT CHESS :: I = 0, J = 0, K = 0, NUM = 0; // Initialization Horizontal Direction Winning Composite Multiple FOR (i = 0; I <11; i ) { For (int J = 0; j <7; j ) {for (k = 0; k <5; k ) {Playerboard [i] [j k] [Num] = true; computerboard [i] [j k ] [Num] = true;} Num ;}} // Initialize the number of winning combined combined portions in the vertical direction (j = 0; j <11; j ) {for (int i = 0; i <7; i ) {for ( K = 0; K <5; K ) {Playerboard [i k] [J] [NUM] = True; ComputerBoard [i k] [j] [num] = true;} Num ;}}
// Initialization Positive diagonal direction Winning combination for (i = 0; i <7; i ) {for (j = 0; j <7; j ) {for (k = 0; k <5; k ) {Playerboard [i k] [j k] [NUM] = true; Computerboard [i k] [j k] [Num] = true;} Num ;}} // Initializing the opposition direction Winning combination For (i = 0; i <7; i ) for (j = 10; j> = 4; j -) {for (k = 0; k <5; k ) {Playerboard [i k] [J K] [ Num] = true; ComputerBoard [i k] [j k] [NUM] = true;} Num ;} Num = 0;}
// Record the status of the chess pie :: Recordchessstatus (int horizontal, int value) {// The storage location is the same as the number of steps of the play, that is, from 1 to 121, Allchess [(PlayerCount Computercount)]. Point.x = horizontal; allchess [(playercount computercount)]. Point.y = value; Allchess [(PlayerCount ComputerCount)]. status = player; // Record the winning group of the current node for (int I = 0; i <2; i ) for (int J = 0; J <252; J ) Allchess [(PlayerCount ComputerCount)]. Win [i] [j] = win [i] [j]; return; } // Calculate the number of chess pieces that have been occupied by some party in a victory in the array of WIN [2] [252], the parameters are horizontal, vertical position, and finally record the falling lace is computer or chess INT CHESS :: Compute (int.com) {// If the position of the chess pie is not in the 11 * 11 range, return error if ((horizontal> = 11) || (Vertical> = 11) || Horizontal <0) || (Vertical <0)) RETURN-1; for (int i = 0; i <252; i ) {// If the fallman is a chess hand IF (Player == 1) {IF (PlayerBoard [ Horizontal] [Vertical] [I] && (WIN [1] [i]! = -1)) {// Statistics This combination has taken a few children, if it is -1 indicates that there is a combination that has been occupied by the opponent It is impossible to win WIN [1] [i] ;} if (computerboard [horizontal] [vertical] [i]) {// Because of the chess It has fallen under this combination, so the computer is not possible to pass this combination of victory Computerboard [horizontal] [vertical] [i] = false; win [0] [i] = -1;}} // If the fall is computer Else IF (Player == 0) {if (Computerboard [horizontal] [vertical] [i] && (win [0] [i]! = -1)) {// Statistics This combination has taken a few children, if it is -1 indicates that there is an opponent to occupy the opponent, it is impossible to win Win [0] [i] ;} if (Playerboard [horizontal] [vertical] [i]) {// Because the player is already here The computer is not possible in combination, so the computer cannot pass this combination. Playerboard [horizontal] [vertical] [i] = false; win [1] [i] = -1;}}}}}} Return 1;} // Construction function chess: : chess () {inn (); initwincompages ();
Initialmark (); start = true; player = true; computer = false;} // computer By calculating the chess position int check :: computerplay () {if (computer) {INT i = 0, J = 0, K = 0; // Used to store the highest score of computer and chess hands in the chessboard int playermax = 0, computermax = 0; int PX = -1, py = -1, cx = -1, cy = -1; // Speaking of the last determined chess position INT x, y; // If the first step for the computer, it is preferentially placed in the central position IF (Start) of the chessboard {if (Board [5] [5] == 2) {X = 5; y = 5;} else {x = 5; y = 4;} Board [x] [y] = 0; Compute (x, y, 0); ComputerCount ; // Record the status of the chess pieces Recordchesstatus (x, y, 0); Computer = false; player = true; start = false; return 1;}
// Calculate the chess board score for (i = 0; i <11; i ) for (j = 0; j <11; j ) {// If this location scores IF on this location IF (Board [i] [j] == 2) {for (int K = 0; k <252; k ) {/ / determine which win in this position is (Playerboard [i] [j] [k] ) {Switch (win [1] [k]) {// By calculating a combination of five sub-individuals, 1 child five, 2 sub 50, 3 sub 500, 4 sub 5000 case 1: PlayERGRADE [i ] [j] = playergrade [i] [j] 5; break; cas 2: playergrade [i] [j] = playergrade [i] [j] 50; break; case 3: playergrade [i] [j] = Playergrade [i] [j] 500; Break; case 4: playergrade [i] [j] = playergrade [i] [j] 5000; break;}}}} // If the current location is greater than the highest found before Position, save the current location information IF (PlayerGrade [i] [j]> playermax) {Playermax = Playergrade [i] [j]; px = i; py = j;}}
/ / Calculate the score of the computer in the board position for (i = 0; i <11; i ) for (j = 0; j <11; j ) {// If this location scores the location IF (Board) [i] [j] == 2) {for (int K = 0; k <252; k ) {/ / determine which win in this position is (ComputerBoard [i] [j] [k] ) {Switch (win [0] [k]) {// By calculating the number of children with a calculation combination, a child is scored, 1 child five points, 2 sub 50, 3 sub 500, 4 sub 5000 // Let the computer are equivalent Under the conditions, it is more important than the attack. If you focus on the defensive synonym, Case 1: Computergrade [i] [j] = computergrade [i] [j] 6; break; casse 2: computergrade [ I] [j] = computergrade [i] [j] 51; break; cas 3: computergrade [i] [j] = computergrade [i] [j] 501; break; case 4: computergrade [i] [j ] = computergrade [i] [j] 5001; Break;}}}} // If the current location is greater than the highest split position previously found, the current location information IF (ComputerGrade [i] [j]> computermax) {computermax = Computergrade [i] [j]; cx = I; cy = j;}} // If the highest score of the computer is larger than the chess hand, the computer attack IF (Computermax> Playermax) {x = cx; y = cy;} else {x = px; y = py; } // Place the best chess point to the computer box [x] [y] = 0; // calculate the number of chess pieces that have been occupied by some part after the falling group compute (x, y, 0); ComputerCount ; // Record the status of the chess pieces Recordchesstatus (x, y, 0);
/ / Pickup people conversion computer = false; player = true;
IF (computercount == 60) // Survival on the board {GameOver = true; Winner = -1;} initialmark (); return 1;} initialmark (); return -1;} // player in the chessboard Falls Int Chess :: PlayerPlayChes (int horizontal, int value) {if (player) {// If the position of the chess pieces are not in the 11 * 11 range, return error if ((horizontal> = 11) || (vertical> = 11) || (horizontal <0) || (Vertical <0)) Return -1; if (Board [horizontal]! = 2) Return -1; Board [horizontal] [Vertical] = 1; PlayerCount ; // Record the status of the chess pieces Recordchesstatus (Horizontal, Vertical, 1);
// Calculate the chess pieces on the chess player in a combination of chess pieces in a combination of chess pieces if (! Compute (horizontal, vertical, 1)) return -1; player = false; computer = true; if (Playercount == 60) // Survival situation on the board {GameOver = true; winner = -1;} Return 1;} else return -1;}
// Repent Curse, only to repent for the self shss :: contritionchess () {// to store the current step int adjust; point point; int player = 2; step = computercount playercount; if (Step> 1 ) {// First revocation of the computer's chess position // is confirmed whether the rigidity is computer IF (Allchess [Step] .status == 0) && (Allchess [Step] .point.x! = -1)) {Point = Allchess [Step] .point; Board [Point.x] [Point.y] = 2; Allchess [Step] .status = 2; Allchess [Step] .point.x = -1; AllChess [STEP]. Point.y = -1; // Restore STEP-1 combined winning number for (int i = 0; i <2; i ) for (int J = 0; J <252; J ) WIN [I] [J] = AllChess [(PlayerCount ComputerCount]. Win [I] [J]; ComputerCount--; Step -;} // revoked chess IF (Allchess [Step] .status == 1) && (Allchess " [step] .point.x! = -1)) {Point = allchess [step] .point; board [Point.x] [Point.y] = 2; Allchess [Step] .status = 2; Allchess [STEP] .point.x = -1; allchess [step] .point.y = -1; // Restore STEP-1 combined winning number for (INT i = 0; i <2; i ) for (int J = 0; J <252; J ) WIN [I] [J] = Allchess [(PlayerCount CoMP Utercount)]. Win [i] [j]; playercount--; step--;}} return 1;} // Judgment whether the game end BOOL CHESS :: GameOver (int player) {IF (! GameOver) {INT i; // If the player is chess, it is determined whether there is a combination of five sons in WIN [1] [i] (player == 1) {for (i = 0; i <252; i ) IF (Win [1] [i] == 5) {// game end gameover = true; Winner = 1; return true;}} f (player == 0) {for (i = 0; i <252; i ) IF (Win [0 ] [i] == 5) {// Game end gameover = true;