コード例 #1
0
ファイル: game.cpp プロジェクト: mariokostelac/connect4-mpi
void game() {

    int column;

    while (!state.is_full()) {

        draw_field();

        do {
            printf("Where to, my friend [0..%d]> ", WIDTH - 1);
            fflush(stdout);
            if (scanf("%d", &column) == -1) return;
        } while (state.play_column(PLAYER, column) == -1);

        if (state.winner() == PLAYER) {
            printf("you won!!!\n");
            draw_field();
            return;
        }

        computer_turn();
        if (state.winner() == COMPUTER) {
            printf("you lost!!!\n");
            draw_field();
            return;
        }
    }
}
コード例 #2
0
ファイル: main.c プロジェクト: ThyArmageddon/TicTacToe
int main(void)
{
     char player1 = CROSS;
     char player2 = NOUGHT;

     char winner;
     char currnt_player = player1;
     char board[NUM_ALL];
     char *br;
     bool ended = false;
     int nrounds = 0;
     int mode;

     int wplayer1 = 0;
     int wplayer2 = 0;
     int nreplays = 0;
     int nstalemates = 0;


     mode = select_mode();
     if (mode == 1) {
        max_depth = set_diff();
     }
     while (!ended) {

        if (nrounds == 0) {
           for (int i = 0; i < NUM_ALL; ++i) {
              board[i] = ' ';
            }
            br = &board[0];
            simulate(br);
        }
        if (mode == 1) {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              computer_turn(br, player1, currnt_player, &nrounds);
           }
        } else {
           if (currnt_player == player1) {
              human_turn(br, currnt_player, &nrounds);
           } else {
              human_turn(br, currnt_player, &nrounds);
           }
        }

        currnt_player = player_next(currnt_player, player1, player2);
        simulate(br);
        winner = game_winner(br, player1, player2);
        ended = game_ended(winner, nrounds);

        if (ended) {
           ++nreplays;
           if (winner == player1) {
              ++wplayer1;
           } else if (winner == player2) {
              ++wplayer2;
           } else {
              ++nstalemates;
           }
           rover_stats(winner, player1, player2, wplayer1,
                       wplayer2, nreplays, nstalemates, mode);
           if (restart()) {
              if (winner == CROSS) {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              } else if (winner == NOUGHT) {
                 if (player1 == NOUGHT) {
                    player1 = CROSS;
                    player2 = NOUGHT;
                    currnt_player = player1;
                 } else {
                    player1 = NOUGHT;
                    player2 = CROSS;
                    currnt_player = player2;
                 }
              } else {
                 if (player1 == CROSS) {
                    currnt_player = player1;
                 } else {
                    currnt_player = player2;
                 }
              }
              nround_announce(winner, player1, player2, currnt_player);
              nrounds = 0;
              winner = NO_MATCH;
              ended = false;
           } else {
              gover_stats(wplayer1, wplayer2, nreplays, nstalemates, mode);
           }
        }
     }
     return 0;
}
コード例 #3
0
ファイル: Draughts.c プロジェクト: liadwg/ex3_draughts
int main(void)
{
	char board[BOARD_SIZE][BOARD_SIZE];
	init_board(board);
	printf(WELCOME_TO_DRAUGHTS);
	printf(ENTER_SETTINGS);
	char *command = input2str(stdin);
	int win_pos = 0;

	while (strcmp(command, "quit") != 0){
		if (strcmp(command, "start") == 0){
			if (is_valid_board(board)) break;
			else{
				printf(WROND_BOARD_INITIALIZATION);
				free(command);
				command = input2str(stdin);
				continue;
			}
		}
		exc(command,board);
		free(command);
		command = input2str(stdin);
	}
	
	if (strcmp(command, "start") == 0){
		// initially we printed the board at the start of the game, commented out in order to match the running examples.
		//if (user_color == WHITE) print_board(board);
		while (1){
			if (user_color == WHITE){
				int ret_val = user_turn(board, WHITE);
				if (ret_val == QUIT) break;
				if (ret_val == WIN_POS){
					printf(BLACK_WIN);
					win_pos = 1;
					break;
				}
				if (computer_turn(board, BLACK) == WIN_POS){
					printf(WHITE_WIN);
					win_pos = 1;
					break;
				}
			}
			else{
				if (computer_turn(board, WHITE) == WIN_POS){
					printf(BLACK_WIN);
					win_pos = 1;
					break;
				}
				int ret_val = user_turn(board, BLACK);
				if (ret_val == QUIT) break;
				if (ret_val == WIN_POS){
					printf(WHITE_WIN);
					win_pos = 1;
					break;
				}
			}
		}
	}
	if (win_pos == 1){
		free(command);
		command = input2str(stdin);
	}
		free(command);
}