Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The names of your functions must exactly match the names given in this assignment. The order of the parameters in your parameter list must exactly

The names of your functions must exactly match the names given in this assignment. The order of the parameters in your parameter list must exactly match the order . Use Python 3 and comments miniChess is played on a 3 x 3 chessboard. The two players face each other across the board. Each player begins with three either three white pawns or three black pawns, placed one to a square in each of the three squares nearest that player. The player with the white pawns moves first. A player may choose to move one of his or her pawns in one of these ways: A pawn may be moved one square forward to an empty square A pawn may be moved one square diagonally forward to a square occupied by an opponent's pawn. The opponent's pawn is then removed. The players alternate moving pawns until one of the players wins. A player wins when: A player's pawn has advanced to the other end of the board, or The opponent has no pawns remaining on the board, or It is the opponent's turn to move a pawn but is unable to do so. As envisioned by its inventor, miniChess was intended to be played with only six pawns on a 3 x 3 board. Now, however, we are extending the definition of miniChess to include any similar game involving n white pawns, n black pawns, and a n x n board. Over the next two weeks, you are to construct the core functionality necessary for a computer program to play this game. For this program, the current state of the game (i.e., the board) is represented as a list of lists. In the list of lists, the first sublist is the top row of the board, and the last sublist is the bottom row of the board. A 0 is an empty square, a 1 is a white pawn on the square, and a 2 is a black pawn on the square. For example, a board that looks like this when displayed on your monitor: -ww w-- bbb will be represented in your program like this: [[0,1,1],[1,0,0],[2,2,2]] The core functionality that you will provide will take the form of two functions. The first function is the move generator, called move_maker. This function expects two arguments. The first argument is a list of lists representing the current board. The second argument indicates the color of the pawns that your program is moving: 1 indicates that your program controls the white pawns, and 2 says that your program controls the black pawns. Your function returns a list of the possible new boards that can result from the current board in one move. For example, if your program controls the black pawns, and the current board is the board shown above, then your move_maker function should behave like this: >>> board = [[0, 1, 1], [1, 0, 0], [2, 2, 2]] >>> move_maker(board, 2) [[[0, 1, 1], [2, 0, 0], [2, 0, 2]], [[0, 1, 1], [1, 2, 0], [2, 0, 2]], [[0, 1, 1], [1, 0, 2], [2, 2, 0]]] As this example illustrates, black has three legal next moves given the current board. Your function should generate all three of these moves, but they won't necessarily appear in the same order as in this example, depending on how your function computes the possible next moves. The second function is called move_chooser and expects two arguments. The first argument is a list of possible next moves returned by your move_maker function. The second argument indicates the color of the pawns controlled by your program. Your move_chooser function evaluates each of the boards/moves in the second argument and returns the best one. That board is your program's next move and becomes the new current board. Here's an example of how this function should behave: >>> board = [[0, 1, 1], [1, 0, 0], [2, 2, 2]] >>> mlist = move_maker(board, 2) >>> mlist [[[0, 1, 1], [2, 0, 0], [2, 0, 2]], [[0, 1, 1], [1, 2, 0], [2, 0, 2]], [[0, 1, 1], [1, 0, 2], [2, 2, 0]]] >>> move_chooser(mlist, 2) [[0, 1, 1], [2, 0, 0], [2, 0, 2]] Part of the grade for your move_chooser function will be based on the sophistication of the algorithm you use for evaluating the individual boards. An overly simple board evaluator will get a lower grade than a board evaluator that takes into account a lot of information from the current board. In other words, the "smarter" your board evaluator is, the better your grade will be. You may want to use the board evaluation metric discussed in class as inspiration for your own. Although we view your functions as two different programming assignments, you must submit both functions in the same file via SmartSite. Some extra notes: 5) Your functions dont print, and they dont ask for keyboard input. 6) White always starts at the top of the board, and white always makes the first move. The 2 functions should replace the following. ## Here's the crude, unpolished first version of a user## interface for your miniChess function## It doesn't really know much about who wins the game## or when...although it does know that if it can't make## a move, the human player wins.## It does not validate the coordinates for the move## that is entered by the user. You can move any pawn## on either side to any square. So be careful.## We have supplied "dummy" move_maker and## move_chooser functions so that this## program will run without causing an## error. But the program will not play## the game correctly until you replace## our two functions with your functions.## Put your definitions for move_maker and## move_chooser up here in this space.#### Replace the two function definitions below## with your functions.#### DO NOT LEAVE OUR FUNCTIONS HEREdef move_maker(board,color):return [board]def move_chooser(list_of_boards, color):return list_of_boards[0]## keep everything below as is ======================================## print a board## 0 prints as '-', 1 prints as 'w', 2 prints as 'b'def printBoard(board):print(" ", end = "")for i in range(0, len(board[0])):print(str(i)+" ", end = "")print(" ")row = 0for r in board:print(row, " ", end = "")for c in r:if c == 1: print("w ", end = "")elif c == 2:print("b ", end = "")else:print("- ", end = "")print()row = row + 1print()## create an initial board, with dimension## passed as the argument. 1s at the top,## 2s at the bottom, 0s everywhere elsedef makeInitBoard(dim):board = []for i in range(0,dim):row = []for j in range(0,dim):row.append(0)board.append(row)for i in range(0,dim):board[0][i] = 1board[dim - 1][i] = 2return board## this is the user interface for the miniChess game.## just run the program and type 'miniChess()' in the## interaction windowdef miniChess():from random import randintprint("Welcome to miniChess")## ask for board size and create initial boarddim = int(input("What size board would you like? (enter an integer greater than 2): "))bheight = dimbwidth = dimb = makeInitBoard(dim)print(" Here's the initial board... ")printBoard(b)## ask user to select color of pawns## if user selects white, then the program's color is 2 (i.e., black)## if user selects black, then the program's color is 1 (i.e., white) while True:answer = input("Choose the white pawns or black pawns (enter 'w' or 'b' or 'quit'): ")if answer == "w":mycolor = 2breakif answer == "b":mycolor = 1breakif answer == "quit":print("Ending the game")return## if program has white pawns, generate program's first moveif mycolor == 1:print("Here's my opening move... ")column = randint(0, bwidth - 1)b[1][column] = b[0][column]b[0][column] = 0printBoard(b)## game loopwhile True:## ask for user's move## coordinates are not validated at this timeprint(" Enter the coordinates of the pawn you wish to move:")fromrow = int(input(" row: "))fromcol = int(input(" col: "))print("Enter the coordinates of the destination square: ")torow = int(input(" row: "))tocol = int(input(" col: ")) # oopsb[torow][tocol] = b[fromrow][fromcol]b[fromrow][fromcol] = 0print("This is your move... ")printBoard(b)## here is where the program uses the functions created by the studentpossiblemoves = move_maker(b, mycolor) # don't change this function callif possiblemoves == []:print("I can't move Congratulations! You win!")returnb = move_chooser(possiblemoves, mycolor) # don't change this function call print("Here's my response... ")printBoard(b)

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

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago