[C++] - 6x6 board game

Pages: 12
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 have had something like this yet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 #include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

void draw_board()
{ 
     for (int i=0;i<6;i++)
     {
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << " |"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     cout << i+1 <<"|"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     }
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << "    A     B     C     D     E     F   "<<endl;
     cout << endl << endl;
}

void choose_field()
{
     char sign;
     int line;
     int column;
     cout <<"Line: ";
     cin >> line;
     cout <<"Column: ";
     cin >> column;
          if(column==1)
          {
          sign='A';
          }
          if(column==2)
          {
          sign='B';
          }
          if(column==3)
          {
          sign='C';
          }
          if(column==4)
          {
          sign='D';
          }
          if(column==5)
          {
          sign='E';
          }
          if(column==6)
          {
          sign='F';
          }
          
     if (line>=1 and line<=6 and column>=1 and column<=6)
     cout << "Chosen field is:  " <<sign<<line << endl;
     else 
     cout << "This field doesn't exist: " << endl;
}




int main ()
{
draw_board();
choose_field();
system("pause");
return 0;
}



But it doesn't fix. Thanks for all of the answers.
Last edited on
You use = when you should be using ==.
= is assignment.
== is compare equality.
I've changed it already, but thanks :) I would like to change my code and my drawing function to something compared with array.
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.
Last edited on
Uhmmm, but how can I made my draw_board function related with an array?
Is that so hard to figure out? Iterate through the rows and columns of the board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void draw_board()
{ 
    for (int r=0;r<6;r++)
    {  cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
        cout << " |"; 
        for (int c=0; c<6; c++)
            cout << "  " << board[r][c] < "   |"; 
        cout << endl;
        cout << i+1 <<"|"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     }
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << "    A     B     C     D     E     F   "<<endl;
     cout << endl << endl;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
const int MAX_WIERSZ = 6;
const int MAX_KOLUMNA =6;
char board[MAX_WIERSZ][MAX_KOLUMNA];
int numer_wiersza;
int numer_kolumny;
/*
void rysuj_plansze()
{ 
     for (int i=0;i<6;i++)
     {
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << " |"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     cout << i+1 <<"|"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     }
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << "    A     B     C     D     E     F   "<<endl;
     cout << endl << endl;
}
*/
void inicjuj_plansze()
{  for (int i=0; i<MAX_WIERSZ; i++){
      for (int j=0; j<MAX_KOLUMNA; j++)
        board[i][j] = ' ';
        }
}
void rysuj_plansze()
{ 
    for (int i=0;i<MAX_WIERSZ;i++)
    {  cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
        cout << " |"; 
        for (int j=0; j<MAX_KOLUMNA; j++)
            cout << "  " << board[i][j] << "  |"; 
        cout << endl;
        cout << i+1 <<"|"<< setw(6) <<"|" << setw(6) <<"|" << setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<< setw(6) <<"|"<<endl;
     }
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << "    A     B     C     D     E     F   "<<endl;
     cout << endl << endl;
}


void wybierz_pole()
{
     char znak;
     //int numer_wiersza;
     //int numer_kolumny;
     cout <<"podaj wiersz: ";
     cin >> numer_wiersza;
     cout <<"podaj kolumne (A=1, B=2, C=3, D=4, E=5, F=6): ";
     cin >> numer_kolumny;
          if(numer_kolumny==1)
          {
          znak='A';
          }
          if(numer_kolumny==2)
          {
          znak='B';
          }
          if(numer_kolumny==3)
          {
          znak='C';
          }
          if(numer_kolumny==4)
          {
          znak='D';
          }
          if(numer_kolumny==5)
          {
          znak='E';
          }
          if(numer_kolumny==6)
          {
          znak='F';
          }
          
     if (numer_wiersza>=1 and numer_wiersza<=6 and numer_kolumny>=1 and numer_kolumny<=6)
     cout << "wybrane pole to: " <<znak<<numer_wiersza << endl;
     else 
     cout << "takie pole nie istnieje" << endl;
}

void rysuj_kamien()
{
     board[numer_wiersza-1][numer_kolumny-1] = '#';
     cout << board[MAX_WIERSZ][MAX_KOLUMNA];
}
     
     




int main ()
{
inicjuj_plansze();
rysuj_plansze();
wybierz_pole();
rysuj_kamien();
system("pause");
return 0;
}

I have something like this. But when i type a field for example A5, '#' doesn't appear. What should i change?
Last edited on
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.
Last edited on
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 :)
Last edited on
And how can I generate the computer player moves? And avoid double typing of one field?
Sorry for my stupidity, but I'm new in c++ programming.
What should I do, when I want to change the sign from "#" to ...

You really need to make an attempt to do this yourself. I can show you the code, but you will learn more if you try to do it your self.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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.

And how can I generate two numbers?

http://www.cplusplus.com/reference/cstdlib/rand/
Ok. But now, when I'm typing the field like 5x6 (5F), two symbols appear. My code right now is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
const int MAX_WIERSZ =6;
const int MAX_KOLUMNA =6;
char board[MAX_WIERSZ-1][MAX_KOLUMNA-1];
int numer_wiersza;
int numer_kolumny;


 
void inicjuj_plansze()
{  for (int i=0; i<MAX_WIERSZ; i++){
      for (int j=0; j<MAX_KOLUMNA; j++)
        board[i][j] = ' ';
        }
}
void rysuj_plansze()
{   for (int i=0;i<MAX_WIERSZ;i++)
    {   
        cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
        cout << i+1 <<"|"; 
        for (int j=0; j<MAX_KOLUMNA; j++)
            if (board[i][j] == '#')
              cout << " ### |"; 
            else
              cout << "     |";
        cout << endl;
        cout << " |"; 
        for (int j=0; j<MAX_KOLUMNA; j++)
            if (board[i][j] == '#')
              cout << " ### |"; 
            else
              cout << "     |";
        cout << endl;
       }
     cout << " +-----+-----+-----+-----+-----+-----+"<<endl;
     cout << "    A     B     C     D     E     F   "<<endl;
     cout << endl << endl;
}
void wybierz_pole()
{
     char litera;
     cout <<"podaj wiersz: ";
     cin >> numer_wiersza;
     cout <<"podaj kolumne (A=1, B=2, C=3, D=4, E=5, F=6): ";
     cin >> numer_kolumny;
          if(numer_kolumny==1)
          {
          litera='A';
          }
          if(numer_kolumny==2)
          {
          litera='B';
          }
          if(numer_kolumny==3)
          {
          litera='C';
          }
          if(numer_kolumny==4)
          {
          litera='D';
          }
          if(numer_kolumny==5)
          {
          litera='E';
          }
          if(numer_kolumny==6)
          {
          litera='F';
          }
          
     if (numer_wiersza>=1 and numer_wiersza<=MAX_WIERSZ-1 and numer_kolumny>=1 and numer_kolumny<=MAX_KOLUMNA-1)
     cout << "wybrane pole to: " <<numer_wiersza << litera <<endl;
     else 
     cout << "takie pole nie istnieje" << endl << "podaj inne pole: ";
}

void rysuj_kamien()
{   
     if (numer_wiersza>=1 and numer_wiersza<=MAX_WIERSZ and numer_kolumny>=1 and numer_kolumny<=MAX_KOLUMNA)
     board[numer_wiersza-1][numer_kolumny-1] = '#';
     cout << board[MAX_WIERSZ-1][MAX_KOLUMNA-1];
}
     
     




int main ()
{

inicjuj_plansze();
rysuj_plansze();
wybierz_pole();
rysuj_kamien();
system("cls");
rysuj_plansze();        
wybierz_pole();
rysuj_kamien();
system("cls");
rysuj_plansze();
system("pause");
return 0;
}


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.

