C++ print chessboard help

C++ print array string

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 7696 Views - Last Post: 12 January 2009 - 09:30 PM Rate Topic: -----

#1 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

C++ print chessboard help

Post icon  Posted 22 December 2008 - 03:45 PM

Hi
I am new to C/C++ and am trying to write chess code that will simply output the start chess position (as a text diagram) to the console. (Later I hope to add a move generator and build from here a simple console chess engine- and that will be pretty challenging. As I do this I will learn more about C/C++ programming).

Anyway here is the code I have now (and when I try compiling & running I get error messages and I have spent quite a while trying to fix this and would appreciate some of your help with this):

#include <iostream>
	 
	 int ChessBoard[8][8];
	 int main()
		
		   {
		 int ChessBoard[8][8] = {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, };
	
	
		for (int i = 0; i<8; i++)
		   for (int j=0; j<8; j++)					 
						   
			{							  
							 
printf	{"'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r',\n"};
printf	{"'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p',\n"};
printf	{"' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n"};
printf	{"' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n"};
printf	{"' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n"};
printf	{"' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n"};
printf	{"'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',\n"};
printf	{"'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R',\n"};
 printf( "\n" );
		  }

	 return 0;
 }



I look forward to help to fix this.Thanks & best wishes

Is This A Good Question/Topic? 0
  • +

Replies To: C++ print chessboard help

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C++ print chessboard help

Posted 22 December 2008 - 04:16 PM

Well, if the only thing you want to do is print it to the screen, not save it anywhere, then you don't even need the int ChessBoard[8][8] = {...

It will print if you change your { and }'s to ( and )'s.
You don't need the for loops at all.
see?
#include <iostream>
     
     int ChessBoard[8][8];
     int main()
        
           {
//         int ChessBoard[8][8] = {
//    {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
//    {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
//    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
//    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
//    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
//    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
//    {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
//    {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, };
//    
//    
              
printf    ("'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r',\n");
printf    ("'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p',\n");
printf    ("' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n");
printf    ("' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n");
printf    ("' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n");
printf    ("' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',\n");
printf    ("'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',\n");
printf    ("'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R',\n");
printf( "\n" );


system("Pause");
     return 0;
}



I added system("Pause") just to pause so we can read the output.

p.s. did you want to do this in C, or C++? It looks to me as if you have a mixture. //I do that too, sometimes! ;)

This post has been edited by OliveOyl3471: 22 December 2008 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: C++ print chessboard help

Posted 22 December 2008 - 04:53 PM

Thanks OliveOyl3471 for your helpful & quick reply.
That was very silly of me to get the wrong braces for the printf function
(& careless to overlook it!).

How do you know that short piece of code is a mix of C & C++?
As I am a beginner to C/C++ programming both look very similar to me & this could get quite confusing. Which would be better for me to start programming mainly with?

Thanks for adding system (Pause); (is getchar (); any better or worse?).

I will try & look at the onsite tutorials to help my learning.

Once again many thanks for your help.Best wishes to you
Was This Post Helpful? 0
  • +
  • -

#4 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C++ print chessboard help

Posted 22 December 2008 - 05:57 PM

You're welcome :D

It's not silly at all. Those little syntax errors vex us all, at one time or another!

I know partly because of what I was taught in C++ class, but mostly from visiting dream.in.code and looking at lots & lots of code. C programs use printf and C++ programs use #include<iostream> . You usually see different header files for a C program.

getchar() is better, but there are other options as well. Here's a discussion about it, which should tell you everything you need to know:
Holding the execution window open


As for which language you should use, that is up to you. I find C++ to be better, mainly because it is newer and therefore most likely to be used more. And I think you can do more with it than you can with C, but I'm no expert on either language, so I cannot say for sure. Also my colleges offer classes in C++ but not in C, so I think C is being deprecated now.

Personally, I like them both! :)
Was This Post Helpful? 0
  • +
  • -

#5 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: C++ print chessboard help

Posted 24 December 2008 - 10:56 AM

Hi OliveOyl3471


Thanks for your further help & useful link.

One of the main reasons I have taken up 'hobby/amateur' programming is around my interest in computer chess.
Most computing languages have some form of chess game/chess related capabilities (parsing chess [ie .pgn files] etc). I started with Java and using Swing/AWT reasonable graphics and GUI results can be achieved.
I have also looked at Perl & it's chess capabilities. Many chess engines are coded in C or C++ or C# and so now I am turning my attention to these languages. However there are many more engines available on the internet than chess GUI (Winboard/Xboard is the C coded main free chess GUI available). There are other chess options in Ruby (Cairo) , Python and others (ie wxWidgets).

I would now like to investigate what graphics capabilities there are with C and C++ (& later C#), other than just displaying the simple text version of the chessboard my code gives. Could you advise of the graphic capabilities in C & C++ please?

Thanks & best wishes
Was This Post Helpful? 0
  • +
  • -

#6 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: C++ print chessboard help

Posted 24 December 2008 - 11:09 AM

It's not necessarily the capabilities of the languages, so much as what type of project you're making. For example, with that type of heading for main -- int main (void) -- that tells me that you're working in a console application. The only thing you get in a Win32 console app is the text based graphics.

The 'cool' fun looking graphics part and using sprites comes into play when you work with engines, DirectX, and OpenGL. You can, however, make some better looking apps by creating a Windows Form application. I would recommend that you use MS Visual Studio (you can dl a free version from their website) in either C++ or C#.
Was This Post Helpful? 0
  • +
  • -

#7 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: C++ print chessboard help

Posted 24 December 2008 - 02:43 PM

Thanks Psionics

I will start to look into OpenGL & creating Windows Form apps for better graphics.
There is a console website tutorial at http://adrianxw.dk/S.../Consoles4.html
which shows a 'normal' looking chessboard with b&w squares and seems to me to suggest this sort of graphic can be achieved with a Win32 Console app. Sadly the tutorial gives no more detail of how to code for this type of graphic. Is that sort of thing possible in Win32 console app?

I have MSVisC++ Express Ed IDE- I must try & use it more! (I also have DevC++ which I use a bit more).

Thanks & best wishes (also have a good Christmas!).
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: C++ print chessboard help

Posted 24 December 2008 - 05:25 PM

For the original poster. You can write a chess program in anything. You see them in C, C++, Java, Javascript, and all things in between. Writing a chess engine is like a rite of passage for some programmers. Writing a really good chess engine, that's hard. If you don't know what your doing, it probably doesn't matter if you're using C or BASIC; it'll be slow.

The problem is more a function of scope than skill. Consider, from the starting position, there are 20 possible moves (each pawn and knight has two options). Then, the other side also has 20. That's 400 possibles before you get past depth 0 analysis. It's a geometric progression of decision trees before you even start crunching the numbers. There's a reason the IBM chess computer comes from the parallel processing devision; it takes a lot of calculations.

If you've played against a modern chess program you'll find they're usually very quick on openings. Even the easiest can clobber you starting out if you're not careful. The reason for this is that they have pre loaded thousands of chess games. If the board should happen to slip into a pattern that's in the database, you could be playing some grandmaster without knowing it.

For a roll your own chess program, you'll want some kind of structure to hold board values. To actually play, you have to make as many boards as their are possible options.

Here's a C++ model a threw together. It's rudimentary and only does printing at the moment, but it illustrates the kind of thing you'd want.

#include <iostream>
using namespace std;

class BoardSquare {
public:
	enum SideType { black, white };
	enum PieceType { king, queen, bishop, knight, rook, pawn, empty };
protected:
	SideType side;
	PieceType piece;
public:
	BoardSquare() : piece(empty) { }
	BoardSquare(SideType side, PieceType piece) : side(side), piece(piece) { }
	SideType getSide() const { return this->side; }
	PieceType getPiece() const { return this->piece; }
	char getDisplayChar() const;
};

char BoardSquare::getDisplayChar() const {
	if (piece==empty) { return ' '; }
	
	char ch;
	if (piece==king) { ch = 'K';
	} else if (piece==queen) { ch = 'Q';
	} else if (piece==bishop) { ch = 'B';
	} else if (piece==knight) { ch = 'N';
	} else if (piece==rook) { ch = 'R';
	} else { ch = 'P';
	}
	return (side==black) ? ch+32 : ch;
}

class Board {
protected:
	static const int SIZE = 64;
	static const int ROWS = 8;
	static const int COLS = 8;
	const BoardSquare Empty;
	BoardSquare *squares[SIZE];
	int calcPos(int row, int col) const { return row*COLS + col; }
public:
	Board();
	~Board();
	const BoardSquare &getSquare(int pos) const;
	const BoardSquare &getSquare(int row, int col) const { return getSquare(calcPos(row, col)); }
	void movePiece(int src, int dst);
	void show() const;
};

Board::~Board() {
	for(int i=0; i<Board::SIZE; i++) { 
		if (this->squares[i] != NULL) {
			delete this->squares[i];
		}
	}
}


Board::Board() {
	for(int i=0; i<Board::SIZE; i++) { 
		this->squares[i] = NULL;
	}

	for(int col=0; col<Board::COLS; col++) { 
		squares[calcPos(1, col)] = new BoardSquare(BoardSquare::black, BoardSquare::pawn);
		squares[calcPos(6, col)] = new BoardSquare(BoardSquare::white, BoardSquare::pawn);
		
		BoardSquare::PieceType piece;
		if (col==0 || col==7) { piece = BoardSquare::rook;
		} else if (col==1 || col==6) { piece = BoardSquare::knight;
		} else if (col==2 || col==5) { piece = BoardSquare::bishop;
		} else if (col==3) { piece = BoardSquare::queen;
		} else { piece = BoardSquare::king;
		}
		squares[calcPos(0, col)] = new BoardSquare(BoardSquare::black, piece);
		squares[calcPos(7, col)] = new BoardSquare(BoardSquare::white, piece);
	}
}

const BoardSquare &Board::getSquare(int pos) const { 
	BoardSquare *sq = this->squares[pos];
	return (sq==NULL) ? Empty : *sq;
}

void Board::show() const {
	cout << " +---+---+---+---+---+---+---+---+" << endl;
	for(int row=0; row<Board::ROWS; row++) { 
		cout << " | ";
		for(int col=0; col<Board::COLS; col++) {
			cout << getSquare(row, col).getDisplayChar() << " | ";
		}
		cout << endl << " +---+---+---+---+---+---+---+---+" << endl;
	} 
}


Was This Post Helpful? 0
  • +
  • -

#9 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: C++ print chessboard help

Posted 26 December 2008 - 09:00 AM

Hi baavgai
Thanks for your reply and extensive code sample.
I will try and study your code to understand it better.
Firstly though I get an error message when I try to compile & run it:
[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
Can you tell me why? (& how to fix this!).

Thanks & best wishes
Was This Post Helpful? 0
  • +
  • -

#10 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: C++ print chessboard help

Posted 27 December 2008 - 01:33 AM

It means your project was set for "Windows GUI" and not a "Console."
Was This Post Helpful? 0
  • +
  • -

#11 seriousazn  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-December 08

Re: C++ print chessboard help

Posted 27 December 2008 - 01:51 AM

Hey you kind of went about outputting it wrong because if you only wanted to output why have the 'int ChessBoard', also a mistake was that you made it 'int' instead of 'char' because the items in your chessboard are letters, not numbers here's your code roughly modified to output the board

#include <iostream>
using std::cout;
	 
	 char ChessBoard[8][8];
	 int main()
	   
		   {
		 char ChessBoard[8][8] = {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, };


	//i = columns j = rows
	int j = 0;
	int rowexit = 0;
	for( int i = 0; i < 8; i++ )	{
	while( rowexit <= i )	{

			if( j = 8 )	{
				j = 0;
				cout << "\n";
				++rowexit;
			}	  

				while( j < 8 )	{
			 	cout << ChessBoard[i][j] << " ";
				++j;
				}
			
		}
	}


		  

	 return 0;
 }


Was This Post Helpful? 0
  • +
  • -

#12 rpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 65
  • Joined: 13-August 07

Re: C++ print chessboard help

Posted 27 December 2008 - 12:43 PM

Hi seriousazn (& Hyper)

First seriousazn,

Your code is good (nice!).Thank you-the chessboard prints to console well now. I am just going through those loops.....

Lastly you have used:
using std::cout;
while baavgai used:
using namespace std;

I need to know why (what these are!).

-Hyper
I still cannot compile baavgai code example in DevC++ IDE (either as empty project or as console app).How can I compile it without that winmain error?


Thanks for your feedback guys! Best wishes
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: C++ print chessboard help

Posted 27 December 2008 - 12:59 PM

The reason he used std::cout is just because he didn't have the using namespace std; line. When you have that line, the std:: is not needed. It's more of a style choice I'd say.

I'll give you a rough explanation/example.

// prints "Hello" to the screen.

std::cout << "Hello" << endl;

//---------------------------------------

// this does the same thing, in a slightly different way.

using namespace std;

cout << "Hello" << endl;


The typical way you'd say the first command verbally is...

namespace to look in::member to look in the namespace for.

Hope this helps! :D

This post has been edited by Locke37: 27 December 2008 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#14 seriousazn  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-December 08

Re: C++ print chessboard help

Posted 27 December 2008 - 03:07 PM

View Postrpd, on 27 Dec, 2008 - 11:43 AM, said:

Hi seriousazn (& Hyper)

First seriousazn,

Your code is good (nice!).Thank you-the chessboard prints to console well now. I am just going through those loops.....

Lastly you have used:
using std::cout;
while baavgai used:
using namespace std;

I need to know why (what these are!).

-Hyper
I still cannot compile baavgai code example in DevC++ IDE (either as empty project or as console app).How can I compile it without that winmain error?


Thanks for your feedback guys! Best wishes

haha basically std::cout replaces your printf() because std::cout is a c++ function and printf() is a c function im guessing? u can change it back if you want i prefer std::cout because its what i'm used to good luck on your chessboard game!
Was This Post Helpful? 0
  • +
  • -

#15 seriousazn  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-December 08

Re: C++ print chessboard help

Posted 27 December 2008 - 03:20 PM

here i replaced the std::cout with printf() so that you would probably understand it better, hopefully im using printf() correctly

**also if you haven't fully comprehended(big word =O) why i didn't use two for() functions it was because it wouldn't have let me modify j because it would have been a local thing i wouldn't be allowed to touch outside of the for()
#include <iostream>
//took away the std::cout
	 
	 char ChessBoard[8][8];
	 int main()
	   
		   {
		 char ChessBoard[8][8] = {
	{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
	{'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
	{'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
	{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, };


	//i = columns j = rows
	int j = 0;
	int rowexit = 0;
	for( int i = 0; i < 8; i++ )	{
	while( rowexit <= i )	{

			if( j = 8 )	{
				j = 0;
				printf( "\n" );
				++rowexit;
			}	  

				while( j < 8 )	{
				 printf( "%c ", ChessBoard[i][j] );

				++j;
				}
		   
		}
	}


		 

	 return 0;
 }



View PostLocke37, on 27 Dec, 2008 - 11:59 AM, said:

The reason he used std::cout is just because he didn't have the using namespace std; line. When you have that line, the std:: is not needed. It's more of a style choice I'd say.

I'll give you a rough explanation/example.

// prints "Hello" to the screen.

std::cout << "Hello" << endl;

//---------------------------------------

// this does the same thing, in a slightly different way.

using namespace std;

cout << "Hello" << endl;


The typical way you'd say the first command verbally is...

namespace to look in::member to look in the namespace for.

Hope this helps! :D

Nice explanation! =D also i used using std::cout because this book i have says its better to be more specific( even though i don't really know why i try to be specific as a good practice for later times )

This post has been edited by seriousazn: 27 December 2008 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3