Changing array value

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:

string space[]={"01" , "02", "03", "04", "05", "06", "07" "08"};

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??

The help is really appreciated!


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.
Last edited on
if you want to move randomly where haven't 'piece' you must search in the string where it not found a 'piece'
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.

void print_board(){

string spaces[]={"01", "02", "03","04","05","06","07","08","09","10","11","12","13","14","15","16",
"17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32"};
vector< vector<string> >board(8, vector<string>(8));
int counter =0;
for(int j=0; j<board.size();j++){
for(int i=0; i<board.size();i++){
if(i==j || (i-j)%2==0){board[j][i]="--"; cout << board[j][i];}
else{board[j][i]=spaces[counter];cout <<board[j][i]; counter ++;}
}
cout << endl;
}
}

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?
Try to ignore the requirement to use an array or vector. How would you do it if you had no restrictions?
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.
ok what i did is this

int start_board[8][8]={ 0,H1,0,H2,0,H3,0,H4,
7,0,7,0,7,0,7,0,
0,7,0,7,0,7,0,7,
7,0,7,0,7,0,7,0,
0,7,0,7,0,7,0,7,
7,0,7,0,7,0,7,0,
0,7,0,7,0,7,0,7,
7,0,7,0,FX,0,7,0,};

Where the sevens are the spaces available for moves;
then, what i did was to store a string into the numbers by using this

for (int i=0; i<8 ; i++){
cout<<endl;
for(int j=0; j<8; j++){
switch(board[i][j]){
case 0: piece ="--";break;
case H1: piece ="H1";break;
case H2: piece ="H2";break;
case H3: piece ="H3";break;
case H4: piece ="H4";break;
case FX: piece ="FX";break;
case 7: piece=spaces[counter]; counter++;break;
}
cout << piece;
}}
cout << endl;

That is as far as i've gone. Now my dilema is, how to move the H1 to the next available spot
I was thinking, em maybe using something like this

board[x+1][y+1] = board[x][y];
board[x][y] = 7;

but i am still wondering, when the program asks which piece to move, how will it know where that piece is in the array?
Last edited on
Mmm, a board holding int is ok as long as you're clear what the int represents.

As you ought to know, an int is machine word and is a sequence of bits.
1
2
3
4
4 3 2 1 0 (bit position)
─┬─┬─┬─┬─┐
 │0│0│0│0│
─┴─┴─┴─┴─┘


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
}
Topic archived. No new replies allowed.