Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // M[][] is the matrix which stores everything.
- // Let 1st player be 1 and 2nd player be -1
- // Initialize the matrix with all 0
- int number_of_column = 6;
- int number_of_rows = 7;
- // Check for sinking effect when something is added (I have not added the boundary conditions try that)
- // Add checks for invalid selection.
- // The position to add the piece was give 2,3
- int i = 2;
- for(; i < number_of_rows && M[i][3] != 0; i++)
- // The loop will stop with i containing the value of first non-zero element (non free spot, we have to insert it one element above)
- M[i-1][3] = 1 // For 1st player. // Remember to check for edge cases here and invalid options its important.
- // Check horizontal connect 4 (Try to implement vertical in a similar manner)
- // Say the user just added 1 to M[a][b]
- // Check the left side of the piece (include the piece)
- int count = 0; // Stores how many continous same player pieces we saw.
- for(int i = b; i > 0 && M[a][i] == M[a][b]; i--) {
- count ++;
- }
- // Check the right side but do not include the piece.
- for(int i = b+1; i < number_of_column && M[a][i] == M[a][b]; i++) {
- count ++;
- }
- // Check if we say 4 pieces of the same type
- if(count >= 4) {
- // 1st user wins.
- }
- // Diagonal check.
- // 2 cases, 45 degree diagonal and 135 degree diagonal.
- // I will show you 45, you can implement 135.
- // Check the right-up direction including the piece.
- // Piece was added to M[a][b]
- int count = 0;
- for(int i=a, j=b; i >= 0 && j < number_of_column && M[i][j] == M[a][b]; i--, j++) {
- count ++;
- }
- // Check the left-bottom direction not including the piece.
- for(int i=a+1, j=b-1; i < number_of_rows && j > 0 && M[i][j] == M[a][b]; i++, j--) {
- count ++;
- }
- // Check if we have connect 4 same like above.
- if(count >= 4) {
- // 1st user wins.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement