finished play_at()

AuthorKonata <konata@posteo.jp>
Date
Commitf60dc8cd674cc9a4b67757f3022f1ef32c55d3af
Parent5d57823
2 files changed, 80 insertions(+), 49 deletions(-)
Msimple-go.c
@@ -79,65 +79,94 @@ bool play_at(game_state* game, size_t y, size_t x)
7979 if(get_board_at(game->board, y, x) != EMPTY)
8080 return false;
8181
82- //check if placement could lead to suicide
83- if(get_board_at(game->board, y-1, x) != EMPTY &&
84- get_board_at(game->board, y, x-1) != EMPTY &&
85- get_board_at(game->board, y+1, x) != EMPTY &&
86- get_board_at(game->board, y, x+1) != EMPTY)
87- //if so, check if placement kills a surrounding group
82+ bool can_place = false;
83+
84+ char up = get_board_at(game->board, y-1, x);
85+ char left = get_board_at(game->board, y, x-1);
86+ char down = get_board_at(game->board, y+1, x);
87+ char right = get_board_at(game->board, y, x+1);
88+
89+ //first check for group to kill, then for empty field, and last for group woth liberties
90+ if(up == (game->black_turn ? WHITE : BLACK))
8891 {
89- if(get_board_at(game->board, y-1, x) == (game->black_turn ? WHITE : BLACK))
92+ go_board* enemy_group = create_board(game->board->size);
93+ find_group(game->board, enemy_group, y-1, x);
94+ if(count_liberties(game->board, enemy_group) <= 1)
9095 {
91-
92- go_board* enemy_group = create_board(game->board->size);
93- find_group(game->board, enemy_group, y-1, x);
94- if(count_liberties(game->board, enemy_group) > 1)
95- {
96- delete_board(enemy_group);
97- return false;
98- } else {
99- kill_group(game->board, enemy_group);
100- }
96+ kill_group(game->board, enemy_group);
97+ can_place = true;
10198 }
102- if(get_board_at(game->board, y, x-1) == (game->black_turn ? WHITE : BLACK))
99+ delete_board(enemy_group);
100+ } else if(up == EMPTY) {
101+ can_place = true;
102+ } else {
103+ go_board* friendly_group = create_board(game->board->size);
104+ find_group(game->board, friendly_group, y-1, x);
105+ if(count_liberties(game->board, friendly_group) > 1)
106+ can_place = true;
107+ delete_board(friendly_group);
108+ }
109+
110+ if(left == (game->black_turn ? WHITE : BLACK))
111+ {
112+ go_board* enemy_group = create_board(game->board->size);
113+ find_group(game->board, enemy_group, y, x-1);
114+ if(count_liberties(game->board, enemy_group) <= 1)
103115 {
104- go_board* enemy_group = create_board(game->board->size);
105- find_group(game->board, enemy_group, y, x-1);
106- if(count_liberties(game->board, enemy_group) > 1)
107- {
108- delete_board(enemy_group);
109- return false;
110- } else {
111- kill_group(game->board, enemy_group);
112- }
116+ kill_group(game->board, enemy_group);
117+ can_place = true;
113118 }
114- if(get_board_at(game->board, y+1, x) == (game->black_turn ? WHITE : BLACK))
119+ } else if(left == EMPTY) {
120+ can_place = true;
121+ } else {
122+ go_board* friendly_group = create_board(game->board->size);
123+ find_group(game->board, friendly_group, y, x-1);
124+ if(count_liberties(game->board, friendly_group) > 1)
125+ can_place = true;
126+ delete_board(friendly_group);
127+ }
128+
129+ if(down == (game->black_turn ? WHITE : BLACK))
130+ {
131+ go_board* enemy_group = create_board(game->board->size);
132+ find_group(game->board, enemy_group, y+1, x);
133+ if(count_liberties(game->board, enemy_group) <= 1)
115134 {
116- go_board* enemy_group = create_board(game->board->size);
117- find_group(game->board, enemy_group, y+1, x);
118- if(count_liberties(game->board, enemy_group) > 1)
119- {
120- delete_board(enemy_group);
121- return false;
122- } else {
123- kill_group(game->board, enemy_group);
124- }
135+ kill_group(game->board, enemy_group);
136+ can_place = true;
125137 }
126- if(get_board_at(game->board, y, x+1) == (game->black_turn ? WHITE : BLACK))
138+ } else if(down == EMPTY) {
139+ can_place = true;
140+ } else {
141+ go_board* friendly_group = create_board(game->board->size);
142+ find_group(game->board, friendly_group, y+1, x);
143+ if(count_liberties(game->board, friendly_group) > 1)
144+ can_place = true;
145+ delete_board(friendly_group);
146+ }
147+
148+ if(right == (game->black_turn ? WHITE : BLACK))
149+ {
150+ go_board* enemy_group = create_board(game->board->size);
151+ find_group(game->board, enemy_group, y, x+1);
152+ if(count_liberties(game->board, enemy_group) <= 1)
127153 {
128- go_board* enemy_group = create_board(game->board->size);
129- find_group(game->board, enemy_group, y, x+1);
130- if(count_liberties(game->board, enemy_group) > 1)
131- {
132- delete_board(enemy_group);
133- return false;
134- } else {
135- kill_group(game->board, enemy_group);
136- }
154+ kill_group(game->board, enemy_group);
155+ can_place = true;
137156 }
138- //TODO: ...or if it connects to own group that has liberties left
157+ } else if(right == EMPTY) {
158+ can_place = true;
159+ } else {
160+ go_board* friendly_group = create_board(game->board->size);
161+ find_group(game->board, friendly_group, y, x+1);
162+ if(count_liberties(game->board, friendly_group) > 1)
163+ can_place = true;
164+ delete_board(friendly_group);
139165 }
140166
167+ if(!can_place)
168+ return false;
169+
141170 set_board_at(game->board, y, x, game->black_turn ? BLACK : WHITE);
142171 game->black_turn = !game->black_turn;
143172 return true;
Mtest.c
@@ -43,13 +43,15 @@ void test2(void)
4343 set_board_at(test_board, 2, 1, WHITE);
4444
4545 play_at(test_game, 2, 2);
46-
4746 print_board(test_game->board);
4847
4948 set_board_at(test_board, 2, 1, EMPTY);
5049
5150 play_at(test_game, 2, 2);
51+ print_board(test_game->board);
52+
5253
54+ play_at(test_game, 2, 1);
5355 print_board(test_game->board);
5456
5557 delete_game(test_game);