Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "prototypes.h"
  4. #include <string.h>
  5.  
  6.  
  7. void createBoard(int boardSize, struct slot **upLeft, struct slot **upRight, struct slot **downLeft, struct slot **downRight,struct **board){
  8. srand(time(0));
  9.     //The board is represented as a pointer of pointer to slots
  10.     //This allocates in memory the space for the pointers to each row of the board
  11.  
  12.  
  13.     int x;
  14.     for(int i =0; i< boardSize; i++){
  15.         //This allocates in memory the space for the slots in each row of the board
  16.         board[i] = malloc(boardSize * sizeof(struct slot));
  17.  
  18.         //For each slot it sets up the row and column number
  19.         for(int j=0;j < boardSize; j++){
  20.             board[i][j].row = i;
  21.             board[i][j].column = j;
  22.  
  23.             x = rand()%3;
  24.             //If statements will change slot type depending on value of x
  25.  
  26.               if(x == 0){
  27.                strcpy(board[i][j].slotType, "Hill");
  28.                 }
  29.  
  30.                if (x == 1){
  31.                  strcpy(board[i][j].slotType, "Ground");
  32.                  }
  33.  
  34.                 else if (x == 2){
  35.                  strcpy(board[i][j].slotType, "City");
  36.                   }
  37.  
  38.                 printf("slot type is %s\n",board[i][j].slotType);
  39.             }
  40.  
  41.         }
  42.  
  43.  
  44.     //It sets up the adjacent slots for the slots that are
  45.     //not at the borders. These slots have 4 adjacent elements
  46.     for(int i =1; i< boardSize-1; i++){
  47.         for(int j=1;j < boardSize-1; j++){
  48.             board[i][j].up = &board[i-1][j];
  49.             board[i][j].right = &board[i][j+1];
  50.             board[i][j].down = &board[i+1][j];
  51.             board[i][j].left = &board[i][j-1];
  52.         }
  53.     }
  54.  
  55.     //It sets up the adjacent slots for the slots that are
  56.     //in the first and the last row, except the slots at the edges.
  57.     //
  58.     for(int j = 1; j < boardSize -1; j++){
  59.         //It sets up the adjacent slots for the slots that are in the first row.
  60.         //These slots don't have an adjacent element on top of them
  61.         // because they are on the first row of the board
  62.         board[0][j].right = &board[0][j+1];
  63.         board[0][j].left = &board[0][j-1];
  64.         board[0][j].down = &board[1][j];
  65.  
  66.         //It sets up the adjacent slots for the slots that are in the last row.
  67.         //These slots don't have an adjacent element on down them
  68.         // because they are on the last row of the board
  69.         board[boardSize - 1][j].right = &board[boardSize - 1][j+1];
  70.         board[boardSize -1][j].left = &board[boardSize - 1][j-1];
  71.         board[boardSize - 1][j].up = &board[boardSize - 2][j];
  72.     }
  73.  
  74.     //It sets up the adjacent slots for the slots that are
  75.     //in the first and the last column, except the slots at the edges.
  76.     //
  77.     for(int i = 1; i < boardSize -1; i++){
  78.         //It sets up the adjacent slots for the slots that are in the first column.
  79.         //These slots don't have an adjacent element on the left
  80.         // because they are on the first column of the board
  81.         board[i][0].right = &board[i][1];
  82.         board[i][0].up = &board[i-1][0];
  83.         board[i][0].down = &board[i+1][0];
  84.  
  85.         //It sets up the adjacent slots for the slots that are in the last column.
  86.         //These slots don't have an adjacent element on the right
  87.         // because they are on the last column of the board
  88.         board[i][boardSize-1].left = &board[i][boardSize-2];
  89.         board[i][boardSize -1].up = &board[i -1][boardSize-1];
  90.         board[i][boardSize -1].down = &board[i+1][boardSize -1];
  91.     }
  92.  
  93.  
  94.     //It sets up the adjacent slots for the slot at position (0,0).
  95.     //This only has only 2 adjacent slots: right and down
  96.     board[0][0].right = &board[0][1];
  97.     board[0][0].down = &board[1][0];
  98.  
  99.     //It sets up the adjacent slots for the slot at position (0,boardSize -1).
  100.     //This only has only 2 adjacent slots: left and down
  101.     board[0][boardSize-1].left = &board[0][boardSize-2];
  102.     board[0][boardSize -1].down = &board[1][boardSize -1];
  103.  
  104.     //It sets up the adjacent slots for the slot at position (boarSize -1 ,0).
  105.     //This only has only 2 adjacent slots: up and right
  106.     board[boardSize -1][0].right = &board[boardSize -1][1];
  107.     board[boardSize -1][0].up = &board[boardSize -2][0];
  108.  
  109.     //It sets up the adjacent slots for the slot at position (boarSize -1 ,boardSize-1).
  110.     //This only has only 2 adjacent slots: up and left
  111.     board[boardSize - 1][boardSize-1].up = &board[boardSize-2][boardSize-1];
  112.     board[boardSize - 1][boardSize -1].left = &board[boardSize -1][boardSize -2];
  113.  
  114.  
  115.  
  116.  
  117.  
  118.     //assigns a pointer to slot at position (0, 0)
  119.     *upLeft = &board[0][0];
  120.     //assigns pointer of pointer to slot at position (0, boardSize -1)
  121.     *upRight = &board[0][boardSize -1];
  122.     //assigns pointer of pointer to slot at position ( boardSize -1,)
  123.     *downLeft = &board[boardSize -1][0];
  124.     //assigns pointer of pointer to slot at position (boardSize -1, boardSize -1)
  125.     *downRight = &board[boardSize -1][boardSize -1];
  126.  
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement