Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "prototypes.h"
- #include <string.h>
- void createBoard(int boardSize, struct slot **upLeft, struct slot **upRight, struct slot **downLeft, struct slot **downRight,struct **board){
- srand(time(0));
- //The board is represented as a pointer of pointer to slots
- //This allocates in memory the space for the pointers to each row of the board
- int x;
- for(int i =0; i< boardSize; i++){
- //This allocates in memory the space for the slots in each row of the board
- board[i] = malloc(boardSize * sizeof(struct slot));
- //For each slot it sets up the row and column number
- for(int j=0;j < boardSize; j++){
- board[i][j].row = i;
- board[i][j].column = j;
- x = rand()%3;
- //If statements will change slot type depending on value of x
- if(x == 0){
- strcpy(board[i][j].slotType, "Hill");
- }
- if (x == 1){
- strcpy(board[i][j].slotType, "Ground");
- }
- else if (x == 2){
- strcpy(board[i][j].slotType, "City");
- }
- printf("slot type is %s\n",board[i][j].slotType);
- }
- }
- //It sets up the adjacent slots for the slots that are
- //not at the borders. These slots have 4 adjacent elements
- for(int i =1; i< boardSize-1; i++){
- for(int j=1;j < boardSize-1; j++){
- board[i][j].up = &board[i-1][j];
- board[i][j].right = &board[i][j+1];
- board[i][j].down = &board[i+1][j];
- board[i][j].left = &board[i][j-1];
- }
- }
- //It sets up the adjacent slots for the slots that are
- //in the first and the last row, except the slots at the edges.
- //
- for(int j = 1; j < boardSize -1; j++){
- //It sets up the adjacent slots for the slots that are in the first row.
- //These slots don't have an adjacent element on top of them
- // because they are on the first row of the board
- board[0][j].right = &board[0][j+1];
- board[0][j].left = &board[0][j-1];
- board[0][j].down = &board[1][j];
- //It sets up the adjacent slots for the slots that are in the last row.
- //These slots don't have an adjacent element on down them
- // because they are on the last row of the board
- board[boardSize - 1][j].right = &board[boardSize - 1][j+1];
- board[boardSize -1][j].left = &board[boardSize - 1][j-1];
- board[boardSize - 1][j].up = &board[boardSize - 2][j];
- }
- //It sets up the adjacent slots for the slots that are
- //in the first and the last column, except the slots at the edges.
- //
- for(int i = 1; i < boardSize -1; i++){
- //It sets up the adjacent slots for the slots that are in the first column.
- //These slots don't have an adjacent element on the left
- // because they are on the first column of the board
- board[i][0].right = &board[i][1];
- board[i][0].up = &board[i-1][0];
- board[i][0].down = &board[i+1][0];
- //It sets up the adjacent slots for the slots that are in the last column.
- //These slots don't have an adjacent element on the right
- // because they are on the last column of the board
- board[i][boardSize-1].left = &board[i][boardSize-2];
- board[i][boardSize -1].up = &board[i -1][boardSize-1];
- board[i][boardSize -1].down = &board[i+1][boardSize -1];
- }
- //It sets up the adjacent slots for the slot at position (0,0).
- //This only has only 2 adjacent slots: right and down
- board[0][0].right = &board[0][1];
- board[0][0].down = &board[1][0];
- //It sets up the adjacent slots for the slot at position (0,boardSize -1).
- //This only has only 2 adjacent slots: left and down
- board[0][boardSize-1].left = &board[0][boardSize-2];
- board[0][boardSize -1].down = &board[1][boardSize -1];
- //It sets up the adjacent slots for the slot at position (boarSize -1 ,0).
- //This only has only 2 adjacent slots: up and right
- board[boardSize -1][0].right = &board[boardSize -1][1];
- board[boardSize -1][0].up = &board[boardSize -2][0];
- //It sets up the adjacent slots for the slot at position (boarSize -1 ,boardSize-1).
- //This only has only 2 adjacent slots: up and left
- board[boardSize - 1][boardSize-1].up = &board[boardSize-2][boardSize-1];
- board[boardSize - 1][boardSize -1].left = &board[boardSize -1][boardSize -2];
- //assigns a pointer to slot at position (0, 0)
- *upLeft = &board[0][0];
- //assigns pointer of pointer to slot at position (0, boardSize -1)
- *upRight = &board[0][boardSize -1];
- //assigns pointer of pointer to slot at position ( boardSize -1,)
- *downLeft = &board[boardSize -1][0];
- //assigns pointer of pointer to slot at position (boardSize -1, boardSize -1)
- *downRight = &board[boardSize -1][boardSize -1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement