int main (void) { initialize_curses(); initialize_field_window(); bdinit (_board); // Randomly choose who goes first enum { USER, // get input from standard input PROGRAM // get input from program }; unsigned input[2]; _humanPlayer = nrand(2); input[_humanPlayer] = USER; input[!_humanPlayer] = PROGRAM; _plyr[_humanPlayer] = "you"; _plyr[!_humanPlayer] = "me"; for (unsigned color = BLACK;; color = !color) { bdisp(); unsigned curmove; if (input[color] == USER) curmove = _lastHumanMove = usermove(); else curmove = _lastComputerMove = pickmove (color); int mv = makemove(color, curmove); if (mv != MOVEOK) { // Game finished. Display result and quit. if (mv != RESIGN) display_game_result_message (mv, input[color] == USER); break; } } return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { if (setlocale( LC_ALL, "en_US.utf8" ) == NULL) error("setlocale"); initialize_curses(); unlink_queues(); // delete any stale queues // central data structure contains pointers to various components -- // sockets, message queues, and curses windows. // mqd_t *ob_mq = get_mq_fd(QUEUES[0], O_CREAT | O_RDWR); mqd_t *ib_mq = get_mq_fd(QUEUES[1], O_CREAT | O_RDWR); long *sock_fd = get_socket_fd( SERVER, PORT ); CONFIG config = { .ob_mq = *ob_mq, .ib_mq = *ib_mq, .sk = *sock_fd, .w1 = w1, .w2 = w2, .w3 = w3, }; // define an array of worker threads // void (*workers[]) = { t_socket_line_writer, // reads from message queue, writes to socket t_socket_line_reader, // reads from socket, writes to message queue t_curses_term_writer, // reads from message queue, writes to term t_curses_term_reader, // reads from term, writes to message queue }; // launch threads and wait for them to complete their work // pthread_t T[ LEN( workers ) ]; for (int t = 0; t < LEN(T); t++) { pthread_create(&T[t], NULL, workers[t], &config); } for (int i = 0; i < LEN(T); i++) { pthread_join(T[i], NULL); } // Clean up file handles close(config.sk); mq_close(config.ob_mq); mq_close(config.ib_mq); free(ob_mq); free(ib_mq); free(sock_fd); // clean up curses delwin(w1); delwin(w2); delwin(w3); unlink_queues(); endwin(); return 0; }
int main (void) { initialize_curses(); initialize_windows(); shuffle_deck_and_deal(); instructions(); enum EPlayer player = nrand(NPLAYERS); printplayer (player); waddstr (_wmsg, "get to start.\n"); for (;;) { unsigned m = makemove (player); if (_hand[OTHER(player)][m]) goodmove (player, m); else if (!gofish (player, m)) player = OTHER(player); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { int err = 0; int id2 = 0, c; double yk = 0.0; /* controller output */ int target_tz_index; if (geteuid() != 0) { printf("TMON needs to be run as root\n"); exit(EXIT_FAILURE); } while ((c = getopt_long(argc, argv, "c:dlht:vgz:", opts, &id2)) != -1) { switch (c) { case 'c': no_control = 0; strncpy(ctrl_cdev, optarg, CDEV_NAME_SIZE); break; case 'd': start_daemon_mode(); printf("Run TMON in daemon mode\n"); break; case 't': ticktime = strtod(optarg, NULL); if (ticktime < 1) ticktime = 1; break; case 'l': printf("Logging data to /var/tmp/tmon.log\n"); logging = 1; break; case 'h': usage(); break; case 'v': version(); break; case 'g': debug_on = 1; break; case 'z': target_thermal_zone = strtod(optarg, NULL); break; default: break; } } if (pthread_mutex_init(&input_lock, NULL) != 0) { fprintf(stderr, "\n mutex init failed, exit\n"); return 1; } start_syslog(); if (signal(SIGINT, tmon_sig_handler) == SIG_ERR) syslog(LOG_DEBUG, "Cannot handle SIGINT\n"); if (signal(SIGTERM, tmon_sig_handler) == SIG_ERR) syslog(LOG_DEBUG, "Cannot handle SIGINT\n"); if (probe_thermal_sysfs()) { pthread_mutex_destroy(&input_lock); closelog(); return -1; } initialize_curses(); setup_windows(); signal(SIGWINCH, resize_handler); show_title_bar(); show_sensors_w(); show_cooling_device(); update_thermal_data(); show_data_w(); prepare_logging(); init_thermal_controller(); nodelay(stdscr, TRUE); err = pthread_create(&event_tid, NULL, &handle_tui_events, NULL); if (err != 0) { printf("\ncan't create thread :[%s]", strerror(err)); tmon_cleanup(); exit(EXIT_FAILURE); } /* validate range of user selected target zone, default to the first * instance if out of range */ target_tz_index = zone_instance_to_index(target_thermal_zone); if (target_tz_index < 0) { target_thermal_zone = ptdata.tzi[0].instance; syslog(LOG_ERR, "target zone is not found, default to %d\n", target_thermal_zone); } while (1) { sleep(ticktime); show_title_bar(); show_sensors_w(); update_thermal_data(); if (!dialogue_on) { show_data_w(); show_cooling_device(); } cur_thermal_record++; time_elapsed += ticktime; controller_handler(trec[0].temp[target_tz_index] / 1000, &yk); trec[0].pid_out_pct = yk; if (!dialogue_on) show_control_w(); if (tmon_exit) break; } tmon_cleanup(); return 0; }