|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T l
Length: 6257 (0x1871) Types: TextFile Names: »logging.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987 └─⟦this⟧ »EUUGD18/General/Challenge/logging.c«
#include "challenge.h" record(player, card) int player, card; { int i; if(move_logged == 0) { record_start(); if(start_player == HUMAN) { fprintf(logfile, "\nH:"); for(i = 0; i < size2; i++) { record_card(s_human[i]); } fprintf(logfile, "\nC:"); for(i = 0; i < size2; i++) { record_card(s_computer[i]); } } else { fprintf(logfile, "\nC:"); for(i = 0; i < size2; i++) { record_card(s_computer[i]); } fprintf(logfile, "\nH:"); for(i = 0; i < size2; i++) { record_card(s_human[i]); } } } for(i = move_logged; i < nr_moves; i++) { if((i & 1) == 0) { fprintf(logfile, "\n%3d.", i / 2 + 1); } record_card(moves[i]); } move_logged = nr_moves; if(((player == COMPUTER) && (nr_computer == 1)) || ((player == HUMAN ) && (nr_human == 1))) { if(moves[nr_moves - 1] != PICK_UP) { fprintf(logfile, " and wins"); } } } record_card(card) int card; { int value, suit; if(card == PICK_UP) { fprintf(logfile, " pu"); return(0); } if(card == PASS) { fprintf(logfile, " --"); return(0); } if(card == RESIGN) { fprintf(logfile, " resigns"); return(0); } if(card == DRAWN) { fprintf(logfile, " draw"); return(0); } if(card == WIN) { fprintf(logfile, " wins"); return(0); } value = VALUE(card); suit = SUIT(card); fprintf(logfile, " %c%c", "AKQJX98765432"[value], "scdh"[suit]); } record_start() { fprintf(logfile, "*S%d,D%d*", size, depth); } record_end() { fprintf(logfile, "\n*E*\n"); } record_stop() { if(move_logged != 0) { fprintf(logfile, "\n*X*\n"); } logfile = NULL; } open_log() { if(old_logfile == NULL) { logfile = fopen("challenge.log", "w"); old_logfile = logfile; } else { logfile = old_logfile; } } read_game() { int c, c1, player; again: nr_moves = 0; c = fgetc(logfile); if(c == EOF) { move(COMMAND_LINE, 1); printw("no more games on this file"); quit(); } if(c != '*') { invalid_file(); } c = fgetc(logfile); if(c != 'S') { invalid_file(); } c = fgetc(logfile); if(c < '1' || c > '8') { invalid_file(); } size = c - '0'; c = fgetc(logfile); if(c != ',') { invalid_file(); } c = fgetc(logfile); if(c != 'D') { invalid_file(); } c = fgetc(logfile); depth = 0; while(c >= '0' && c <= '9') { depth = depth * 10 + depth; c = fgetc(logfile); } if(c != '*') { invalid_file(); } c = fgetc(logfile); if(c != '\n') { invalid_file(); } c = fgetc(logfile); if(c != 'C' && c != 'H') { invalid_file(); } c1 = fgetc(logfile); if(c1 != ':') { invalid_file(); } nr_human = 0; nr_computer = 0; nr_table = 0; top = -1; if(c == 'C') { read_hand(computer, &nr_computer); c = fgetc(logfile); if(c != 'H') { invalid_file(); } c = fgetc(logfile); if(c != ':') { invalid_file(); } read_hand(human, &nr_human); start_player = COMPUTER; } else { read_hand(human, &nr_human); c = fgetc(logfile); if(c != 'C') { invalid_file(); } c = fgetc(logfile); if(c != ':') { invalid_file(); } read_hand(computer, &nr_computer); start_player = COMPUTER; } player = start_player; for(;;) { if(fgetc(logfile) == '*') { break; } while((c = fgetc(logfile)) != '.') { if(c == '*') { break; } if(c == '\n' || c == EOF) { invalid_file(); } } while((c = fgetc(logfile)) == ' ') { if(c == '\n' || c == EOF) { invalid_file(); } } if(c == '*') { break; } ungetc(c, logfile); c = read_card(); if(c < 0) { break; } step(player, c); interchange(&player); while((c = fgetc(logfile)) == ' ') { if(c == '\n' || c == EOF) { invalid_file(); } } ungetc(c, logfile); c = read_card(); if(c < 0) { break; } step(player, c); interchange(&player); if(fgetc(logfile) != '\n') { invalid_file(); } } c = fgetc(logfile); while(fgetc(logfile) != '\n') ; if(c == 'X') { goto again; } if(c != 'E') { invalid_file(); } if(player != HUMAN) { step(HUMAN, PASS); } } invalid_file() { move(COMMAND_LINE - 1, 1); printw("gamefile is invalid"); quit(); } read_hand(hand, nr_hand) char *hand; int *nr_hand; { char c; *nr_hand = 0; for(;;) { while((c = fgetc(logfile)) == ' ') ; if(c == '\n') { return; } ungetc(c, logfile); hand[(*nr_hand)++] = read_card(); } } read_card() { char c; int value, suit; c = fgetc(logfile); switch(c) { case 'A': value = 0; break; case 'K': value = 1; break; case 'Q': value = 2; break; case 'J': value = 3; break; case 'X': value = 4; break; case '9': value = 5; break; case '8': value = 6; break; case '7': value = 7; break; case '6': value = 8; break; case '5': value = 9; break; case '4': value = 10; break; case '3': value = 11; break; case '2': value = 12; break; case 'p': value = -1; break; default: while((c = fgetc(logfile)) != '\n') { if(c == EOF) { invalid_file(); } } c = fgetc(logfile); if(c != '*') { invalid_file(); } return(-1); } c = fgetc(logfile); switch(c) { case 's': suit = 0; break; case 'c': suit = 1; break; case 'd': suit = 2; break; case 'h': suit = 3; break; case 'u': if(value != -1) { invalid_file(); } return(PICK_UP); default: invalid_file(); } if(value == -1) { invalid_file(); } return(value * 4 + suit); } step(player, card) { int i; i = card; if(i != PASS) { if(i != PICK_UP) { if((i = have(player, card)) < 0) { invalid_file(); } } else { save_pick_up(); } if(!valid(player, i)) { invalid_file(); } do_move(player, i); } moves[nr_moves++] = card; } have(player, card) int player, card; { char *deck; int nr_deck, i; if(player == COMPUTER) { deck = &computer[0]; nr_deck = nr_computer; } else { deck = &human[0]; nr_deck = nr_human; } for(i = 0; i < nr_deck; i++) { if(deck[i] == card) { return(i); } } return(-1); }