Hello, I am having a little trouble on thinking how to do this.
The program that I'm working on is a little more than this, but the concept is the same.
I have a string array of five element already declared:
Each number represents a "space" on what is supposedly to be a board, like a checker board.
Then I'm supposed to ask the user to input a "piece" and prompt the user to choose the space it wants the piece to move. So lets say when I run the program it starts like this:
// The P1, P2 & P3 should already be on the space when I run the program, that is one of my questions
P1 P2 03 04 05 06 07 P3
Which piece do you want to move: P1 //lets say
Where do you want to move P1: 03
After the input, P3 is supposedly to randomly move through the board, so it starts at 08 but after the user inputs its move, P3 automatically moves to a random place not occupied by a piece.
It should now look like this, asking again for another movement
01 P2 P1 04 05 P3 07 08
Which piece would you like to move? :
Where do you want to move <<piece;
My questions are, how can I make the P1 and P2 be on the space while keeping the number of the board..
In what way can I tell the program to move the piece to the space the user wants the piece to move? Since the value is not a value of a number, but rather a string, and it's stored in an array.
and How can I write P3's movements??
Your requirements don't seem to be supported by your choice of data structure. I'd say you have change the way the information is stored. Remember, the actual display doesn't have to match the way the data is stored.
im doing it like that because the assignment requires you to use an array, or vector;this is how the board should look like, i was just using that analogy to grasp the concept of inserting something to something like this, Ex a piece to this board.
This is the board without the pieces.
and yes, i want to move randomly but, i don't know how, im barely starting with c++ and i just know the basics, is there something that can look for the string that does not have a piece. ha and how would i put a piece in this board?
I can't really think of some other way of displaying a board :S, maybe cause i haven't sleep trying to do this or maybe cause i really suck at programming... >.<
I know you are trying to guide me in the right way but.. can you give be a hint plz?
how can I make the P1 and P2 be on the space while keeping the number of the board
The problem is in the way the board is represented.
Now, you've used a string. Why string? You were probably driven down that direction because you can display it. But it turns out, you want to be able to represent more than one piece being on the same place on the board.
If the board is 8 x 8, and a piece can appear anywhere on the board, each piece could just have coordiates.
It might be better to represent the board as a 2D array rather than a vector of vectors.
Your print_board() function should then check the piece coordinate against the cell being drawn to decide whether to print it there or not.
Now, if each piece is numbered, piece 1, piece 2 and so on, you represent a piece by its position. So if piece 1 is at board[0][0], then bit 1 in that value will be set. If piece 2 is also at board[0][0] then bit 2 in that value will be set.
If you move piece 1 from board[0][0] to board[0][3], you clear bit 1 from board[0][0] and set it at board[0][1].
When you want to display the board, you check what pieces are set at each position and display something appropriate.
The board is intialised to all zeros. And when you start, you place your pieces on the board in their initial positions. Then you prompt the user for input and act on it.
Back to bits
You can set a bit to indicate a piece being present and clear it to indicate it being abset.
void setbit(int& value, int bit)
{
int mask = 1;
mask <<= bit;
value = value | mask;
}
void clearbit(int& value, int bit)
{
int mask = 1;
mask <<= bit;
value = value & ~mask;
}
bool bitset(int value, int bit)
{
int mask = 1;
mask <<= bit;
return (value & mask) != 0;
}
For example:
1 2 3 4 5 6 7 8 9
int main()
{
int cell = 0; // clear all bits to begin with
setbit(cell, 2); // set bit 2 in cell
bool isset = bitset(cell, 2); // check if bit 2 is set in cell
clearbit(cell, 2); // clear bit 2 in cell
return 0;
}