Vous êtes sur la page 1sur 11

001 #include<iostream> 002 #include<string> 003 #include<stdlib> 004 005 006 // in this example pieces aer described as integer

values 007 // we will make them constants, so that if at any time we want to change their values we can do so here

008 // but will still need to recompile 009 010 const int pawn = 100; 011 const int bishop = 305; 012 const int knight = 300; 013 const int rook = 500; 014 const int queen = 900; 015 const int king = 2000; 016 017 // an alternative would be to use string constants or another data type 018 019 //now we need a board to put the pieces on and move around on 020 //the board data type should match the pieces data type 021 022 // the board in regular chess is always 8x8, but for speedy legal move generator //other programs use larger than 8x8 where an 8x8 real board exists in a larger array ie 12x14

023 // but for simplicity of understanding we will use the simple 8x8 024 025 int board[8][8]; 026 027 // board [rank] [file]; 028 029 const startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, pawn, 0, 0, 0, 0, 0, 0, 0, 0, 030 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, bishop, -queen, -king, -bishop, -knight, -rook}; 031 032 // the startup constant contains the standard starting position of all // where rank = 0 - 7 (1 to 8 on a real chess board) and file = 0 - 7 (a h)

chess games (not variants) 033 //each side has 8 pieces and 8 pawns / all pawns are on the colours respective ranks

034 // for black pieces we use -piecetype. (negative) 035 036 void setup (void) { 037 int i, j; 038 039 040 041 042 043 044 } 045 046 047 048 049 050 051 //it is standard in FEN notations and other text of a chess board to express each piece by it's first letter // next we need a function that will display the board some way either graphics or text // in this case we will print to the screen a text version of the boards contents //the two for loops run through all the iteratins of the 8x8 array and copy the starting position to the real board. } } for (i = 0; i < 8; i++){ for (j = 0; j < 8; j++){ board[i][j] = startup[i][j]; //setup starting position

052 // except the knight which uses 'N' 053 054 // the black pieces are lower case while the white pieces are upper case 055 056 057 void printb (void){ 058 using namespace std; // this must be here in order to begin using strings. 059 int a, b; 060 string piece; 061 for (a = 7; a > -1; a--){ // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down 062 cout << endl; 063 for (b = 0; b < 8; b++){ // otherwise it is impossible to distinguish black pieces from white pieces

064 switch (board[a][b]){ 065 case 0: 066 piece = "-"; 067 break; 068 case pawn: 069 piece = "P"; 070 break; 071 case knight: 072 piece = "N"; 073 break; 074 case bishop: 075 piece = "B"; 076 break; 077 case rook: 078 piece = "R"; 079 break; 080 case queen: 081 piece = "Q"; 082 break; 083 case king: 084 piece = "K"; 085 break; 086 case -pawn: 087 piece = "p"; 088 break; 089 case -knight: 090 piece = "n"; 091 break; 092 case -bishop: 093 piece = "b"; 094 break; 095 case -rook: 096 piece = "r"; 097 break; 098 case -queen: 099 piece = "q"; 100 break; 101 case -king:

102 piece = "k"; 103 break; 104 } 105 106 } 107 } 108 109 cout << endl << endl; 110 } 111 112 113 // every program in win32 console must have a main 114 115 116 int main (void) { 117 118 using namespace std; 119 120 //we need to tell the user about the program 121 122 123 124 125 126 string passd; // this will be the string that contains info from the user 127 setup(); //we must set up the initial position 128 129 while (1){ // a while loop that always loops; except when a break; statement occurs 130 131 getline (cin, passd ); to do //ask the user to input what he wants the app cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555"<< endl << endl; cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl; cout << "commands: exit = quit, abort = quit, print = displays the board,"<< endl << "new = new game" << endl << endl; .. and how to use it cout << " " << piece << " ";

if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) 132 == "abort"|| passd.substr(0, 4) == "quit") { //test //for quit or exit statements

133 134 135 136 137 138 139 140 } } }

