Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. // M[][] is the matrix which stores everything.
  2. // Let 1st player be 1 and 2nd player be -1
  3. // Initialize the matrix with all 0
  4. int number_of_column = 6;
  5. int number_of_rows = 7;
  6.  
  7. // Check for sinking effect when something is added (I have not added the boundary conditions try that)
  8. // Add checks for invalid selection.
  9. // The position to add the piece was give 2,3
  10. int i = 2;
  11. for(; i < number_of_rows && M[i][3] != 0; i++)
  12. // 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)
  13. M[i-1][3] = 1   // For 1st player. // Remember to check for edge cases here and invalid options its important.
  14.  
  15. // Check horizontal connect 4 (Try to implement vertical in a similar manner)
  16. // Say the user just added 1 to M[a][b]
  17. // Check the left side of the piece (include the piece)
  18. int count = 0;  // Stores how many continous same player pieces we saw.
  19. for(int i = b; i > 0 && M[a][i] == M[a][b]; i--) {
  20.     count ++;
  21. }
  22. // Check the right side but do not include the piece.
  23. for(int i = b+1; i < number_of_column && M[a][i] == M[a][b]; i++) {
  24.     count ++;
  25. }
  26. // Check if we say 4 pieces of the same type
  27. if(count >= 4) {
  28.     // 1st user wins.
  29. }
  30.  
  31. // Diagonal check.
  32. // 2 cases, 45 degree diagonal and 135 degree diagonal.
  33. // I will show you 45, you can implement 135.
  34. // Check the right-up direction including the piece.
  35. // Piece was added to M[a][b]
  36. int count = 0;
  37. for(int i=a, j=b; i >= 0 && j < number_of_column && M[i][j] == M[a][b]; i--, j++) {
  38.     count ++;
  39. }
  40. // Check the left-bottom direction not including the piece.
  41. for(int i=a+1, j=b-1; i < number_of_rows && j > 0 && M[i][j] == M[a][b]; i++, j--) {
  42.     count ++;
  43. }
  44. // Check if we have connect 4 same like above.
  45. if(count >= 4) {
  46.     // 1st user wins.
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement