Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python code, design an AI class to defeat agents in a 6x6 board game. This board game, named Jordan, is always fully observable, strategic

In python code, design an AI class to defeat agents in a 6x6 board game. This board game, named Jordan, is always fully observable, strategic and deterministic. The game always results in a win for one of the two players. We are required to code an agent that takes in sensory input and reasons about it, then outputs an action at each time step. Thus, we need to create a program that can read in a representation of the board game and output a legal move in Jordan. I would also need an evaluation function to evaluate how good a board is to my agent. Aside from the evaluation function, I would also need to decide a strategy on the search space. Most likely I would need to do minimax search with alpha-beta pruning for optimal results. The game is as follows: Black wins by moving one piece to the opposite side, row index 5 and White wins by moving one piece to row index 0. Please follow this index and write the code only for moving Black. Pieces can move one space directly forward or diagonally forward, and only capture diagonally forward. For example, the black pawn at (4,2) coordinate can go to (5,1), (5,2) or (5,3). Additionally, a black pawn at (2,4) can only choose to move diagonally right or capture the white pawn by going diagonally left, given that black is at (2,4), 2 whites are at (3,3) and (3,4). The black pawn cannot move or capture by moving forward since its forward move is blocked by the white at (3,4). Given an action request, the agent should output a pair of coordinates in a list using the coordinate system. For example the black standing at (2,4) moving to (3,3) would make my agent output two lists : [2,4] and [3,3]. The action should always be legal, and shall not cross the boundary of the 6x6 grid. Your agent must always make a move and is not allowed to skip moves. Program should not take more than 3 real-time seconds to make a move. Additionally, provide reasonings to the algorithms implemented, data structures used and the evaluation function. It should be correct and consistent. The following functions are given and provided: ROW, COL = 6, 6 def initial(): state = [ ['B']*COL, ['B']*COL, # 2 black rows ['_']*COL, ['_']*COL, # 2 empty rows ['W']*COL, ['W']*COL, # 2 white rows ] return state def print(board): horizontal_rule = '+' + ('-'*5 + '+') * COL for i in range(len(board)): print(horizontal_rule) print('| ' + ' | '.join(' ' if board[i][j] == '_' else board[i][j] for j in range(COL)) + ' |') print(horizontal_rule) def invert(curr_board, in_place=True): board = curr_board if not in_place: board = copy.deepcopy(curr_board) board.reverse() for i in range(len(board)): for j in range(len(board[i])): if board[i][j] == 'W': board[i][j] = 'B' elif board[i][j] == 'B': board[i][j] = 'W' return board def is_valid(board, from_, to_): if board[from_[0]][from_[1]] != 'B': return False elif (to_[0]<0 or to_[0]>=ROW) or (to_[1]<0 or to_[1]>=COL): return False elif to_[0]!=(from_[0]+1): return False elif to_[1]>(from_[1]+1) or to_[1]<(from_[1]-1): return False elif to_[1]==from_[1] and board[to_[0]][to_[1]]!='_': return False elif ((to_[1]==from_[1]+1) or (to_[1]==from_[1]-1)) and board[to_[0]][to_[1]]=='B': return False else: return True def random(board): from_, to_ = [0, 0], [0, 0] for i in range(len(board)): for j in range(len(board[i])): if board[i][j]=='B': from_[0], from_[1] = i, j to_[0] = from_[0] + 1 to_[1] = from_[1] if is_valid(board, from_, to_): return from_, to_ to_[1] = from_[1] + 1 if is_valid(board, from_, to_): return from_, to_ to_[1] = from_[1] - 1 if is_valid(board, from_, to_): return from_, to_ def state_change(curr_board, from_, to_, in_place=True): board = curr_board if not in_place: board = copy.deepcopy(curr_board) if is_valid(board, from_, to_): board[from_[0]][from_[1]] = '_' board[to_[0]][to_[1]] = 'B' return board def is_over(board): flag = any( board[ROW-1][i] == 'B' or \ board[0][i] == 'W' for i in range(COL) ) wcount, bcount = 0, 0 for i in range(ROW): for j in range(COL): if board[i][j] == 'B': bcount+=1 elif board[i][j] == 'W': wcount+=1 if wcount==0 or bcount==0: flag = True return flag and this is the starter code: import time import copy class Jordan: def AI(self, board): # TODO: Replace starter code with your AI for r in range(len(board)): for c in range(len(board[r])): # check if B can move forward directly if board[r][c] == 'B' and board[r+1][c] == '_': src = [r, c] dst = [r+1, c] return src, dst # valid move return [0, 0], [0, 0] # invalid move

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions