C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-27-2009, 01:07 AM   #1
Registered User
 
Join Date: May 2009
Posts: 94
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
jamort is offline   Reply With Quote
Old 06-27-2009, 02:16 AM   #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.
binyo66 is offline   Reply With Quote
Old 06-27-2009, 02:31 AM   #3
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,309
>> 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;
}
__________________
Code:
int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
<1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
(l)))*0.5)+0.5)){for(size_t l0000=0,l00000=(size_t)(0x50*(l00-floor(l00)));l0000<l00000;++l0000
)putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Last edited by Sebastiani; 06-27-2009 at 02:48 AM.
Sebastiani is offline   Reply With Quote
Old 06-27-2009, 03:00 AM   #4
Registered User
 
Join Date: May 2009
Posts: 94
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
jamort is offline   Reply With Quote
Old 06-27-2009, 03:18 AM   #5
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
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.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 06-27-2009, 03:23 AM   #6
Registered User
 
Join Date: May 2009
Posts: 94
lol... yeah i figured that im working on it as i post this.. any tips?
jamort is offline   Reply With Quote
Old 06-27-2009, 03:25 AM   #7
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
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.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 06-27-2009, 04:12 AM   #8
Registered User
 
Join Date: May 2009
Posts: 94
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
jamort is offline   Reply With Quote
Old 06-27-2009, 04:14 AM   #9
The Beautiful C++ Utopia
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,455
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.
__________________
WARNING: Any and all code samples I post are not tested unless explicitly mentioned otherwise. Use at your own risk.
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Why did the Java creators shoot themselves in the foot?
Elysia is offline   Reply With Quote
Old 06-27-2009, 04:17 AM   #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*/
}; 
__________________
MSDN <- Programmers Haven!

Last edited by legit; 06-27-2009 at 04:23 AM.
legit is offline   Reply With Quote
Old 06-27-2009, 04:18 AM   #11
Registered User
 
Join Date: May 2009
Posts: 94
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()?
jamort is offline   Reply With Quote
Old 06-27-2009, 04:22 AM   #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!
legit is offline   Reply With Quote
Old 06-27-2009, 04:22 AM   #13
Registered User
 
Join Date: May 2009
Posts: 94
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.
jamort is offline   Reply With Quote
Old 06-27-2009, 04:31 AM   #14
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,309
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:
int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
<1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
(l)))*0.5)+0.5)){for(size_t l0000=0,l00000=(size_t)(0x50*(l00-floor(l00)));l0000<l00000;++l0000
)putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}
Sebastiani is offline   Reply With Quote
Old 06-27-2009, 04:40 AM   #15
Registered User
 
Join Date: May 2009
Posts: 94
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.
jamort is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22