aboutsummaryrefslogtreecommitdiffstats
path: root/Not Jack/Algorithm.py
diff options
context:
space:
mode:
authorRossTheRoss <mstrapp@protonmail.com>2021-04-13 17:22:52 -0500
committerRossTheRoss <mstrapp@protonmail.com>2021-04-13 17:22:52 -0500
commit77a99164bb53b207ba8efec1a37238379f595f44 (patch)
tree47f95873e3f005749853efb5d9e2c6f986c077a8 /Not Jack/Algorithm.py
parentRearrange things more I guess (diff)
downloadcsci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar.gz
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar.bz2
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar.lz
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar.xz
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.tar.zst
csci4511w-77a99164bb53b207ba8efec1a37238379f595f44.zip
Rearrange and add more
Diffstat (limited to 'Not Jack/Algorithm.py')
-rw-r--r--Not Jack/Algorithm.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/Not Jack/Algorithm.py b/Not Jack/Algorithm.py
new file mode 100644
index 0000000..de4fbc0
--- /dev/null
+++ b/Not Jack/Algorithm.py
@@ -0,0 +1,67 @@
+
+class Algo: # A class for defining algorithms used (minimax and alpha-beta pruning)
+
+ def miniMax(State, Ply_num): # Function for the minimax algorithm
+
+ for i in range(State.Current.dimY):
+ for j in range(State.Current.dimX):
+ if State.Current.Mat[i][j] == ' ' and (j, i) not in State.children:
+ State.Make(j, i, True)
+ if Ply_num < 2:
+ return (i, j)
+
+ Minimum_Score = 1000
+ i = 0
+ j = 0
+ for k, z in State.children.items():
+ Result = Algo.Maximum(z, Ply_num - 1, Minimum_Score)
+ if Minimum_Score > Result:
+ Minimum_Score = Result
+ i = k[0]
+ j = k[1]
+
+ return (i, j)
+
+
+ def Maximum(State, Ply_num, Alpha): # Alpha-beta pruning function for taking care of Alpha values
+ if Ply_num == 0:
+ return State.CurrentScore
+
+ for i in range(State.Current.dimY):
+ for j in range(State.Current.dimX):
+ if State.Current.Mat[i][j] == ' ' and (j, i) not in State.children:
+ State.Make(j, i, False)
+
+ Maximum_Score = -1000
+ i = 0
+ j = 0
+ for k, z in State.children.items():
+ Result = Algo.Minimum(z, Ply_num - 1, Maximum_Score)
+ if Maximum_Score < Result:
+ Maximum_Score = Result
+ if Result > Alpha:
+ return Result
+
+ return Maximum_Score
+
+
+ def Minimum(State, Ply_num, Beta): # Alpha-beta pruning function for taking care of Beta values
+ if Ply_num == 0:
+ return State.CurrentScore
+
+ for i in range(State.Current.dimY):
+ for j in range(State.Current.dimX):
+ if State.Current.Mat[i][j] == ' ' and (j, i) not in State.children:
+ State.Make(j, i, True)
+
+ Minimum_Score = 1000
+ i = 0
+ j = 0
+ for k, z in State.children.items():
+ Result = Algo.Maximum(z, Ply_num - 1, Minimum_Score)
+ if Minimum_Score > Result:
+ Minimum_Score = Result
+ if Result < Beta:
+ return Result
+
+ return Minimum_Score