You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.6 KiB
Python
122 lines
3.6 KiB
Python
class FIAR():
|
|
def __init__(self, height=6, width=7):
|
|
self.height = height # y
|
|
self.width = width # x
|
|
self.field = []
|
|
# field[x][y]
|
|
for x in range(width):
|
|
col = []
|
|
for y in range(height):
|
|
col.append(None)
|
|
self.field.append(col)
|
|
|
|
def getTransformedField(self):
|
|
output = []
|
|
for col in self.field:
|
|
output += col
|
|
return output
|
|
|
|
def printField(self):
|
|
redBG = "\33[41m"
|
|
greenBG = "\33[42m"
|
|
clearBG = "\33[0m"
|
|
dashes = (self.width * 2)+1
|
|
print("-" * dashes)
|
|
for y in range(self.height, -1, -1):
|
|
for x in range(self.width):
|
|
if y == self.height:
|
|
print("|" + str(x+1), end='')
|
|
elif self.field[x][y] == 1:
|
|
print("|" + redBG + "x" + clearBG, end='')
|
|
elif self.field[x][y] == 2:
|
|
print("|" + greenBG+"o" + clearBG, end='')
|
|
else:
|
|
print("|" + " ", end='')
|
|
print("|")
|
|
print("-" * dashes)
|
|
|
|
def setStone(self, col, player):
|
|
if(col in list(range(self.width))):
|
|
has_set = False
|
|
for i in range(self.height):
|
|
if self.field[col][i] == None:
|
|
self.field[col][i] = player
|
|
has_set = True
|
|
break
|
|
if not has_set:
|
|
raise OverflowError
|
|
else:
|
|
raise IndexError
|
|
|
|
def proveWinner(self):
|
|
# Tie:
|
|
tie = True
|
|
for x in range(self.width):
|
|
if(self.field[x][self.height-1] == None):
|
|
tie = False
|
|
break
|
|
if tie:
|
|
return 3
|
|
|
|
# Horizontal:
|
|
for y in range(self.height):
|
|
for x in range(self.width-3):
|
|
if self.field[x][y] != None:
|
|
first = self.field[x][y]
|
|
isWinner = True
|
|
|
|
for i in range(1, 4):
|
|
if self.field[x+i][y] != first:
|
|
isWinner = False
|
|
break
|
|
|
|
if isWinner:
|
|
return first
|
|
|
|
# Vertical:
|
|
for x in range(self.width):
|
|
for y in range(self.height-3):
|
|
if self.field[x][y] != None:
|
|
first = self.field[x][y]
|
|
isWinner = True
|
|
|
|
for i in range(1, 4):
|
|
if self.field[x][y+i] != first:
|
|
isWinner = False
|
|
break
|
|
|
|
if isWinner:
|
|
return first
|
|
|
|
# Diagonal Up:
|
|
for x in range(self.width-3):
|
|
for y in range(self.height-3):
|
|
if self.field[x][y] != None:
|
|
first = self.field[x][y]
|
|
isWinner = True
|
|
|
|
for i in range(1, 4):
|
|
if self.field[x+i][y+i] != first:
|
|
isWinner = False
|
|
break
|
|
|
|
if isWinner:
|
|
return first
|
|
|
|
# Diagonal Down:
|
|
for x in range(3, self.width):
|
|
for y in range(self.height-3):
|
|
if self.field[x][y] != None:
|
|
first = self.field[x][y]
|
|
isWinner = True
|
|
|
|
for i in range(1, 4):
|
|
if self.field[x-i][y+i] != first:
|
|
isWinner = False
|
|
break
|
|
|
|
if isWinner:
|
|
return first
|
|
|
|
return False
|