Aies Lab Dharani
Aies Lab Dharani
global N
N=4
def printSolution(board):
for i in range(N):
for j in range(N):
print (board[i][j],end=' ')
print()
def solveNQ():
board = [ [0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
if solveNQUtil(board, 0) == False:
print ("Solution does not exist")
return False
printSolution(board)
return True
solveNQ()
class Graph:
def init (self):
self.graph = defaultdict(list)
def addEdge(self,u,v):
self.graph[u].append(v)
def DFS(self):
V = len(self.graph)
visited =[False]*(V)
for i in range(V):
if visited[i] == False:
self.DFSUtil(i, visited)
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print ("Following is Depth First Traversal")
g.DFS()
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
REG NO: 211423243098 NAME:DHARANIDHARAN S
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
OUTPUT
Move disk 1 from source A to destination C
Move disk 2 from source A to destination B
Move disk 1 from source C to destination B
Move disk 3 from source A to destination C
Move disk 1 from source B to destination A
Move disk 2 from source B to destination C
Move disk 1 from source A to destination C
Move disk 4 from source A to destination B
Move disk 1 from source C to destination B
Move disk 2 from source C to destination A
Move disk 1 from source B to destination A
Move disk 3 from source C to destination B
Move disk 1 from source A to destination C
Move disk 2 from source A to destination B
Move disk 1 from source C to destination B
for v in open_set:
if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
n=v
if n == stop_node or Graph_nodes[n] == None:
pass
else:
for (m, weight) in get_neighbors(n):
if n == stop_node:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(start_node)
path.reverse()
print('Path found: {}'.format(path))
return path
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
def heuristic(n):
H_dist = {
'A': 11,
'B': 6,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
}
return H_dist[n]
Graph_nodes = {
'A': [('B', 6), ('F', 3)],
'B': [('A', 6), ('C', 3), ('D', 2)],
'C': [('B', 3), ('D', 1), ('E', 5)],
'D': [('B', 2), ('C', 1), ('E', 8)],
'E': [('C', 5), ('D', 8), ('I', 5), ('J', 5)],
'F': [('A', 3), ('G', 1), ('H', 7)],
'G': [('F', 1), ('I', 3)],
'H': [('F', 7), ('I', 2)],
'I': [('E', 5), ('G', 3), ('H', 2), ('J', 3)],
}
aStarAlgo('A', 'J')
def print_board(self):
for row in self.board:
print(' | '.join(cell if cell != '' else ' ' for cell in row))
print('-' * 5)
def evaluate(self):
win_conditions = [
[self.board[0][0], self.board[0][1], self.board[0][2]],
[self.board[1][0], self.board[1][1], self.board[1][2]],
[self.board[2][0], self.board[2][1], self.board[2][2]],
[self.board[0][0], self.board[1][0], self.board[2][0]],
[self.board[0][1], self.board[1][1], self.board[2][1]],
[self.board[0][2], self.board[1][2], self.board[2][2]],
[self.board[0][0], self.board[1][1], self.board[2][2]],
[self.board[2][0], self.board[1][1], self.board[0][2]]
]
def generate_possible_moves(self):
moves = []
for i in range(3):
for j in range(3):
if self.board[i][j] == '':
new_board = [row[:] for row in self.board]
new_board[i][j] = 'X' if self.player_turn else 'O'
moves.append(GameState(new_board, not self.player_turn))
return moves
def is_terminal(self):
return self.evaluate() != 0 or all(cell != '' for row in self.board for cell in row)
if maximizing_player:
max_eval = -math.inf
for move in state.generate_possible_moves():
eval = minimax(move, depth - 1, alpha, beta, False)
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_eval
else:
min_eval = math.inf
for move in state.generate_possible_moves():
eval = minimax(move, depth - 1, alpha, beta, True)
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break
return min_eval
current_board = [
['X', '', ''],
['', '', ''],
['', '', '']
]
initial_state = GameState(current_board, False)
print("Current Board State:")
initial_state.print_board()
depth = 3
move = best_move(initial_state, depth)
if move:
position = [(i, row.index('O')) for i, row in enumerate(move.board) if 'O' in row][0]
print(f"Best move for the opponent: {position}")
else:
print("No valid move found")
| |
| |
def heuristic(state):
conflicts = 0
for i in range(8):
for j in range(i + 1, 8):
if state[i] == state[j] or abs(state[i] - state[j]) == abs(i - j):
conflicts += 1
return conflicts
def generate_successors(state):
successors = []
for row in range(8):
for col in range(8):
if col != state[row]:
new_state = state[:]
new_state[row] = col
successors.append(new_state)
return successors
def first_choice_hill_climbing(max_iterations=1000):
iteration = 0
while iteration < max_iterations:
current_state = generate_initial_state()
current_heuristic = heuristic(current_state)
while current_heuristic > 0:
successors = generate_successors(current_state)
next_state = min(successors, key=lambda s: heuristic(s))
next_heuristic = heuristic(next_state)
if current_heuristic == 0:
return current_state
iteration += 1
return None
def print_state(state):
board = [['0' for _ in range(8)] for _ in range(8)]
for row in range(8):
board[state[row]][row] = '1'
for row in board:
REG NO: 211423243098 NAME:DHARANIDHARAN S
print(' '.join(row))
print()
solution = first_choice_hill_climbing(max_iterations=5000)
if solution:
print("Solution found:")
print_state(solution)
else:
print("No solution found or local maximum reached.")
def print_solution(solution):
if solution:
for region, color in solution.items():
print(f"Region {region} is colored {color}")
else:
print("No solution found")
if failed == 0:
print("You Win")
print("The word is: ", word)
break
print()
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")
guess a character:g
Wrong
You have 11 more guesses
_
_
_
_
_
_
guess a character:r
_
_
_
_
_
r
guess a character:t
Wrong
You have 10 more guesses
_
_
_
_
_
r
guess a character:p
p_
_
_
_
r
guess a character:l
pl_
_
_
r
REG NO: 211423243098 NAME:DHARANIDHARAN S
guess a character:y
pl_
y_
r
guess a character:a
play_
r
guess a character:e
player
You Win
def create_board():
return(np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]))
def possibilities(board):
l = []
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == 0:
l.append((i, j))
return(l)
def evaluate(board):
winner = 0
for player in [1, 2]:
if (row_win(board, player) or
col_win(board, player) or
diag_win(board, player)):
winner = player
if np.all(board != 0) and winner == 0:
winner = -1
return winner
def play_game():
board, winner, counter = create_board(), 0, 1
print(board)
sleep(2)
while winner == 0:
for player in [1, 2]:
board = random_place(board, player)
print("Board after " + str(counter) + " move")
print(board)
sleep(2)
counter += 1
winner = evaluate(board)
if winner != 0:
break
return(winner)
print("Winner is: " + str(play_game()))