2022-07-25 14:46:45 +00:00
|
|
|
from GameMap import *
|
|
|
|
from Player import *
|
2022-08-15 04:28:17 +00:00
|
|
|
from Timer import *
|
2022-07-25 14:46:45 +00:00
|
|
|
|
2022-07-25 10:33:08 +00:00
|
|
|
class GameLogic():
|
2022-07-25 14:46:45 +00:00
|
|
|
|
2022-08-15 04:26:45 +00:00
|
|
|
def __init__(self, gameMap, playerList, winRowLength=3, individualMoves=1):
|
2022-07-25 14:46:45 +00:00
|
|
|
|
|
|
|
self.gameMap = gameMap
|
|
|
|
self.playerList = playerList
|
|
|
|
self.winRowLength = winRowLength
|
|
|
|
self.individualMoves = individualMoves
|
2022-08-15 04:31:52 +00:00
|
|
|
self.timer = Timer()
|
2022-07-25 14:46:45 +00:00
|
|
|
|
2022-08-07 06:23:10 +00:00
|
|
|
def check_who_win(self):
|
|
|
|
# Should have used vectors instead of committing tuples mithosis,
|
|
|
|
# but whatever, it works anyway, and if it works, why code more
|
|
|
|
# for nothing?
|
|
|
|
for player in self.playerList:
|
|
|
|
markList = player.get_markList()
|
|
|
|
# since every mark is checked, there's no need to go into
|
|
|
|
# opposite directions, so, there is (0,1) but no (0, -1).
|
|
|
|
# This twice decreases the amount of computation needed.
|
2022-08-07 06:32:16 +00:00
|
|
|
# maybe TODO - Optimize even more by preventing repeated calculations.
|
2022-08-07 06:23:10 +00:00
|
|
|
directions = ((0, 1), (1, 0), (1,1), (-1, 1))
|
|
|
|
for mark in markList:
|
|
|
|
for direction in directions:
|
2022-08-15 04:29:25 +00:00
|
|
|
if self._is_line(markList, mark, direction):
|
2022-08-07 06:23:10 +00:00
|
|
|
return player
|
|
|
|
return False
|
|
|
|
|
2022-08-15 04:29:25 +00:00
|
|
|
def _is_line(self, markList, mark, direction):
|
2022-08-07 06:23:10 +00:00
|
|
|
for i in range(1, self.winRowLength):
|
|
|
|
seeked = (mark[0] + direction[0] * i, mark[1] + direction[1] * i)
|
|
|
|
if seeked in markList:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
return True
|