Ejemplo n.º 1
0
void mazeTraversal(char maze[][COLS],const int xCoord,const int yCoord,
                   int row,int col,int direction)
{static bool flag=false;//开始位置标志变量
 maze[row][col]='x';  //在当前位置插入x
 if(coordsAreEdge(row,col)&&row!=xCoord&&col!=yCoord) {
  cout<<endl<<"成功走出迷宫!\n";return;}
 else if(row==xCoord&&col==yCoord&&flag) {
  cout<<"\n返回迷宫开始位置.\n";return;}
 else {flag=true;
  for(int move=direction,count=0;count<4;++count,++move,move%=4)
   switch(move) {
     case DOWN://向下移动
       if(validMove(maze,row+1,col)) { 
        mazeTraversal(maze,xCoord,yCoord,row+1,col,LEFT);
        return;}
       break;
     case RIGHT://向右移动
       if( validMove(maze,row,col+1)) { 
        mazeTraversal(maze,xCoord,yCoord,row,col+1,DOWN);
        return;}
       break;
     case UP://向上移动
       if(validMove(maze,row-1,col)) {
        mazeTraversal(maze,xCoord,yCoord,row-1,col,RIGHT);
        return;}
       break;
     case LEFT://向左移动
       if(validMove(maze,row,col-1)) { 
        mazeTraversal(maze,xCoord,yCoord,row,col-1,UP);
        return;}
       break;}
}}
Ejemplo n.º 2
0
void test_valid_moves(){
	int i = 0;
	for(i=0; i < rows; ++i){
		assert_true(validMove(g_board,4)==1);
		makeMove(g_board, 4);
	}
	assert_true(validMove(g_board,4)==0);
}
Ejemplo n.º 3
0
void test_undo_move(){
	//if I am able to insert 7th entry in a column
	//after undoing the 8th entry then my test succeeds
		
	int i = 0;
	for(i=0; i < rows; ++i){
		assert_true(validMove(g_board,4)==1);
		makeMove(g_board, 4);
	}

	assert_true(validMove(g_board,4)==0);
	undoMove(g_board);
	assert_true(validMove(g_board,4)==1);
	
}
Ejemplo n.º 4
0
int Maze::DFS(int x, int y)
{

  //The following commented out lines were for debugging
  //cout << "Our current coordinates are: " << x << "," <<y<<endl;  
  //cout << "This is the current character: " << maze[x][y]<<endl;
  //cout << count << endl;

  if ((x<=mazey && x>=0) && (y<=mazex && y>=0)){

    //Basic Case: We've reached the solution
    if (x==dest_x && y==dest_y){
      testFlag = true;
    }

    if(!testFlag){

      maze[x][y]='+';  /* mark this node as being visited */

      //Move South
      if ( ((x+1)<=mazey) && (validMove((x+1), y) ) ) 
	DFS( x+1, y );

      //Move East
      if ( ((y+1)<=mazex) && (validMove(x, (y+1)) ) )
	 DFS( x, y+1 );      

      //Move North
      if ( ((x-1)>=0) && (validMove((x-1), y) ) )
	DFS( x-1, y );

      //Move West
      if ( ((y-1)>=0) && (validMove(x, y-1) ) )
	DFS( x, y-1 );

      if (testFlag){
	maze[x][y]='*';     /* mark the solution path */
        distance++;             /* increment the cost of the path */
      }
      else if (testFlag && maze[x][y]=='+'){ 
          maze[x][y]=' ';    /* remove our bread crumb when we're done */
      }     
      
    }
  }

  return distance;
}   
Ejemplo n.º 5
0
// moves card(s) at, and on top of, position x,y to the "best" position on the board.
// moves to the left most columb in the case of a tie.
// moves to the columb that will create the largest stack of correctly stacked cards.
void Game::autoMoveCards( unsigned int x, unsigned int y )
{

  std::vector< sf::Vector2i > validMoves; // .x is the position, .y is the size of the stack

  if ( ! movableStack( x, y ) )
    return;

  for ( unsigned int i = 0; i < board.size(); i++ )
    if ( i != x )
      if ( validMove( x, y, i ) )
        validMoves.push_back( sf::Vector2i( i, getMovableStackSize(i) ) );

  sf::Vector2i bestMove( -1, -1 );
  for ( unsigned int i = 0; i < validMoves.size(); i++ )
  {
    //std::cout << bestMove.x << ":" << bestMove.y << " & " << validMoves[i].x << ":" << validMoves[i].y << std::endl;

    if ( bestMove.y < validMoves[i].y )
    {
      //std::cout << "new best move" << std::endl;
      bestMove = validMoves[i];
    }
  }

  if ( bestMove.x != -1 )
    moveCards( x, y, bestMove.x );
}
Ejemplo n.º 6
0
// Function places a piece at the passed column. The function searches the
// column from the bottom row up in order to place the passed piece. The
// passed piece is placed at the first EMPTY position from the bottom of
// the passed column. The row it was placed on is returned for convenience.
// This function calls the public validMove() function to ensure that the
// move is valid. If the move is invalid, a -1 is returned.
//      Parameters:         int c: the column to place the piece in
//                          piece p: the player making the move
//      Returns:            r (int) row piece was placed in; -1 if
//                          column represents an invalid move
int Board::placePiece(int c, piece p)
{
    // only place if move is valid
    if (validMove(c)) {
        // start from bottom, find first empty space
        for (int r = rows - 1; r >= 0; r--) {
            if (vBoard[r][c] == EMPTY) {
                // place in empty place
                vBoard[r][c] = p;

                // update sprite
                if (p == RED) {
                    boardSprite[r][c].setTexture(red);
                }
                else {
                    boardSprite[r][c].setTexture(black);
                }

                return r;
            }
        }
    }
    std::cout << "Board::placePiece: Error, invalid move." << std::endl;
    return -1;
}
Ejemplo n.º 7
0
void MainWindow::onMoveWasMade(const Grid *newMove)
 {
#ifdef DEBUG_VIEW
    print("mainWindow->onMoveWasMade");
#endif
    switchCurrentPlayer();
    emit validMove(newMove);
 }
