+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
Programming Project 5
Programming Projects are to be submitted to gradescope.
Due date: Oct 10, Thursday at 7pm
In this programming project you will implement a number of Python functions to implement a simplified version of chess. Make sure you follow the provided style guide (you will be graded on it!).
Name your program one_d_chess.py
to submit to gradescope. Make sure that gradescope gives you the points for passing the test cases.
This programming project is an adaptation of Ben Dicken’s 1 Dimensional Chess Programming Assignment.
In this programming project, you will be implementing a simpler variant of chess: 1D Chess! 1D chess is a variant of the game that is played on a board that has only one column of spaces, rather than a grid of spaces, as in typical chess. There are actually multiple variants of 1D chess. In this PA, you should implement a custom variant of 1D chess, which I’ll refer to as “110 1D chess”.
1D Chess Rules
In 110 1D chess, the board will be 1 by 9. Each player will get one king, and two knights. The white pieces go on the left, and the black on the right. The starting positions for the pieces of the game should be as follows:
Kings and Knights move and kill their opponents differently. Knights can move either left or right, and when they move thy jump two pieces over. If they land on another piece, they kill them and take their spot. The king can move left or right, until they hit either another piece or an edge of the board. If a king hits another piece, they kill them and take their place. In chess, a player cannot typically kill one of their own pieces. In 110 1D chess, a king or knight can kill one of their own, if they hit or land on them, so be careful!
Functions to implement
Start Game
Write a function called create_board
that takes no arguments. It returns a list with the initial state of the board (as shown in the test case below)
Test case:
print( create_board() ) # ["WKi", "WKn", "WKn",
# "EMPTY", "EMPTY", "EMPTY",
# "BKn", "BKn", "BKi"]
Print Board
This functions should have one parameter variable, the board list. The job of this function is to simply print out the board to standard output. If the board is passed in and in the initial game state, the output should look like so:
Test case for initial board state:
= create_board()
board print( printable_board(board) ) # "+-----------------------------------------------------+\n
# | WKi | WKn | WKn | | | | BKn | BKn | BKi |\n
# +-----------------------------------------------------+"
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
Check move validity
This function has three parameter variables. The board (a list, representing the 1 by 9 board), the position (index) of the player to move, and player (WHITE or BLACK). This function should return a boolean (True
or False
). True if the desired move is valid, False otherwise. A move is valid if:
- The position (index) is a valid index in the
board
list - The position is an index of one of the current player’s pieces on the board
You do not need a while loop or for loop within this function. It can be accomplished with if statements. If the above two conditions are True, the function should return True. In any other case, return False.
Test cases:
= create_board()
board print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
assert is_valid_move(board, 1, "BLACK" ) == False
assert is_valid_move(board, 9, "BLACK" ) == False
assert is_valid_move(board, -1, "BLACK" ) == False
assert is_valid_move(board, 6, "BLACK" ) == True
assert is_valid_move(board, 2, "WHITE" ) == True
Move a king
This function has the same three parameters as the move function. move_king
should actually move a king on the board (thus, it should change the board list). In 110 1d Chess, a king can move either left or right. The king will move until either it reaches another piece (killing that piece and taking it’s place), or an end of the board.
This function should move the king at the provided position in the correct direction. You should use a for-loop or while-loop to iterate through the elements in the list and determine the spot it should end up at.
Test cases:
= create_board()
board print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
0, "LEFT")
move_king(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
8, "LEFT")
move_king(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKi | | +-----------------------------------------------------+
Move a knight
This function has the same three parameters as the move
function. move_knight
should actually move a knight on the board (thus, it should change the board list). In 110 1d Chess, a knight can move either left or right. A knight moves 2 locations. If it lands on a space that another piece occupies, then it kills that piece. It can jump over other pieces. If it cannot move due to the ending position of the move being out-of-bounds of the board, the knight should stay in its place.
This function should move the knight at the provided position in the correct direction.
Test cases:
= create_board()
board print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
6, "LEFT")
move_knight(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | BKn | | | BKn | BKi | +-----------------------------------------------------+
2, "RIGHT")
move_knight(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | | | WKn | | | BKn | BKi | +-----------------------------------------------------+
Make a move
Function name is move
and it has three parameter variables. The board (a list, representing the 1 by 9 board), the position (index) of the player to move, and a direction (either "LEFT"
or "RIGHT"
). This function should determine if the piece to be moved is a king or a knight. If it is a king, call the move_king
function. If a knight, call move_knight.
Test case:
= create_board()
board print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
0, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
8, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKi | | +-----------------------------------------------------+
Game over
This function should be named is_game_over
and it takes the board list as its only parameter, and should determine if the game is over or not. If the white king does not exist on the board, then the game is over, and black wins. In this case, the function should print the board, print Black wins!
and return True
. If the black king does not exist on the board, then the game is over, and white wins. In this case, the function should print White wins!
and return True
. If both kings are still in the board list, then the function should print nothing, and return False
.
Test case:
= create_board()
board print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | WKn | | | | BKn | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
2, "RIGHT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | | | WKn | | BKn | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
6, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | WKn | | | BKn | | | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
1, "RIGHT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | | | WKn | BKn | | | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
4, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| WKi | | BKn | WKn | | | | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
0, "RIGHT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| | | WKi | WKn | | | | BKn | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
7, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| | | WKi | WKn | | BKn | | | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
3, "RIGHT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| | | WKi | | | WKn | | | BKi | +-----------------------------------------------------+
assert is_game_over(board) == False
8, "LEFT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| | | WKi | | | BKi | | | | +-----------------------------------------------------+
assert is_game_over(board) == False
2, "RIGHT")
move(board, print( printable_board(board) )
+-----------------------------------------------------+
| | | | | | WKi | | | | +-----------------------------------------------------+
assert is_game_over(board) == True
White wins!
+-----------------------------------------------------+
| | | | | | WKi | | | | +-----------------------------------------------------+