Hi, I have a project exercise in c++ language. I have to admit I'm not so good in programming right now, so please try to help me.
The exercise:
Board Game. Board is 6x6 (36 fields). It's for two players (one computer - he's playing random). The players in turns add the stones to the board (all of stones are the same and one stone=one field). If after one player puts the stone on the board and 6 or 3 the stones will be in one line (horizontally, upright or aslant), he will receive 2 points (when it's 6) and 1 point(when it's 3). The game ends, when all of the fields are full.
The board has to look something like this: http://oi62.tinypic.com/2edvyw5.jpg
The stone should be '#', but it's indifferently. Could you give me some advices?
I would like to change my code and my drawing function to something compared with array.
You don't actually have a board allocated in your program, such as:
char board[6][6];
Then you're going to need to initialize the board
1 2 3 4 5
void init_board ()
{ for (int r=0; r<6; r++)
for (int c=0; c<6; c++)
board[r][c] = ' ';
}
Once the user enters the line and column, you're going to need to place a stone on the board:
board[line-1][column-1] = '#';
Then you need to check if the placed stone is one of three or size in a row and adjust the players score accordingly.
You need a computer turn to randomly select an empty field.
You need to check if the computer's turn was one of three or six in a row and adjust the computer's score accordingly.
You need a loop that continues until the board is full.
And you need to change draw_board so that it displays the contents of the board in the proper places.
After you prompt the user for his play (rysuj_kamien), you stop.
You want to display the board again, so you need to call rysuj_plansze again.
1 2 3 4 5 6 7 8 9
int main ()
{ inicjuj_plansze();
rysuj_plansze();
wybierz_pole();
rysuj_kamien();
rysuj_plansze();
system("pause");
return 0;
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.
Thanks. I have a couple of questions again. What should I do, when I want to change the sign from "#" to
###
###
And how can I count the punctation?
Thanks for all your advices :)
void rysuj_plansze()
{ for (int i=0;i<MAX_WIERSZ;i++)
{ // First line
cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
// second line
cout << " |";
for (int j=0; j<MAX_KOLUMNA; j++)
if (board[i][j] == '#')
cout << " ### |";
else
cout << " |";
cout << endl;
// Third line
cout << " |";
for (int j=0; j<MAX_KOLUMNA; j++)
if (board[i][j] == '#')
cout << " ### |";
else
cout << " |";
cout << endl;
}
// Bottom line
cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
cout << " A B C D E F "<<endl;
cout << endl << endl;
}
And how can I count the punctation?
I'm not sure what you mean by that. Are you talking about counting if there are 3 or 6 cells in a row?
how can I generate the computer player moves?
Generate two random numbers (row, col) between 0 and 5.
Test if the cell is occupied. If it is, then loop back and generate another pair of numbers until you find a cell that is not occupied.
Thanks. I'm learning better, when I see the solution. Yes, I'm talking about counting if there are 3 or 6 cells in a row, in a column or aslant. And how can I generate two numbers?
I'm talking about counting if there are 3 or 6 cells in a row, in a column or aslant.
I'm not going to write it for you. Best way to start is work it out on paper.
If the player (or computer) places a marker in column x, then see if all cells in the column are filled (6 in a column). Check if all cells in the row are filled (6 in a row). I'll leave the diagonals to you.
If it's not 6 in a row, then check two cells above are filled or two cells below are filled, check if one above and one below are filled (3 in a col). Do the same for the current row. Keep in mind that one or two cells above and one or two cells below might not be valid locations. Same with 1 or 2 cells to the left or right.
Line 75: You have a problem when you enter 6 for either row or col.
e.g. numer_wiersza<=MAX_WIERSZ-1 compares the row to 5 and returns false causing "takie pole nie istnieje" to be displayed. Because you clear the screen before displaying the board, you don't see the error and the code proceeds with a possibly invalid value.
You need a loop within wybierz_pole so that it only exits when row and col are valid and continues to prompt if they are not valid.
Not really. That's going to display "this field is full" for every cell that is not a space. This is perfect for a function, rather than trying to test inline.
Try this:
1 2 3 4 5 6 7 8 9
bool isBoardFull ()
{ for (int i=0; i<MAX_WIERSZ; i++)
{ for( int j=0; j<MAX_KOLUMNA; j++)
{ if (board[i][j] == ' ')
returnfalse; // No point in checking further
}
}
returntrue; // No spaces found
}
Note: Line 8, you're using the assignment operator, not the equality operator.
I used your code, but where I should make "cout << "this field is full" ?
And when I delete system("cls") I see, that when new board appears, the first line is moved to the right. I modified my code, I used indicators, what do You think?