Skip to content

Commit b1b3826

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 057380d commit b1b3826

File tree

4 files changed

+160
-98
lines changed

4 files changed

+160
-98
lines changed

maths/Game Theory/AlphaBetaPruning/alphabetapruning.py

+54-30
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
11
from random import choice
22
from math import inf
33

4-
board = [[0, 0, 0],
5-
[0, 0, 0],
6-
[0, 0, 0]]
4+
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
5+
76

87
def Gameboard(board):
9-
chars = {1: 'X', -1: 'O', 0: ' '}
8+
chars = {1: "X", -1: "O", 0: " "}
109
for x in board:
1110
for y in x:
1211
ch = chars[y]
13-
print(f'| {ch} |', end='')
14-
print('\n' + '---------------')
15-
print('===============')
12+
print(f"| {ch} |", end="")
13+
print("\n" + "---------------")
14+
print("===============")
15+
1616

1717
def Clearboard(board):
1818
for x, row in enumerate(board):
1919
for y, col in enumerate(row):
2020
board[x][y] = 0
2121

22+
2223
def winningPlayer(board, player):
23-
conditions = [[board[0][0], board[0][1], board[0][2]],
24-
[board[1][0], board[1][1], board[1][2]],
25-
[board[2][0], board[2][1], board[2][2]],
26-
[board[0][0], board[1][0], board[2][0]],
27-
[board[0][1], board[1][1], board[2][1]],
28-
[board[0][2], board[1][2], board[2][2]],
29-
[board[0][0], board[1][1], board[2][2]],
30-
[board[0][2], board[1][1], board[2][0]]]
24+
conditions = [
25+
[board[0][0], board[0][1], board[0][2]],
26+
[board[1][0], board[1][1], board[1][2]],
27+
[board[2][0], board[2][1], board[2][2]],
28+
[board[0][0], board[1][0], board[2][0]],
29+
[board[0][1], board[1][1], board[2][1]],
30+
[board[0][2], board[1][2], board[2][2]],
31+
[board[0][0], board[1][1], board[2][2]],
32+
[board[0][2], board[1][1], board[2][0]],
33+
]
3134

3235
if [player, player, player] in conditions:
3336
return True
3437

3538
return False
3639

40+
3741
def gameWon(board):
3842
return winningPlayer(board, 1) or winningPlayer(board, -1)
3943

44+
4045
def printResult(board):
4146
if winningPlayer(board, 1):
42-
print('X has won! ' + '\n')
47+
print("X has won! " + "\n")
4348

4449
elif winningPlayer(board, -1):
45-
print('O\'s have won! ' + '\n')
50+
print("O's have won! " + "\n")
4651

4752
else:
48-
print('Draw' + '\n')
53+
print("Draw" + "\n")
54+
4955

5056
def blanks(board):
5157
blank = []
@@ -56,32 +62,44 @@ def blanks(board):
5662

5763
return blank
5864

65+
5966
def boardFull(board):
6067
if len(blanks(board)) == 0:
6168
return True
6269
return False
6370

71+
6472
def setMove(board, x, y, player):
6573
board[x][y] = player
6674

75+
6776
def playerMove(board):
6877
e = True
69-
moves = {1: [0, 0], 2: [0, 1], 3: [0, 2],
70-
4: [1, 0], 5: [1, 1], 6: [1, 2],
71-
7: [2, 0], 8: [2, 1], 9: [2, 2]}
78+
moves = {
79+
1: [0, 0],
80+
2: [0, 1],
81+
3: [0, 2],
82+
4: [1, 0],
83+
5: [1, 1],
84+
6: [1, 2],
85+
7: [2, 0],
86+
8: [2, 1],
87+
9: [2, 2],
88+
}
7289
while e:
7390
try:
74-
move = int(input('Enter a number between 1-9: '))
91+
move = int(input("Enter a number between 1-9: "))
7592
if move < 1 or move > 9:
76-
print('Invalid Move! Try again!')
93+
print("Invalid Move! Try again!")
7794
elif not (moves[move] in blanks(board)):
78-
print('Invalid Move! Try again!')
95+
print("Invalid Move! Try again!")
7996
else:
8097
setMove(board, moves[move][0], moves[move][1], 1)
8198
Gameboard(board)
8299
e = False
83-
except(KeyError, ValueError):
84-
print('Enter a number!')
100+
except (KeyError, ValueError):
101+
print("Enter a number!")
102+
85103

86104
def getScore(board):
87105
if winningPlayer(board, 1):
@@ -93,6 +111,7 @@ def getScore(board):
93111
else:
94112
return 0
95113

114+
96115
def abminimax(board, depth, alpha, beta, player):
97116
row = -1
98117
col = -1
@@ -127,6 +146,7 @@ def abminimax(board, depth, alpha, beta, player):
127146
else:
128147
return [row, col, beta]
129148

149+
130150
def o_comp(board):
131151
if len(blanks(board)) == 9:
132152
x = choice([0, 1, 2])
@@ -139,6 +159,7 @@ def o_comp(board):
139159
setMove(board, result[0], result[1], -1)
140160
Gameboard(board)
141161

162+
142163
def x_comp(board):
143164
if len(blanks(board)) == 9:
144165
x = choice([0, 1, 2])
@@ -151,6 +172,7 @@ def x_comp(board):
151172
setMove(board, result[0], result[1], 1)
152173
Gameboard(board)
153174

175+
154176
def makeMove(board, player, mode):
155177
if mode == 1:
156178
if player == 1:
@@ -164,16 +186,17 @@ def makeMove(board, player, mode):
164186
else:
165187
x_comp(board)
166188

189+
167190
def pvc():
168191
while True:
169192
try:
170-
order = int(input('Enter to play 1st or 2nd: '))
193+
order = int(input("Enter to play 1st or 2nd: "))
171194
if not (order == 1 or order == 2):
172-
print('Please pick 1 or 2')
195+
print("Please pick 1 or 2")
173196
else:
174197
break
175-
except(KeyError, ValueError):
176-
print('Enter a number')
198+
except (KeyError, ValueError):
199+
print("Enter a number")
177200

178201
Clearboard(board)
179202
if order == 2:
@@ -187,6 +210,7 @@ def pvc():
187210

188211
printResult(board)
189212

213+
190214
# Driver Code
191215
print("=================================================")
192216
print("TIC-TAC-TOE using MINIMAX with ALPHA-BETA Pruning")

maths/Game Theory/MonteCarloTreeSearch (MCTS)/monte_carlo_tree_search.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
See also https://en.wikipedia.org/wiki/Monte_Carlo_tree_search
55
https://gist.github.com/qpwo/c538c6f73727e254fdc7fab81024f6e1
66
"""
7+
78
from abc import ABC, abstractmethod
89
from collections import defaultdict
910
import math
@@ -131,4 +132,4 @@ def __hash__(self):
131132
@abstractmethod
132133
def __eq__(node1, node2):
133134
"Nodes must be comparable"
134-
return True
135+
return True

maths/Game Theory/MonteCarloTreeSearch (MCTS)/tictactoe.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
_TTTB = namedtuple("TicTacToeBoard", "tup turn winner terminal")
2727

28+
2829
# Inheriting from a namedtuple is convenient because it makes the class
2930
# immutable and predefines __init__, __repr__, __hash__, __eq__, and others
3031
class TicTacToeBoard(_TTTB, Node):
@@ -126,4 +127,4 @@ def new_tic_tac_toe_board():
126127

127128

128129
if __name__ == "__main__":
129-
play_game()
130+
play_game()

0 commit comments

Comments
 (0)