|
| 1 | +''' |
| 2 | +Board.py |
| 3 | +
|
| 4 | +This module contains the class "Board" |
| 5 | +
|
| 6 | +Board is where the pieces move on |
| 7 | +
|
| 8 | +last modified: August 19, 2021 |
| 9 | +by: Freeman Sun (GunesOzgur) |
| 10 | +
|
| 11 | +GitHub: GunesOzgur |
| 12 | +
|
| 13 | +--------------------------------------- |
| 14 | +
|
| 15 | +Board class has these data attributes: |
| 16 | +
|
| 17 | + - Board.cnvs: Canvas() object that the board will be on |
| 18 | + |
| 19 | + - Board.x: horizontal position of the board |
| 20 | +
|
| 21 | + - Board.y: vertical position of the board |
| 22 | +
|
| 23 | + - Board.bgEven: square image for checked background |
| 24 | +
|
| 25 | + - Board.bgOdd: square image for checked background |
| 26 | +
|
| 27 | + - Board.green: green mino image for moving pieces |
| 28 | +
|
| 29 | + - Board.red: red mino image for settled pieces |
| 30 | +
|
| 31 | + - Board.EMPTY: value of empty squares on board matrix |
| 32 | +
|
| 33 | + - Board.GREEN: value of green minos on board matrix |
| 34 | +
|
| 35 | + - Board.RED: value of red minos on board matrix |
| 36 | +
|
| 37 | + - Board.fullRows: list of filled rows |
| 38 | +
|
| 39 | + - Board.mtrx: matrix for square values |
| 40 | +
|
| 41 | + - Board.sqrID: matrix Canvas IDs of visible squares |
| 42 | +
|
| 43 | +----------------------------------------- |
| 44 | +
|
| 45 | +Board class has these methods: |
| 46 | +
|
| 47 | + - Board.update(): update board acc. to board matrix |
| 48 | +
|
| 49 | + - Board.clearFulls(): find and clear filled rows |
| 50 | +
|
| 51 | + - Board.printMtrx(): print matrix values |
| 52 | +
|
| 53 | + - Board.write(x, y, value): change the board matrix |
| 54 | + elements (square values) |
| 55 | +
|
| 56 | + - Board.reset(): reset the board matrix |
| 57 | +
|
| 58 | +---------------------------------------- |
| 59 | +
|
| 60 | +''' |
| 61 | + |
| 62 | +import tkinter as tk |
| 63 | + |
| 64 | +src = "src/" |
| 65 | + |
| 66 | +class Board: |
| 67 | + |
| 68 | + def __init__(self, cnvs, x, y): |
| 69 | + self.x = x |
| 70 | + self.y = y |
| 71 | + self.cnvs = cnvs |
| 72 | + |
| 73 | + self.bgEven = tk.PhotoImage(file=src+"bg0.png") |
| 74 | + self.bgOdd = tk.PhotoImage(file=src+"bg1.png") |
| 75 | + self.green = tk.PhotoImage(file=src+"mino_green.png") |
| 76 | + self.red = tk.PhotoImage(file=src+"mino_red.png") |
| 77 | + |
| 78 | + self.EMPTY = 0 |
| 79 | + self.GREEN = 2 |
| 80 | + self.RED = 3 |
| 81 | + |
| 82 | + self.fullRows = [] |
| 83 | + |
| 84 | + # Matrix for square values |
| 85 | + # 0 1 2 3 4 5 6 7 8 9 10 |
| 86 | + self.mtrx = [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 0 invisible |
| 87 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 1 invisible |
| 88 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 2 invisible |
| 89 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 3 |
| 90 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 4 |
| 91 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 5 |
| 92 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 6 |
| 93 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 7 |
| 94 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 8 |
| 95 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 9 |
| 96 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 10 |
| 97 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 11 |
| 98 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 12 |
| 99 | + [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], # 13 |
| 100 | + [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] # 14 invisible |
| 101 | + |
| 102 | + # Matrix for tkinter.Canvas IDs |
| 103 | + # 0 1 2 3 4 5 6 7 8 9 10 |
| 104 | + self.sqrID = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 0 |
| 105 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 1 |
| 106 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 2 |
| 107 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 3 |
| 108 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 4 |
| 109 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 5 |
| 110 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 6 |
| 111 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 7 |
| 112 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 8 |
| 113 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 9 |
| 114 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 10 |
| 115 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 11 |
| 116 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 12 |
| 117 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] # 13 |
| 118 | + # for the 14th row ID not necessary |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + # initial print of board |
| 123 | + # from row 3 to row 13 (the visible ones) |
| 124 | + for j in range(3, len(self.mtrx)-1): |
| 125 | + # the first and the last elements of a row are |
| 126 | + # parts of the board frame |
| 127 | + # no need to print them |
| 128 | + for i in range(1, len(self.mtrx[1])-1): |
| 129 | + # If the square is empty |
| 130 | + if self.mtrx[j][i] == self.EMPTY: |
| 131 | + if (i+j)%2: |
| 132 | + self.sqrID[j][i] = self.cnvs.create_image( |
| 133 | + 30*i+self.x, |
| 134 | + 30*(j-3)+self.y, |
| 135 | + anchor=tk.NE, |
| 136 | + image=self.bgOdd) |
| 137 | + |
| 138 | + else: |
| 139 | + self.sqrID[j][i] = self.cnvs.create_image( |
| 140 | + 30*i+self.x, |
| 141 | + 30*(j-3)+self.y, |
| 142 | + anchor=tk.NE, |
| 143 | + image=self.bgEven) |
| 144 | + |
| 145 | + # If the square is green |
| 146 | + elif self.mtrx[j][i] == self.GREEN: |
| 147 | + self.sqrID[j][i] = self.cnvs.create_image( |
| 148 | + 30*i+self.x, |
| 149 | + 30*(j-3)+self.y, |
| 150 | + anchor=tk.NE, |
| 151 | + image=self.green) |
| 152 | + |
| 153 | + # If the square is red |
| 154 | + elif self.mtrx[j][i] == self.RED: |
| 155 | + self.sqrID[j][i] = self.cnvs.create_image( |
| 156 | + 30*i+self.x, |
| 157 | + 30*(j-3)+self.y, |
| 158 | + anchor=tk.NE, |
| 159 | + image=self.red) |
| 160 | + |
| 161 | + #def printIDs(self): |
| 162 | + # for j in range(1, len(self.mtrx)-1): |
| 163 | + # for i in range(1, len(self.mtrx[1])-1): |
| 164 | + # print("[",j,"][",i,"]: ", self.sqrID[j][i]) |
| 165 | + |
| 166 | + def update(self): |
| 167 | + for j in range(3, len(self.mtrx)-1): |
| 168 | + for i in range(1, len(self.mtrx[1])-1): |
| 169 | + if self.mtrx[j][i] == self.EMPTY: |
| 170 | + if (i+j)%2: |
| 171 | + self.cnvs.itemconfig(self.sqrID[j][i], image=self.bgOdd) |
| 172 | + else: |
| 173 | + self.cnvs.itemconfig(self.sqrID[j][i], image=self.bgEven) |
| 174 | + |
| 175 | + elif self.mtrx[j][i] == self.GREEN: |
| 176 | + self.cnvs.itemconfig(self.sqrID[j][i], image=self.green) |
| 177 | + |
| 178 | + |
| 179 | + elif self.mtrx[j][i] == self.RED: |
| 180 | + self.cnvs.itemconfig(self.sqrID[j][i], image=self.red) |
| 181 | + |
| 182 | + def clearFulls(self): |
| 183 | + self.point = 0 # reset point |
| 184 | + # Look for the full rows from top to bottom |
| 185 | + for j in range(3, len(self.mtrx)-1): |
| 186 | + if self.mtrx[j].count(0) == 0: |
| 187 | + # If a full row is detected, crear it |
| 188 | + for i in range(1,len(self.mtrx[j])): |
| 189 | + self.mtrx[j][i] = 0 |
| 190 | + |
| 191 | + self.point += 1 # count point |
| 192 | + |
| 193 | + # After clearing, pull down the blocks above |
| 194 | + for j2 in range(-j, -1): |
| 195 | + self.mtrx[-j2] = self.mtrx[-j2-1].copy() |
| 196 | + |
| 197 | + return self.point |
| 198 | + |
| 199 | + def printMtrx(self): |
| 200 | + for j in range(len(self.mtrx)): |
| 201 | + print(self.mtrx[j]) |
| 202 | + |
| 203 | + def write(self, x, y, value): |
| 204 | + self.mtrx[y][x] = value |
| 205 | + |
| 206 | + def reset(self): |
| 207 | + for j in range(1, len(self.mtrx)-1): |
| 208 | + for i in range(1, len(self.mtrx[1])-1): |
| 209 | + self.mtrx[j][i] = 0 |
| 210 | + |
0 commit comments