Skip to content

Commit 1af910e

Browse files
committed
Followed pep 8
1 parent fc99ec5 commit 1af910e

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,6 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
# pycharm files
104+
.idea/

AI_Reversi/AI.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
previousGeneration = 1271
1212
mutationAmount = 0.05
1313

14+
1415
# sigmoid function
1516
def sigmoid(x):
16-
return 1 / (1 + math.exp(-x))
17+
return 1 / (1 + math.exp(-x))
18+
1719

1820
# negative function
1921
def negative(x):
20-
return -x
22+
return -x
23+
2124

2225
# evaluate the neural network
2326
def evaluate(board, structure, weights, turn):
@@ -32,6 +35,7 @@ def evaluate(board, structure, weights, turn):
3235
layer = sigmoidForArray(layer) # apply sigmoid function
3336
return layer
3437

38+
3539
# choose the best move
3640
def chooseMove(board, sturcture, weights, turn):
3741
results = evaluate(board, structure, weights, turn) # results of the nerual network
@@ -45,14 +49,16 @@ def chooseMove(board, sturcture, weights, turn):
4549
bestMove = coordinates
4650
return bestMove
4751

52+
4853
# generate random neural network
4954
def generateNetwork(structure):
5055
weights = []
5156
for i in range(len(structure) - 1):
5257
weights.append(np.array([[random.uniform(-1, 1) for _ in range(structure[i] + 1)] for _ in range(structure[i + 1])]))
5358
return weights
5459

55-
# function for loading neural networks
60+
61+
# function for loading neural networks
5662
def loadNetworks(structure, previousGeneration):
5763
with open("gen" + str(previousGeneration), "r") as file:
5864
lines = file.read().splitlines()
@@ -78,6 +84,7 @@ def loadNetworks(structure, previousGeneration):
7884

7985
return networks
8086

87+
8188
# function for saving neural networks
8289
def saveNetworks(networks, sortedNetworksTuples, previousGeneration):
8390
file = open("gen" + str(previousGeneration), "w")

AI_Reversi/reversi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
w = -1 # white tile ID
99
b = 1 # black tile ID
1010

11+
1112
def playerASCII(): # defining tile ID
1213
return white if turn == w else black
1314

15+
1416
def countChess(board, turn): # count how many pieces are on the board
1517
return sum(board[i].count(turn) for i in range(n))
1618

19+
1720
def printBoard(board): # print the board
1821
print()
1922
print(" ", end = "")
@@ -30,6 +33,7 @@ def printBoard(board): # print the board
3033
print(white + ":" + str(countChess(w)), end = "")
3134
print()
3235