break; if (passd.substr(0, 5) == "print") printb(); if (passd.substr(0, 3) == "new") setup(); 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && { // this statement makes sure both squares executing //a move { // ask for a new game { // display the board

if (passd.substr(0, passd.substr(1, 1) >= "1" && 141 1) >= "a" && passd.substr(2, passd.substr(3, 1) <= "8") are on the chess board when 142 143 144 145 146 147 148 149 150 151 152 int a, b, c, d; // execute move

// then display new board position

a = passd[0] - 'a'; b = passd[1] - '1'; c = passd[2] - 'a'; d = passd[3] - '1';

153 //executes the move if its on the board! 154 155 156 157 158 159 160 } } printb(); //prints out to the screen the updated position after moving the pieces } board[d][c] = board[b][a]; board[b][a] = 0;

The two most important parts of a chess program, is the Board and Pieces Everything else depends on the existence of those two parts.

There are several ways of implementing the board and pieces in a computer language, but for the sake of simplicity and ease of understanding we will use an [8]x[8] board with integers. int board[8][8]; many strong engines use a array of larger than 8x8 so that legal move generation will be speedier. int board[14][16]; where the 8x8 array lies inside the larger array. Next we must have the pieces defined somehow so that we can recognize them. The pieces must have the same type as the board. in this case we are using integer.
1 const int pawn = 100; 2 const int bishop = 305; 3 const int knight = 300; 4 const int rook = 500; 5 const int queen = 900; 6 const int king = 2000;

Although in early chess Bishops and Knights are both equal we cannot define them the same value and expect the compiler to distinguish the two, so in this case I gave Bishops a value a little bit greater than a knights values. Next we need a const array to hold the starting position of a chess game. This will come in handy when the user wants to restart a new game. Now that we have already stated the piece value as conststants, we can call them by their name instead of writting in the values.
const startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, queen, -king, -bishop, -knight, -rook};

Now that is done. We need a function that sets up the board for the start of a game.
1 void setup (void) { 2 int i, j; 3 4 5 6 7 } } for (i = 0; i < 8; i++){ for (j = 0; j < 8; j++){ board[i][j] = startup[i][j]; //setup starting position

8 9}

It is a good idea to put function prototypes before int main() {...} a function must come before or after int main but never inside int main. Now a good idea is to have a function that display the pieces on the board that the user can understand and follow.
01 void printb (void){ 02 using namespace std; // this must be here in order to begin using strings. 03 int a, b; 04 string piece; 05 for (a = 7; a > -1; a--){ // we must iterate the ranks down from 7 to 0 otherwise the board will be upside down 06 cout << endl; 07 for (b = 0; b < 8; b++){ 08 switch (board[a][b]){ 09 case 0: 10 piece = "-"; 11 break; 12 case pawn: 13 piece = "P"; 14 break; 15 case knight: 16 piece = "N"; 17 break; 18 case bishop: 19 piece = "B"; 20 break; 21 case rook: 22 piece = "R"; 23 break; 24 case queen: 25 piece = "Q"; 26 break; 27 case king: 28 piece = "K"; 29 break; 30 case -pawn:

31 piece = "p"; 32 break; 33 case -knight: 34 piece = "n"; 35 break; 36 case -bishop: 37 piece = "b"; 38 break; 39 case -rook: 40 piece = "r"; 41 break; 42 case -queen: 43 piece = "q"; 44 break; 45 case -king: 46 piece = "k"; 47 break; 48 } 49 50 } 51 } 52 53 cout << endl << endl; 54 } cout << " " << piece << " ";

The above code will print out the board for whites perspective. To change to blacks perspective just change a to count up 0 - 7 and b to count down 7 - 0. It is common in FEN notation and other text notations to have white uppercase first letter of each piece and black as lowercase. Except that N = knight. (N = white knight and n = black knight).

01 int main (void) { 02 03 using namespace std; 04 05 //we need to tell the user about the program 06 07 cout << "welcome to simplechess 1.0!" << endl << "created by Deepglue555"<< endl << endl; .. and how to use it

08 09 10

cout << "please enter your moves in 4 letter algebraic" << endl << "ie e2e4 in lower case only" << endl; cout << "commands: exit = quit, abort = quit, print = displays the board,"<< endl << "new = new game" << endl << endl;

11 string passd; // this will be the string that contains info from the user 12 setup(); //we must set up the initial position 13 14 while (1){ // a while loop that always loops; except when a break; statement occurs 15 16 17 getline (cin, passd ); to do //ask the user to input what he wants the app

if (passd.substr(0, 4) == "exit" || passd.substr(0, 5) == "abort"|| passd.substr(0, 4) == "quit") { //test //for quit or exit statements 18 break; 19 20 21 22 23 24 25 } if (passd.substr(0, 5) == "print") printb(); } if (passd.substr(0, 3) == "new") setup(); } 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && { // this statement makes sure both squares executing //a move { // ask for a new game { // display the board

if (passd.substr(0, passd.substr(1, 1) >= "1" && 26 >= "a" && passd.substr(2, 1) passd.substr(3, 1) <= "8") are on the chess board when 27 28 29 30 31 32 33 34 35 36 37 int a, b, c, d;

// execute move // then display new board position

a = passd[0] - 'a'; b = passd[1] - '1'; c = passd[2] - 'a'; d = passd[3] - '1';

38 //executes the move if its on the board! 39 board[d][c] = board[b][a];

40 41 42 43 44 45 }

board[b][a] = 0; printb(); //prints out to the screen the updated position after moving the pieces } }

Now that we have int main, what should it do? well here it starts with using namespace std; so we can now use strings, cout, cin and getline. Next comes the cout << statements to tell the user the name of the program, the author and how to use it. Next i declared the string for passing info to the program from the user. I named it passd. Next we call setup(); which sets up the board before play starts. (if we didn't our board would be filled with some strange integes most of which would be nonsense and not understood ). Next i setup a while(1) loop that would ultimately go forever, if it weren't for a break statement that is activated when the user types quit, abort or exit. Next i used the string passd to get info from the user. getline (cin, passd ); it is then run through a set of tests to see if it matches a statement for a predetermined action. Ie user inputs 'print' will now print the contents of the board, user inputs 'exit' and the program will end.
if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) 01 >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8") { // this statement makes sure both squares are on the chess board when executing //a move 02 // execute move 03 04 05 06 07 08 09 10 11 a = passd[0] - 'a'; b = passd[1] - '1'; c = passd[2] - 'a'; d = passd[3] - '1'; int a, b, c, d; // then display new board position

12 13 //executes the move if its on the board! 14 board[d][c] = board[b][a]; 15 16 printb(); //prints out to the screen the updated position after moving the pieces 18 } 17 board[b][a] = 0;

The above statement make sure that the four characters are correct for 4 letter algebraic coordinates if they are then the move is performed on the board then the updated position is shown on the screen. Then it will repeat all over for more input from the user. Part 2 will contain checking for legal moves.

Vous aimerez peut-être aussi