⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 board.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			if (tempCnt == 4) evenRow = !evenRow;
			else              ++tempCnt;

      if(m_Board[ii] == RED) {
				// Upper right hand corner
				if (ii != 12 && ii != 20) {
					if (evenRow) {
						if (m_Board[ii-4] == WHITE && m_Board[ii+3] == empty)    ++dead;
					}
					else if (m_Board[ii-3] == WHITE && m_Board[ii+4] == empty) ++dead;
				}

				// Upper left hand corner
				if (ii != 13 && ii != 21) {
					if (evenRow) {
						if (m_Board[ii-5] == WHITE && m_Board[ii+4] == empty)    ++dead;
					}
					else if (m_Board[ii-4] == WHITE && m_Board[ii+5] == empty) ++dead;
				}

				// Lower right hand corner
				if (ii != 12 && ii != 20) {
					if (evenRow) {
						if ((m_Board[ii+4] == WHITE || m_Board[ii+4] == white) && m_Board[ii-5] == empty)    ++dead;
					}
					else if ((m_Board[ii+5] == WHITE || m_Board[ii+5] == white) && m_Board[ii-4] == empty) ++dead;
				}
			
				// Lower left hand corner
				if (ii != 13 && ii != 21) {
					if (evenRow) {
						if ((m_Board[ii+3] == WHITE || m_Board[ii+3] == white) && m_Board[ii-4] == empty)    ++dead;
					}
					else if ((m_Board[ii+4] == WHITE || m_Board[ii+4] == white) && m_Board[ii-3] == empty) ++dead;
				}
      } 
    }  
  } 
  else {
    for (int ii = 6; ii <= 27; ++ii) {
			if (tempCnt == 4) evenRow = !evenRow;
			else              ++tempCnt;

      if (m_Board[ii] == WHITE) {	
				// Upper right hand corner
				if (ii != 12 && ii != 20) {
					if (evenRow) {
						if ((m_Board[ii-4] == RED || m_Board[ii-4] == red) && m_Board[ii+3] == empty)    ++dead;
					}
					else if ((m_Board[ii-3] == RED || m_Board[ii-3] == red) && m_Board[ii+4] == empty) ++dead;
				}

				// Upper left hand corner
				if (ii != 13 && ii != 21) {
					if (evenRow) {
						if ((m_Board[ii-5] == RED || m_Board[ii-5] == red) && m_Board[ii+4] == empty)    ++dead;
					}
					else if ((m_Board[ii-4] == RED || m_Board[ii-4] == red) && m_Board[ii+5] == empty) ++dead;
				}

				// Lower right hand corner
				if (ii != 12 && ii != 20) {
					if (evenRow) {
						if (m_Board[ii+4] == RED && m_Board[ii-5] == empty)    ++dead;
					}
					else if (m_Board[ii+5] == RED && m_Board[ii-4] == empty) ++dead;
				}

				// Lower left hand corner
				if (ii != 13 && ii != 21) {
					if (evenRow) {
						if (m_Board[ii+3] == RED && m_Board[ii-4] == empty)    ++dead;
					}
					else if (m_Board[ii+4] == RED && m_Board[ii-3] == empty) ++dead;
				}
      } 
    }
  } 	
  return dead;
}

/*******************************************************************************************/

/**
 * Board::operator=():
 *  Overloaded Equal Operator.
 * 
 *  Return Type : Board& -> A reference to the current board
 *  Arguments   : 
 *  	const Board &rhs : The board to be copied.
 */
Board& Board::operator=( const Board &rhs )
{
	if (this != &rhs) {
		memcpy( m_Board, rhs.m_Board, sizeof(m_Board) );
		m_numRedPieces   = rhs.m_numRedPieces;
		m_numWhitePieces = rhs.m_numWhitePieces;
		m_numRedKings    = rhs.m_numRedKings;
		m_numWhiteKings  = rhs.m_numWhiteKings;
	}
  return *this;
}

/*******************************************************************************************/

/**
 * Board::operator==():
 *  Overloaded Equivalency Operator.
 * 
 *  Return Type : bool -> Whether or not the two boards are the same.
 *  Arguments   : 
 *  	const Board &rhs : The board to be compared to 'this' board.
 */
bool Board::operator==( const Board &rhs )
{
  for (int ii = 1; ii < BOARD_SIZE; ++ii) {
     if (m_Board[ii] != rhs.m_Board[ii] ) return false;
	}

  return true;
}   

/*******************************************************************************************/

/**
 *  operator<<():
 *  Overloaded Extraction Operator.  This method is used for writting the board information
 *  to the screen.
 * 
 *  Return Type : ostream& -> A reference to the provided ostream.
 *  Arguments   : 
 *  	ostream &os	: A reference to the stream.
 *  	Board &bd	  : The board to display.
 */
