Advertisement
EXTREMEXPLOIT

Connect 4 Matrix Structure

Nov 12th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. class Matrix:
  2.     def __init__(self, Columns, Rows):
  3.         self.GameMatrix = []
  4.         self.Columns = Columns
  5.         self.Rows = Rows
  6.  
  7.         for _ in range (Rows):
  8.             self.GameMatrix.append([None] * Columns)
  9.  
  10.     def Restart(self):
  11.         self.GameMatrix = []
  12.  
  13.         for _ in range(self.Rows):
  14.             self.GameMatrix.append([None] * self.Columns)
  15.  
  16.         return self.GameMatrix
  17.  
  18.     def Add(self, xPosition, yPosition, Value):
  19.         self.GameMatrix[xPosition][yPosition] = Value
  20.         return Value
  21.  
  22.     def CheckVictory(self, Item):
  23.         # Checking Vertical
  24.         for i in range(self.Columns):
  25.             for j in range(self.Rows - 3):
  26.                 if self.GameMatrix[j][i] == self.GameMatrix[j + 1][i] == self.GameMatrix[j + 2][i] == self.GameMatrix[j + 3][i] == Item:
  27.                     Winner_Positions = [[j, i], [j + 1, i], [j + 2, i], [j + 3, i]]
  28.                     return True, Winner_Positions
  29.  
  30.         # Checking Horizontal.
  31.         for i in range(self.Rows):
  32.             for j in range(self.Columns - 3):
  33.                 if self.GameMatrix[i][j] == self.GameMatrix[i][j + 1] == self.GameMatrix[i][j + 2] == self.GameMatrix[i][j + 3] == Item:
  34.                     Winner_Positions = [[i, j], [i, j + 1], [i, j + 2], [i, j + 3]]
  35.                     return True, Winner_Positions
  36.  
  37.         # Checking Diagonal.
  38.         for i in range(self.Rows):  # Right.
  39.             for j in range(self.Columns):
  40.                 try:
  41.                     if self.GameMatrix[j][i] == self.GameMatrix[j - 1][i + 1] == self.GameMatrix[j - 2][i + 2] == self.GameMatrix[j - 3][i + 3] == Item:
  42.                         Winner_Positions = [[j, i], [j - 1, i + 1], [j - 2, i + 2], [j - 3, i + 3]]
  43.                         return True, Winner_Positions
  44.                 except:
  45.                     pass
  46.  
  47.         for i in range(self.Rows):  # Left.
  48.             for j in range(self.Columns):
  49.                 try:
  50.                     if self.GameMatrix[i][j] == self.GameMatrix[i + 1][j + 1] == self.GameMatrix[i + 2][j + 2] == self.GameMatrix[i + 3][j + 3] == Item:
  51.                         Winner_Positions = [[i, j], [i + 1, j + 1], [i + 2, j + 2], [i + 3, j + 3]]
  52.                         return True, Winner_Positions
  53.                 except:
  54.                     pass
  55.  
  56.         return False, None
  57.  
  58.     def GetColumnState(self, Column):
  59.         for Row in reversed(range(self.Rows)):
  60.             if self.GameMatrix[Row][Column] is None: # If we find an empty position on the column, we return it.
  61.                 return Row
  62.  
  63.         return False # Column is full.
  64.  
  65.     def isFull(self):
  66.         for Row in self.GameMatrix:
  67.             for i in Row:
  68.                 if i is None:
  69.                     return False
  70.         return True
  71.  
  72.     def isEmpty(self):
  73.         for Row in self.GameMatrix:
  74.             for i in Row:
  75.                 if i is not None:
  76.                     return False
  77.         return True
  78.  
  79.     def GetSize(self):
  80.         return self.Rows, self.Columns
  81.  
  82.     def __str__(self):
  83.         String = ""
  84.         for Row in self.GameMatrix:
  85.             String += str("|")
  86.             for i in Row:
  87.                 if i == 0 or i == 1:
  88.                     String += str("  ")
  89.                     String += str(i)
  90.                     String += str("  ")
  91.                 else:
  92.                     String += str("  ")
  93.                     String += str("▢")
  94.                     String += str("  ")
  95.             String += str("|")
  96.             String += str("\n")
  97.  
  98.         return String
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement