#🔒 Mayday, SOS

59 messages · Page 1 of 1 (latest)

regal oasis
#

I am struggling greatly, python is not my strong suit. I’m working on this assignment where I need to create a chess game, basically what I’m struggling with is I need to print the chess board in this specific format but I keep running into an error, “Typeerror: Unhashable Type: ‘Slice’” my code is supposed to make the chess board come up as an empty board that i can then place the chess pieces on, but the error prevents that from happening.

dense monolithBOT
#

@regal oasis

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

regal oasis
#

is my code so far:


def new_board():
    return {chr(ord('a')+i):[None]*8 for i in range(8)}


def colour(board, square):
    if not is_valid_square(square):
        raise ValueError("Invalid square")
    file, rank = square[0], int(square[1])-1
    piece = board[file][rank]
    return None if piece is None else piece[0]


def is_valid_square(square):
    if len(square) != 2:
        return False
    file, rank = square[0], square[1]
    return (file in 'abcdefgh' and 
            rank in '12345678')


def count_pieces(board, colour):
    total = 0
    for b in board:
        if square[0] == (colour):
            total + 1
        else:
            return total
    

def piece(board, square):
    if square != None:
        return square[1]
    else:
        return None
    

def place_piece(board, square, colour, piece):
    board[square[0]][int(square[1]) - 1] = (colour, piece)
#
    
def pretty(board):
       
    return f"""    a    b    c    d    e    f    g    h     
              8  {board[0][8]}  {board[1][8]}  {board[2][8]}  {board[3][8]}  {board[4][8]}   {board[5][8]}   {board[6][8]}   {board[7][8]}  8  
              7  {board[0][7]}  {board[1][7]}  {board[2][7]}  {board[3][7]}  {board[4][7]}   {board[5][7]}   {board[6][7]}   {board[7][7]}  7  
              6  {board[0][6]}  {board[1][6]}  {board[2][6]}  {board[3][6]}  {board[4][6]}   {board[5][6]}   {board[6][6]}   {board[7][6]}  6 
              5  {board[0][5]}  {board[1][5]}  {board[2][5]}  {board[3][5]}  {board[4][5]}   {board[5][5]}   {board[6][5]}   {board[7][5]}  5  
              4  {board[0][4]}  {board[1][4]}  {board[2][4]}  {board[3][4]}  {board[4][4]}   {board[5][4]}   {board[6][4]}   {board[7][4]}  4  
              3  {board[0][3]}  {board[1][3]}  {board[2][3]}  {board[3][3]}  {board[4][3]}   {board[5][3]}   {board[6][3]}   {board[7][3]}  3  
              2  {board[0][2]}  {board[1][2]}  {board[2][2]}  {board[3][2]}  {board[4][2]}   {board[5][2]}   {board[6][2]}   {board[7][2]}  2  
              1  {board[0][1]}  {board[1][1]}  {board[2][1]}  {board[3][1]}  {board[4][1]}   {board[5][1]}   {board[6][1]}   {board[7][1]}  1  
                  a    b    c    d    e    f    g    h    """
                  
board = new_board()
print(pretty(board))

#
def new_game():
    
    board = new_board()
    
    starting_positions = [(place_piece(board, "a1", "W", "R")), (place_piece(board, "b1", "W", "N")), 
    (place_piece(board, "c1", "W", "B")), (place_piece(board, "d1", "W", "Q")), 
    (place_piece(board, "e1", "W", "Q")), (place_piece(board, "f1", "W", "B")), 
    (place_piece(board, "g1", "W", "N")), (place_piece(board, "h1", "W", "R")), 
    (place_piece(board, "a2", "W", "P")), (place_piece(board, "b2", "W", "P")), 
    (place_piece(board, "c2", "W", "P")), (place_piece(board, "d2", "W", "P")), 
    (place_piece(board, "e2", "W", "P")), (place_piece(board, "f2", "W", "P")), 
    (place_piece(board, "g2", "W", "P")), (place_piece(board, "h2", "W", "P")), 
    (place_piece(board, "a7", "B", "P")), (place_piece(board, "b7", "B", "P")), 
    (place_piece(board, "c7", "B", "P")), (place_piece(board, "d7", "B", "P")), 
    (place_piece(board, "e7", "B", "P")), (place_piece(board, "f7", "B", "P")), 
    (place_piece(board, "g7", "B", "P")), (place_piece(board, "h7", "B", "P")), 
    (place_piece(board, "a8", "B", "R")), (place_piece(board, "b8", "B", "N")), 
    (place_piece(board, "c8", "B", "B")), (place_piece(board, "d8", "B", "Q")), 
    (place_piece(board, "e8", "B", "K")), (place_piece(board, "f8", "B", "B")), 
    (place_piece(board, "g8", "B", "N")), (place_piece(board, "h8", "B", "R"))]
    
    return starting_positions
    return board

    while True:
        turn = "W"
        #if #turn was made / piece was moved
            #turn = "B"
            #Cant figure this out ?

    def print_game_state(b, turn):
        if turn == "W":
            print("Whites Turn")
        else:
            print("Blacks Turn")
    
new_game()

#

Mayday, SOS

midnight dawn
#

@regal oasis send the error

#

and in which line is it

regal oasis
#

8 {board[0:8]} {board[1:8]} {board[2:8]} {board[3:8]} {board[4:8]} {board[5:8]} {board[6:8]} {board[7:7]} 8

TypeError: unhashable type: 'slice'

#

thats exactly what it says.

midnight dawn
#

new_board()

#

returns a dictionary

#

and you are trying to access it as a list

#

board is a dictionary

#

@regal oasis make sense?

regal oasis
#

okay so i need to change it to a list somehow. i think i can do that.

midnight dawn
#

yea if that's how you want it

regal oasis
#

well im looking at the assignment and it says it can be anything, i thought it had to be a dictionary but

midnight dawn
#

also I believe what you are trying to do here
board[0:8]

#

I think you mean

#

board[0][8]

midnight dawn
regal oasis
regal oasis
midnight dawn
#

[0:8] will result in getting for you the array from index 0 up to index 8 (8 excluded)

regal oasis
#

right yes i remember learning that now.

midnight dawn
#

whereas [0][8] assuming that you will change board to a 2d list, it will get the first list then the element in index 8

#

so maybe you need to change 8 to 7

#

any other questions, or anything not clear yet?

regal oasis
#

i wasnt really sure if it should be 8 or 7, because my assignment says that when i call upon board [0][1] for example, im supposed to call it be using "place_piece(board, "a1", "W", "K")", the board is supposed to start at a1, but in code it starts at 0 so i didnt know if i should start at [0][0] or [0][1]

#

sorry im a slow typer

#

now the error just says 0?

#
8  {board[0][8]}  {board[1][8]}  {board[2][8]}  {board[3][8]}  {board[4][8]}   {board[5][8]}   {board[6][8]}   {board[7][8]}  8

KeyError: 0
rustic pawn
#

Because you made the board a dict that has letters as keys.

#

Dict can only be indexed with its keys, not position

#

As xVip said, you should make your board a 2d list - ie. a list of lists

regal oasis
#

I'm trying to do that now, but i dont know how to take this and turn it into a list. ```py
def new_board():
return {chr(ord('a')+i):[None]*8 for i in range(8)}

#

im not even sure how it this turns into a dictionary, this part was given to me in the assignment set up code

rustic pawn
#

You said the assignment wants you to do the board[0][0] kind of indexing. That function and this kind of indexing are not compatible

#

So either the assignment is badly done or there's more written there

regal oasis
#
# ENGI 1020 -- Fall 2024
## Assignment #4 -- Chess Game
**Due: Nov 22, 2024 @ 11:59 PM**





1. Data Structure Overview

The chess board should be represented as a data structure that can track:
- Position of pieces (a1 through h8)
- Color of pieces (White 'W' or Black 'B')
- Type of pieces (K, Q, R, B, N, P)

Suggested board representation:

# Example board structure using dictionary
board = {
    'a': [None, None, None, None, None, None, None, None],
    'b': [None, None, None, None, None, None, None, None],
    # ... files c through h
}


- Files are represented as keys ('a' through 'h')
- Ranks are represented as list indices (0 through 7 for ranks 1 through 8)
- Each position contains either None (empty) or a string like "WP" (White Pawn)


################################################################################################

2. Function Specifications

board.py Module

new_board()
- Returns: dict (empty chess board)
- Example:

board = new_board()
print(board['a'])  # Output: [None, None, None, None, None, None, None, None]


colour(board, square)
- Parameters:
  - board: dict (the chess board)
  - square: str (e.g., "a1", "e4")
- Returns: str ("W" or "B") or None
- Input validation:
  - Square must be valid (a1-h8)
- Example:

board = new_board()
place_piece(board, "e1", "W", "K")
print(colour(board, "e1"))  # Output: "W"
print(colour(board, "e2"))  # Output: None


count_pieces(board, player)
- Parameters:
  - board: dict
  - player: str ("W" or "B")
- Returns: int (number of pieces)
- Example:

board = new_game()[0]  # Get board from new game
print(count_pieces(board, "W"))  # Output: 16 (at start of game)


[Continue with similar specifications for other functions...]



############################################```
#
####################################################

Development Steps

3.1. Create Empty Files:

board.py
def new_board():
    pass

def colour(board, square):
    pass

Add other function stubs...
3.2. Implement Basic Structure:

def new_board():
    return {chr(ord('a')+i):[None]*8 for i in range(8)}


3.3. Add Core Logic:

def colour(board, square):
    if not is_valid_square(square):
        raise ValueError("Invalid square")
    file, rank = square[0], int(square[1])-1
    piece = board[file][rank]
    return None if piece is None else piece[0]


3.4. Add Helper Functions:

def is_valid_square(square):
    if len(square) != 2:
        return False
    file, rank = square[0], square[1]
    return (file in 'abcdefgh' and 
            rank in '12345678')

################################################################################################

Common Pitfalls to Avoid

4.1. Index Confusion:
   
Remember: Chess ranks start at 1, but list indices start at 0
Convert between them correctly: rank 1 → index 0

4.2. Square Representation:
   
Always validate square format (e.g., "a1", not "1a" or "A1")
Check for valid files (a-h) and ranks (1-8)

4.3. Piece Placement:
   
Verify piece type is valid before placement
Maintain consistency in piece representation (e.g., "WP" for White Pawn)```
regal oasis
#

im just very confused.

rustic pawn
#

So use the board['a'][0] and so on in printing, like the task wants you to do

regal oasis
#

a1 here is board[0][0] no?

rustic pawn
regal oasis
#

oh.

rustic pawn
regal oasis
#

oh so i wrote this part very wrong

#

your background is cool by the way.

#

okay ill change the code to what i think it should be now one sec

#

holy shit the board prints properly!

#

okay so the thing that dosent work now is the count_pieces function

#

it just keeps saying square isn't defined

regal oasis
#

!close

dense monolithBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.