user3820970
user3820970

Reputation: 3

Updating my array board game

So I'm creating a game. It has a 5 by 5 board filled with characters a, b and c. I need to create a function where if the board detects the same letter next to each other, it disappears and the emptied cells are replaced with a new set of letters (a,b,c). So a bit like the candy crush game. I also need to display the number of moves that are made before the game ends. Here's where I am so far

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 10

//creates board and fills in the letters randomly
int board()
{
    char grid[MAX][MAX];
    char letter[3] = {'a', 'b', 'c'};
    int i,j,row,col;

    printf("Please enter your grid size: ");
    scanf("%d %d", &row, &col);
    if(row < 10 && col < 10){
        for(i=0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                grid[i][j] = letter[rand()%3];
            }
        }
        for(i=0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                printf("%c ", grid[i][j]);
            }
            printf("\n");
        }
    }
    else{
        printf("Board is too big\n");
        board();
    }
    return 0;
}

//the count doesn't quite do what I need it to
int moveCount()
    {
    char s;
    printf("Press s to start: ");
    scanf("%c", &s);
    if(s == 's' || s == 'S'){
        int count;
        int max = 10;
        for(count=1; count < max; count++)
            if(count == max){
                -printf("No more moves can be made");
            }
            else{
                printf("Number of moves made: %d\n", count);
            }
    }
    else{
        printf("That is not s\n");
        moveCount();
    }
}

//Trying to check to make sure that n board is always atleast three cells
int inputCheck(){
    int n, m;
    if(n == 3 || n > 3 && m == 1 || m > 1){
        moveCount();
    }
}

int main()
{
    board();
    inputCheck();
}

What's the best way to implement a function that checks if neighbouring cells are the same and then deletes them. I would imagine doing something like if(myArray[0][0] == 'a' && myArray[0][1] == 'a'{do something}...but i don't know if that's the best way or how I would loop that. Also how to correctly implement a count that displays the move made? I realise this code has a lot of flaws but I'm quite new so go easy please. Thanks for any help or a push in the right direction.

Upvotes: 0

Views: 186

Answers (3)

fingaz
fingaz

Reputation: 231

The board function is set up just fine. As the previous answers said parameters are the best way to check a value if you are going to check them within a different function, if you wish to check them within your function a simple if command would do the trick.

I would not pass an entire array as a parameter, instead I would use a pointer to that specific cell. Then, upon a person choosing a cell they are given a memory address that you could then compare the information stored inside that memory address with the other they are comparing.

Quick Pointer Lesson - * is used to create a pointer. For instance, char *ch = array; would point to the memory address of the entire array. And then through more research you will be able to go to a specific memory address in a 2-D array, such as your board, see what is at that location and compare it to the contents contained in another memory address within your 2-D array.

Why would you want to to this? Since this is not Java, we can about memory management in C and using an entire array as a parameter is the easy but more memory costly way of doing it. Plus, pointers are a fundamental element within most programming languages and knowing them well will make you a much better programmer.

Happy Travels!!

Also this will also be easier to go through your board to say, this person chose this address at array[3][2], there are only four memory address they would be choosing from at that point. Which ever way they choose to go, the memory address will be there and you will be able to compare both with minimal system usage and a quick response.

Upvotes: 0

Aumnayan
Aumnayan

Reputation: 669

In answer to your actual question, something like this would work. This is rather sloppy, but it's my 5 min answer. I assume grid is the actual board, which exists only in your board() function at the moment, so I simply added that as a parameter. AKA You're going to have to make it fit your actual game.

inline int clamp (int v, int min, int max) {
  return (v < min) ? min: (v > max) ? max: v;
}

void place (char ltr, int x, int y, char grid[MAX][MAX])
{
  grid[y][x] = ltr; // TODO: put bounds checking around x & y
  for (int i = clamp(y - 1, 0, MAX); i <= clamp (y + 1, 0, MAX); i++) {
    for (int j = clamp(x - 1, 0, MAX); j <= clamp(x + 1, 0, MAX); j++) {
      if (i != y || j != x && grid[i][j] == ltr) {
        grid[i][j] = '\0';  // TODO: replace null char with desired one.
      }
    }
  }
}

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

A serious bug here:

int n, m;
if(n == 3 || n > 3 && m == 1 || m > 1){

n and m are used uninitialized.

And you need to #include <stdlib.h> for rand()

Upvotes: 1

Related Questions