Lab Assignment 02
Lab Assignment 02
TASKS:
CODE:
#!/usr/bin/env python
# coding: utf-8
# In[2]:
N, M = 5, 5
n_players = 2
count_boxes = [0,0]
def print_grid():
for i in range(n_players):
if i < n_players-1:
print()
for i in range(N):
print(' . ', end = '')
for j in range(M):
if j < M-1:
print()
for j in range(M+1):
print()
for i in range(n_players):
def check_full():
return False
return True
if(i1 == i2):
if(not horizontal_grid[i1][j1]):
return True
if(j1 == j2):
if(vertical_grid[i1][j1] == False):
return True
return False
#This function checks if the given position is valid in the grid or not
return True
return False
return True
return False
if(i1 == i2):
horizontal_grid[i1][j1] = True
else:
vertical_grid[i1][j1] = True
return True
return False
def set_box(i,j,player):
grid[i][j] = marks[player]
count_boxes[player] += 1
if(is_complete_box(i1 - 1,j1)):
set_box(i1 - 1, j1, player)
return True
if(is_complete_box(i1,j1-1)):
return True
return False
flag = False
if (is_complete_box(i1 - 1, j1)):
flag = True
if (is_complete_box(i1, j1 - 1)):
set_box(i1, j1 - 1, player)
flag = True
flag = True
flag = True
return flag
mini = min(i1,i2)
maxi = max(i1,i2)
i1 = mini
i2 = maxi
mini = min(j1,j2)
maxi= max(j1,j2)
j1 = mini
j2 = maxi
def grid_clear():
for i in range(N-1):
for j in range(M-1):
grid[i][j] = '.'
for i in range(N):
for j in range(M):
horizontal_grid[i][j] = False
vertical_grid[i][j] = False
count_boxes[0] = 0
count_boxes[1] = 0
def read_input():
#MAIN FUNCTION
def play_game():
print("***********")
player = 0
while True:
print_grid()
print(i1)
print(j2)
if check_full():
print_grid()
if count_boxes.count(max(count_boxes)) == 1:
idx_max_player = count_boxes.index(max(count_boxes))
else:
break
while True:
grid_clear()
play_game()
if c not in 'yY':
break
# # MINIMAX
# In[1]:
#!/usr/bin/env python3
import platform
import time
"""
using Python.
Year: 2017
License: GNU GENERAL PUBLIC LICENSE (GPL)
"""
HUMAN = -1
COMP = +1
board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
def evaluate(state):
"""
"""
if wins(state, COMP):
score = +1
score = -1
else:
score = 0
return score
* Three rows [X X X] or [O O O]
* Three cols [X X X] or [O O O]
* Two diagonals [X X X] or [O O O]
"""
win_state = [
return True
else:
return False
def game_over(state):
"""
"""
def empty_cells(state):
"""
"""
cells = []
if cell == 0:
cells.append([x, y])
return cells
"""
:param x: X coordinate
:param y: Y coordinate
"""
if [x, y] in empty_cells(board):
return True
else:
return False
"""
:param x: X coordinate
:param y: Y coordinate
"""
if valid_move(x, y):
board[x][y] = player
return True
else:
return False
if player == COMP:
else:
if depth == 0 or game_over(state):
score = evaluate(state)
x, y = cell[0], cell[1]
state[x][y] = player
state[x][y] = 0
score[0], score[1] = x, y
if player == COMP:
else:
return best
def clean():
"""
"""
os_name = platform.system().lower()
if 'windows' in os_name:
system('cls')
else:
system('clear')
def render(state, c_choice, h_choice):
"""
"""
print(state)
chars = {
-1: h_choice,
+1: c_choice,
0: ' '
str_line = '---------------'
print('\n' + str_line)
symbol = chars[cell]
print('\n' + str_line)
"""
:return:
"""
depth = len(empty_cells(board))
if depth == 0 or game_over(board):
return
clean()
if depth == 9:
x = choice([0, 1, 2])
y = choice([0, 1, 2])
else:
x, y = move[0], move[1]
set_move(x, y, COMP)
time.sleep(1)
"""
:return:
"""
depth = len(empty_cells(board))
if depth == 0 or game_over(board):
return
move = -1
moves = {
print(move)
clean()
# print(moves)
try:
coord = moves[move]
if not can_move:
print('Bad move')
move = -1
print('Bye')
exit()
except (KeyError, ValueError):
print('Bad choice')
def main():
"""
"""
clean()
h_choice = '' # X or O
c_choice = '' # X or O
try:
print('')
print('Bye')
exit()
print('Bad choice')
if h_choice == 'X':
c_choice = 'O'
else:
c_choice = 'X'
# Human may starts first
clean()
try:
print('Bye')
exit()
print('Bad choice')
if first == 'N':
ai_turn(c_choice, h_choice)
first = ''
human_turn(c_choice, h_choice)
ai_turn(c_choice, h_choice)
if wins(board, HUMAN):
clean()
print('YOU WIN!')
clean()
print(f'Computer turn [{c_choice}]')
print('YOU LOSE!')
else:
clean()
print('DRAW!')
exit()
if _name_ == '_main_':
main()
# # game.py
# In[4]:
if game_state.is_gameover():
if our_turn:
score = -2 #worst non-possible score. A win, tie, or even a loss will change this
score = temp_max
best_move = move
# print(alpha)
break
else:
score = 2 #worst non-possible score. A win, tie, or even a loss will change this
print(temp_min)
score = temp_min
best_move = move
# print(beta)
break
# In[5]:
#!/usr/bin/env python3
import random
import alphabeta as ab
import gamestate as gs
def get_p_move(game):
while True:
if raw_move.isdigit():
p_move = int(raw_move) - 1
if game.board[p_move] == '_':
break
return p_move
if player_goes_first:
while True:
game.pretty_print()
p_move = get_p_move(game)
if game.is_gameover(): break
if game.is_gameover(): break
else:
while True:
score, ai_move = ab.alphabeta(game)
if game.is_gameover(): break
game.pretty_print()
p_move = get_p_move(game)
if game.is_gameover(): break
game.pretty_print()
def get_symbols():
exit()
ai_char = 'O'
else:
ai_char = 'X'
if _name_ == '_main_':
try:
start_board = ['_'] * 9
player_goes_first = bool(random.randint(0,2))
run_game(game, player_goes_first)
except KeyboardInterrupt:
print('\n')
exit()
# In[ ]