Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Matrix:
- def __init__(self, Columns, Rows):
- self.GameMatrix = []
- self.Columns = Columns
- self.Rows = Rows
- for _ in range (Rows):
- self.GameMatrix.append([None] * Columns)
- def Restart(self):
- self.GameMatrix = []
- for _ in range(self.Rows):
- self.GameMatrix.append([None] * self.Columns)
- return self.GameMatrix
- def Add(self, xPosition, yPosition, Value):
- self.GameMatrix[xPosition][yPosition] = Value
- return Value
- def CheckVictory(self, Item):
- # Checking Vertical
- for i in range(self.Columns):
- for j in range(self.Rows - 3):
- if self.GameMatrix[j][i] == self.GameMatrix[j + 1][i] == self.GameMatrix[j + 2][i] == self.GameMatrix[j + 3][i] == Item:
- Winner_Positions = [[j, i], [j + 1, i], [j + 2, i], [j + 3, i]]
- return True, Winner_Positions
- # Checking Horizontal.
- for i in range(self.Rows):
- for j in range(self.Columns - 3):
- if self.GameMatrix[i][j] == self.GameMatrix[i][j + 1] == self.GameMatrix[i][j + 2] == self.GameMatrix[i][j + 3] == Item:
- Winner_Positions = [[i, j], [i, j + 1], [i, j + 2], [i, j + 3]]
- return True, Winner_Positions
- # Checking Diagonal.
- for i in range(self.Rows): # Right.
- for j in range(self.Columns):
- try:
- if self.GameMatrix[j][i] == self.GameMatrix[j - 1][i + 1] == self.GameMatrix[j - 2][i + 2] == self.GameMatrix[j - 3][i + 3] == Item:
- Winner_Positions = [[j, i], [j - 1, i + 1], [j - 2, i + 2], [j - 3, i + 3]]
- return True, Winner_Positions
- except:
- pass
- for i in range(self.Rows): # Left.
- for j in range(self.Columns):
- try:
- if self.GameMatrix[i][j] == self.GameMatrix[i + 1][j + 1] == self.GameMatrix[i + 2][j + 2] == self.GameMatrix[i + 3][j + 3] == Item:
- Winner_Positions = [[i, j], [i + 1, j + 1], [i + 2, j + 2], [i + 3, j + 3]]
- return True, Winner_Positions
- except:
- pass
- return False, None
- def GetColumnState(self, Column):
- for Row in reversed(range(self.Rows)):
- if self.GameMatrix[Row][Column] is None: # If we find an empty position on the column, we return it.
- return Row
- return False # Column is full.
- def isFull(self):
- for Row in self.GameMatrix:
- for i in Row:
- if i is None:
- return False
- return True
- def isEmpty(self):
- for Row in self.GameMatrix:
- for i in Row:
- if i is not None:
- return False
- return True
- def GetSize(self):
- return self.Rows, self.Columns
- def __str__(self):
- String = ""
- for Row in self.GameMatrix:
- String += str("|")
- for i in Row:
- if i == 0 or i == 1:
- String += str(" ")
- String += str(i)
- String += str(" ")
- else:
- String += str(" ")
- String += str("▢")
- String += str(" ")
- String += str("|")
- String += str("\n")
- return String
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement