From 77a99164bb53b207ba8efec1a37238379f595f44 Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Tue, 13 Apr 2021 17:22:52 -0500 Subject: Rearrange and add more --- Jack/board2.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ Jack/game.py | 0 Jack/main2.py | 20 ++++++++++++++ Jack/node2.py | 17 ++++++++++++ Not Jack/Algorithm.py | 67 +++++++++++++++++++++++++++++++++++++++++++++ Not Jack/DotsNBoxes.py | 63 +++++++++++++++++++++++++++++++++++++++++++ Not Jack/Nodes.py | 18 +++++++++++++ Not Jack/main.py | 40 +++++++++++++++++++++++++++ board2.py | 73 -------------------------------------------------- game.py | 0 main2.py | 20 -------------- node2.py | 17 ------------ 12 files changed, 298 insertions(+), 110 deletions(-) create mode 100644 Jack/board2.py create mode 100644 Jack/game.py create mode 100644 Jack/main2.py create mode 100644 Jack/node2.py create mode 100644 Not Jack/Algorithm.py create mode 100644 Not Jack/DotsNBoxes.py create mode 100644 Not Jack/Nodes.py create mode 100644 Not Jack/main.py delete mode 100644 board2.py delete mode 100644 game.py delete mode 100644 main2.py delete mode 100644 node2.py diff --git a/Jack/board2.py b/Jack/board2.py new file mode 100644 index 0000000..3aecaa5 --- /dev/null +++ b/Jack/board2.py @@ -0,0 +1,73 @@ +from random import * + +class Board: #A class for managing different situations and states happening in the game and on the board + def __init__(self, board): + self.board=board + def get_board(self): + return self.board + def print_board(self): + for i in self.board: + for j in i: + print(j,end="") + print() + def play_move(self,v1,v2): #v1 and v2 correlate to the first and second vertex that you would draw to on the board vertex 1 is always smaller than vertex 2 + if((v2-v1)==1): + if(v1<5): + c=0 + elif(v1<9): + c=2 + v1=v1-4 + v2=v2-4 + elif(v1<13): + c=4 + v1=v1-8 + v2=v2-8 + else: + c=6 + v1=v1-12 + v2=v2-12 + + x1=v1+3*(v1-1) + x2=v2+3*(v2-1) + for i in range (x1,x2-1): + self.board[c][i]="-" + else: + if(v2<9): + c=1 + elif(v2<13): + c=3 + else: + c=5 + if(v2%4==1): + self.board[c][0]="|" + elif(v2%4==2): + self.board[c][4]="|" + elif(v2%4==3): + self.board[c][8]="|" + else: + self.board[c][12]="|" + if(self.check_fill()): + return 1 + else: + return 0 + def check_fill(self): + x=0 + while(x<=4): + y=0 + while(y<=8): + if(self.board[x][y+2]=='-' and self.board[x+1][y]=='|' and self.board[x+1][y+4]=='|' and self.board[x+2][y+2]=='-'): + if(self.board[x+1][y+2]!='X'): + self.board[x+1][y+2]='X' + return True + + y=y+4 + x=x+2 + + return False + def is_legal(self,v1,v2): # we might not need this + if(v1 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() diff --git a/board2.py b/board2.py deleted file mode 100644 index 3aecaa5..0000000 --- a/board2.py +++ /dev/null @@ -1,73 +0,0 @@ -from random import * - -class Board: #A class for managing different situations and states happening in the game and on the board - def __init__(self, board): - self.board=board - def get_board(self): - return self.board - def print_board(self): - for i in self.board: - for j in i: - print(j,end="") - print() - def play_move(self,v1,v2): #v1 and v2 correlate to the first and second vertex that you would draw to on the board vertex 1 is always smaller than vertex 2 - if((v2-v1)==1): - if(v1<5): - c=0 - elif(v1<9): - c=2 - v1=v1-4 - v2=v2-4 - elif(v1<13): - c=4 - v1=v1-8 - v2=v2-8 - else: - c=6 - v1=v1-12 - v2=v2-12 - - x1=v1+3*(v1-1) - x2=v2+3*(v2-1) - for i in range (x1,x2-1): - self.board[c][i]="-" - else: - if(v2<9): - c=1 - elif(v2<13): - c=3 - else: - c=5 - if(v2%4==1): - self.board[c][0]="|" - elif(v2%4==2): - self.board[c][4]="|" - elif(v2%4==3): - self.board[c][8]="|" - else: - self.board[c][12]="|" - if(self.check_fill()): - return 1 - else: - return 0 - def check_fill(self): - x=0 - while(x<=4): - y=0 - while(y<=8): - if(self.board[x][y+2]=='-' and self.board[x+1][y]=='|' and self.board[x+1][y+4]=='|' and self.board[x+2][y+2]=='-'): - if(self.board[x+1][y+2]!='X'): - self.board[x+1][y+2]='X' - return True - - y=y+4 - x=x+2 - - return False - def is_legal(self,v1,v2): # we might not need this - if(v1