[C++] - 6x6 board game

Pages: 12
But if You know, when I can replace the indicators with something better, You could help me :)
Line 112: This is a function declaration, not a function call. Since it's a bool function, you probably want to do something different if the board is full (like exiting the loop).

Why are you dynamically allocating the board? The only reason to do that is if you were to ask the user the dimension of the board they want to play with. Keep it simple and keep the board as as a fixed array.

Also, why are you dynamically allocating number of rows and columns? There is absolutely no reason to do this even if you were to support a variable size board.

You don't delete the menory you allocate. This results in a memory leak.
Don't use dynamic memory if you don't have a good reason to do so.


I consulted this exercise with my friend, and he gave me advice like this. But I agree, it isnt't the simplest way. I tried to change that, but I just can't, program didn't work. How should I change it? I will be grateful, if You give me one tip.
The following code plays. It does not implement two players, nor does it score plays.

For my own sanity, I had to change some of the function and variable names to English.
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
#include <cstdlib>
#include <iostream>
#include <iomanip>

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


void initialize_board (char board[MAX_WIERSZ][MAX_KOLUMNA])
{   for (int i=0; i<MAX_WIERSZ; i++) 
    {   for (int j=0; j<MAX_KOLUMNA; j++)
            board[i][j] = ' ';        
    }
}

void display_board (const char board[MAX_WIERSZ][MAX_KOLUMNA])
{   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;
}

bool validate_field (const char board[MAX_WIERSZ][MAX_KOLUMNA], int row, int col)
{   if (row < 1 || row > MAX_WIERSZ)
    {   cout << "Invalid row" << endl;
        return false;
    }
    if (col < 1 || col > MAX_KOLUMNA)
    {   cout << "Invalid col" << endl;
        return false;
    }
    if (board[row-1][col-1] != ' ')
    {   cout << "Field is occupied" << endl;
        return false;
    }
    return true;    //  okay to place stone here
}

void select_field (char board[MAX_WIERSZ][MAX_KOLUMNA])
{   char litera;
    int row, col;

    do
    {   cout << "podaj wiersz: ";
        cin >> row;
        cout << "podaj kolumne (A-F): ";
        cin >> litera;
        litera = toupper(litera);
        col = litera - 'A' + 1;
    }
    while (! validate_field (board, row, col));    
    board[row-1][col-1] = '#';
}

bool is_board_full (char board[MAX_WIERSZ][MAX_KOLUMNA])
{  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[MAX_WIERSZ][MAX_KOLUMNA]; 
    
    initialize_board (board);
    while (! is_board_full(board))
    {   display_board (board);
        select_field (board);        
    }
    display_board( board );
    system ("pause");
    return 0;
}
You are an awesome guy. Honestly, nowadays human's kindness becomes dead, so I'm really surprised. I used this code and changed something, I will show You tomorrow, and have a little bit questions, and I hope that You will help me :) Thanks for all Your engagement. :)
I'm happy to help when someone makes an effort.

Now, I want to see you add the computer's turn and scoring.

I made two select_field2 function, using rand, it works, but not at all. The program is looping up at some point, I don't know why. also when I type a letter instead a number in "podaj wiersz: ", program loops.
That's my functions:

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

// first 

void select_field2 (char board[MAX_WIERSZ][MAX_KOLUMNA])
{
     char litera;
     int row;
     int col;
     
     do{
     srand(time(NULL));
     row = rand()%6+1;
     litera = rand()%6+65; // ASCII
     //litera = toupper(litera);
     col = litera - 64;
     }
     while (! check_field (board, row, col));    
     board[row-1][col-1] = '#';
     
     cout << "wybrane pole to: " << row << litera << endl;
}

// second

void select_field2(char board[MAX_WIERSZ][MAX_KOLUMNA])
     {
     srand(time(NULL));
     int row = rand()%6+1;
     int co = rand()%6+1;
     char litera;
     if(col == 1)
     litera = 'A';
     if(col == 2)
     litera = 'B';
     if(col == 3)
     litera = 'C';
     if(col == 4)
     litera = 'D';
     if(col == 5)
     litera = 'E';
     if(col == 6)
     litera = 'F';
     
     while (! check_field (board, row, col));    
     board[row-1][col-1] = '#';
      
     cout << "wybrane pole to: " << row << litera << endl;
     }
Line 11,27: You should call srand() only ONCE at the beginning of your program. Otherwise, you reset the RNG to return the same set of numbers each time rand() is called.

I don't see your code for check_field(). I'm assuming it's similar to validate_field(), but without the couts.

Line 44: If check_field() returns false, that while statement is going to loop forwver.
Did you intend a do/while loop here (lines 28-44)?
I had used it once. I'v shown You my two options of function, which do the same. I don't know where my fail is.

My check_field is the same as Your validate_field.

Last edited on
I mean that at some point my program is hanging, when drawing the field six or seven time.
I'm not experiencing any hangs with the following code (only posting modified functions):
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
//  Check computer's turn
bool check_field (const char board[MAX_WIERSZ][MAX_KOLUMNA], int row, int col)
{   //  No need to check that row and col are valid 
    return (board[row-1][col-1] == ' ');
}

void player_turn (char board[MAX_WIERSZ][MAX_KOLUMNA])
{   char litera;
    int row, col;

    do
    {   cout << "podaj wiersz: ";
        cin >> row;
        cout << "podaj kolumne (A-F): ";
        cin >> litera;
        litera = toupper(litera);
        col = litera - 'A' + 1;
    }
    while (! validate_field (board, row, col));    
    board[row-1][col-1] = '#';
}

void computer_turn (char board[MAX_WIERSZ][MAX_KOLUMNA])
{    char litera;
     int row;
     int col;
     
     do
     {  row = rand()%6+1;
        col = rand()%6+1;     
        litera = col + 'A' - 1;
     }
     while (! check_field (board, row, col));    
     board[row-1][col-1] = '#';     
     cout << "Computer move is: " << row << litera << endl;
}

int main()
{   char board[MAX_WIERSZ][MAX_KOLUMNA]; 
    int turn_number = 0;
    
    srand ((unsigned int)time(NULL));
    initialize_board (board);
    while (! is_board_full(board))
    {   display_board (board);
        if (turn_number & 1)
            computer_turn (board);
        else
            player_turn (board);      
        turn_number++;
    }
    cout << "The game is over." << endl;
    display_board (board);
    system ("pause");
    return 0;
}


Now, all you need is to work on scoring.


Last edited on
Yes, I'm going to do this. But when I type in "podaj wiersz: " (row) any letter, program crashed. How can I correct it? And could You explain me this: if (turn_number & 1) ??
Last edited on
But when I type in "podaj wiersz: " (row) any letter, program crashed.

Not sure what you did. All I know is the code I posted above runs to completion.


And could You explain me this: if (turn_number & 1)


That is a bit-wise and. See bitwise operators here:
http://www.cplusplus.com/doc/tutorial/operators/

By doing a bit-wise and of turn_number and 1, we are checking if the lower order bit is set. turn_number increments each time through the loop. As turn_number increments, the low order bit toggles from 0 to 1. By checking if the low order bit is set or not, we're determining whose turn it is. i.e. User plays on even number turns (bit is off). Computer plays on odd number turns (bit is on).


Last edited on
When cmd shows "podaj wiersz: " and I enter no matter which letter, program loops and shows: "podaj wiersz: "podaj kolumne (A-F): ". Thanks :)
Could You help me with counting the score? How should I begin it?
I gave you a description of how to start here:
http://www.cplusplus.com/forum/beginner/148595/#msg779357

I have function 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
void countpoints(char board[MAX_WIERSZ][MAX_KOLUMNA], int row, int col)
{
    int row;
    int col;
	int points =0;
    int counter =0;
	for(int i=0;i<6;i++) //for a col
{
if(board[row[i]=='#') counter++;	
}
if(counter==3) points=points+1;
else if(counter==6) points=points+2;
counter=0;
for(int i=0;i<6;i++) // for a row
{
if(board[i][col]=='#') counter++;	
}
if(counter==3) points=points+1;
else if(counter==6) points=points+2;
counter=0;
if(row==col) //for first diagonal
{
	for(int i=0;i<6;i++)
{
	if(boardi][i]=='#') counter++;
}
if(counter==3) punkty=points+1;
else if(counter==6) points=points+2;
counter=0;
}
if(row+col==5) //for second diagonal
{

		for(int i=0;i<6;i++)
{
if(board[i][i]=='#') counter++;
}
if(counter==3) points=points+1;
else if(counter==6) points=points+2;
}
return points;
}


Did you mean something like this?
Last edited on
Well, something like that. :)

Line 9: You're missing a ] after row.

Lines 9-12, 14-19, etc: You only want to allow a point for 3 in a row, if the position played is one of the three. Consider ##____ and i play position 4-6. Counter will be three, but I don't have three in a row.

Line 11,18,27,38: What if the pattern is ##_#__ and I play the first blank? counter will be 4, but I made 3 in a row.

Line 12,19,28,39: You probably want to check for 6 in a row first.

Line 25: You're missing a [.

Line 27: Where is punkty defined? Do you mean points?

Did you even try to compile this? You have a number of syntax errors.
Topic archived. No new replies allowed.
Pages: 12