I made it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
const int MAX_WIERSZ = 6;
const int MAX_KOLUMNA = 6;
char board[ MAX_WIERSZ ][ MAX_KOLUMNA ];
int numer_wiersza;
int numer_kolumny;



void inicjuj_plansze()
{ for( int i = 0; i < MAX_WIERSZ; i++ ) {
        for( int j = 0; j < MAX_KOLUMNA; j++ )
             board[ i ][ j ] = ' ';
        
    }
}
void rysuj_plansze()
{ for( int i = 0; i < MAX_WIERSZ; i++ )
    {
        cout << " +-----+-----+-----+-----+-----+-----+" << endl;
        cout << i + 1 << "|";
        for( int j = 0; j < MAX_KOLUMNA; j++ )
        if( board[ i ][ j ] == '#' )
             cout << " ### |";
        else
             cout << "     |";
        
        cout << endl;
        cout << " |";
        for( int j = 0; j < MAX_KOLUMNA; j++ )
        if( board[ i ][ j ] == '#' )
             cout << " ### |";
        else
             cout << "     |";
        
        cout << endl;
    }
    cout << " +-----+-----+-----+-----+-----+-----+" << endl;
    cout << "    A     B     C     D     E     F   " << endl;
    cout << endl << endl;
}
void wybierz_pole()
{
    char litera;
    cout << "podaj wiersz: ";
    cin >> numer_wiersza;
    cout << "podaj kolumne (A=1, B=2, C=3, D=4, E=5, F=6): ";
    cin >> numer_kolumny;
    if( numer_kolumny == 1 )
    {
        litera = 'A';
    }
    if( numer_kolumny == 2 )
    {
        litera = 'B';
    }
    if( numer_kolumny == 3 )
    {
        litera = 'C';
    }
    if( numer_kolumny == 4 )
    {
        litera = 'D';
    }
    if( numer_kolumny == 5 )
    {
        litera = 'E';
    }
    if( numer_kolumny == 6 )
    {
        litera = 'F';
    }
    
    if( numer_wiersza >= 1 and numer_wiersza <= MAX_WIERSZ and numer_kolumny >= 1 and numer_kolumny <= MAX_KOLUMNA )
         cout << "wybrane pole to: " << numer_wiersza << litera << endl;
    else
         cout << "takie pole nie istnieje" << endl << "podaj inne pole: ";
    
}

void rysuj_kamien()
{
    if( numer_wiersza >= 1 and numer_wiersza <= MAX_WIERSZ and numer_kolumny >= 1 and numer_kolumny <= MAX_KOLUMNA )
         board[ numer_wiersza - 1 ][ numer_kolumny - 1 ] = '#';
    
    cout << board[ MAX_WIERSZ - 1 ][ MAX_KOLUMNA - 1 ];
}







int main()
{
    
    inicjuj_plansze();
    for (int i=0 ; i<36 ;i++)
    {
    rysuj_plansze();
    wybierz_pole();
    rysuj_kamien();
    system( "cls" );
    }
    rysuj_plansze();
    wybierz_pole();
    rysuj_kamien();
    system( "cls" );
    rysuj_plansze();
    system( "pause" );
    return 0;
}


How can I check if the field is full or no?
You initialized the board to spaces (line 17). Simply check if board[i][j] contains a space or a #.
1
2
3
4
5
6
7
8
9
10
11
inicjuj_plansze();
    for (int i=0 ; i<36 ;i++)
    {
    rysuj_plansze();
    wybierz_pole();
    for( int i = 0; i < MAX_WIERSZ; i++ ) {
        for( int j = 0; j < MAX_KOLUMNA; j++ )
             if (board[ i ][ j ] = ' ') ;
             else cout << "this field is full"; }
    rysuj_kamien();
    system( "cls" );

like this?
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] == ' ')
             return false;  // No point in checking further
      }          
    }
    return true;  // No spaces found
}


Note: Line 8, you're using the assignment operator, not the equality operator.
Last edited on
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?