36+
3337
def generateBoard(): # prepare board
3438
board = [[0 for _ in range(n)] for _ in range(n)] # the board
3539
board[n // 2 - 1][n // 2 - 1] = w
@@ -38,14 +42,17 @@ def generateBoard(): # prepare board
3842
board[n // 2 - 1][n // 2] = b
3943
return board
4044

45+
4146
def outOfBound(x, y): # if is out of board/bounds
4247
return x < 0 or x >= n or y < 0 or y >= n
4348

49+
4450
def neighbourIsOpposite(board, x, y, dx, dy, turn):
4551
if outOfBound(x + dx, y + dy):
4652
return False
4753
return board[x + dx][y + dy] == opposite(turn)
4854

55+
4956
def checkSandwich(board, x, y, dx, dy, turn):
5057
if dx == 0 and dy == 0:
5158
return False
@@ -58,9 +65,11 @@ def checkSandwich(board, x, y, dx, dy, turn):
5865
else: # space is unoccupied
5966
return False
6067

68+
6169
def opposite(turn): # change turn
6270
return w if turn == b else b
6371

72+
6473
def isLegal(board, x, y, turn):
6574
if outOfBound(x, y): # out of bound
6675
return False
@@ -73,15 +82,18 @@ def isLegal(board, x, y, turn):
7382
return True
7483
return False
7584

85+
7686
def replaceNeighbours(board, x, y, dx, dy, turn):
7787
if checkSandwich(board, x, y, dx, dy, turn):
7888
board[x + dx][y + dy] = turn
7989
replaceNeighbours(board, x + dx, y + dy, dx, dy, turn)
8090

91+
8192
def generateValidMovesList(board, turn):
8293
return [(i,j) for i in range(n) for j in range(n) \
8394
if isLegal(board, i, j, turn)]
8495

96+
8597
def makeMove(board, x, y, turn):
8698
board[x][y] = turn
8799
for i in range(-1, 2):

AI_Reversi/reversi_play.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@
33
import random
44
import AI
55

6-
generation = 861 ## Gerneation to play against
7-
n = 8 # board size 8,6,4,2(needs to be even)
8-
white = "☺" # white tile ascii
9-
black = "☻" # black tile ascii
10-
bg = "•" # bg tile ascii
6+
generation = 2500 # Generation to play against
7+
n = 8 # board size 8,6,4,2(needs to be even)
8+
white = "☺" # white tile ascii
9+
black = "☻" # black tile ascii
10+
bg = "•" # bg tile ascii
1111

12-
w = -1 # white tile ID
13-
b = 1 # black tile ID
12+
w = -1 # white tile ID
13+
b = 1 # black tile ID
1414

15-
board = [[0 for _ in range(n)] for _ in range(n)] # the board
15+
board = [[0 for _ in range(n)] for _ in range(n)] # the board
1616

1717

18-
def playerASCII(): # defining tile ID
18+
def playerASCII(): # defining tile ID
1919
return white if turn == w else black
2020

21-
def countChess(turn): # count how many pieces are on the board
21+
22+
def countChess(turn): # count how many pieces are on the board
2223
return sum(board[i].count(turn) for i in range(n))
2324

24-
def printBoard(): # print the board
25+
26+
def printBoard(): # print the board
2527
print()
2628
print(" ", end = "")
2729
for i in range(n):
2830
print(i, end = " ")
2931
print()
30-
for i in range(n): # making a 2-D array
32+
for i in range(n): # making a 2-D array
3133
print(i, end = " ")
3234
for j in range(n):
3335
print(white if board[j][i] == w else \
@@ -37,51 +39,56 @@ def printBoard(): # print the board
3739
print(white + ":" + str(countChess(w)), end = "")
3840
print()
3941

40-
def initBoard(): # prepare board
42+
43+
def initBoard(): # prepare board
4144
board[n // 2 - 1][n // 2 - 1] = w
4245
board[n // 2][n // 2] = w
4346
board[n // 2][n // 2 - 1] = b
4447
board[n // 2 - 1][n // 2] = b
4548

46-
## custom board for testing
47-
#global board
48-
#board = [[b,b,b,b,b,b,b,b],
49-
# [b,b,b,b,b,b,b,b],
50-
# [b,b,b,w,b,b,b,b],
51-
# [b,w,b,b,b,w,b,b],
52-
# [b,b,b,b,w,b,b,b],
53-
# [b,b,b,0,w,b,b,b],
54-
# [b,b,0,0,w,w,0,0],
55-
# [b,0,0,0,w,w,w,0]]
56-
57-
def outOfBound(x, y): # if is out of board/bounds
49+
# custom board for testing
50+
#global board
51+
#board = [[b,b,b,b,b,b,b,b],
52+
# [b,b,b,b,b,b,b,b],
53+
# [b,b,b,w,b,b,b,b],
54+
# [b,w,b,b,b,w,b,b],
55+
# [b,b,b,b,w,b,b,b],
56+
# [b,b,b,0,w,b,b,b],
57+
# [b,b,0,0,w,w,0,0],
58+
# [b,0,0,0,w,w,w,0]]
59+
60+
61+
def outOfBound(x, y): # if is out of board/bounds
5862
return x < 0 or x >= n or y < 0 or y >= n
5963

64+
6065
def neighbourIsOpposite(x, y, dx, dy):
6166
if outOfBound(x + dx, y + dy):
6267
return False
6368
return board[x + dx][y + dy] == opposite(turn)
6469

70+
6571
def checkSandwich(x, y, dx, dy, turn):
6672
if dx == 0 and dy == 0:
6773
return False
68-
if outOfBound(x + dx, y + dy): # out of bound
74+
if outOfBound(x + dx, y + dy): # out of bound
6975
return False
70-
if board[x + dx][y + dy] == turn: # sandwiched
76+
if board[x + dx][y + dy] == turn: # sandwiched
7177
return True
7278
elif board[x + dx][y + dy] == opposite(turn):
7379
return checkSandwich(x + dx, y + dy, dx, dy, turn)
7480
else: # space is unoccupied
7581
return False
7682

77-
def opposite(turn): # change turn
83+
84+
def opposite(turn): # change turn
7885
return w if turn == b else b
7986

8087

8188
def isLegal(x, y, turn):
82-
if outOfBound(x, y): # out of bound
89+
if outOfBound(x, y): # out of bound
8390
return False
84-
if board[x][y] != 0: # space is occupied
91+
if board[x][y] != 0: # space is occupied
8592
return False
8693
for i in range(-1, 2):
8794
for j in range(-1, 2):
@@ -90,20 +97,23 @@ def isLegal(x, y, turn):
9097
return True
9198
return False
9299

100+
93101
def replaceNeighbours(x, y, dx, dy, turn):
94102
if checkSandwich(x, y, dx, dy, turn):
95103
board[x + dx][y + dy] = turn
96104
replaceNeighbours(x + dx, y + dy, dx, dy, turn)
97105

106+
98107
def generateValidMovesList():
99108
return [(i,j) for i in range(n) for j in range(n) \
100109
if isLegal(i, j, turn)]
101110

111+
102112
initBoard()
103113

104-
turn = b # turn indicator b or w(Black Or White), black goes first
105-
whiteCanPlay = True # if white is not stuck
106-
blackCanPlay = True # if black is not stuck
114+
turn = b # turn indicator b or w(Black Or White), black goes first
115+
whiteCanPlay = True # if white is not stuck
116+
blackCanPlay = True # if black is not stuck
107117

108118
# legal check
109119
playerTile = input("Choose your chess color. Type 'b' for black and white otherwise. Black goes first: ")
@@ -147,7 +157,7 @@ def generateValidMovesList():
147157
print("Invalid Format. ", end = "")
148158
else:
149159
# computer's turn
150-
#x, y = random.choice(validMoves)
160+
# x, y = random.choice(validMoves)
151161
x, y = AI.chooseMove(board, AI.structure, bestNetwork, turn)
152162
print(playerASCII() + " Computer's move:", x, y)
153163

0 commit comments

Comments
 (0)