C Board  

Go Back   C Board > Community Boards > Projects and Job Recruitment

Reply
 
LinkBack Thread Tools Display Modes
Old 05-26-2007, 11:00 PM   #1
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
the chess project

Hi all, long time, i introduced (in other forum) the idea of programming AI chess, that is; human vs computer (his pc, to be specific). But soon, the idea faded and died.

But, I'm bringing it again now . I wrote a little code and did some skteches, and decided how the board gonna be presented.

The language obviously is C++. The board is gonna be represented in the memory manually (dynamically) within a multi-dimensional array, [8][8].

My idea is to make a generic abstract class Pieces, that has all the pure virtual functions. Then, every piece have its own class, inherited from Pieces. The idea behind is pretty simple as you might think: Make general container (array) and fill it with pieces.

The board is gonna be a type of : Pieces, the generic class. Then we dynamically allocate the individual pieces. The emprty squares, that are in between the white pieces and the black pieces, are represented as null (0) square.

Making a move is pretty much taking the piece and change its memory (according to the desired location on the board), so we first change the memory address, then we nullify the original position (which no more hold any piece).
This is the array sketch, the zero insize a box indicates that it's a null position (no piece in it) :



The next picture below shows what happens to the board when a piece is moved:


And as we know, the possible openings (the very first move) are :
20 = 16 for Pawns + 4 for Knights


this is the code i got to so far (don't pay attention to either functions or data members for now):
Code:
class Pieces
{
protected:
	string m_Name;

public:
	Pieces(string name = "Piece"): m_Name(name){}
	virtual ~Pieces(){}

	virtual string GetName() = 0;
	virtual bool isLegal(Pieces* aPiece) = 0;
};



class Pawn : public Pieces 
{
public:
	Pawn(string name = "Pawn"): Pieces(name){}
	~Pawn(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

class Knight : public Pieces 
{
public:
	Knight(string name = "Knight"): Pieces(name){}
	~Knight(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

class Bishop : public Pieces 
{
public:
	Bishop(string name = "Bishop"): Pieces(name){}
	~Bishop(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

class Rook : public Pieces 
{
public:
	Rook(string name = "Rook"): Pieces(name){}
	~Rook(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

class King : public Pieces 
{
public:
	King(string name = "King"): Pieces(name){}
	~King(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

class Queen : public Pieces 
{
public:
	Queen(string name = "Queen"): Pieces(name){}
	~Queen(){}

	string GetName()
	{
		return m_Name;
	}

	bool isLegal(Pieces* aPiece)
	{
		return 0;
	}
};

int main()
{
	Pieces* board[8][8];
	
	//Allocating white pieces
	board[0][0] = new Rook();
	board[0][1] = new Knight();
	board[0][2] = new Bishop();
	board[0][3] = new Queen();
	board[0][4] = new King();
	board[0][5] = new Bishop();
	board[0][6] = new Knight();
	board[0][7] = new Rook();
	board[1][0] = new Pawn();
	board[1][1] = new Pawn();
	board[1][2] = new Pawn();
	board[1][3] = new Pawn();
	board[1][4] = new Pawn();
	board[1][5] = new Pawn();
	board[1][6] = new Pawn();
	board[1][7] = new Pawn();
	
	//Nullify the empty squares 
	board[2][0] = false;
	board[2][1] = false;
	board[2][2] = false;
	board[2][3] = false;
	board[2][4] = false;
	board[2][5] = false;
	board[2][6] = false;
	board[2][7] = false;

	board[3][0] = false;
	board[3][1] = false;
	board[3][2] = false;
	board[3][3] = false;
	board[3][4] = false;
	board[3][5] = false;
	board[3][6] = false;
	board[3][7] = false;

	board[4][0] = false;
	board[4][1] = false;
	board[4][2] = false;
	board[4][3] = false;
	board[4][4] = false;
	board[4][5] = false;
	board[4][6] = false;
	board[4][7] = false;

	board[5][0] = false;
	board[5][1] = false;
	board[5][2] = false;
	board[5][3] = false;
	board[5][4] = false;
	board[5][5] = false;
	board[5][6] = false;
	board[5][7] = false;

	//Allocation black pieces
	board[6][0] = new Pawn();
	board[6][1] = new Pawn();
	board[6][2] = new Pawn();
	board[6][3] = new Pawn();
	board[6][4] = new Pawn();
	board[6][5] = new Pawn();
	board[6][6] = new Pawn();
	board[6][7] = new Pawn();
	board[7][0] = new Rook();
	board[7][1] = new Knight();
	board[7][2] = new Bishop();
	board[7][3] = new Queen();
	board[7][4] = new King();
	board[7][5] = new Bishop();
	board[7][6] = new Knight();
	board[7][7] = new Rook();

	getch();
	return 0;
}
by looking at the sketch of the array, I could easily allocate the pieces.

Please, share info about what you know about programming chess. If you wanna help please do. And have a positive conversation.
__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
OS: Microsoft XP Media Center
IDE: Microsoft Visual Studio 2005 pro edition
Current Porject: Developing a 2D card game...
Hussain Hani is offline   Reply With Quote
Old 05-27-2007, 12:10 AM   #2
Registered User
 
Join Date: May 2007
Posts: 88
No offense, but that code is kinda funny. You have this nice polymorphic hierarchy going, and then you get to the main function and it's like you've never even heard of a for loop.

Teasing aside, I would suggest making the board itself an object, not just an array of Pieces*. Each piece will, among other things, need to know its own color and have a bitmap of itself for display purposes. Moving the pieces won't be as trivial as you think, there's lots of "what ifs" involved.
UMR_Student is offline   Reply With Quote
Old 05-27-2007, 02:32 AM   #3
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
don't worry about how the code looks i know it's messy. It's just the beginning, I even wrote all the classes and their functions in the same code.

I disagree with you in the bitmap thing. The computer will give you its move just like this :
[0][5] - [5][5] array position

, so on your chess board, you make the computer move. Then you enter your move in the same way.

So you are playing on a "physical" chess board, making your move, entering it, receving computer's move, make it on the board
__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
OS: Microsoft XP Media Center
IDE: Microsoft Visual Studio 2005 pro edition
Current Porject: Developing a 2D card game...
Hussain Hani is offline   Reply With Quote
Old 05-27-2007, 03:00 AM   #4
Registered User
 
Join Date: Oct 2006
Posts: 124
Avoid assigning false to any of the board entries, it suggests that the array contains boolean values, whereas it really containes pointers. If you want to null those, set them to 0 instead. Likewise, when you return a bool from a function, you should return either true or false, not 0.
MWAAAHAAA is offline   Reply With Quote
Old 05-27-2007, 03:56 PM   #5
semi-colon generator
 
ChaosEngine's Avatar
 
Join Date: Sep 2005
Location: Chch, NZ
Posts: 597
some comments
Quote:
Originally Posted by Hussain Hani View Post
The language obviously is C++. The board is gonna be represented in the memory manually (dynamically) within a multi-dimensional array, [8][8].
as previously mention the board should be a class too
Quote:
Originally Posted by Hussain Hani View Post

My idea is to make a generic abstract class Pieces, that has all the pure virtual functions. Then, every piece have its own class, inherited from Pieces. The idea behind is pretty simple as you might think: Make general container (array) and fill it with pieces.
Pieces is the wrong name. The class abstracts a single Piece not many Pieces.
Quote:
Originally Posted by Hussain Hani View Post
Please, share info about what you know about programming chess. If you wanna help please do. And have a positive conversation.
Looking at the rest of your code, I'd get rid of the m_name string. A piece doesn't need to know it's name. That is already encapsulated by the class name. And certainly don't provide a default parameter for the Piece class!

While this might sound like premature optimisation, you really want to be as efficient as possible in chess. Unless you're going to do some really funky AI, (like implementing a self-modifying genetic algorithm that "learns" as it plays), chess is essentially a number crunching exercise.

The general algorithm for a chess AI is like this:
  1. get a list of all legal moves
  2. iterate through the list
  3. for each move, evaluate the "goodness" of the move, i.e. if move results in checkmate for the AI, that's good. If it loses it's queen that's bad.
  4. here's where it gets really complicated and processor intensive. in the last point I said "if a move loses a queen it's bad" but what if that move allows checkmate the move after? So you need recursive iteration.

so let's say you have 15 legal moves available. 3 of them will result in you losing the game in the next move so you can discard them on the first pass. that leaves 12. you must evaluate those 12 and then try to guess what the opposition will do and re-evaluate your own position. A average-to-good chess player will try to think at least 5 moves ahead. So you can see the number of evaluations you must do grows exponentially.

so for our 15 moves we must call evaluate() 15 times, then switch perspective and call it 12 times then for each of the 12 times, we must get a list of possible moves (let's say 10 each for the sake of arguement) and then switch and evaluate the opposition again and then evaluate each of the 120 moves and the 10 possible next moves for each of those...

plus you're essentially doing this for both players. and that's only 3 moves ahead!!

now you can see why IBM builds supercomputers for this!

It's not impossible though. There's lots of good info on chess programming out there, and with a bit of work you should be able to make a decent program that can beat most people. Have a look on gamedev.net. I recall reading a really good article that covers the basics of memory structure and algorithms for chess programs.
__________________
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan

Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
ChaosEngine is offline   Reply With Quote
Old 05-27-2007, 04:19 PM   #6
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
Thanks for your reply. I already read the article, the author did not complete it. He promised that he's gonna an example in Java... nothing is updated.

Updates:
1-I cancelled the "false" assignment for the empty squares, cuz I had a problem returning their name. So I made a new class called Empty, which does not do anything, except assigning it to empty square.

2-Add new data member called "Color", to know the piece's color (white or black).


This is my idea of how you are going to play the game:
1-For your convenience, you let your friend set on the black pieces side of the chess board, with the program infront of him
2-You make move
3-Your friend gives (inputs) the program the move
4-The program gives your friend the computer's move
5-Your friend makes the move on the board

So there will be no ASCII graphical board. The program will just give you or you give it inputd and responds to it and you making the move on the real chess board infront of you
__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
OS: Microsoft XP Media Center
IDE: Microsoft Visual Studio 2005 pro edition
Current Porject: Developing a 2D card game...
Hussain Hani is offline   Reply With Quote
Old 05-27-2007, 04:59 PM   #7
semi-colon generator
 
ChaosEngine's Avatar
 
Join Date: Sep 2005
Location: Chch, NZ
Posts: 597
Quote:
Originally Posted by Hussain Hani View Post
Thanks for your reply. I already read the article, the author did not complete it. He promised that he's gonna an example in Java... nothing is updated.
fair enough, I read it years ago.

Quote:
Originally Posted by Hussain Hani View Post

This is my idea of how you are going to play the game:
1-For your convenience, you let your friend set on the black pieces side of the chess board, with the program infront of him
2-You make move
3-Your friend gives (inputs) the program the move
4-The program gives your friend the computer's move
5-Your friend makes the move on the board

So there will be no ASCII graphical board. The program will just give you or you give it inputd and responds to it and you making the move on the real chess board infront of you
the board graphics are not the problem. if you can get a good chess AI working adding an ascii board will be about 1% of the total work involved!

one thing you might consider before trying chess is implementing Reversi. It's similar to chess from a programming point of view but it's a lot easier to calculate the "goodness".
ChaosEngine is offline   Reply With Quote
Old 05-28-2007, 12:14 AM   #8
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
honestly, yesterday, i tried to implement a simple chess AI on a piece of paper, I did not face hard problems. The biggest problem is to implement a tree of a huge conditional statements, that I get lost in it. I'm trying to get guys working with me, so please if anyone wanna help, he is more than welcome.

I will keep ya guys updated
__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
OS: Microsoft XP Media Center
IDE: Microsoft Visual Studio 2005 pro edition
Current Porject: Developing a 2D card game...
Hussain Hani is offline   Reply With Quote
Old 05-28-2007, 02:33 AM   #9
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,688
Didn't we do this already?
Chess project

Moved.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Displaying a Struct rockstarpirate C++ Programming 16 05-05-2008 09:05 AM
Chess project Hussain Hani Projects and Job Recruitment 45 10-27-2006 06:02 PM
Dynamic Binding gpr1me C++ Programming 1 03-24-2006 09:01 AM
Game Independent Anti-cheat Project Needs Programmers GIA Project Lea Projects and Job Recruitment 3 09-15-2005 07:41 PM


All times are GMT -6. The time now is 07:43 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