Advertisement
Guest User

connect 4

a guest
Oct 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. /**
  2.  * Homework 7
  3.  * Connect Four Game
  4.  *
  5.  * Ben Margolis - 10235419
  6.  * Erik Veloz Perez - 00908094
  7.  *
  8.  * This program will run a game of Connect Four with the input from one player,
  9.  * against the computer (AI). The program will either terminate after a win or
  10.  * at the players request once the game is done.
  11.  **/
  12.  
  13. #include <iostream>
  14. #include <vector>
  15. #include <cassert>
  16.  
  17. using namespace std;
  18. using Row = vector<char>;
  19. using Board = vector<Row>;
  20.  
  21.  
  22. bool connect_col(Board b, int col);
  23. bool connect_vert(Board b);
  24.  
  25. bool connect_single_fd(Board b, int row, int col);
  26. bool connect_fd_row(Board b, int row);
  27. bool connect_forward_diag(Board b);
  28.  
  29. bool connect_single_bd(Board b, int row, int col);
  30. bool connect_bd_row(Board b, int row);
  31. bool connect_backward_diag(Board b);
  32.  
  33.  
  34. bool connect_diag(Board b);
  35.  
  36.  
  37. bool connect_four(Board b);
  38.  
  39. void run();
  40. void test();
  41.  
  42.  
  43. int main() {
  44.     char t;
  45.  
  46.     cout << "Enter [t] to test, or any other character to run." << endl;
  47.     cin >> t;
  48.     if(t == 't') {
  49.       test();
  50.     } else {
  51.       run();
  52.     }
  53. }
  54.  
  55.  
  56. void run() {
  57.     Board gb;
  58.     gb = {
  59.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
  60.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
  61.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
  62.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
  63.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} },
  64.         { {' '}, {' '}, {' '}, {' '}, {' '}, {' '}, {' '} }
  65.     };
  66. }
  67.  
  68. void test() {
  69.  
  70. }
  71.  
  72.  
  73. // Checks a single column for a win
  74. bool connect_col(Board b, int col) {
  75.     int i = 0;
  76.  
  77.     while(i < b.size() - 3) {
  78.         if( b[i][col] == b[i+1][col] &&
  79.             b[i+1][col] == b[i+2][col] &&
  80.             b[i+2][col] == b[i+3][col] ) {
  81.             return true;
  82.         }
  83.         i = i + 1;
  84.     }
  85.     return false;
  86. }
  87.  
  88.  
  89. bool connect_vert(Board b) {
  90.     int i = 0;
  91.     while(i < b[0].size()) {
  92.         if(connect_col(b, i)){
  93.             return true;
  94.         }
  95.         i = i + 1;
  96.     }
  97.     return false;
  98. }
  99. // checks a single set of four spaces for a forward diag connect four
  100. // Assumes valid input
  101.  
  102.  
  103. bool connect_single_fd(Board b, int row, int col) {
  104.     return b[row][col]     == b[row+1][col-1] &&
  105.            b[row+1][col-1] == b[row+2][col-2] &&
  106.            b[row+2][col-2] == b[row+3][col-3];
  107. }
  108.  
  109.  
  110.  
  111. bool connect_fd_row(Board b, int row) {
  112.     int i = 3;
  113.     while(i < 7) {
  114.         if(connect_single_fd(b, row, i)){
  115.             return true;
  116.         }
  117.         i = i + 1;
  118.     }
  119.     return false;
  120. }
  121.  
  122.  
  123.  
  124. bool connect_forward_diag(Board b) {
  125.     int i = 0;
  126.  
  127.     while(i < 3) {
  128.         if(connect_fd_row(b, i)) {
  129.             return true;
  130.         }
  131.         i = i + 1;
  132.     }
  133.     return false;
  134. }
  135.  
  136. // checks a single set of four spaces for a backwards diag connect four
  137. // Assumes valid input
  138.  
  139. bool connect_single_bd(Board b, int row, int col) {
  140.     return b[row][col]     == b[row+1][col+1] &&
  141.            b[row+1][col+1] == b[row+2][col+2] &&
  142.            b[row+2][col+2] == b[row+3][col+3];
  143. }
  144.  
  145. bool connect_bd_row(Board b, int row) {
  146.     int i = 0;
  147.     while(i < 4) {
  148.         if(connect_single_bd(b, row, i)){
  149.             return true;
  150.         }
  151.         i = i + 1;
  152.     }
  153.     return false;
  154. }
  155.  
  156. bool connect_backward_diag(Board b) {
  157.     int i = 0;
  158.  
  159.     while(i < 3) {
  160.         if(connect_bd_row(b, i)) {
  161.             return true;
  162.         }
  163.         i = i + 1;
  164.     }
  165.     return false;
  166. }
  167.  
  168.  
  169. bool connect_diag(Board b) {
  170.     return connect_forward_diag(b) || connect_backward_diag(b);
  171. }
  172.  
  173.  
  174.  
  175. bool connect_four(Board b) {
  176.     return /*connect_across() ||*/ connect_vert(b) || connect_diag(b);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement