Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 20
#define BOARD_SIZE 4
#define ROWS 4
#define COLS 5
void initArray();
void printCards(int arr[], int taken[], int arrSize);
void shuffle(int arr[], int arrSize);
int linearSearch(const int arr[], int arrSize, int taken);
int setCardIndex(int row, int col);
int main() {
int running = 0;
int gameBoard[SIZE] = {0};
int taken[SIZE] = {0};
int initStorage;
int storage[10];
for (initStorage = 0; initStorage < 10; initStorage++)
storage[initStorage] = -1;
printf("\nWelcome to memory!\n");
int cardRow[2], cardCol[2];
initArray(gameBoard);
shuffle(gameBoard, SIZE);
// Put the next taken pair in the next index
int pos = 0;
do {
printf("\n\nFlip two cards!");
printf("\nInput row and column for the first card: ");
scanf("%d %d", &cardRow[0], &cardCol[0]);
printf("\nInput row and column for the second card: ");
scanf("%d %d", &cardRow[1], &cardCol[1]);
int getCard1, getCard2;
getCard1 = gameBoard[setCardIndex(cardRow[0], cardCol[0])];
getCard2 = gameBoard[setCardIndex(cardRow[1], cardCol[1])];
//int foundAt = linearSearch(gameBoard, SIZE, storage[0]);
int foundAt[10];
// Logic wrong here?
int c;
for (c = 0; c < 10; c++) {
int j;
for (j = 0; j < 10; j++) {
foundAt[c] = linearSearch(gameBoard, SIZE, storage[j]);
}
printf("\n\nFound at: %d | %d\n\n", foundAt[c], c);
}
// If the first card is the same as the second card. It is a match.
if (getCard1 == getCard2 &&
/*foundAt == -1 &&*/ // -1 = NOT_FOUND
(cardRow[0] != cardRow[1] || cardCol[0] != cardCol[1])) {
printf("\nMatch!\n");
// Only need one card to compare if it exits in the shuffled array
storage[pos] = getCard1;
taken[pos] = 1;
pos++;
}
/* #### ONLY TO SEE OUTPUT #### REMOVE LATER */
int x = 0;
printf("\nTaken: ");
for (x = 0; x < SIZE; x++)
printf("%d, ", taken[x]);
int z = 0;
printf("\nStorage: ");
for (z = 0; z < 10; z++)
printf("%d, ", storage[z]);
/* #### ONLY TO SEE OUTPUT #### REMOVE LATER */
// Check if the user has picked a position outside of the board
if (cardRow[0] < 0 || cardRow[0] > 3 || cardRow[1] < 0 || cardRow[1] > 3 ||
cardCol[0] < 0 || cardCol[0] > 4 || cardCol[1] < 0 || cardCol[1] > 4) {
printf("\nYou have picked a position outside the board!\n");
}
// If the user has enetered the same row and column twice
if (cardRow[0] == cardRow[1] && cardCol[0] == cardCol[1]) {
printf("\nYou have not choosen two different cards!");
}
else {
printf("\nCard1 is %d and Card2 is %d", getCard1, getCard2);
}
printCards(gameBoard, taken, SIZE);
} while (running != -1);
return 0;
}
void initArray(int arr[]) {
// Fill an array with pairs of 1, 2, 3, ..., 9
int i;
int fillNumbers = 0;
for (i = 0; i < SIZE; i++) {
arr[i] = i;
if (i >= 10) {
arr[i] = fillNumbers;
fillNumbers++;
}
//printf("%d, ", gameBoard[i]);
}
}
int linearSearch(const int arr[], int arrSize, int taken) {
int found = 0;
int location = -1;
int i;
for (i = 0; i < arrSize && !found; i++) {
if (taken == arr[i]) {
found = 1;
location = i;
}
}
return location;
}
void shuffle(int arr[], int arrSize) {
srand((unsigned int)time(NULL));
int i;
for (i = arrSize - 1; i > 0; i--) {
int randNum = rand() % (i + 1);
int temp = arr[i];
arr[i] = arr[randNum];
arr[randNum] = temp;
//printf("%d, ", arr[i]);
}
}
void printCards(int arr[], int taken[], int arrSize) {
int p;
printf("\n");
for (p = 0; p < arrSize; p++) {
printf("%d, ", arr[p]);
}
printf("\n\n 0 1 2 3 4");
printf("\n--|--------------");
int i, rows = 0;
int j = 0;
for (i = 0; i < ROWS; i++) {
if (taken[i] == 0)
printf("\n%d | * ", rows);
else
printf("\n%d | %d ", rows, arr[i]);
for (j = 0; j < ROWS; j++) {
if (taken[i] == 0)
printf(" * ");
else
printf(" %d ", arr[i*COLS + j]); // this should be something like cardRow[0]*COLS + cardCol[0]
}
rows++;
}
}
int setCardIndex(int row, int col) {
int index = row * COLS + col;
return index;
}