I have a code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
const int MAX_WIERSZ = 6;
const int MAX_KOLUMNA = 6;


void inicjuj_plansze( char ** board )
{ for( int i = 0; i < MAX_WIERSZ; i++ ) {
        for( int j = 0; j < MAX_KOLUMNA; j++ )
             board[ i ][ j ] = ' ';
        
    }
}
void rysuj_plansze( char ** board )
{ for( int i = 0; i < MAX_WIERSZ; i++ )
    {
        cout << " +-----+-----+-----+-----+-----+-----+" << endl;
        cout << i + 1 << "|";
        for( int j = 0; j < MAX_KOLUMNA; j++ )
        if( board[ i ][ j ] == '#' )
             cout << " ### |";
        else
             cout << "     |";
        
        cout << endl;
        cout << " |";
        for( int j = 0; j < MAX_KOLUMNA; j++ )
        if( board[ i ][ j ] == '#' )
             cout << " ### |";
        else
             cout << "     |";
        
        cout << endl;
    }
    cout << " +-----+-----+-----+-----+-----+-----+" << endl;
    cout << "    A     B     C     D     E     F   " << endl;
    cout << endl << endl;
}
void wybierz_pole( int * numer_wiersza, int * numer_kolumny )
{
    char litera;
    cout << "podaj wiersz: ";
    cin >> * numer_wiersza;
    cout << "podaj kolumne (A=1, B=2, C=3, D=4, E=5, F=6): ";
    cin >> * numer_kolumny;
    if( * numer_kolumny == 1 )
    {
        litera = 'A';
    }
    if( * numer_kolumny == 2 )
    {
        litera = 'B';
    }
    if( * numer_kolumny == 3 )
    {
        litera = 'C';
    }
    if( * numer_kolumny == 4 )
    {
        litera = 'D';
    }
    if( * numer_kolumny == 5 )
    {
        litera = 'E';
    }
    if( * numer_kolumny == 6 )
    {
        litera = 'F';
    }
    
    if( * numer_wiersza >= 1 && * numer_wiersza <= MAX_WIERSZ && * numer_kolumny >= 1 && * numer_kolumny <= MAX_KOLUMNA )
         cout << "wybrane pole to: " << * numer_wiersza << litera << endl;
    else
         cout << "takie pole nie istnieje"<<endl;
    
}

void rysuj_kamien( char ** board, int * numer_wiersza, int * numer_kolumny )
{
    if( * numer_wiersza >= 1 and * numer_wiersza <= MAX_WIERSZ and * numer_kolumny >= 1 and * numer_kolumny <= MAX_KOLUMNA )
         board[ * numer_wiersza - 1 ][ * numer_kolumny - 1 ] = '#';
    
    cout << board[ MAX_WIERSZ - 1 ][ MAX_KOLUMNA - 1 ];
}
bool sprawdzenie_miejsca (char ** board)
{  for (int i=0; i<MAX_WIERSZ;  i++) 
   { for( int j=0;  j<MAX_KOLUMNA; j++)
      {  if (board[i][j] == ' ')
             return false;  // No point in checking further
      }          
    } 
    return true;  // No spaces found
}

int main()
{
    char ** board = new char *[ MAX_WIERSZ ]; //alokacja pamięci dla tablicy dynamicznej
    for( int i = 0; i < MAX_WIERSZ; i++ )
         board[ i ] = new char[ MAX_KOLUMNA ];
    
    int * numer_wiersza = new int;
    int * numer_kolumny = new int;
    
    inicjuj_plansze( board );
    for( int i = 0; i < 36; i++ )
    {
        rysuj_plansze( board );
        wybierz_pole( numer_wiersza, numer_kolumny );
        bool sprawdzenie_miejsca(board);
        rysuj_kamien( board, numer_wiersza, numer_kolumny );
    }
    rysuj_plansze( board );
    system( "pause" );
    return 0;
}

Pages: 12