home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / cxx / chess2.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.4 KB  |  137 lines

  1. // Copyright (c) 1993 SAS Institute, Inc, Cary, NC USA
  2. // All Rights Reserved
  3. //
  4. // This c++ example is a very basic chess program.
  5. // This version contains a logic error that will
  6. // be found with the CodeProbe debugger.
  7.  
  8. #include <iostream.h>
  9. #include <string.h>
  10.  
  11. class chessPiece {
  12.    char name[10];          // full name of the piece
  13.    char color;             // is it white or black
  14.    char code;              // display abbreviation 
  15.    int value;              // value of piece in points
  16.  
  17. public:
  18.   chessPiece(const char *n, char c, char co, int v) {
  19.     // construct and initialize a chess piece
  20.     strcpy(name, n);
  21.     color = c;
  22.     code = co;
  23.     value = v;
  24.   }
  25.  
  26.   void showPiece() {
  27.     // display the piece to the user
  28.     cout << "| " << color << code << "  ";
  29.   }
  30.  
  31. };
  32.  
  33. class chessBoard {
  34.    class chessPiece *board[8][8];  // the chess board
  35. public:
  36.    chessBoard();
  37.    ~chessBoard();
  38.  
  39.    void showBoard();
  40.    void movePiece(int old_r, int old_c, int new_r, int new_c);
  41. };
  42.  
  43. chessBoard::chessBoard(){
  44.   // Take out the pieces and setup the board
  45.   int r,c;
  46.  
  47.   for (r=0; r<8; r++) 
  48.     for (c=0; c<8; c++)
  49.       board[r][c] = NULL;
  50.  
  51.   for (c=0; c<8; c++) {
  52.      board[1][c] = new chessPiece ("pawn", 'w', 'P', 1);
  53.      board[6][c] = new chessPiece ("pawn", 'b', 'P', 1);
  54.   }
  55.  
  56.   board[0][0] = new chessPiece ("rook", 'w', 'R', 5);
  57.   board[0][7] = new chessPiece ("rook", 'w', 'R', 5);
  58.   board[7][0] = new chessPiece ("rook", 'b', 'R', 5);
  59.   board[7][7] = new chessPiece ("rook", 'b', 'R', 5);
  60.  
  61.   board[0][1] = new chessPiece ("knight", 'w', 'N', 3);
  62.   board[0][6] = new chessPiece ("knight", 'w', 'N', 3);
  63.   board[7][1] = new chessPiece ("knight", 'b', 'N', 3);
  64.   board[7][6] = new chessPiece ("knight", 'b', 'N', 3);
  65.  
  66.   board[0][2] = new chessPiece ("bishop", 'w', 'B', 3);
  67.   board[0][5] = new chessPiece ("bishop", 'w', 'B', 3);
  68.   board[7][2] = new chessPiece ("bishop", 'b', 'B', 3);
  69.   board[7][5] = new chessPiece ("bishop", 'b', 'B', 3);
  70.  
  71.   board[0][3] = new chessPiece ("queen", 'w', 'Q', 9);
  72.   board[7][3] = new chessPiece ("queen", 'b', 'Q', 9);
  73.  
  74.   board[0][4] = new chessPiece ("king", 'w', 'K', 3);
  75.   board[7][4] = new chessPiece ("king", 'b', 'K', 3);
  76. }
  77.  
  78. chessBoard::~chessBoard(){
  79.   // put the pieces back in the box
  80.   int r, c; 
  81.   for (r=0; r<8; r++) 
  82.     for (c=0; c<8; c++)
  83.       delete board[r][c];
  84. }
  85.  
  86. void chessBoard::movePiece(int old_r, int old_c, int new_r, int new_c) {
  87.   // perform a simple piece move
  88.  
  89.   // !!!this statement is now correct!!!
  90.   board[new_r][new_c] = board[old_r][old_c];
  91.  
  92.   board[old_r][old_c] = NULL;
  93.  
  94. void chessBoard::showBoard() {
  95.   // display the chess board as a grid containing
  96.   // the pieces in their current position.       
  97.   int r,c;
  98.  
  99.   for (r=7; r>=0; r--) {
  100.     cout << "-------------------------------------------------" << endl;
  101.     for (c=0; c<8; c++)
  102.       if (board[r][c] != NULL)
  103.          board[r][c]->showPiece();
  104.       else cout << "|     ";
  105.     cout << '|' << endl;
  106.  
  107.   }
  108.   cout << "------------------------------------------------" << endl;
  109. }
  110.  
  111.  
  112. int main () {
  113.  
  114.   class chessBoard gameboard;  // constructs and setups up the pieces
  115.  
  116.   // start the game with the Sicilian opening
  117.   gameboard.movePiece(1,4, 3,4);
  118.   gameboard.movePiece(6,2, 4,2);
  119.   gameboard.movePiece(0,6, 2,5);
  120.   gameboard.movePiece(7,1, 5,2);
  121.  
  122.   // display the board to the user
  123.   gameboard.showBoard();
  124.  
  125.   // the game board and chess pieces are automatically 
  126.   // (put away) destructed
  127.  
  128.   return 0;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.