Thread: Something is wrong.

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Okay work on one problem at a time.

    What exactly are you entering into the program, and what exactly is the expected results with that input?

    When I enter 5 5 1 I get the following output:

    [code]
    $ $ $ $ $
    . . . . .
    1 1 1 1 1
    3 3 3 3 3
    2 2 2 2 2
    [code]
    I did add a printf() of a newline character after the inner for() loop to help see what is going on.

    What would you expect from that input?

  2. #17
    Registered User
    Join Date
    Apr 2020
    Posts
    62
    I expect a $ on the top left in board[0][0] only, Only one # somewhere else. And a 1 in e different cell.Something like this.


    $ . . . .
    . . . # .
    . . . . .
    1 . . . .
    . . . . .

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Righting the ship.

    Read this, several times.
    A development process

    Each time you reach for the ctrl-c/ctrl-v combination on the keyboard, you need to stop and think what you're doing.
    You look at the two copies of the code, and you look at what changed.
    The thing(s) you changed becomes the parameter(s) to a function.

    So instead of 20+ lines to read in 3 numbers, I have one function and 3 separate calls with suitable parameters.

    Sure, placeEnemies doesn't have the convoluted placement 'rules' that your version had, but consider this a foundation on which to build.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MIN_SIZE  4
    #define MAX_SIZE  15
    
    int getNumber(int min, int max) {
      int result;
      do {
        scanf("%d",&result);
      } while ( !(result >= min && result <= max) );
      return result;
    }
    
    void clearBoard(char board[MAX_SIZE][MAX_SIZE], int rows, int cols ) {
      for ( int r = 0 ; r < rows ; r++ ) {
        for ( int c = 0 ; c < cols ; c++ ) {
          board[r][c] = '.';
        }
      }
    }
    
    void placeHero(char board[MAX_SIZE][MAX_SIZE]) {
      board[0][0] = '$';
    }
    
    void placeEnemies(char board[MAX_SIZE][MAX_SIZE], int rows, int cols, int number_of_enemies) {
      for ( int i = 0 ; i < number_of_enemies ; i++ ) {
        int r, c;
        do {
          r = rand() % rows;
          c = rand() % cols;
        } while ( board[r][c] != '.' );
        board[r][c] = i + '0';
      }
    }
    
    void placeObstacles(char board[MAX_SIZE][MAX_SIZE], int rows, int cols, int number_of_obstacles) {
      for ( int i = 0 ; i < number_of_obstacles ; i++ ) {
        int r, c;
        do {
          r = rand() % rows;
          c = rand() % cols;
        } while ( board[r][c] != '.' );
        board[r][c] = '#';
      }
    }
    
    void printBoard(char board[MAX_SIZE][MAX_SIZE], int rows, int cols) {
      for ( int r = 0 ; r < rows ; r++ ) {
        for ( int c = 0 ; c < cols ; c++ ) {
          printf(" %c ",board[r][c]);
        }
        printf("\n");
      }
    }
    
    int calcEnemies(int N, int M, int diff) {
      int levels[] = { 5, 10, 10 };
      return ( (N*M) * levels[diff-1] ) / 100;
    }
    
    int calcObstacles(int N, int M, int diff) {
      int levels[] = { 5, 5, 10 };
      return ( (N*M) * levels[diff-1] ) / 100;
    }
    
    int main(void)
    {
        int N,M,diff,number_of_enemies=0,number_of_obstacles=0,money=0,total_money=0,enemies_to_kill;
        char board[MAX_SIZE][MAX_SIZE];
        char board_interaction;
    
        printf("Give the two dimensions of the board.\n");
        N = getNumber(4,15);
        M = getNumber(4,15);
        printf("Give the number of the difficulty of the game.\n1. Easy\n2. Medium\n3. Hard\n");
        diff = getNumber(1,3);
    
        number_of_enemies = calcEnemies(N, M, diff);
        number_of_obstacles = calcObstacles(N, M, diff);
    
        enemies_to_kill=number_of_enemies;
    
        clearBoard(board,N,M);
        placeHero(board);
        placeObstacles(board,N,M,number_of_obstacles);
        placeEnemies(board,N,M,number_of_enemies);
        printBoard(board,N,M);
    
        printf("\nLevel money spent:%d \n",money);
        printf("Game money spent:%d \n",total_money);
        printf("Make your move(s):");
        scanf(" %c",&board_interaction);
    
        return 0;
    }
    Examples.
    Code:
    $ ./a.out 
    Give the two dimensions of the board.
    4 4
    Give the number of the difficulty of the game.
    1. Easy
    2. Medium
    3. Hard
    3
     $  .  .  . 
     .  .  .  0 
     .  .  .  . 
     .  .  #  . 
    
    Level money spent:0 
    Game money spent:0 
    Make your move(s):$ 
    $ ./a.out 
    Give the two dimensions of the board.
    8 8
    Give the number of the difficulty of the game.
    1. Easy
    2. Medium
    3. Hard
    2
     $  .  .  .  .  .  .  . 
     .  .  .  #  .  1  .  # 
     .  .  .  2  0  .  .  . 
     .  .  .  .  .  .  3  . 
     5  .  4  .  .  .  .  . 
     .  .  .  .  .  .  .  . 
     .  .  .  .  .  .  .  . 
     .  .  .  .  .  .  #  . 
    
    Level money spent:0 
    Game money spent:0 
    Make your move(s):$
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why so wrong numbers? As I write so wrong?
    By Dmy in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2017, 02:10 PM
  2. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM

Tags for this Thread