Thread: creating chess game...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    creating chess game...

    Hi I'm in the process of creating a chess game and I have a couple of questions..
    Code:
    #include <iostream>
    
    using namespace std;
    
    char *chess_board[8][8];//declare cheesboard a two dimensional array
    char *color[2] = {"black", "white"};//declare two colors
    char *pieces[16] = {"castle 1", "castle 2", "knight 1", //all pieces
    "knight 2", "rooke 1", "rooke 2", "king", "quenn", 
    "pawn 1", "pawn 2", "pawn 3", "pawn 4", "pawn 5", 
    "pawn 6", "pawn 7", "pawn 8"};

    so I'm trying to have it take the arrays and take say the color array and pieces array and combining them to say black knight 1, white knight 1, etc...
    Also I'm wondering if there is another way of going about were i have pawn 1, 2, 3, and having it just pawn but the problem is there are eight on each side...

    I am trying to approach this from the AI side once I get the idea.. I felt this was more of a c++ question which is why i posted on the c++ board... any help is always greatly appreciated...
    thanks
    CJ

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    If I am going to write chess programs, probably I would declare the same variable but with all type of int. Because somewhere we have to compare those variables, and string is much slower than numbers. Also when using number, we can easily weight the variable. For instance, pawn is weight 1. knight is weight 3 and so on. Also, I found there is a simple chess program for windows 3.0 dates 1990 came with code (unfortunately the sources inside my hard disk already corrupted), but you may try to find it in internet, it was created by Daryl Baker.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> If I am going to write chess programs, probably I would declare the same variable but with all type of int.

    I agree. The texual representation isn't really useful from within the class, so you could probably just translate from integer id to string when necessary. Here's a possible starting point:

    Code:
    #include <ostream>
    #include <string>
    
    namespace chess {
    
    struct piece
    {
    	enum color_type
    	{
    		first_color,
    		black = first_color, 
    		white, 
    		color_count
    	};
    	
    	enum rank_type
    	{
    		first_rank,
    		king = first_rank, 
    		queen, 
    		castle, 
    		bishop, 
    		knight, 
    		pawn, 
    		rank_count
    	};
    
    	piece( int rank, int color, int ordinal = 0 )
    	: rank( rank ), color( color ), ordinal( ordinal )
    	{	}
    
    	friend bool operator == ( piece const& lhs,  piece const& rhs )
    	{
    		return 
    			lhs.rank == rhs.rank 
    			&&
    			lhs.color == rhs.color 
    			&&
    			lhs.ordinal == rhs.ordinal;				
    	}
    	
    	friend std::ostream& operator << ( std::ostream& out,  piece const& rhs )
    	{
    		return out 
    			<< translate_color( rhs.color ) << "." 
    			<< translate_rank( rhs.rank ) << "." 
    			<< rhs.ordinal;				
    	}	
    	
    	static std::string translate_color( int color )
    	{
    		if( color == white )
    			return "white";
    		if( color == black )
    			return "black";
    	/*
    		Or maybe throw an exception here
    	*/
    		return std::string( );	
    	}
    	
    	static std::string translate_rank( int rank )
    	{
    		if( rank == king )
    			return "king";
    		if( rank == queen )
    			return "queen";
    		if( rank == castle )
    			return "castle";
    		if( rank == bishop )
    			return "bishop";
    		if( rank == knight )
    			return "knight";
    		if( rank == pawn )
    			return "pawn";
    	/*
    		Or maybe throw an exception here
    	*/
    		return std::string( );	
    	}
    	
    	static int ordinals_of_rank( int rank )
    	{	
    		if( rank == king )
    			return 1;
    		if( rank == queen )
    			return 1;
    		if( rank == castle )
    			return 2;
    		if( rank == bishop )
    			return 2;
    		if( rank == knight )
    			return 2;
    		if( rank == pawn )
    			return 8;
    	/*
    		Or maybe throw an exception here
    	*/
    		return 0;	
    	}
    	
    	int
    		color;
    	int
    		rank;
    	int
    		ordinal;
    };
    
    } // namespace chess
    
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    int main( void )
    {
    	using namespace chess;
    	using namespace std;
    	vector< piece >
    		pieces;
    	for( int color = piece::first_color; color < piece::color_count; ++color )
    		for( int rank = piece::first_rank; rank < piece::rank_count; ++rank )
    			for( int ordinal = 0, ordinal_count = piece::ordinals_of_rank( rank );  ordinal < ordinal_count; ++ordinal )
    				pieces.push_back( piece( rank, color, ordinal ) );
    	copy( pieces.begin( ), pieces.end( ), ostream_iterator< piece >( cout, "\n" ) );
    	return 0;
    }
    Last edited by Sebastiani; 06-27-2009 at 02:48 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    106
    Ok I'm not new to programming but im new as far as looking at it from a game programmers perspective....
    The one question is though were I have to have castle.. and stuff like that it has to be string?...
    I'm thinking about making the chessboard a class... I've got the code but I have little experience with declaring seperate classes like and it say there and error
    Code:
    #include <iostream>
    
    using namespace std;
    
    int black;
    int white;
    int pawn[8], rooke[2], knight[2], quenn, king, castle[2];
    
    class chess_board()
    {
          int num1;
          int num2;
          int chess_board[num1][num2];
          for (num1 = 0, num2 = 0; num2 < 8, num1 < 8; num2++, num1++) 
              
    
    }
    its says error

    9 J:\expected unqualified-id before ')' token

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you really need to brush up your skills on classes, because you're doing it all wrong, which is why you get errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    106
    lol... yeah i figured that im working on it as i post this.. any tips?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Try the site tutorials or a good book (such as Accelerated C++).
    A good book such as the one I mentioned will give you enough programming skills to attempt thinks, me thinks.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    106
    ok so im looking at some stuff on class and I see that you have to declare something public or private.. which was probably one of my problems.... I'm going to keep reading and see what happens.... one thing is that the book thati read when i got started was like a ear ago and forgot some things... the code psted above like third post i can read most of theres parts that i dont know but im busy figuring it out

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The biggest problem is that a class is not a function -- you cannot simply paste code inside a class!
    The code goes inside functions inside the class.
    And another example is that a declaration of a class does not end with () after its name, like functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Number 1 - Definitions of class' don't have parenthesis:
    Code:
    class Chess_Board()
    Number 2 - You don't need to declare something public, protected or private explicitly, unless they are public or protected... The member functions and variables are private by default.

    Number 3 - You need a terminating semi-colon to indicate that you are finished defining your class
    Code:
    class Chess_Board
    {
    /*Some code here*/
    };
    Last edited by legit; 06-27-2009 at 04:23 AM.
    MSDN <- Programmers Haven!

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    106
    so you can make a class inside of a namespace? and it will work? then you refure back to the class when you get to fuctions such as int main()?

  12. #12
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by jamort View Post
    so you can make a class inside of a namespace? and it will work?
    Sure you can.
    MSDN <- Programmers Haven!

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    106
    Quote Originally Posted by legit View Post
    Number 1 - Definitions of class' don't have parenthesis:
    Code:
    class Chess_Board()
    Number 2 - You don't need to declare something public or private explicitly, unless they are public or protected... The member functions and variables are private by default.

    Number 3 - You need a terminating semi-colon to indicate that you are finished defining your class
    Code:
    class Chess_Board
    {
    /*Some code here*/
    };
    ok that gave me some help i didnt think about including a semicolon... yes ive done loops there pieces of cake... but this is a little bit harder to figure out...
    edit...
    saw new post
    ok thanks that helps...
    the reason i added () was because i was trying to get it to work and didnt know if that was a problem
    Last edited by jamort; 06-27-2009 at 04:32 AM.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You really need to stop what you're doing and get a hold of a good programming text. Read it cover to cover at least twice before attempting to write any more code. Otherwise, you're just wasting your time (and potentially others).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    106
    I can do everything else i had just never done classes and didnt really understand them
    I am going to reread c++ without fear... though
    Last edited by jamort; 06-27-2009 at 04:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  4. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  5. chess game
    By meka in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 02:33 PM