void vertical_column1(CuTest *tc) { int num_rows = 7; int num_columns = 7; int array[num_rows][num_columns]; int answer; ct_initialize(num_rows, num_columns, array); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Single 0 in column 1", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Two 0s in column 1", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Three 0s in column 1", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "4 in a row, vertical", 0, answer); }
void horizontal_row0(CuTest *tc) { int num_rows = 7; int num_columns = 7; int array[num_rows][num_columns]; int answer; ct_initialize(num_rows, num_columns, array); place_token(0, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Single 0 in column 0", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "0s in columns {0,1}", NO_WINNER_YET, answer); place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "0s in columns {0,1, 2}", NO_WINNER_YET, answer); place_token(0, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "4 in a row, horizontal", 0, answer); }
void place_token_invalid_index(CuTest *tc) { int num_rows = 100; int num_columns = 100; int array[num_rows][num_columns]; int answer = 0; ct_initialize(num_rows, num_columns, array); answer = place_token(1, 100, num_rows, num_columns, array); CuAssertIntEquals_Msg(tc, "Should return error, invalid index ", -1, answer); answer = place_token(1, -1, num_rows, num_columns, array); CuAssertIntEquals_Msg(tc, "Should return error, invalid index ", -1, answer); }
int main(void) { const char *TOKENS = "AB"; char board[ROWS * COLS]; memset(board, ' ', ROWS * COLS); int round, done = 0; for (round = 0; round < ROWS * COLS && !done; round++) { print_board(board); while (!place_token(board, round % 2, TOKENS)) { print_board(board); puts("**Column full!**\n"); } done = check_win(board); } print_board(board); if (round == ROWS * COLS && !done) { puts("Tie Game! Play Again"); } else { round--; printf("Player %d (%c) wins!\n", round % 2 + 1, TOKENS[round % 2]); } return 0; }
void one_player(char **grid, int line, int col) { int token; int i; i = who_begin(); while (++i < line * col) { if (i == 0) display(grid); i % 2 == 1 ? (token = ai(grid, col)) : (token = get_token(ft_strlen(grid[0]) + 1, 'X')); if (token > -1) { grid = place_token(grid, token, i); display(grid); if (test_win(grid, token) == 1) { victory(grid, token); return ; } } else i--; } ft_putendl("\n\nMatch nul\n"); }
int main(int argc, char *argv[3]) { if (argc < 3) { printf("Not enough inputs!\n"); return -1; } int game_in_session = 1; int cell_dimen = atoi(argv[1]); int length_to_win = atoi(argv[2]); int current_player = 0; int column; int win; int board[cell_dimen][cell_dimen]; printf("Board size: %d x %d, To win: %d\n", cell_dimen, cell_dimen, length_to_win); initialize(cell_dimen, cell_dimen, board); do { printf("Ready player %d\n", current_player + 1); printf("Enter column: "); check_valid_input(&column); int placed = place_token(current_player, column, cell_dimen, cell_dimen, board); if (placed < 0) { printf("\nCouldn't place token at column %d\n\n", column); continue; } win = winner(cell_dimen, cell_dimen, length_to_win, board); print_board(cell_dimen, cell_dimen, board); current_player = current_player == 0 ? 1 : 0; if (win >= 0) { game_in_session = 0; break; } } while (game_in_session); print_winner(win); return 0; }
void place_token_c5(CuTest *tc) { int num_rows = 7; int num_columns = 7; int array[num_rows][num_columns]; ct_initialize(num_rows, num_columns, array); place_token(1, 5, num_rows, num_columns, array); // make sure there is a 1 at the bottom of column 5 and a -1 everywhere else CuAssertIntEquals_Msg(tc, "Drop 1 into empty column 5", 1, array[num_rows - 1][5]); place_token(1, 5, num_rows, num_columns, array); CuAssertIntEquals_Msg(tc, "Drop 1 into column 5 on 1 full", 1, array[num_rows - 2][5]); place_token(1, 5, num_rows, num_columns, array); CuAssertIntEquals_Msg(tc, "Drop 1 into column 5 on 2 full", 1, array[num_rows - 3][5]); int r, c; for (r = 0; r < num_rows; r++) { for (c = 0; c < num_columns; c++) { if (r < (num_rows - 3) || c != 5) { CuAssertIntEquals_Msg(tc, "Should be empty", EMPTY, array[r][c]); } } } }
void place_token_c99(CuTest *tc) { int num_rows = 100; int num_columns = 100; int array[num_rows][num_columns]; ct_initialize(num_rows, num_columns, array); place_token(1, 99, num_rows, num_columns, array); CuAssertIntEquals_Msg(tc, "Drop 1 into empty column 99", 1, array[num_rows - 1][99]); int r, c; for (r = 0; r < num_rows; r++) { for (c = 0; c < num_columns; c++) { if (r != (num_rows - 1) || c != 99) { CuAssertIntEquals_Msg(tc, "Should be empty", EMPTY, array[r][c]); } } } }
void place_token_c1(CuTest *tc) { int num_rows = 7; int num_columns = 7; int array[num_rows][num_columns]; ct_initialize(num_rows, num_columns, array); place_token(1, 3, num_rows, num_columns, array); // make sure there is a 1 at the bottom of column 3 and a -1 everywhere else CuAssertIntEquals_Msg(tc, "Drop 1 into empty column 3", 1, array[num_rows - 1][3]); int r, c; for (r = 0; r < num_rows; r++) { for (c = 0; c < num_columns; c++) { if (r != (num_rows - 1) || c != 3) { CuAssertIntEquals_Msg(tc, "Should be empty", EMPTY, array[r][c]); } } } }
int main(void) { t_info info; int found; init_info(&info); get_player(&info); while (1) { if (info.turns % info.pnum == 0) { get_input(&info); get_available_coords(&info); found = place_token(&info); if (check_results(&info, found) == 0) return (0); if (info.turns == 0) set_opp_xy(&info); cleanup(&info); } info.turns++; } return (0); }
void tie_game(CuTest *tc) { /* 1 0 1 1 0 1 0 0 1 0 0 0 0 1 0 1 */ // player then column int num_rows = 4; int num_columns = 4; int array[num_rows][num_columns]; int answer; ct_initialize(num_rows, num_columns, array); /* column 0*/ place_token(0, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 1", NO_WINNER_YET, answer); place_token(1, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 2", NO_WINNER_YET, answer); place_token(0, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 3", NO_WINNER_YET, answer); place_token(1, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 4", NO_WINNER_YET, answer); /* column 1*/ place_token(1, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 5", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 6", NO_WINNER_YET, answer); place_token(1, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 7", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 8", NO_WINNER_YET, answer); /* column 2*/ place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 9", NO_WINNER_YET, answer); place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 10", NO_WINNER_YET, answer); place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 11", NO_WINNER_YET, answer); place_token(1, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 12", NO_WINNER_YET, answer); /* column 3 */ place_token(1, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 13", NO_WINNER_YET, answer); place_token(0, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 14", NO_WINNER_YET, answer); place_token(0, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 15", NO_WINNER_YET, answer); place_token(1, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 16 -- Winner!", 2, answer); }
void backward_diagonal(CuTest *tc) { /* . . . . . . . . . . . . . . . . . . . . . . . . 0 . . . . . 0 1 . . . . 0 0 0 . . . 0 1 1 1 . 1 . */ int num_rows = 7; int num_columns = 7; int array[num_rows][num_columns]; int answer; ct_initialize(num_rows, num_columns, array); /* column 0*/ place_token(0, 0, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 1", NO_WINNER_YET, answer); /* column 1*/ place_token(1, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 2", NO_WINNER_YET, answer); place_token(0, 1, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 3", NO_WINNER_YET, answer); /* column 2*/ place_token(1, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 4", NO_WINNER_YET, answer); place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 5", NO_WINNER_YET, answer); place_token(1, 5, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 6", NO_WINNER_YET, answer); place_token(0, 2, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 7", NO_WINNER_YET, answer); /* column 3 */ place_token(1, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 8", NO_WINNER_YET, answer); place_token(0, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 9", NO_WINNER_YET, answer); place_token(1, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 10", NO_WINNER_YET, answer); place_token(0, 3, num_rows, num_columns, array); answer = winner(num_rows, num_columns, 4, array); CuAssertIntEquals_Msg(tc, "Step 11 -- Winner!", 0, answer); }
int main(int argc, char *argv[]) { int num_rows = 8; int length_to_win = 4; if (argc == 3) { if (is_number(argv[1])) num_rows = atoi(argv[1]); else { crash_and_burn(); } if (num_rows < 3) { printf(YELLOW"Error, minimum board size for rows/cols is 3. Exiting.\n"RESET); exit(1); } else if (num_rows > 100) { printf(YELLOW"Error, the board size cannot be greater than 100. Exiting.\n"RESET); exit(1); } if (is_number(argv[2])) length_to_win = atoi(argv[2]); else { crash_and_burn(); } if (length_to_win > num_rows) { printf(YELLOW"Error, the length to win cannot be greater than the number of rows/cols. Exiting.\n"RESET); exit(1); } } else if (argc == 2) { if (is_number(argv[1])) num_rows = atoi(argv[1]); else { crash_and_burn(); } if (num_rows < 3) { printf(YELLOW"Error, minimum board size for rows/cols is 3. Exiting.\n"RESET); exit(1); } if (length_to_win > num_rows) length_to_win = num_rows; } else { printf("Warning! Too many arguments. Loading default settings.\n"); } int board[num_rows][num_rows]; printf("Creating a Connect FOUR board of size " RED "%d" RESET " x " RED "%d" RESET ". To win a length of" RED" %d" RESET " is required\n\n\n", num_rows, num_rows, length_to_win); init_board(num_rows, num_rows, board); int players = 2; int column = -1; while (1) { print_board(num_rows, num_rows, board, column); printf("Board size is" RED " %d x %d " RESET "and ;a length of "RED "%d " RESET "is required to win.\n", num_rows, num_rows, length_to_win); int is_winner = -1; int is_full = -1; int was_placed = 0; // if it is 1, that means a token was placed successfully printf("\nIt is player %d's turn.\n", players % 2); printf("Please enter a column number between %d and %d to place a token in.\n", 0, num_rows - 1); char input[256]; scanf("%s", input); if (is_number(input)) { column = atoi(input); } else { printf(YELLOW "\n\nWarning, please enter integers only for the column you wish to place a token into.\n\n" RESET); column = -1; // so it won't place a token in the previously selected column } was_placed = place_token(players % 2, column, num_rows, num_rows, board); // check to see if a token was placed is_winner = winner(num_rows, num_rows, length_to_win, board); // check to see if there is a winner is_full = is_board_full(num_rows, num_rows, board); // check to see if the board is full - stalemate if (is_winner != -1) { // check to see if there is a winner first printf("\n\n\n"); printf("Player %d has won!\n", is_winner); print_board(num_rows, num_rows, board, column); exit(1); } if (is_full != -1) { // check to see if the board is full, resulting in a tie printf("\n\n\n"); printf("Tie game!\n"); print_board(num_rows, num_rows, board, column); exit(1); } if (was_placed) { // token was successfully placed, no winner, no tie printf(GREEN"\n\nSuccessfully" RESET " placed a token in column " GREEN "%d\n\n" RESET, column); players++; // goes to the next player } } }