Matching game help!!

I encountered the problem for the numbers to remain if they are a pair until the game end. I have no idea on how to do it so please anyone help!?

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

#include <stdio.h>
#include <iostream>
#include <windows.h>
void func();

void main()
{
	func();

}


void func()
{
	int num[4][4]={1,2,3,4,1,2,3,4,5,6,7,8,5,6,7,8};
	int arrayA[8],arrayB[8],arrayC[8],arrayD[8];
	int round,row,col;
	


		
    system("cls");

	printf("Welcome Player2 !");

	printf("\n\n");
	
	printf("**************************************************************************\n");
	printf("GUESS THE TWO LOCATION OF A NUMBER GAME<guess the number behind the zero:>\n");
	printf("**************************************************************************\n");

		
	for(row=0;row<4;row++)
	{
		for(col=0;col<4;col++)
		{
		printf("0\t");
		}

		printf("\n");
	}

	
	printf("\n\n");

	for(round=0; round<8;round++)
	{
	
		do
		{
	do
	{

	flushall();
	printf("Which array would you like to open:<e.g: 1 1>:  ");
	scanf("%d%d",&arrayA[round],&arrayB[round]);

	if(arrayA[round]>4 || arrayB[round]>4 || arrayA[round]<1 || arrayB[round]<1)
	{
		printf("\nInvalid position please retry\n\n");
	}
	
	}
	while(arrayA[round]>4 || arrayB[round]>4 || arrayA[round]<1 || arrayB[round]<1);

	 system("cls");

	
	for(row=0;row<4;row++)
	{
		for(col=0;col<4;col++)
		{
			
	       if(row == arrayA[round]-1 && col == arrayB[round]-1)
			{
				printf("%d\t",num[arrayA[round]-1][arrayB[round]]-1);
			}
			else
			{
			printf("%d\t",0);
			}	

		}
		
		printf("\n");
	}
	 


	do
	{
	printf("Which 2nd array would you like to guess as a match: ");
	scanf("%d%d",&arrayC[round],&arrayD[round]);
	
	if(arrayC[round]>4 || arrayD[round]>4 || arrayC[round]<1 || arrayD[round]<1)
	{
		printf("Invalid position please retry\n");
	}
	else if(arrayA[round]==arrayC[round] && arrayB[round]==arrayD[round])
			{
				printf("You already guess this postion please reselect second position!!");
			}
	flushall();
	printf("\n\n");
	}
	while(arrayC[round]>4 || arrayD[round]>4 || arrayC[round]<1 || arrayD[round]<1 || arrayA[round]==arrayC[round] && arrayB[round]==arrayD[round]);

	
		for(row=0;row<4;row++)
	   {
		for(col=0;col<4;col++)
		{

			if(row == arrayA[round]-1 && col == arrayB[round]-1)
			{
				printf("%d\t",num[arrayA[round]-1][arrayB[round]-1]);
			}
			
			else if(row == arrayC[round]-1 && col == arrayD[round]-1)
			{
				printf("%d\t",num[arrayC[round]-1][arrayD[round]-1]);
			}
		
			else 
			{
			printf("0\t");
			}

		}

		printf("\n");
	
	}
		if(num[arrayA[round]-1][arrayB[round]-1] != num[arrayC[round]-1][arrayD[round]-1])
	{
			Sleep(500);
				system("cls");

				for(row=0;row<4;row++)
				{
					for(col=0;col<4;col++)
					{
						printf("0\t");
					}
					printf("\n");
				}

		}
	}
	while(num[arrayA[round]-1][arrayB[round]-1] != num[arrayC[round]-1][arrayD[round]-1]);
	}



}
You have the 2D num array.

1
2
3
4
5
6
7
    int num[4][4]=
    {
        {1,2,3,4},
        {1,2,3,4},
        {5,6,7,8}, 
        {5,6,7,8}
    };


Consider also keeping another 2D array that indicates when values should be shown.

bool showNum[4][4] = { false }; // initialize

Then, when it's time to display the 'board' you use showNum[i][j] to determine if you should show the user num[i][j] or 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void showBoard(const int num[][4], const bool show[][4])
{
    for ( unsigned y=0; y<4; ++y )
    {
        for ( unsigned x=0; x<4; ++x )
        {
            if ( show[y][x] )
                printf("%d\t", num[y][x]) ;
            else
                printf("0\t") ;
        }
        printf("\n") ;
    }
}

Topic archived. No new replies allowed.