Browse Source

Added Bag code

Rick Novile 2 months ago
parent
commit
7d3c579aca
11 changed files with 172 additions and 100 deletions
  1. 12
    12
      README.md
  2. 1
    1
      makefile
  3. 53
    17
      src/bag.c
  4. 12
    0
      src/bag.h
  5. 1
    0
      src/common.h
  6. 8
    1
      src/main.c
  7. 38
    38
      src/piece.c
  8. 29
    29
      src/piece.h
  9. 17
    0
      src/standard.tvb
  10. 0
    1
      src/string.c
  11. 1
    1
      src/structs.h

+ 12
- 12
README.md View File

@@ -4,15 +4,15 @@ Coming Soon TM
4 4
 
5 5
 TODO:
6 6
 
7
--Most things
8
--Sound
9
--Graphics
10
--Input
11
--File handling (saving and loading)
12
--INI Parsing
13
--Optimizing and fixing up memory problems (Y'see, I'm *very* good at C...)
14
--Gamerule handling
15
--Main game logic loop
16
--Scoring system
17
--Adding more defines for things like default screen size
18
--Get rid of the second TODO in main.c (but I'm too lazy right *Now*)
7
+- Most things
8
+- Sound
9
+- Graphics
10
+- Input
11
+- File handling (saving and loading)
12
+- INI Parsing
13
+- Optimizing and fixing up memory problems (Y'see, I'm *very* good at C...)
14
+- Gamerule handling
15
+- Main game logic loop
16
+- Scoring system
17
+- Adding more defines for things like default screen size
18
+- Get rid of the second TODO in main.c (but I'm too lazy right *Now*)

+ 1
- 1
makefile View File

@@ -10,7 +10,7 @@ vpath %.c $(SEARCHPATH)
10 10
 vpath %.h $(SEARCHPATH)
11 11
 
12 12
 _OBJS += main.o
13
-_OBJS += piece.o string.o util.o
13
+_OBJS += piece.o string.o util.o bag.o
14 14
 
15 15
 OBJS = $(patsubst %,$(OUT)/%,$(_OBJS))
16 16
 

+ 53
- 17
src/bag.c View File

@@ -1,32 +1,68 @@
1 1
 #include "bag.h"
2 2
 
3
-//TODO: Add functions to bag.h, fix get_piece (I'm rather certain it *doesn't* work, but I haven't tested it yet)
4
-int get_s(Bag* bag) {
5
-    return bag->data[1];
3
+int get_bag_s(Bag* bag) {
4
+    return substring_to_int(bag->data, 1, 2);
6 5
 }
7 6
 
8
-int get_i(Bag* bag) {
9
-    return bag->state[0];
7
+int get_bag_i(Bag* bag) {
8
+    return bag->index;
10 9
 }
11 10
 
12
-int get_q(Bag* bag) {
13
-    return bag->sequence[get_i(bag)];
11
+int get_bag_q(Bag* bag) {
12
+    return bag->sequence[get_bag_i(bag)];
14 13
 }
15 14
 
16
-void inc_i(Bag* bag) {
17
-    bag->state[0]++;
15
+void set_bag_i(Bag* bag, int input) {
16
+    bag->index = input;
18 17
 }
19 18
 
20
-//This likely doesn't work, since the char* being returned will be blank outside of the function scope
21
-//Should replace with a solution ala get_substring or int_to_str in string.c
22
-//Although... I haven't tested this yet, so maybe it does work
19
+void inc_bag_i(Bag* bag) {
20
+    bag->index++;
21
+}
23 22
 
24
-//But probably not
25 23
 char* get_piece(Bag* bag) {
26
-    int piece_index = get_q(bag);
27
-    int piece_states = get_substring(bag->data, piece_index + 3, piece_index + 4);
28
-    int piece_size = get_substring(bag->data, piece_index + 5, piece_index + 6);
29
-    int piece_end = index + (piece_size * piece_size * piece_states);
24
+    int piece_index = get_bag_q(bag);
25
+    int piece_states = substring_to_int(bag->data, piece_index + 3, piece_index + 4);
26
+    int piece_size = substring_to_int(bag->data, piece_index + 5, piece_index + 6);
27
+    int piece_end = 6 + piece_index + (piece_size * piece_size * piece_states);
30 28
     char* piece_str = get_substring(bag->data, piece_index, piece_end);
31 29
     return piece_str;
30
+}
31
+
32
+char* draw_piece(Bag* bag) {
33
+    int size = get_bag_s(bag);
34
+    int index = get_bag_i(bag);
35
+    char* piece = get_piece(bag);
36
+    if(index + 1 >= size) {
37
+        set_bag_i(bag, 0);
38
+        randomize_sequence(bag);
39
+    } else {
40
+        inc_bag_i(bag);
41
+    }
42
+    return piece;
43
+}
44
+
45
+void randomize_sequence(Bag* bag) {
46
+    int size = get_bag_s(bag);
47
+    for(int first_index = 0; first_index < size - 1; first_index++) {
48
+        int second_index = first_index + rand() / (RAND_MAX / (size - first_index) + 1);
49
+        int holder = bag->sequence[second_index];
50
+        bag->sequence[second_index] = bag->sequence[first_index];
51
+        bag->sequence[first_index] = holder;
52
+    }
53
+}
54
+
55
+void build_bag(Bag* bag, char* input) {
56
+    bag->index = 0;
57
+    bag->data = input;
58
+    int bag_size = get_bag_s(bag);
59
+    bag->sequence = malloc(sizeof(int) * bag_size);
60
+    int current_index = 3;
61
+    for(int current_piece = 0; current_piece < get_bag_s(bag); current_piece++) {
62
+        int states = substring_to_int(bag->data, current_index + 3, current_index + 4);
63
+        int size = substring_to_int(bag->data, current_index + 5, current_index + 6);
64
+        bag->sequence[current_piece] = current_index;
65
+        current_index += states * size * size + 7;
66
+    }
67
+    randomize_sequence(bag);
32 68
 }

+ 12
- 0
src/bag.h View File

@@ -2,7 +2,19 @@
2 2
 #define tivien_bag_h
3 3
 
4 4
 #include "common.h"
5
+#include "string.h"
5 6
 
7
+int get_bag_s(Bag* bag);
8
+int get_bag_i(Bag* bag);
9
+int get_bag_q(Bag* bag);
6 10
 
11
+void set_bag_i(Bag* bag, int input);
12
+
13
+void inc_bag_i(Bag* bag);
14
+
15
+char* get_piece(Bag* bag);
16
+char* draw_piece(Bag* bag);
17
+void build_bag(Bag* bag, char* input);
18
+void randomize_sequence(Bag* bag);
7 19
 
8 20
 #endif

+ 1
- 0
src/common.h View File

@@ -5,6 +5,7 @@
5 5
 #include <stdlib.h>
6 6
 #include <string.h>
7 7
 #include <limits.h>
8
+#include <time.h>
8 9
 
9 10
 #include "structs.h"
10 11
 #include "defines.h"

+ 8
- 1
src/main.c View File

@@ -17,7 +17,14 @@
17 17
 #include "structs.h"
18 18
 #include "defines.h"
19 19
 #include "common.h"
20
+#include "bag.h"
20 21
 
21 22
 int main(int argc, char** argv) {
22
-
23
+    srand(time(NULL));
24
+    char* test_bag_data = "0030070403001111000010010011000111100110010010005040301011100001001101000011101001011001000401021111";
25
+    Bag test_bag;
26
+    build_bag(&test_bag, test_bag_data);
27
+    char* test_piece = get_piece(&test_bag);
28
+    printf("%s\n", test_piece);
29
+    return 0;
23 30
 }

+ 38
- 38
src/piece.c View File

@@ -24,120 +24,120 @@ void build_piece(Piece* piece, char* data_str) {
24 24
     }
25 25
 }
26 26
 
27
-int get_x(Piece* piece) {
27
+int get_piece_x(Piece* piece) {
28 28
     return piece->state[0];
29 29
 }
30 30
 
31
-int get_y(Piece* piece) {
31
+int get_piece_y(Piece* piece) {
32 32
     return piece->state[1];
33 33
 }
34 34
 
35
-int get_r(Piece* piece) {
35
+int get_piece_r(Piece* piece) {
36 36
     return piece->state[2];
37 37
 }
38 38
 
39
-int get_v(Piece* piece) {
39
+int get_piece_v(Piece* piece) {
40 40
     return piece->state[3];
41 41
 }
42 42
 
43
-int get_o(Piece* piece) {
43
+int get_piece_o(Piece* piece) {
44 44
     return piece->state[4];
45 45
 }
46 46
 
47
-int get_m(Piece* piece) {
47
+int get_piece_m(Piece* piece) {
48 48
     return piece->data[0];
49 49
 }
50 50
 
51
-int get_c(Piece* piece) {
51
+int get_piece_c(Piece* piece) {
52 52
     return piece->data[1];
53 53
 }
54 54
 
55
-int get_s(Piece* piece) {
55
+int get_piece_s(Piece* piece) {
56 56
     return piece->data[2];
57 57
 }
58
-int get_z(Piece* piece) {
58
+int get_piece_z(Piece* piece) {
59 59
     return piece->data[3];
60 60
 }
61 61
 
62
-void set_x(Piece* piece, int input) {
62
+void set_piece_x(Piece* piece, int input) {
63 63
     piece->state[0] = input;
64 64
 }
65 65
 
66
-void set_y(Piece* piece, int input) {
66
+void set_piece_y(Piece* piece, int input) {
67 67
     piece->state[1] = input;
68 68
 }
69 69
 
70
-void set_r(Piece* piece, int input) {
70
+void set_piece_r(Piece* piece, int input) {
71 71
     piece->state[2] = input;
72 72
 }
73 73
 
74
-void set_v(Piece* piece, int input) {
74
+void set_piece_v(Piece* piece, int input) {
75 75
     piece->state[3] = input;
76 76
 }
77 77
 
78
-void set_o(Piece* piece, int input) {
78
+void set_piece_o(Piece* piece, int input) {
79 79
     piece->state[4] = input;
80 80
 }
81 81
 
82
-void set_m(Piece* piece, int input) {
82
+void set_piece_m(Piece* piece, int input) {
83 83
     piece->data[0] = input;
84 84
 }
85 85
 
86
-void set_c(Piece* piece, int input) {
86
+void set_piece_c(Piece* piece, int input) {
87 87
     piece->data[1] = input;
88 88
 }
89 89
 
90
-void set_s(Piece* piece, int input) {
90
+void set_piece_s(Piece* piece, int input) {
91 91
     piece->data[2] = input;
92 92
 }
93 93
 
94
-void set_z(Piece* piece, int input) {
94
+void set_piece_z(Piece* piece, int input) {
95 95
     piece->data[3] = input;
96 96
 }
97 97
 
98
-void inc_x(Piece* piece) {
99
-    set_x(piece, get_x(piece) + 1);
98
+void inc_piece_x(Piece* piece) {
99
+    set_piece_x(piece, get_piece_x(piece) + 1);
100 100
 }
101 101
 
102
-void inc_y(Piece* piece) {
103
-    set_y(piece, get_y(piece) + 1);
102
+void inc_piece_y(Piece* piece) {
103
+    set_piece_y(piece, get_piece_y(piece) + 1);
104 104
 }
105 105
 
106
-void inc_r(Piece* piece) {
107
-    set_r(piece, get_r(piece) + 1);
106
+void inc_piece_r(Piece* piece) {
107
+    set_piece_r(piece, get_piece_r(piece) + 1);
108 108
 }
109 109
 
110
-void dec_x(Piece* piece) {
111
-    set_x(piece, get_x(piece) - 1);
110
+void dec_piece_x(Piece* piece) {
111
+    set_piece_x(piece, get_piece_x(piece) - 1);
112 112
 }
113 113
 
114
-void dec_y(Piece* piece) {
115
-    set_y(piece, get_y(piece) - 1);
114
+void dec_piece_y(Piece* piece) {
115
+    set_piece_y(piece, get_piece_y(piece) - 1);
116 116
 }
117 117
 
118
-void dec_r(Piece* piece) {
119
-    set_r(piece, get_r(piece) - 1);
118
+void dec_piece_r(Piece* piece) {
119
+    set_piece_r(piece, get_piece_r(piece) - 1);
120 120
 }
121 121
 
122
-int get_cell(Piece* piece, int x, int y, int r) {
123
-    int size = get_z(piece);
122
+int get_piece_cell(Piece* piece, int x, int y, int r) {
123
+    int size = get_piece_z(piece);
124 124
     int cell_index = (size * size * r) + (size * y) + x;
125 125
     char output = piece->matrix[cell_index] - '0';
126 126
     return (int)output;
127 127
 }
128 128
 
129 129
 void rotate_piece_cw(Piece* piece) {
130
-    if(get_r(piece) >= get_s(piece) - 1) {
131
-        set_r(piece, 0);
130
+    if(get_piece_r(piece) >= get_piece_s(piece) - 1) {
131
+        set_piece_r(piece, 0);
132 132
     } else {
133
-        inc_r(piece);
133
+        inc_piece_r(piece);
134 134
     }
135 135
 }
136 136
 
137 137
 void rotate_piece_ccw(Piece* piece) {
138
-    if(get_r(piece) <= 0) {
139
-        set_r(piece, get_s(piece) - 1);
138
+    if(get_piece_r(piece) <= 0) {
139
+        set_piece_r(piece, get_piece_s(piece) - 1);
140 140
     } else {
141
-        dec_r(piece);
141
+        dec_piece_r(piece);
142 142
     }
143 143
 }

+ 29
- 29
src/piece.h View File

@@ -7,34 +7,34 @@
7 7
 
8 8
 void build_piece(Piece* piece, char* data_str);
9 9
 
10
-int get_x(Piece* piece);
11
-int get_y(Piece* piece);
12
-int get_r(Piece* piece);
13
-int get_v(Piece* piece);
14
-int get_o(Piece* piece);
15
-int get_m(Piece* piece);
16
-int get_c(Piece* piece);
17
-int get_s(Piece* piece);
18
-int get_z(Piece* piece);
19
-
20
-void set_x(Piece* piece, int input);
21
-void set_y(Piece* piece, int input);
22
-void set_r(Piece* piece, int input);
23
-void set_v(Piece* piece, int input);
24
-void set_o(Piece* piece, int input);
25
-void set_m(Piece* piece, int input);
26
-void set_c(Piece* piece, int input);
27
-void set_s(Piece* piece, int input);
28
-void set_z(Piece* piece, int input);
29
-
30
-void inc_x(Piece* piece);
31
-void inc_y(Piece* piece);
32
-void inc_r(Piece* piece);
33
-
34
-void dec_x(Piece* piece);
35
-void dec_y(Piece* piece);
36
-void dec_r(Piece* piece);
37
-
38
-int get_cell(Piece* piece, int x, int y, int r)
10
+int get_piece_x(Piece* piece);
11
+int get_piece_y(Piece* piece);
12
+int get_piece_r(Piece* piece);
13
+int get_piece_v(Piece* piece);
14
+int get_piece_o(Piece* piece);
15
+int get_piece_m(Piece* piece);
16
+int get_piece_c(Piece* piece);
17
+int get_piece_s(Piece* piece);
18
+int get_piece_z(Piece* piece);
19
+
20
+void set_piece_x(Piece* piece, int input);
21
+void set_piece_y(Piece* piece, int input);
22
+void set_piece_r(Piece* piece, int input);
23
+void set_piece_v(Piece* piece, int input);
24
+void set_piece_o(Piece* piece, int input);
25
+void set_piece_m(Piece* piece, int input);
26
+void set_piece_c(Piece* piece, int input);
27
+void set_piece_s(Piece* piece, int input);
28
+void set_piece_z(Piece* piece, int input);
29
+
30
+void inc_piece_x(Piece* piece);
31
+void inc_piece_y(Piece* piece);
32
+void inc_piece_r(Piece* piece);
33
+
34
+void dec_piece_x(Piece* piece);
35
+void dec_piece_y(Piece* piece);
36
+void dec_piece_r(Piece* piece);
37
+
38
+int get_piece_cell(Piece* piece, int x, int y, int r);
39 39
 
40 40
 #endif

+ 17
- 0
src/standard.tvb View File

@@ -0,0 +1,17 @@
1
+MSSDDD...DD/IIQqQqQq
2
+
3
+M - Mode
4
+SS - Size 
5
+DDD...DD - Piece
6
+II - Index
7
+QqQqQq - Sequence
8
+
9
+0 03 0070403001111000010010011000111100110010010 0050403010111000010011010000111010010110010 00401021111
10
+
11
+start @ 3
12
+get_substring[6..7] = states
13
+get_substring[8..9] = size
14
+
15
+
16
+
17
+3 A B

+ 0
- 1
src/string.c View File

@@ -31,7 +31,6 @@ void add_char(char* string, char addition) {
31 31
     strncpy(string, holder, new_size);
32 32
 }
33 33
 
34
-
35 34
 //Returns an array of the values string[low] to string[high]
36 35
 char* get_substring(char* string, int low, int high) {
37 36
     size_t output_size = CHAR_SIZE * (high - low + 1);

+ 1
- 1
src/structs.h View File

@@ -11,7 +11,7 @@ typedef struct {
11 11
 typedef struct {
12 12
     //char* name;
13 13
     char* data;
14
-    char* state;
14
+    int index;
15 15
     int* sequence; //The integers here represent the index value of the start of each piece
16 16
 } Bag;
17 17
 

Loading…
Cancel
Save