How do i make the board reprint with an X if there is a match

How do i make my board reprint the board but with an 'X' in the section that there is a match, and how do i make the board with randomised numbers?


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 <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


class PairBoard 
{
public:
	
	int r1, c1, r2, c2;
	char comma;
	void initBoard() 
	{
		for (int r = 0; r < 2; r++)
		{
			for (int c = 0; c < 5; c++)
			{
				if ((c % 2) >= 0)
				{
					board[0][0] = 'A';
					board[0][1] = 'B';
					board[0][2] = 'C';
					board[0][3] = 'D';
					board[0][4] = 'E';
					board[1][0] = 'F';
					board[1][1] = 'G';
					board[1][2] = 'H';
					board[1][3] = 'I';
					board[1][4] = 'J';
				}
			}
		}
	}

	void printBoard() {
		for (int r = 0; r < 2; r++)
		{
			for (int c = 0; c < 5; c++)
			{
				if (board[r][c] == 'X')
				{

				}
				else
				{
					cout << board[r][c];
				}
			}
			cout << endl;
		}
	}

	void selection() 
	{
		cout << "Select the first cards row and column, seperated by a comma." << endl;
		cin >> r1 >> comma >> c1;
		cout << "Select the second cards row and column, seperated by a comma." << endl;
		cin >> r2 >> comma >> c2;
		
	}

	void match()
	{
		if (board[r1][c1] == board[r2][c2])
		{
			board[r1][c1] = 'X';
			cout << board[r1][c1];
			cout << "Match" << endl;
		}
		else
		{
			cout << "no match" << endl;
		}
	}
private:
	char board[2][5];
};


int main()
{
	srand((unsigned)time(NULL));
	PairBoard pairBoard;
	pairBoard.initBoard();
	pairBoard.printBoard();
	pairBoard.selection();
	pairBoard.match();

	cin.get();
	cin.clear();
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	return 0;
}
1. In your printBoard function, it looks like you purposefully stop it from printing an X.
1
2
3
4
5
6
7
8
if (board[r][c] == 'X')
{
	// nothing happens here
}
else
{
	cout << board[r][c];
}

So... why not just print out the X?

2. Random letter:
Define as
1
2
3
4
char random_letter()
{
    return rand()%26 + 'A';
}


Call as
board[r][c] = random_letter();


Your initBoard function seems a little unorthodox.

Line 19: You do realize this is always going to be true? This means that every time through the inner loop, you're going to initialize the whole board.

Line 15,17: What is the point of the two for loops? You never use r or c (except in line 19).
Last edited on
Registered users can post here. Sign in or register to post.