7 Replies - 11628 Views - Last Post: 25 August 2010 - 09:07 AM Rate Topic: -----

#1 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Using an Array to build a chess board

Posted 24 August 2010 - 01:02 PM

I'm learning about arrays right now and i'm having trouble with this bit of code I just made:

// Making a chess board with 2d arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int ROWS = 8;
	const int COLS = 8;
	const string board[ROWS][COLS]= { 		// initialize the array and create an 8x8 chess board
									{'Rk', 'Kt', 'Bi', 'Qu', 'Kg', 'Bi', 'Kt', 'Rk'},
									{'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn'},
									{'  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '},
									{'  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '},
									{'  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '},
									{'  ', '  ', '  ', '  ', '  ', '  ', '  ', '  '},
									{'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn', 'Pn'},
									{'Rk', 'Kt', 'Bi', 'Qu', 'Kg', 'Bi', 'Kt', 'Rk'} 
									};
	

	cout << "\tHere is a chess board\n\n";
	
	for (int i = 0; i < ROWS; ++i)
	{
		for (int j = 0; j < COLS; ++j)
			cout << board[i][j];
		cout << endl;
	}
	
	return 0;
}



I'm sure there is a cleaner and simpler way to actually make a chess board, but i'm just starting out.
Compiler error says "invalid conversion from int to char" where i'm defining the chess pieces.

Is This A Good Question/Topic? 0
  • +

Replies To: Using an Array to build a chess board

#2 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Re: Using an Array to build a chess board

Posted 24 August 2010 - 01:08 PM

I should use " instead of ', right?

yeah that fixed it. so using ' is to call char and " is for strings? is that what i just learned?

This post has been edited by nimajneb: 24 August 2010 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Using an Array to build a chess board

Posted 24 August 2010 - 01:08 PM

View Postnimajneb, on 24 August 2010 - 02:02 PM, said:

I'm sure there is a cleaner and simpler way to actually make a chess board


No really, at some point all the pieces have to land in the slots.

You need to use double quotes for strings, e.g. {"Rk", "Kt",
Was This Post Helpful? 0
  • +
  • -

#4 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Re: Using an Array to build a chess board

Posted 24 August 2010 - 01:28 PM

ok, so new problem. trying to replace string text on my board to show that a piece has moved.

edit: nm. fixed it. i had string set to const

This post has been edited by nimajneb: 24 August 2010 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Using an Array to build a chess board

Posted 25 August 2010 - 12:36 AM

You can use bit-boards to keep track of a piece's position on the board, you will have to google it for a better explanation, but with a 64 bit integer and 64 squares on the board, you get the idea.

There's probably a really good way to implement bit-boards.

--
There was an excellent article on this somewhere on the web, for x64.

This post has been edited by taylorc8: 25 August 2010 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Using an Array to build a chess board

Posted 25 August 2010 - 05:07 AM

View Posttaylorc8, on 25 August 2010 - 01:36 AM, said:

You can use bit-boards to keep track of a piece's position on the board, you will have to google it for a better explanation, but with a 64 bit integer and 64 squares on the board, you get the idea.


And there are six distinct chess pieces, two sides, or an empty cell. So, any position on the board can contain one of 13 possible values. You may represent that in a nibble, making for 32 bytes. You can avoid a grid entirely, but the overhead of storing position is almost the same.

At the base cost of 64bytes per board, it's hardly worth getting picky about. I'd honestly trade storage for computational overhead in any case; particularly for a chess heuristic.

I'd probably do it like this:
#include<iostream>

using namespace std;

struct BoardPos { int row, col; };

struct BoardMove { BoardPos from, to; };

class Board {
public:
	static const int ROWS=8, COLS=8;
	Board() { init(); }
	void init();
	void clear();
	char getValue(int row, int col) const { return data[row][col]; }
	void show(ostream &out) const;
	void show() const { show(std::cout); }
private:
	char data[ROWS][COLS];
};

void Board::init() {
	clear();
	const char *rank = "RNBQKNBR";
	for(int col=0; col<COLS; col++) {
		data[0][col] = rank[col] + 32;
		data[1][col] = 'p';
		data[6][col] = 'P';
		data[7][col] = rank[col];
	}
}

void Board::clear() {
	for(int row=0; row<ROWS; row++) {
		for(int col=0; col<COLS; col++) {
			data[row][col] = ' ';
		}
	}
}

void Board::show(ostream &out) const {
	for(int row=0; row<ROWS; row++) {
		for(int col=0; col<COLS; col++) {
			out << getValue(row, col) << ' ';
		}
		out << std::endl;
	}
}


int main() {
	Board board;
	board.show();
	cout << sizeof(board) << endl;
	return 0;
}



Just brain storming here. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Re: Using an Array to build a chess board

Posted 25 August 2010 - 07:09 AM

Thanks for the replies! I'm still trying to get the gist of C++, so that was only a bit over my head. I'm versioning out how i'm building this board on my DiC blog, and I think that building it with vectors is next. One day, I'm sure I'll be building it with this method.
Was This Post Helpful? 0
  • +
  • -

#8 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Using an Array to build a chess board

Posted 25 August 2010 - 09:07 AM

I just thought I'd point out that you don't seem to distinguish between black and white pieces. You might want to change your naming conventions to something like "Wp"/"Bp" for pawns instead of "Pn".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1