Ejemplo n.º 8
0
/* validMove should return true IFF the chosen column is not full */
void test_validMove() {
  board_type *board = createBoard(X, Y);
  int i;

  CU_ASSERT_TRUE(validMove(board, 0));

  for(i = 0; i < Y - 1; i++)
    makeMove(board, 0);

  CU_ASSERT_TRUE(validMove(board, 0));

  makeMove(board, 0);

  CU_ASSERT_FALSE(validMove(board, 0));

  free(board);
}
Ejemplo n.º 9
0
//! Checks if the player with the color can move any piece to (x,y).
bool Board::canMoveTo(Color color, int x, int y)
{
	for(auto iter = mPieces.begin(); iter != mPieces.end(); iter++)	{
		if((*iter)->getColor() == color && validMove((*iter), x, y))
			return true;
	}
	
	return false;
}
Ejemplo n.º 10
0
 void nQueens::solve(int colNum){
     if(colNum == sizeOfBoard)
         return;
     for(int row = 0; row < sizeOfBoard; ++row)
         if(validMove(row, colNum)){
             placeQueenAt(row, colNum);
             solve(colNum + 1);
             removeQueenAt(row, colNum);
         }
     return ;
 }
Ejemplo n.º 11
0
//! Can the player move the king?
bool Board::canMoveKing(Color color)
{
	Piece* king = getPiece(KING, color);
	Position pos = king->getPos();

	if(validMove(king, pos.x+1, pos.y) || validMove(king, pos.x+1, pos.y-1) || validMove(king, pos.x, pos.y-1) || validMove(king, pos.x-1, pos.y-1) ||
		   validMove(king, pos.x-1, pos.y) || validMove(king, pos.x-1, pos.y+1) || validMove(king, pos.x, pos.y+1) || validMove(king, pos.x+1, pos.y+1))
		return true;
	else
		return false;
}
Ejemplo n.º 12
0
Move GameManager::getValidMove(std::ifstream &input) const {
    std::string message;
    std::string testData;
    
    if (input.good()) {
        std::getline(input, testData);
    }
    
    Move move = currentPlayer->getMove(board, message, testData);
    while (!validMove(move)) {
        if (input.good()) {
            std::getline(input, testData);
        }
        message = "That is not a legal move.";
        move = currentPlayer->getMove(board, message, testData);
    }
    
    return move;
}
Ejemplo n.º 13
0
/**
 * The exploration policy for selecting the next node
 */
treeNode* nextNode(treeNode* current, POLICY p) {
	int ret,move;
	if ((ret = _DOM->getGameStatus(current->rep))!= _DOM->incomplete) {
		return NULL;
	}
	if (current->n == 0){
		move = validMove(current);	
		rep_t newRep = _DOM->cloneRep(current->rep);
		int newSide = current->side; 
		_DOM->makeMove(newRep,&newSide, move); 
		current->children[move] = createNode(newRep,newSide,current);
		return current->children[move];
	}
	if (p == EXPLORATION){
		move = validRandomMove(current);
	} else {
		move =  selectMoveExploitation(current);
	}
	return current->children[move];
}
Ejemplo n.º 14
0
/* move in the direction 'dir' */	
void move(char dir, int undo, int redo) {
	int dirX = 0, dirY = 0;
	if (dir == PUSH_RIGHT || dir == RIGHT) dirX = 1;
	else if (dir == PUSH_LEFT || dir == LEFT) dirX = -1;
	else if (dir == PUSH_DOWN || dir == DOWN) dirY = 1;
	else if (dir == PUSH_UP || dir == UP) dirY = -1;
	if (!playing) return;
	if (!undo && !redo) {
		reset_undone_ptr();
		undone_length = 0;
	}
	int posX = getX(), posY = getY();
	int valid = validMove(posX, posY, posX+dirX, posY+dirY);
	if (!valid) play_sound(HIT_WALL);
	else {
		int box = moveTo(posX, posY, posX+dirX, posY+dirY);
		if (!undo && !replaying) updatePath(dirX, dirY, box);
	}
	
	if (getRemaining() == 0) playing = 0;
}
int GuessNumberTwoPlayer::play() {
    int limitTime = 10;
    bool gameOver = false;
    bool turn     = true;
    nextMove aMove;
    while(!gameOver) {
        int first, second, third;
        if(turn) {aMove = runPlayerA(); std::cout << "-A-" << std::endl;}
        else     {aMove = runPlayerB(); std::cout << "-B-" << std::endl;}

        first  = boost::tuples::get<0>(aMove);
        second = boost::tuples::get<1>(aMove);
        third  = boost::tuples::get<2>(aMove);
        std::cout << "Move! " << first << " , " << second << " , " << third <<
        std::endl;

        if(validMove(first, second, third)){
            turn = !turn;
            board[first][second] = third;

            gameOver = board[0][0] == solution.front();
            gameOver = gameOver && board[0][1] == solution.back();
        }
        else {
            std::cout << "Invalid Move! " << std::endl;
        }
        limitTime--;
        gameOver = gameOver || limitTime < 0;
        std::cout << "TIME LIMIT " << limitTime << std::endl << std::endl;
        //std::cout << "BOARD [ " << board[0][0] << " , " << board[0][1] << " ] "
        //<< std::endl;
    }

    if(limitTime == -1)
        std::cout << "TIME OVER! " << std::endl;

    if(!turn) return 1;
         else return 2;
}
Ejemplo n.º 16
0
bool Board::handleClick(int p, int x, int y, bool select) //takes in player via clicks
{
	if(lastp == p) //if player re-clicks same piece, this deselects it
	{	
		CImg<unsigned char> originalPiece = parseDraw(lastx,lasty,lastPieceP);
		int srcPiece = boardarray[lastx][lasty];
		if(srcPiece >= 0 && srcPiece <= 15)
		{
			if(pieceArray[srcPiece]->getPawnTrans())
			{
				CImg<unsigned char> transImage = parseDraw(lastx, lasty, pieceArray[srcPiece]->getPieceRule());
				originalPiece = transImage;
			}
		}
		lastp = BLANK;
		chessboard.draw_image(lastx*PIXELSQUARESIZE, lasty*PIXELSQUARESIZE, originalPiece);
		debugbox.fill(0).draw_text(0, 0, "PIECE DESELECTED.", green);
		return false; //returns false to main, preventing moving your piece to the same spot which would switch turns if this returned true
	}
	else if(select && lastp != BLANK) //actual movement
	{
		//check what piece to draw
		CImg<unsigned char> piecetodraw = parseDraw(x,y,lastp);//Last p is the piece to move
		lastp = BLANK;//overwrite last piece to prevent double drawing
		
		//checks if valid move, then draws it
		if(validMove(lastx,lasty,x,y))
		{
			moveDraw(piecetodraw,lastx,lasty,x,y);
			lastPieceP = BLANK;
		}
		
		else //transform conditions
		{
			debugbox.fill(0).draw_text(0, 0, "INVALID MOVE.\n MOVE AGAIN.", green);
			int srcPiece = boardarray[lastx][lasty];
			
			CImg<unsigned char> originalPiece = parseDraw(lastx,lasty,lastPieceP);
			if(srcPiece >= 0 && srcPiece <= 15)
			{
				if(pieceArray[srcPiece]->getPawnTrans())
				{
					CImg<unsigned char> transImage = parseDraw(lastx, lasty, pieceArray[srcPiece]->getPieceRule());
					originalPiece = transImage;
				}
			}
			chessboard.draw_image(lastx*PIXELSQUARESIZE, lasty*PIXELSQUARESIZE, originalPiece);
			//debugbox.fill(0).draw_text(0, 0, "PIECE DESELECTED.", green);
		}
	}
	else //first move special case
	{
		if(p != BLANK){
		lastx = x;
		lasty = y;
		
			lastPieceP = p;
		lastp = p;

		CImg<unsigned char> outlines;
		outlines = selection;
		chessboard.draw_image(lastx*PIXELSQUARESIZE, lasty*PIXELSQUARESIZE, outlines);
		chessboard.draw_image(lastx*PIXELSQUARESIZE + 95, lasty*PIXELSQUARESIZE, outlines);
		debugbox.fill(0).draw_text(0, 0, "PIECE SELECTED.", green);
		}
	}
	return true; //if conditions are valid, this returns true to main
}