eecs 183 project 3 ciphers /** * ohhi.cpp * * EECS 183 * Project 3: 0h h1 * * */ #include eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers #include #include "utility.h" #include "ohhi.h" /////////////////////////////////////// // UTILITY FUNCTIONS ////////////////// /////////////////////////////////////// int count_unknown_squares(const int board[MAX_SIZE][MAX_SIZE], int size) { int count = 0; eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (board[i][j] == UNKNOWN) { count++; } } } return count; } eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers /////////////////////////////////////// // VALIDITY CHECKS //////////////////// /////////////////////////////////////// bool row_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE], int size, int row, int color) { eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers for (int i = 0; i < (size - 2); i++) { if (board[row][i] == color && board[row][i + 1] == color && board[row][i + 2] == color) { return false; } } return true; } eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers bool col_has_no_threes_of_color(const int board[MAX_SIZE][MAX_SIZE], int size, int col, int color) { int total = 0; for (int i = 0; i < (size - 2); i++) { if (board[i][col] == color && board[i + 1][col] == color && board[i + 2][col] == color) eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers { total++; } } if (total == 0) { return true; } else { return false; } eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers } bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size) { for (int i = 0; i < size; i++) { if (!row_has_no_threes_of_color(board, size, i, RED) || !row_has_no_threes_of_color(board, size, i, BLUE) || !col_has_no_threes_of_color(board, size, i, RED) || eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers !col_has_no_threes_of_color(board, size, i, BLUE)) { return false; } } return true; } bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE], int size, eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers int row1, int row2){ int count = 0; for (int i = 0; i < size; i++) { if ((board[row1][i] == UNKNOWN )|| (board[row2][i] == UNKNOWN)) { return true; } if (board[row1][i] == board[row2][i]){ eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers count++; } } if (count <= size) { return true; } else { return false; } eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers } bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE], int size, int col1, int col2) { int count = 0; for (int i = 0; i < size; i++) { if ((board[i][col1] == UNKNOWN) || (board[i][col2] == UNKNOWN)) { eecs 183 project 3 ciphers How to use it? eecs 183 project 3 ciphers return true; } if (board[i][col1] == RED) { count++; } if (count < size) { return true; } else { eecs 183 project 3 ciphers How to use it? eecs 183 project 3 ciphers return false; } } } bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size) { if (size == 2) { return (rows_are_different(board, size, 0, 1) && cols_are_different(board, size, 0, 1)); } eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers for (int i = 0; i < size - 1; i++) { for (int j = i + 1; j < size - 1; j++) { if (!rows_are_different(board, size, i, j) || !cols_are_different(board, size, i, j)) { return false; } } eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers } return true; } /////////////////////////////////////// // SOLVING FUNCTIONS ////////////////// /////////////////////////////////////// void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE], eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers int size, int row, bool announce) { int i = 0; for (i = 0; i < size; i++) { if (board[row][i] == RED && board[row][i + 1] == RED) { if (i - 1 >= 0 && i + 2 <= size) { mark_square_as(board, size, row, i - 1, BLUE, announce); mark_square_as(board, size, row, i + 2, BLUE, announce); } eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers else if (i + 1 == size && i - 1 >= 0) { mark_square_as(board, size, row, i - 1, BLUE, announce); } else if (i == 0 && i + 2 <= size) { mark_square_as(board, size, row, i - 1, BLUE, announce); } } else if (board[row][i] == RED && board[row][i + 2] == RED) { mark_square_as(board, size, row, i + 1, BLUE, announce); } eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers } for (i = 0; i < size; i++) { if (board[row][i] == BLUE && board[row][i + 1] == BLUE) { if (i - 1 >= 0 && i + 2 <= size) { mark_square_as(board, size, row, i - 1, RED, announce); mark_square_as(board, size, row, i + 2, RED, announce); } else if (i + 1 == size && i - 1 >= 0) { mark_square_as(board, size, row, i - 1, RED, announce); eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers } else if (i == 0 && i + 2 <= size) { mark_square_as(board, size, row, i - 1, RED, announce); } } else if (board[row][i] == BLUE && board[row][i + 2] == BLUE) { mark_square_as(board, size, row, i + 1, RED, announce); } } eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers } void solve_three_in_a_column(int board[MAX_SIZE][MAX_SIZE], int size, int col, bool announce) { int i = 0; for (i = 0; i < size; i++) { if (board[i][col] == RED && board[i + 1][col] == RED) { if (i - 1 >= 0 && i + 2 <= size) { eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers mark_square_as(board, size, i - 1, col, BLUE, announce); mark_square_as(board, size, i + 2, col, BLUE, announce); } else if (i + 1 == size) { mark_square_as(board, size, i - 1, col, BLUE, announce); } else { mark_square_as(board, size, i + 2, col, BLUE, announce); } } eecs 183 project 3 ciphers How to use it? eecs 183 project 3 ciphers else if (board[i][col] == RED && board[i + 2][col] == RED) { mark_square_as(board, size, i + 1, col, BLUE, announce); } } for (i = 0; i < size; i++) { if (board[i][col] == BLUE && board[i + 1][col] == BLUE) { if (i - 1 >= 0 && i + 2 <= size) { mark_square_as(board, size, i - 1, col, RED, announce); mark_square_as(board, size, i + 2, col, RED, announce); eecs 183 project 3 ciphers How to use it? eecs 183 project 3 ciphers } else if (i + 1 == size) { mark_square_as(board, size, i - 1, col, RED, announce); } else { mark_square_as(board, size, i + 2, col, RED, announce); } } else if (board[i][col] == BLUE && board[i + 2][col] == BLUE) { mark_square_as(board, size, i + 1, col, RED, announce); eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers } } } void solve_balance_row(int board[MAX_SIZE][MAX_SIZE], int size, int row, bool announce) { eecs 183 project 3 ciphers PasteShr eecs 183 project 3 ciphers int totalRed = 0; int totalBlue = 0; for (int i = 0; i < size; i++) { if (board[row][i] == RED) { totalRed++; } else if (board[row][i] == BLUE) { eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers totalBlue++; } } if (totalRed == size / 2) { for (int i = 0; i < size; i++) { if (board[row][i] != RED && board[row][i] != BLUE) { mark_square_as(board, size, row, i, BLUE, announce); } } } eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers else if (totalBlue == size / 2) { for (int i = 0; i < size; i++) { if (board[row][i] != RED && board[row][i] != BLUE) { mark_square_as(board, size, row, i, RED, announce); } } } } eecs 183 project 3 ciphers How to get it for free? eecs 183 project 3 ciphers void solve_balance_column(int board[MAX_SIZE][MAX_SIZE], int size, int col, bool announce) { int totalRed = 0; int totalBlue = 0; int i = 0; for (int i = 0; i < size; i++) { eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers if (board[i][col] == RED) { totalRed++; } if (board[i][col] == BLUE) { totalBlue++; } } if (totalRed == size / 2) { for (int i = 0; i < size; i++) { if (board[i][col] != RED && board[i][col] != BLUE) { eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers mark_square_as(board, size, i, col, BLUE, announce); } } } else if (totalBlue == size / 2) { for (int i = 0; i < size; i++) { if (board[i][col] != RED && board[i][col] != BLUE) { mark_square_as(board, size, i, col, RED, announce); } eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers } } } /////////////////////////////////////// // GAMEPLAY FUNCTIONS ///////////////// /////////////////////////////////////// bool board_is_solved(const int board[MAX_SIZE][MAX_SIZE], int size) { eecs 183 project 3 ciphers How to get it? eecs 183 project 3 ciphers if (count_unknown_squares(board, size) == 0) { if (board_is_valid(board, size)) { return true; } } return false; } eecs 183 project 3 ciphers How to use it? eecs 183 project 3 ciphers bool check_valid_input(int size, int row_input, char col_input, char color_char, int &row, int &col) { char upper = toupper(col_input); char upperColor = toupper(color_char); if (row_input >= 1 && row_input <= size && ((col_input >= 'a' && col_input <= 'a' + size - 1) || (upper >= 'A' && upper <= 'A' + size - 1)) && (upperColor == RED_LETTER || upperColor == BLUE_LETTER || color_char == UNKNOWN_LETTER)) { row = row_input - 1; col = upper = 'A'; eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers return true; } else{ cout << "Sorry, that's not a valid input." << endl; return false; } return false; } bool check_valid_move(const int original_board[MAX_SIZE][MAX_SIZE], eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers const int current_board[MAX_SIZE][MAX_SIZE], int size, int row, int col, int color) { if (original_board[row][col] != UNKNOWN) { cout << "Sorry, orginal square cannot be changed." << endl; return false; } if (!board_is_valid(current_board, size)) { cout << "Sorry, that move violates a rule." << endl; return false; eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers } return true; } /////////////////////////////////////// // S'MORE FUNCTIONS /////////////////// eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers /////////////////////////////////////// void solve_lookahead_row(int board[MAX_SIZE][MAX_SIZE], int size, int row, bool announce) { // your code here } eecs 183 project 3 ciphers How to dowload it? eecs 183 project 3 ciphers void solve_lookahead_column(int board[MAX_SIZE][MAX_SIZE], int size, int col, bool announce) { // your code here } eecs 183 project 3 ciphers