aboutsummaryrefslogtreecommitdiffstats
path: root/Not Jack
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
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 '')
-rw-r--r--Not Jack/Algorithm.py67
-rw-r--r--Not Jack/DotsNBoxes.py63
-rw-r--r--Not Jack/Nodes.py18
-rw-r--r--Not Jack/main.py40
4 files changed, 188 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
diff --git a/Not Jack/DotsNBoxes.py b/Not Jack/DotsNBoxes.py
new file mode 100644
index 0000000..6674762
--- /dev/null
+++ b/Not Jack/DotsNBoxes.py
@@ -0,0 +1,63 @@
+from random import *
+import collections
+from Algorithm import *
+from Board import *
+from Nodes import *
+
+
+class DotsNBoxes: # A class for managing the moves made by the human and the computer
+ def __init__(self, Board_Xdim, Board_Ydim, Ply_num):
+ currentState = Game([], Board_Xdim, Board_Ydim)
+ currentState.Initiate()
+ self.State = Thing(currentState)
+ self.Ply_num = Ply_num
+ self.Score = 0
+
+ def Human(self): # Defining the Human player and his actions/Choices
+ self.State.Draw()
+
+ HumanX = int(input("Please enter the 'X' coordinate of your choice (an integer such as 4): "))
+ HumanY = int(input("Please enter the 'Y' coordinate of your choice (an integer such as 4): "))
+ if (HumanX, HumanY) not in self.State.children:
+ self.State.Make(HumanX, HumanY, False)
+ self.State = self.State.children[(HumanX, HumanY)]
+ else:
+ self.State = self.State.children[(HumanX, HumanY)]
+
+ print("Current Score =====>> Your Score - AI Score = " + str(self.State.CurrentScore),end ="\n\n\n")
+
+ self.Computer()
+
+
+ def Computer(self): # Defining the Computer player and its actions/Choices
+ self.State.Draw()
+
+ move = Algo.miniMax(self.State, self.Ply_num)
+
+ self.State = self.State.children[(move[0], move[1])]
+
+ print("AI selected the following coordinates to play:\n" + "(" ,str(move[0]), ", " + str(move[1]), ")", end = "\n\n")
+
+ print("Current Score =====>> Your Score - AI Score = " + str(self.State.CurrentScore), end = "\n\n\n")
+
+ if len(self.State.children) == 0:
+ self.State.Draw()
+ self.Evaluation()
+ return
+
+ self.Human()
+
+ def Evaluation(self): # Evaluation function for taking care of the final scores
+ print("Stop this Madness!!!\n")
+ if self.State.CurrentScore > 0:
+ print("You won you crazy little unicorn!! You are the new hope for the mankind!")
+ exit()
+ elif self.State.CurrentScore < 0:
+ print("!!! Inevitable Doom!!! You were crushed by the AI!! ")
+ exit()
+ else:
+ print("Draw! Well Congratulations! you are as smart as the AI!")
+ exit()
+
+ def start(self):
+ self.Human()
diff --git a/Not Jack/Nodes.py b/Not Jack/Nodes.py
new file mode 100644
index 0000000..da01944
--- /dev/null
+++ b/Not Jack/Nodes.py
@@ -0,0 +1,18 @@
+class Thing: # A class for Node related operations
+ def __init__(self, currentState):
+ self.Current = currentState
+ self.CurrentScore = 0
+ self.children = {}
+
+ def Make(self, i, j, player): # Function for generating a child node
+ self.children[(i, j)] = Thing(self.Current.Get_currentState())
+ mul = 1
+ if player:
+ mul *= -1
+ self.children[(i, j)].CurrentScore = (self.children[(i, j)].Current.action(i, j) * mul) + self.CurrentScore
+
+ def Populate(self, i, j, Child): # Function for adding a node
+ self.children[(i,j)] = Child
+
+ def Draw(self): # function for drawing the board
+ self.Current.Draw_mat()
diff --git a/Not Jack/main.py b/Not Jack/main.py
new file mode 100644
index 0000000..4e37684
--- /dev/null
+++ b/Not Jack/main.py
@@ -0,0 +1,40 @@
+from Algorithm import *
+from DotsNBoxes import *
+from Board import *
+from Nodes import *
+
+def main():
+ while True:
+
+ print("\t\t!! Welcome to the game of Dots and Boxes !!\n\n Be prepared to be crushed by the power of Artificial Intelligence ... !!\n\n\
+ Kidding! You totally can beat it!\n\n\n")
+
+ x = input("Press 1 to start the game or press 2 to escape from the inevitable doom!!\n\n")
+ if x == "1":
+
+ Board_Xdim = int(input("\nPlease enter the number of rows for the board: \n")) * 2 + 1
+
+ if Board_Xdim < 5:
+ print("\nthe number of rows should atleast be 2\n")
+ exit()
+
+ Board_Ydim = int(input("\nPlease enter the number of columns for the board: \n")) * 2 + 1
+
+ if Board_Ydim < 5:
+ print("\nthe number of columns should atleast be 2\n")
+ exit()
+
+ Ply_num = int(input("\nPlease enter the number of plies used by the AI: \n"))
+
+ if Ply_num < 2:
+ print("\nThe number of plies should be higher than 1\n")
+ exit()
+
+ Match = DotsNBoxes(Board_Xdim, Board_Ydim, Ply_num)
+ Match.start()
+ else:
+ print ("\n\nEscape it is!")
+ exit()
+
+if __name__ == "__main__":
+ main()