Created
April 29, 2021 21:30
-
-
Save eduardonunesp/c3953b651cbdd3914c249a0792087c9b to your computer and use it in GitHub Desktop.
Flatten 2D Matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define BOARD_SIZE 64 | |
typedef enum { | |
COOL = 1 << 0, | |
GREAT = 1 << 1, | |
} op_type_e; | |
typedef struct piece_s { | |
int type; | |
int x, y; | |
} piece_t; | |
piece_t *board[BOARD_SIZE]; | |
void init_board() { | |
int x, y = 0; | |
for (size_t i = 0; i < BOARD_SIZE; i++) | |
{ | |
if (i % 8 == 0 && i > 0) { | |
x = 0; | |
y++; | |
} | |
board[i] = (piece_t*)calloc(sizeof(piece_t), 1); | |
board[i]->type = 0; | |
board[i]->x = x++; | |
board[i]->y = y; | |
} | |
} | |
void clear_board() { | |
for (size_t i = 0; i < BOARD_SIZE; i++) { | |
board[i]->type = 0; | |
} | |
} | |
void set_board_value(int value, int row, int col) { | |
board[row * 8 + col]->type = value; | |
} | |
void move_left(int row, int col, int steps) { | |
int v = board[row * 8 + col]->type; | |
board[row * 8 + col]->type = 0; | |
board[row * 8 + (col - steps)]->type = v; | |
} | |
void move_right(int row, int col, int steps) { | |
int v = board[row * 8 + col]->type; | |
board[row * 8 + col]->type = 0; | |
board[row * 8 + (col + steps)]->type = v; | |
} | |
void print_board() { | |
for (size_t i = 0; i < BOARD_SIZE; i++) { | |
if (i % 8 == 0) { | |
printf("\n"); | |
} | |
printf("[%d](%d,%d)\t", board[i]->type, board[i]->x, board[i]->y); | |
} | |
printf("\n"); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
init_board(); | |
clear_board(); | |
set_board_value(123, 7,7); | |
print_board(); | |
move_left(7,7, 2); | |
print_board(); | |
move_right(7, 5, 1); | |
print_board(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment