Question: PYTHON 3 Two functions need to be written, one function, move_maker, will return all the possible moves for either the white or black pawn. This

PYTHON 3

Two functions need to be written, one function, move_maker, will return all the possible moves for either the white or black pawn. This will look like a list of possible board configurations. The next, move_chooser, will evaluate all possible moves and evaluate which is the best by assigning values to each board configuration. If the board is a winning board, it will have +10 points, if the board is a losing board, it will have -10 points, and if it is neither a winning or losing board, the points will be the number of pawns - the number of opponents pawns. Move_chooser will return the board with the greatest value.

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 nine days, 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:

- w w

w - -

b b b

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]]

Driver below:

def move_maker(board, color): return [board] def move_chooser(list_of_boards, color): return list_of_boards[0] def printBoard(board): print(" ", end = "") for i in range(0, len(board[0])): print(str(i) + " ", end = "") print(" ") row = 0 for 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 + 1 print() def 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] = 1 board[dim - 1][i] = 2 return board def miniChess(): from random import randint print("Welcome to miniChess") dim = int(input("What size board would you like? (enter" "an integer greater than 2): ")) bheight = dim bwidth = dim b = makeInitBoard(dim) printBoard(b) while True: answer = input("Choose the white pawns or black pawns (" "enter 'w' or 'b' or 'quit'): ") if answer == "w": mycolor = 2 break if answer == "b": mycolor = 1 break if answer == "quit": print("Ending the game") return if mycolor == 1: print("Here's my opening move... ") column = randint(0, bwidth - 1) b[1][column] = b[0][column] b[0][column] = 0 printBoard(b) while True: print(" 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: ")) b[torow][tocol] = b[fromrow][fromcol] b[fromrow][fromcol] = 0 print("This is your move... ") printBoard(b) possiblemoves = move_maker(b, mycolor) if possiblemoves == []: print("I can't move Congratulations! You Win!") return b = move_chooser(possiblemoves, mycolor) print("Here's my response... ") printBoard(b)

PYTHON 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!