ostream& operator<<( ostream &os, Board &bd )
{
  char c[5] = {'R', 'r', ' ', 'w', 'W'};
  os << endl;
  for(int ii = 1; ii < BOARD_SIZE; ++ii) {
    os << "   " << '{' << c[bd.m_Board[ii]]   << '}';
	  os << "   " << '{' << c[bd.m_Board[ii+1]] << '}';
	  os << "   " << '{' << c[bd.m_Board[ii+2]] << '}';
	  os << "   " << '{' << c[bd.m_Board[ii+3]] << '}';
	  os << "     |     ";
	  os << "    " << '{' << setw(2) << ii++ << '}';
	  os << "    " << '{' << setw(2) << ii++ << '}';
	  os << "    " << '{' << setw(2) << ii++ << '}';
	  os << "    " << '{' << setw(2) << ii++ << '}' << endl;
			
    os << '{' << c[bd.m_Board[ii]]   << '}' << "   ";
	  os << '{' << c[bd.m_Board[ii+1]] << '}' << "   ";
	  os << '{' << c[bd.m_Board[ii+2]] << '}' << "   ";
	  os << '{' << c[bd.m_Board[ii+3]] << '}' << "   ";
	  os << "     |     ";
	  os << '{' << setw(2) << ii++ << '}' << "    ";
	  os << '{' << setw(2) << ii++ << '}' << "    ";
	  os << '{' << setw(2) << ii++ << '}' << "    ";
	  os << '{' << setw(2) << ii   << '}' << endl;
  }
  os << endl;
  return os;
}

/*******************************************************************************************/

/**
 *  operator<<():
 *  Overloaded Extraction Operator.  This method is used for writting the board information
 *  to a file.
 * 
 *  Return Type : ofstream& -> A reference to the provided ofstream.
 *  Arguments   : 
 *  	ostream &ofs : A reference to the output stream.
 *  	Board &bd	   : The board to saved.
 */
ofstream& operator<<( ofstream &ofs, Board &bd )
{
	return ofs;
}

/*******************************************************************************************/

/**
 * Board::updateSlide():
 *  Updates the board to reflect a slide move.
 * 
 *  Return Type : void  
 *  Arguments   : 
 *  	int from  : The starting position of the slide move. 
 *  	int to	  : The ending position of the slide move.
 */
void  Board::updateSlide( int from, int to )
{
	piece ch = m_Board[from];

	m_Board[from] = empty;
  if (ch == red) {
		if (to == 29 || to == 30 || to == 31 || to == 32) {
			++m_numRedKings;
			m_Board[to] = RED;
		}
    else {
			m_Board[to] = red;
		}
  }
  else if (ch == white) {
    if (to == 1 || to == 2 || to == 3 || to == 4) {
			++m_numWhiteKings;
			m_Board[to] = WHITE;
		}
    else {
			m_Board[to] = white;
		}
  }
  else {
    m_Board[to] = ch;
	}
}

/*******************************************************************************************/

/**
 * Board::updateJump():
 *  Updates the board to reflect a jump move.
 * 
 *  Return Type : void  
 *  Arguments   : 
 *  	int from	: The starting position to jump from.
 *  	int to	  : The ending position of the jump.
 */
void  Board::updateJump( int from, int to )
{
	piece ch = m_Board[from];

  m_Board[from] = empty; // The position we jump from is now empty.

  if (ch == red) {       // Update our current position.
		if (to == 29 || to == 30 || to == 31 || to == 32) {
			++m_numRedKings;
			m_Board[to] = RED;
		}
    else {
			m_Board[to] = red;
		}
  }
  else if (ch == white) {
    if (to == 1 || to == 2 || to == 3 || to == 4) {
			++m_numWhiteKings;
			m_Board[to] = WHITE;
		}
    else {
			m_Board[to] = white;
		}
  }
	else {
		m_Board[to] = ch;
	}

	if (ch == red || ch == RED) {
		--m_numWhitePieces;
		if (ch == RED) --m_numRedKings;
	}
	else {
		--m_numRedPieces;
		if (ch == WHITE) --m_numWhiteKings;
	}

	if      (to - from == 9) m_Board[JUMP_OVER[to][0]] = empty;
	else if (to - from == 7) m_Board[JUMP_OVER[to][1]] = empty;
	else if (from - to == 9) m_Board[JUMP_OVER[to][3]] = empty;
	else                     m_Board[JUMP_OVER[to][2]] = empty;
}

/*******************************************************************************************/

/**
 * Board::init():
 *  Creates an empty board.
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void Board::init( void )
{
	m_numWhitePieces = 0;
	m_numRedPieces   = 0;
	m_numRedKings    = 0;
	m_numWhiteKings  = 0;
	for (int ii = 0; ii < BOARD_SIZE; ++ii) {
		m_Board[ii] = empty;
	}
}

/*******************************************************************************************/

/**
 * Board::init2():
 *  Creates a board with the standard starting board configuration
 * 
 *  Return Type : void 
 *  Arguments   : NONE
 */
void Board::init2( void )
{
	int ii;
  m_Board[0] = empty;
  for (ii = 1; ii < 13; ++ii) {
    m_Board[ii] = red;
	}
  for (; ii < 21; ii++) {
    m_Board[ii] = empty;
	}
  for (; ii < BOARD_SIZE; ++ii) {
    m_Board[ii] = white;
	}
	m_numWhitePieces = 12;
	m_numRedPieces   = 12;
	m_numRedKings    = 0;
	m_numWhiteKings  = 0;
}
   
// ***** End of Board.cpp
/*******************************************************************************************/
/*******************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -