forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic21
151 lines (134 loc) · 4.21 KB
/
magic21
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
/*
THIS PROGRAM SIMULATES A MAGIC TRICK WITH 21 CARDS
by: randerson112358
*/
# include <stdio.h>
void shuffle(int arr[][3], int arr2[][3]);
void updateArray (int arr[][3], int arr2[][3]);
void swap(int arr[][3], int x, int y);
void printMatrix(int arr[][3]);
int main(void)
{
int inRow=0;
int turn=0;
int TRUE = 1, FALSE=0;
//Creating 7 X 3 MATRIX
int arr[7][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
{13,14,15},
{16,17,18},
{19,20,21}};
int arr2[7][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
{13,14,15},
{16,17,18},
{19,20,21}};
printf("Welcome to the Magic game\n");
do{
printf("Please remember a number in this matrix\n");
printMatrix(arr2);
printf("Please Enter the row number your number is in: ");
scanf("%d",&inRow);
if(inRow == 2)
{
shuffle(arr,arr2);
}
else if(inRow == 1)
{
swap(arr2,inRow-1,1);
updateArray(arr,arr2); // arr = arr2
shuffle(arr,arr2);
}
else if(inRow == 3)
{
swap(arr2,inRow-1,1);
updateArray(arr,arr2);
shuffle(arr,arr2);
}
//TO ALLOW A USER TO CHOOSE THE MIDDLE CARD ON FIRST TURN
if(inRow == 2 && turn !=0)
TRUE = FALSE;
turn++;
system("cls");//clear screen windows command (optional)
}while(TRUE );
//THE LAST SHUFFLE AFTER THE USER INPUTS 2ND ROW, THIS SHUFFLE WILL PLACE THE NUMBER IN SPOT 11
//the last shuffle after they tell the card is in the 2nd row
printMatrix(arr2);
printf("Please Enter the row number your number is in: ");
scanf("%d",&inRow);
if(inRow == 2)// if the card is already in the second row, no need to change rows for correct shuffle
{
shuffle(arr,arr2);
}
else if(inRow == 1)// swap row 1 with row 2, in matrix its row 0 with row 1
{
swap(arr2,inRow-1,1);
updateArray(arr,arr2); // arr = arr2
shuffle(arr,arr2);
}
else if(inRow == 3)// swap row 3 with row 2, in matrix its row 2 with row 1
{
swap(arr2,inRow-1,1);
updateArray(arr,arr2);
shuffle(arr,arr2);
}
printf("Your number is %d, I just read your mind FOOL! \n", arr[3][1]);// the answer is always the 11th card which is located in arr[3][1]
// note if I were to print the matrix right now, the users number would be in the 11th spot arr[3][1]
//printMatrix(arr2); //<---- uncomment if u dont believe me
system("pause");
}
/////////////////////////////////////////// FUNCTIONS///////////////////////////////////////////////////////////////////
// Does the shuffling according to 1row then second row then third row
void shuffle(int arr[][3], int arr2[][3])
{
int i,j,k=0,count =1;
for(j=0; j<3; j++)
{
for(i=0; i<7; i++){
arr2[k][((i%3)+j)%3] = arr[i][j];
if(count%3 == 0)
k++;
count++;
}
}
updateArray(arr,arr2);
}
//This updates matrix arr, arr = arr2 where arr2 is the new matrix
void updateArray (int arr[][3], int arr2[][3])
{
int i,j;
for(i=0; i<7; i++)
for(j=0; j<3; j++)
arr[i][j] = arr2[i][j];
}
//swap() swaps the columns of the matrix so that the shuffle works properly
void swap(int arr[][3], int x, int y)
{
int i=0;
for(i=0; i<7; i++)
{
int temp = arr[i][x];
arr[i][x] = arr[i][y];
arr[i][y] = temp;
}
}
//prints the matrix
void printMatrix(int arr[][3])
{
int i,j;
printf("Row1\tRow2\tRow3\t\n");
for(i=0;i<7;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t", arr[i][j]);
}
printf("\n");
}//*/
}