Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Homework 7
- * Connect Four Game
- *
- * Ben Margolis - 10235419
- * Erik Veloz Perez - 00908094
- *
- * This program will run a game of Connect Four with the input from one player,
- * against the computer (AI). The program will either terminate after a win or
- * at the players request once the game is done.
- **/
- #include <iostream>
- #include <vector>
- #include <cassert>
- using namespace std;
- using Row = vector<char>;
- using Board = vector<Row>;
- bool connect_col(Board b, int col);
- bool connect_vert(Board b);
- bool connect_single_fd(Board b, int row, int col);
- bool connect_fd_row(Board b, int row);
- bool connect_forward_diag(Board b);
- bool connect_single_bd(Board b, int row, int col);
- bool connect_bd_row(Board b, int row);
- bool connect_backward_diag(Board b);
- bool connect_diag(Board b);
- bool connect_four(Board b);
- void run();
- void test();
- int main() {
- char t;
- cout << "Enter [t] to test, or any other character to run." << endl;
- cin >> t;
- if(t == 't') {
- test();
- } else {
- run();
- }
- }
- void run() {
- Board gb;
- gb = {
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
- { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} }
- };
- }
- void test() {
- }
- // Checks a single column for a win
- bool connect_col(Board b, int col) {
- int i = 0;
- while(i < b.size() - 3) {
- if( b[i][col] == b[i+1][col] &&
- b[i+1][col] == b[i+2][col] &&
- b[i+2][col] == b[i+3][col] ) {
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- bool connect_vert(Board b) {
- int i = 0;
- while(i < b[0].size()) {
- if(connect_col(b, i)){
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- // checks a single set of four spaces for a forward diag connect four
- // Assumes valid input
- bool connect_single_fd(Board b, int row, int col) {
- return b[row][col] == b[row+1][col-1] &&
- b[row+1][col-1] == b[row+2][col-2] &&
- b[row+2][col-2] == b[row+3][col-3];
- }
- bool connect_fd_row(Board b, int row) {
- int i = 3;
- while(i < 7) {
- if(connect_single_fd(b, row, i)){
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- bool connect_forward_diag(Board b) {
- int i = 0;
- while(i < 3) {
- if(connect_fd_row(b, i)) {
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- // checks a single set of four spaces for a backwards diag connect four
- // Assumes valid input
- bool connect_single_bd(Board b, int row, int col) {
- return b[row][col] == b[row+1][col+1] &&
- b[row+1][col+1] == b[row+2][col+2] &&
- b[row+2][col+2] == b[row+3][col+3];
- }
- bool connect_bd_row(Board b, int row) {
- int i = 0;
- while(i < 4) {
- if(connect_single_bd(b, row, i)){
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- bool connect_backward_diag(Board b) {
- int i = 0;
- while(i < 3) {
- if(connect_bd_row(b, i)) {
- return true;
- }
- i = i + 1;
- }
- return false;
- }
- bool connect_diag(Board b) {
- return connect_forward_diag(b) || connect_backward_diag(b);
- }
- bool connect_four(Board b) {
- return /*connect_across() ||*/ connect_vert(b) || connect_diag(b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement