Skip to content

Commit

Permalink
Fix:died setting (#63)
Browse files Browse the repository at this point in the history
* fix: if someone died, his everything set to NULL

* fix: call died_player() input -1

* feat: check.sh
  • Loading branch information
howard9199 authored Jun 15, 2022
1 parent 8b7047a commit 4d9230b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3,919 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@

*.o
*.exe
*.out
src/third/*/
**/.DS_Store
3,891 changes: 0 additions & 3,891 deletions 1.out

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ build:
@echo "Build complete"

debug:
@$(cc) -o main $(src_dir)/main.c -fsanitize=address -g -D DEBUG
@$(cc) -o main $(src_dir)/main.c -D DEBUG
@echo "Build complete"

test: $(test_files)
Expand Down
7 changes: 7 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
ENV=0
until ./main > 1.out; [ $? -eq 139 ]; do
ENV=`expr $ENV + 1`
echo $ENV
sleep 1
done
10 changes: 5 additions & 5 deletions src/core/ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void ai_initialize(Game* game, i32 player_id) {
歹徒 警長:5
叛徒 警長:A 歹徒:5 A預設6,針對他一次後減少1
*/
for (int i = 0; i < 4; i++) disgust[player_id][i] = 0;
for (int i = 0; i < 7; i++) disgust[player_id][i] = 0;
Player* my = game->players->data[player_id];
switch (my->role->type) {
case Sheriff:
Expand Down Expand Up @@ -138,9 +138,9 @@ i32 ai_request(Game* game, i32 player_id, Cards* candi_card) {
weight[i].target = ai_target;
}
qsort(weight, candi_card->size, sizeof(Weight), ai_weight_cmp); // sort from big to small
print_weight_card(game, player_id, weight, candi_card);
// if event is discard cards
// DEBUG_PRINT("request_type: %d\n", ai_request_type);
// print_weight_card(game, player_id, weight, candi_card);
// if event is discard cards
// DEBUG_PRINT("request_type: %d\n", ai_request_type);

if (ai_request_type == AI_DISCARD) {
i32 missed_cnt = 0;
Expand All @@ -160,7 +160,7 @@ i32 ai_request(Game* game, i32 player_id, Cards* candi_card) {
if (ai->role->type == Traitor && game->players->data[ai_target]->role->type == Sheriff) {
i32 card_type = candi_card->data[weight[0].id]->type;
if (card_type == Bang || card_type == Indians || card_type == Duel) {
disgust[player_id][ai_target]--;
disgust[player_id][ai_target] -= 2;
}
}
return weight[0].id;
Expand Down
60 changes: 48 additions & 12 deletions src/core/cards.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,30 @@ void died_player(Game* game, i32 me_id, i32 enemy_id) {
if (me_id != enemy_id && game->players->data[me_id]->character->type == Vulture_Sam)
discard_card = game->players->data[me_id]->hands;
transfer(enemy->hands, discard_card);
if (NULL != enemy->weapon) discard_card->push(discard_card, enemy->weapon);
if (NULL != enemy->barrel) discard_card->push(discard_card, enemy->barrel);
if (NULL != enemy->mustang) discard_card->push(discard_card, enemy->mustang);
if (NULL != enemy->scope) discard_card->push(discard_card, enemy->scope);
if (NULL != enemy->jail) discard_card->push(discard_card, enemy->jail);
if (NULL != enemy->dynamite) discard_card->push(discard_card, enemy->dynamite);
if (NULL != enemy->weapon) {
discard_card->push(discard_card, enemy->weapon);
enemy->weapon = NULL;
}
if (NULL != enemy->barrel) {
discard_card->push(discard_card, enemy->barrel);
enemy->barrel = NULL;
}
if (NULL != enemy->mustang) {
discard_card->push(discard_card, enemy->mustang);
enemy->mustang = NULL;
}
if (NULL != enemy->scope) {
discard_card->push(discard_card, enemy->scope);
enemy->scope = NULL;
}
if (NULL != enemy->jail) {
discard_card->push(discard_card, enemy->jail);
enemy->jail = NULL;
}
if (NULL != enemy->dynamite) {
discard_card->push(discard_card, enemy->dynamite);
enemy->dynamite = NULL;
}

// Penalties and Rewards
DEBUG_PRINT("Penalties and Rewards.\n");
Expand All @@ -61,12 +79,30 @@ void died_player(Game* game, i32 me_id, i32 enemy_id) {
// Sheriff discards all cards
discard_card = game->discard;
transfer(me->hands, discard_card);
if (NULL != me->weapon) discard_card->push(discard_card, me->weapon);
if (NULL != me->barrel) discard_card->push(discard_card, me->barrel);
if (NULL != me->mustang) discard_card->push(discard_card, me->mustang);
if (NULL != me->scope) discard_card->push(discard_card, me->scope);
if (NULL != me->jail) discard_card->push(discard_card, me->jail);
if (NULL != me->dynamite) discard_card->push(discard_card, me->dynamite);
if (NULL != me->weapon) {
discard_card->push(discard_card, me->weapon);
me->weapon = NULL;
}
if (NULL != me->barrel) {
discard_card->push(discard_card, me->barrel);
me->barrel = NULL;
}
if (NULL != me->mustang) {
discard_card->push(discard_card, me->mustang);
me->mustang = NULL;
}
if (NULL != me->scope) {
discard_card->push(discard_card, me->scope);
me->scope = NULL;
}
if (NULL != me->jail) {
discard_card->push(discard_card, me->jail);
me->jail = NULL;
}
if (NULL != me->dynamite) {
discard_card->push(discard_card, me->dynamite);
me->dynamite = NULL;
}
} else if (enemy->role->type == Criminal) {
DEBUG_PRINT("enemy->role->type == Criminal\n");
player_draw_deck(game, me_id, 3);
Expand Down
6 changes: 3 additions & 3 deletions src/core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void print_status(Game *game);
bool valid_assign_role(Role *role, i32 player_total) {
if (role == NULL) return false;
printf("assign: %s\n", role_name[role->type]);
// printf("assign: %s\n", role_name[role->type]);
if (game_start_role_in_players[player_total - 4][role->type] <= 0) return false;
game_start_role_in_players[player_total - 4][role->type]--;
return true;
Expand Down Expand Up @@ -97,7 +97,7 @@ void game_next(Game *game) {
}
game->turn++;
DEBUG_PRINT("It's player %d turn!!!\n", player->id);
print_status(game);
// print_status(game);

// determine bomb and jail, may just skip this turn
if (player->dynamite != NULL) {
Expand Down Expand Up @@ -147,7 +147,7 @@ void game_next(Game *game) {
} else {
player_draw_deck(game, player->id, 2);
}
print_status(game);
// print_status(game);

// 2.Play any number of cards
i8 bang_used = 0;
Expand Down
20 changes: 13 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ void main_win(Game* game);

i32 main() {
// setup_catcher();
// srand(1481);
srand(218243);
// srand(1481);210909
// printf("ok\n");
time_t seed = time(NULL);
// time_t seed = 1655227962;
printf("seed: %ld\n", seed);
fflush(stdout);
srand(seed);
Game* game = new_game();

char* player_name = $(calloc(1024, sizeof(char)));
printf("Enter player name: ");
scanf("%[^\n]%*c", player_name);
game->join(game, player_name, false);
// printf("Enter player name: ");
// scanf("%[^\n]%*c", player_name);
// game->join(game, player_name, false);

// game->join(game, "Computer A", true);
game->join(game, "Computer A", true);
game->join(game, "Computer B", true);
game->join(game, "Computer C", true);
game->join(game, "Computer D", true);
Expand All @@ -32,7 +37,7 @@ i32 main() {
game->next(game);
for (int i = 0; i < game->players->size; i++) {
if (!game->players->data[i]->dead && game->players->data[i]->hp <= 0)
died_player(game, -1, i);
died_player(game, i, i);
}
}
DEBUG_PRINT("Game End!\n");
Expand All @@ -42,6 +47,7 @@ i32 main() {
$free();

Console.green("Everything is done.");
DEBUG_PRINT("seed: %ld\n", seed);
return EXIT_SUCCESS;
}

Expand Down

0 comments on commit 4d9230b

Please sign in to comment.