-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlock.py
91 lines (75 loc) · 3.98 KB
/
Block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from Cell4State import *
LineState = Cell4State()
class Block() :
def __init__(self) :
# Initially all are in State 0
self.Rows = [ 0 for row in range(4)]
self.Cols = [ 0 for col in range(4)]
self.Diags = [ 0 for diag in range(2)]
self.History = [ [0 for col in range(4)] for row in range(4) ]
# No of Empty Cells
self.EmptyCells = 16
# Current Utility of the Block
self.BlockUtility = 10*LineState.StateUtility[0]
self.Status = "War"
# [ Current State][ Cell No ][ Player No]
def UpdateForward(self,Move,Player) :
# Update Row State
self.BlockUtility += LineState.UtilityChangeForward[ self.Rows[Move[0]] ][ Move[1] ][ Player ]
self.Rows[Move[0]] = LineState.ForwardAd[ self.Rows[Move[0]] ][ Move[1] ][ Player ]
# Update Col State
self.BlockUtility += LineState.UtilityChangeForward[ self.Cols[Move[1]] ][ Move[0] ][ Player ]
self.Cols[Move[1]] = LineState.ForwardAd[ self.Cols[Move[1]] ][ Move[0] ][ Player ]
# check for Block Status
if self.Rows[Move[0]] == LineState.WinState or self.Cols[Move[1]] == LineState.WinState :
self.Status = "Won"
elif self.Rows[Move[0]] == LineState.LoseState or self.Cols[Move[1]] == LineState.LoseState :
self.Status = "Lost"
# if this lies on diagonal 0
if Move[0] == Move[1] :
self.BlockUtility += LineState.UtilityChangeForward[ self.Diags[0] ][ Move[0] ][ Player ]
self.Diags[0] = LineState.ForwardAd[ self.Diags[0] ][ Move[0] ][ Player ]
if self.Diags[0] == LineState.WinState :
self.Status = "Won"
elif self.Diags[0] == LineState.LoseState:
self.Status = "Lost"
# if this lies on diagonal 1
if Move[0] + Move[1] == 3 :
self.BlockUtility += LineState.UtilityChangeForward[ self.Diags[1] ][ Move[0] ][ Player ]
self.Diags[1] = LineState.ForwardAd[ self.Diags[1] ][ Move[0] ][ Player ]
if self.Diags[1] == LineState.WinState :
self.Status = "Won"
elif self.Diags[1] == LineState.LoseState:
self.Status = "Lost"
# Decrement the number of Empty cells
self.EmptyCells -= 1
def UpdateBackward(self,Move,Player) :
# Update Block Stats if relevent
if self.Rows[Move[0]] == LineState.WinState or self.Cols[Move[1]] == LineState.WinState :
self.Status = "War"
elif self.Rows[Move[0]] == LineState.LoseState or self.Cols[Move[1]] == LineState.LoseState :
self.Status = "War"
# Update Row State
self.BlockUtility += LineState.UtilityChangeBackward[ self.Rows[Move[0]] ][ Move[1] ][ Player ]
self.Rows[Move[0]] = LineState.BackwardAd[ self.Rows[Move[0]] ][ Move[1] ][ Player ]
# Update Col State
self.BlockUtility += LineState.UtilityChangeBackward[ self.Cols[Move[1]] ][ Move[0] ][ Player ]
self.Cols[Move[1]] = LineState.BackwardAd[ self.Cols[Move[1]] ][ Move[0] ][ Player ]
# if this lies on diagonal 0
if Move[0] == Move[1] :
if self.Diags[0] == LineState.WinState :
self.Status = "War"
elif self.Diags[0] == LineState.LoseState:
self.Status = "War"
self.BlockUtility += LineState.UtilityChangeBackward[ self.Diags[0] ][ Move[0] ][ Player ]
self.Diags[0] = LineState.BackwardAd[ self.Diags[0] ][ Move[0] ][ Player ]
# if this lies on diagonal 1
if Move[0] + Move[1] == 3 :
if self.Diags[1] == LineState.WinState :
self.Status = "War"
elif self.Diags[1] == LineState.LoseState:
self.Status = "War"
self.BlockUtility += LineState.UtilityChangeBackward[ self.Diags[1] ][ Move[0] ][ Player ]
self.Diags[1] = LineState.BackwardAd[ self.Diags[1] ][ Move[0] ][ Player ]
# Decrement the number of Empty cells
self.EmptyCells += 1