Python: Code cleanup

This commit is contained in:
Martin Thoma
2022-03-17 07:47:03 +01:00
parent bbd9332569
commit dc511472da

View File

@@ -1,81 +1,85 @@
class TicTacToe: class TicTacToe:
def __init__(self,pick,sz=3): def __init__(self, pick, sz=3):
self.pick = pick self.pick = pick
self.dim_sz = sz self.dim_sz = sz
self.board = self.ClearBoard() self.board = self.clear_board()
def ClearBoard(self): def clear_board(self):
board = [['blur' for i in range(self.dim_sz)] for j in range(self.dim_sz)] board = [["blur" for i in range(self.dim_sz)] for j in range(self.dim_sz)]
# made a 3x3 by-default board # made a 3x3 by-default board
return board return board
def MoveRecord(self,r,c): def move_record(self, r, c):
if r > self.dim_sz or c > self.dim_sz: if r > self.dim_sz or c > self.dim_sz:
return "Out of Bounds" return "Out of Bounds"
if self.board[r][c] != 'blur': if self.board[r][c] != "blur":
return "Spot Pre-Occupied" return "Spot Pre-Occupied"
self.board[r][c] = self.pick self.board[r][c] = self.pick
return True return True
def CheckWin(self):# 1 you won, 0 computer won, -1 tie
#Flag syntax -> first player no. , User is Player#1 ; Check set 1 -> row and '\' diagonal & Check set 2 -> col and '/' diagonal def check_win(self): # 1 you won, 0 computer won, -1 tie
# Flag syntax -> first player no. ,
# User is Player#1 ;
# Check set 1 -> row and '\' diagonal & Check set 2 -> col and '/' diagonal
for i in range(0, self.dim_sz): # Rows
for i in range(0,self.dim_sz):#Rows
flag11 = True flag11 = True
flag21 = True flag21 = True
flag12 = True flag12 = True
flag22 = True flag22 = True
for j in range(0,self.dim_sz): for j in range(0, self.dim_sz):
ch2 = self.board[i][j] ch2 = self.board[i][j]
ch1 = self.board[j][i] ch1 = self.board[j][i]
#Row # Row
if ch1 == self.pick:# if it's mine, computer didn't make it if ch1 == self.pick: # if it's mine, computer didn't make it
flag21 = False flag21 = False
elif ch1 == 'blur':#if it's blank no one made it elif ch1 == "blur": # if it's blank no one made it
flag11 = False flag11 = False
flag21 = False flag21 = False
else: flag11 = False# else i didn't make it else:
flag11 = False # else i didn't make it
if ch2 == self.pick:#Same but for Col if ch2 == self.pick: # Same but for Col
flag22 = False flag22 = False
elif ch2 == 'blur': elif ch2 == "blur":
flag12 = False flag12 = False
flag22 = False flag22 = False
else: flag12 = False else:
flag12 = False
if flag11 is True or flag12 is True:# I won if flag11 is True or flag12 is True: # I won
return 1 return 1
if flag21 is True or flag22 is True:#Computer Won if flag21 is True or flag22 is True: # Computer Won
return 0 return 0
#Diagonals# # Diagonals#
flag11 = True flag11 = True
flag21 = True flag21 = True
flag12 = True flag12 = True
flag22 = True flag22 = True
for i in range(0,self.dim_sz): for i in range(0, self.dim_sz):
ch2 = self.board[i][i] ch2 = self.board[i][i]
ch1 = self.board[i][self.dim_sz-1-i] ch1 = self.board[i][self.dim_sz - 1 - i]
if ch1 == self.pick: if ch1 == self.pick:
flag21 = False flag21 = False
elif ch1 == 'blur': elif ch1 == "blur":
flag11 = False flag11 = False
flag21 = False flag21 = False
else:flag11 = False else:
flag11 = False
if ch2 == self.pick: if ch2 == self.pick:
flag22 = False flag22 = False
elif ch2 == 'blur': elif ch2 == "blur":
flag12 = False flag12 = False
flag22 = False flag22 = False
else:flag12 = False else:
flag12 = False
if flag11 or flag12: if flag11 or flag12:
return 1 return 1
@@ -83,118 +87,136 @@ class TicTacToe:
return 0 return 0
return -1 return -1
def NextMove(self): def next_move(self):
AvailableMoves = []# will carry all available moves available_moves = [] # will carry all available moves
PlayerWinSpot = []#if player (user Wins) player_win_spot = [] # if player (user Wins)
CompPick = 'O' comp_pick = "O"
if self.pick == 'O': if self.pick == "O":
CompPick = 'X' comp_pick = "X"
for i in range(0,self.dim_sz): for i in range(0, self.dim_sz):
for j in range(0,self.dim_sz): for j in range(0, self.dim_sz):
if self.board[i][j] == 'blur':#BLANK if self.board[i][j] == "blur": # BLANK
t = (i,j) t = (i, j)
AvailableMoves.append(t)#add it to available moves available_moves.append(t) # add it to available moves
self.board[i][j] = CompPick#Check if I (Computer can win) self.board[i][j] = comp_pick # Check if I (Computer can win)
if self.CheckWin() ==0:#Best Case I(Computer) win! if self.check_win() == 0: # Best Case I(Computer) win!
return i,j; return i, j
self.board[i][j] = self.pick self.board[i][j] = self.pick
if self.CheckWin() == 1: #Second Best Case, he (player) didn't won if (
PlayerWinSpot.append(t) self.check_win() == 1
self.board[i][j] = 'blur' ): # Second Best Case, he (player) didn't won
player_win_spot.append(t)
self.board[i][j] = "blur"
if len(PlayerWinSpot) != 0: if len(player_win_spot) != 0:
self.board[PlayerWinSpot[0][0]] [PlayerWinSpot[0][1]] = CompPick self.board[player_win_spot[0][0]][player_win_spot[0][1]] = comp_pick
return PlayerWinSpot[0][0],PlayerWinSpot[0][1] return player_win_spot[0][0], player_win_spot[0][1]
#print(AvailableMoves) # print(AvailableMoves)
if len(AvailableMoves) == 1: if len(available_moves) == 1:
self.board[ AvailableMoves[0][0] ][ AvailableMoves[0][1] ] = CompPick self.board[available_moves[0][0]][available_moves[0][1]] = comp_pick
return [ AvailableMoves[0][0] ],[ AvailableMoves[0][1] ] return [available_moves[0][0]], [available_moves[0][1]]
if len(AvailableMoves) == 0: if len(available_moves) == 0:
return -1,-1 return -1, -1
c1 , c2 = self.dim_sz//2,self.dim_sz//2 c1, c2 = self.dim_sz // 2, self.dim_sz // 2
#print(c1,c2,self.dim_sz) # print(c1,c2,self.dim_sz)
if (c1,c2) in AvailableMoves:#CENTER if (c1, c2) in available_moves: # CENTER
self.board[c1][c2] = CompPick self.board[c1][c2] = comp_pick
return c1,c2 return c1, c2
for i in range(c1-1,-1,-1):#IN TO OUT for i in range(c1 - 1, -1, -1): # IN TO OUT
gap = c1 - i gap = c1 - i
#checking - 4 possibilities at max # checking - 4 possibilities at max
#EDGES # EDGES
if (c1-gap,c2-gap) in AvailableMoves: if (c1 - gap, c2 - gap) in available_moves:
self.board[c1-gap][c2-gap] = CompPick self.board[c1 - gap][c2 - gap] = comp_pick
return c1-gap,c2-gap return c1 - gap, c2 - gap
if (c1-gap,c2+gap) in AvailableMoves: if (c1 - gap, c2 + gap) in available_moves:
self.board[c1-gap][c2+gap] = CompPick self.board[c1 - gap][c2 + gap] = comp_pick
return c1-gap,c2+gap return c1 - gap, c2 + gap
if (c1+gap,c2-gap) in AvailableMoves: if (c1 + gap, c2 - gap) in available_moves:
self.board[c1+gap][c2-gap] = CompPick self.board[c1 + gap][c2 - gap] = comp_pick
return c1+gap,c2-gap return c1 + gap, c2 - gap
if (c1+gap,c2+gap) in AvailableMoves: if (c1 + gap, c2 + gap) in available_moves:
self.board[c1+gap][c2+gap] = CompPick self.board[c1 + gap][c2 + gap] = comp_pick
return c1+gap,c2+gap return c1 + gap, c2 + gap
#Four Lines # Four Lines
for i in range(0,gap): for i in range(0, gap):
if (c1-gap,c2-gap+i) in AvailableMoves:#TOP LEFT TO TOP RIGHT if (c1 - gap, c2 - gap + i) in available_moves: # TOP LEFT TO TOP RIGHT
self.board[c1-gap][c2-gap+i] = CompPick self.board[c1 - gap][c2 - gap + i] = comp_pick
return c1-gap,c2-gap+i return c1 - gap, c2 - gap + i
if (c1+gap,c2-gap+i) in AvailableMoves:#BOTTOM LEFT TO BOTTOM RIGHT if (
self.board[c1+gap][c2-gap+i] = CompPick c1 + gap,
return c1+gap,c2-gap+i c2 - gap + i,
if (c1-gap,c2-gap) in AvailableMoves:#LEFT TOP TO LEFT BOTTOM ) in available_moves: # BOTTOM LEFT TO BOTTOM RIGHT
self.board[c1-gap+i][c2-gap] = CompPick self.board[c1 + gap][c2 - gap + i] = comp_pick
return c1-gap+i,c2-gap return c1 + gap, c2 - gap + i
if (c1-gap+i,c2+gap) in AvailableMoves:#RIGHT TOP TO RIGHT BOTTOM if (c1 - gap, c2 - gap) in available_moves: # LEFT TOP TO LEFT BOTTOM
self.board[c1-gap+i][c2+gap] = CompPick self.board[c1 - gap + i][c2 - gap] = comp_pick
return c1-gap+i,c2+gap return c1 - gap + i, c2 - gap
if (
c1 - gap + i,
c2 + gap,
) in available_moves: # RIGHT TOP TO RIGHT BOTTOM
self.board[c1 - gap + i][c2 + gap] = comp_pick
return c1 - gap + i, c2 + gap
def Display(Game : TicTacToe):
def display(Game: TicTacToe):
line1 = "" line1 = ""
for i in range(0,Game.dim_sz): for i in range(0, Game.dim_sz):
for j in range(0,Game.dim_sz-1): for j in range(0, Game.dim_sz - 1):
if Game.board[i][j] == 'blur':line1 = line1 + " |" if Game.board[i][j] == "blur":
else:line1 = line1 + " " + Game.board[i][j] + " |" line1 = line1 + " |"
if Game.board[i][Game.dim_sz-1] == 'blur':line1 = line1 + " \n" else:
else:line1 = line1 + " " + Game.board[i][Game.dim_sz-1] + " \n" line1 = line1 + " " + Game.board[i][j] + " |"
#line1 = line1 + " " + Game.board[i][Game.dim_sz-1] + "\n" if Game.board[i][Game.dim_sz - 1] == "blur":
print(line1,"\n\n") line1 = line1 + " \n"
else:
line1 = line1 + " " + Game.board[i][Game.dim_sz - 1] + " \n"
# line1 = line1 + " " + Game.board[i][Game.dim_sz-1] + "\n"
print(line1, "\n\n")
def play(): def play():
Pick = input("Pick 'X' or 'O' ").strip().upper() Pick = input("Pick 'X' or 'O' ").strip().upper()
if Pick == 'O': if Pick == "O":
Game = TicTacToe("O") Game = TicTacToe("O")
else: Game = TicTacToe("X") else:
Display(Game=Game) Game = TicTacToe("X")
display(Game=Game)
while True: while True:
#Display(Game) # Display(Game)
temp = False temp = False
while temp != True: while not temp:
move = list(map(int,input("Make A Move in Grid System from (0,0) to (2,2) ").split()) ) move = list(
temp = Game.MoveRecord(move[0],move[1]) map(
if temp != True: int,
input("Make A Move in Grid System from (0,0) to (2,2) ").split(),
)
)
temp = Game.move_record(move[0], move[1])
if not temp:
print(temp) print(temp)
if Game.CheckWin() == 1: if Game.check_win() == 1:
print("You Won!") print("You Won!")
break; break
print("Your Move:- ") print("Your Move:- ")
Display(Game) display(Game)
C1,C2 = Game.NextMove() C1, C2 = Game.next_move()
if C1 == -1 and C2 == -1: if C1 == -1 and C2 == -1:
print("Game Tie!") print("Game Tie!")
break; break
if Game.CheckWin() == 0: if Game.check_win() == 0:
print("You lost!") print("You lost!")
break; break
print("Computer's Move :-") print("Computer's Move :-")
Display(Game) display(Game)
if __name__ == "__main__": if __name__ == "__main__":
play() play()