Thread: Interesting question (I think)

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Interesting question (I think)

    I've written a console-based chess program which has a Move() function to move pieces if the proposed move is valid. I want to use this function to check for check and checkmate (create a temporary board and go through each possibility and see if the king is in check(mate)). But the move function has cout output, so if I used it it would garble the screen for the user.

    So, instead of writing another function without all of the output, might there be a way to ignore the cout's in the function?

    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You could pass a boolean value that will tell the method whether to draw the board or not. This might be kind of messy depending on how many cout's you have.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You probably need to modularize your program a bit better. The Move() function should only move the piece and not print it out. You can then call a print board routine after you've moved though.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Cshot
    You probably need to modularize your program a bit better. The Move() function should only move the piece and not print it out. You can then call a print board routine after you've moved though.
    Sorry I should have been more specific.

    Here is my BOARD class declaration:

    Code:
    class BOARD
    {
    	public:
    	BOARD(); 
    	~BOARD();
    	void clearBoard();
    	void showBoard() const;
    	void setTurn(int t) { turn =t; }
    	int getTurn() const { return turn;}
    	bool Move(int,int,int,int);
    	
    	private:
    	char board[8][8];
    	int turn; // 1 = white turn, 2 = black turn
    };
    Move() takes for 4 values - x1,y1 and x2,y2. They are the initial and final coordinates. All that Move does is make sure the value is valid - a whole crapload of compound boolean statements, pretty much. Whenever the move is invalid, move prints "Invalid move.". If it IS valid, it changes the board[8][8] array accordingly and calls showBoard() and setTurn().

    I don't expect there to be a solution like I would like, but I figured I'd ask just in case!

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I would imagine having Board contain Piece objects. Then specific objects such as pawns, knights, etc.. would be inherited from the Piece class. Then your specific pieces could have attributes such as current position, whether it is delivering a check, etc....

    However if your Move function is the only one that calls setTurn and showBoard, then maybe it should be moved to Private.

    Anyways that probably doesn't answer your question. My first thoughts were along the same lines as MrWizard. Pass a boolean value to ignore the cout and what not. However, it might just be easier to write a new function. All I can think of write now
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Cshot
    I would imagine having Board contain Piece objects. Then specific objects such as pawns, knights, etc.. would be inherited from the Piece class. Then your specific pieces could have attributes such as current position, whether it is delivering a check, etc....
    Thats actually what I originally thought of when I started writing this, but after staring at my screen for a little bit I realized I didn't know much about inheritance - I recently read up on them, however, so...

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    I guess I'm just going to remove the ouput from the Move() function and have it return a bool - if it returns false "Invalid move" will be displayed.

    making it bool will also help with my check and checkmate functions, which need to check if moves are valid or not.

    cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. A question about an interesting Makefile
    By meili100 in forum Tech Board
    Replies: 2
    Last Post: 08-12-2008, 03:56 PM
  3. interesting question about new and nothrow new
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 03:53 AM
  4. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM