Jump to content

[C] Gameboard manipulation


WarFox
 Share

Recommended Posts

This is part of a homework assignment I had to code, figured I would share it since it might be some good code to read for anyone who hasn't dabbled in C. We also had to implement this same program in java and c++.

 

Premise of the program: Using structures and pointers, create a gameboard and game pieces. Be able to create game pieces, place them on the board and move them around. Pieces can not be placed into an already occupied space, and can not move into an occupied space. This program takes board locations as elements of array. So 0, 0 is a a place on the board. If there are 5 rows and columns, rows ranged from [0 - 4] and columns ranges from [0 - 4].

 

Spoiler

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 *
 * Structure for individual game pieces. Contains labels for pieces.
 *
 */
struct game_piece
{
	char label[30];
};

/**
 *
 * Structure for gameboard. Contains dimensions of array and the array itself.
 *
 */
struct game_board
{
	struct game_piece **board;
	int rows;
	int columns;
};

/**
 *
 * This function intializes game pieces with default values.
 *
 */
void game_piece_init_default(struct game_piece* piece)
{
	strcpy(piece->label, "---");
}

/**
 *
 * This function initializes game pieces, setting the label of the piece.
 *
 */
void game_piece_init(struct game_piece* piece, char* new_label)
{
	strcpy(piece->label, new_label);
}
/**
 *
 *
 * This function returns the label of an input piece.
 *
 */
char* game_piece_get_label(struct game_piece* piece)
{
    return piece->label;
}

/**
 *
 * This function makes a gameboard friendly representation of the label.
 *
 */
char* game_piece_to_string(struct game_piece* piece)
{
    char* temp = (char*) malloc(3*sizeof(char));
    for (int i = 0; i < 3; i++) 
    {
	    temp[i] = piece->label[i];
    }
    return temp;
}

/**
 *
 * This function initializes the gameboard with default pieces and input dimensions.
 *
 */
void game_board_init(struct game_board* game_board, int rows, int cols)
{
	// Sets dimensions of rows
	game_board->rows = rows;
	game_board->columns = cols;

	// Creates 2d array of board with dimensions
	game_board->board = malloc(sizeof(struct game_piece) * game_board->rows);
	for (int i = 0; i < rows; i++) 
	{
		game_board->board[i] = malloc(sizeof(struct game_piece) * game_board->columns);
	}
	
	// Initializes default game pieces in each element
	for(int r = 0; r < rows; r++) 
	{
		for (int c = 0; c < cols; c++) 
		{
			game_piece_init_default(&game_board->board[r][c]);
		}
	}

}

/**
 *
 * This function checks if input row and column exists.
 *
 */
int game_board_is_space_valid(struct game_board* game_board, int row, int col)
{
    if ((row > -1) || (col > -1)) 
    {
	    if ((row < game_board->rows) && (col < game_board->columns)) 
	    {
		    return 1;
	    } 
	    else 
	    {
		    return 0;
	    }
    } 
    else 
    {
	    return 0;
    }
}

/**
 *
 * This function adds piece to spot if space is valid and not occupied.
 *
 */
int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col)
{
    // Checks if space is valid
    if (game_board_is_space_valid(&*game_board, row, col)) 
    {
	    // checks if space is occupied
	    if (!strcmp(game_piece_get_label(&game_board->board[row][col]), "---")) 
	    {
		    game_piece_init(&game_board->board[row][col], game_piece_get_label(piece));
		    return 1;
	    } 
	    else 
	    {
		    return 0;
	    }
    } 
    else 
    {
	    return 0;
    }
}

/**
 *
 * This function moves space if source exists and destination exists and is unoccupied.
 *
 */
int game_board_move_piece(struct game_board* game_board, int src_row, int src_col, int dest_row, int dest_col)
{
    struct game_piece temp = game_board->board[src_row][src_col];
    if (game_board_add_piece(&*game_board, &temp, dest_row, dest_col)) 
    {
	game_piece_init_default(&game_board->board[src_row][src_col]);
	return 1;
    } 
    else 
    {
	return 0;
    }
}

/**
 *
 * This function prints gameboard in user friendly manner.
 *
 */
void game_board_print(struct game_board* game_board)
{
	printf("Gameboard\n----------\n");
	for (int r = 0; r < game_board->rows; r++) 
	{
		for (int c = 0; c < game_board->columns; c++) 
		{
			printf("%s ", game_piece_to_string(&game_board->board[r][c]));
			// Checks is at end of row
			if (c == game_board->columns - 1) 
			{
				printf("\n");
			}
		}
	}
}

int main()
{
    /* declare local variables */
    int row;
    int col;
    int destRow;
    int destCol;
    int rowNum;
    int colNum;
    struct game_board board;
    struct game_piece piece;
    char input_string[30];

    /* get the size of the game board */
    printf("Please enter the number of rows.\n");
    scanf("%d", &rowNum);

    printf("Please enter the number of columns.\n");
    scanf("%d", &colNum);

    game_board_init(&board, rowNum, colNum);

    /* get the first piece's label */
    printf("Please enter a label for a new piece. Enter \"Q\" when done.\n");
    scanf("%s", input_string);

    while (strcmp(input_string, "Q") != 0 && strcmp(input_string, "q") != 0)
    {
        game_piece_init(&piece, input_string);

        /* get the location to place the piece */
        printf("Please enter a row for the piece.\n");
        scanf("%d", &row);

        printf("Please enter a column for the piece.\n");
        scanf("%d", &col);

        /* verify the space is valid then add the piece to the board */
        if (game_board_is_space_valid(&board, row, col))
        {
            if (game_board_add_piece(&board, &piece, row, col))
            {
                printf("New piece \"%s\" added.\n", game_piece_get_label(&piece));
            }
            else
            {
                printf("A piece is already at that space.\n");
            }
        }
        else
        {
            printf("Invalid row and/or column.\n");
        }

        /* get the label for the next piece */
        printf("Please enter a label for a new piece. Enter \"Q\" when done.");
        scanf("%s", input_string);
    }

    /* print the board and check if user wants to move a piece */
    game_board_print(&board);
    printf("Would you like to move a piece? Enter \"Y\" to move a piece.\n");
    scanf("%s", input_string);

    while (strcmp(input_string, "Y") == 0 || strcmp(input_string, "y") == 0)
    {
        /* get the location of the piece */
        printf("Please enter the piece's row.");
        scanf("%d", &row);

        printf("Please enter the piece's column.");
        scanf("%d", &col);

        /* get the destination for the piece */
        printf("Please enter the piece's new row.");
        scanf("%d", &destRow);

        printf("Please enter the piece's new column.");
        scanf("%d", &destCol);

        /* verify both spaces are valid then move the piece */
        if (game_board_is_space_valid(&board, row, col) &&
            game_board_is_space_valid(&board, destRow, destCol))
        {
            if (game_board_move_piece(&board, row, col, destRow, destCol))
            {
                printf("Piece moved to new space.\n");
            }
            else
            {
                printf("A piece is already in that space.\n");
            }
        }
        else
        {
            printf("A row or column is invalid. No piece moved.\n");
        }

        /* print the board and check if the user wants move another piece */
        game_board_print(&board);
        printf("Would you like to move a piece? Enter \"Y\" to move a piece.\n");
        scanf("%s", input_string);
    }

    return 0;
}

 

 

Edited by WarFox
  • I Like This! 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...