#๐Ÿ”’ Chess Game

158 messages ยท Page 1 of 1 (latest)

limber gardenBOT
#

@severe venture

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.

severe venture
#

I need some help with the code for checking if a space is already taken.

#

It's under the verify_move function.

#

But I'm having trouble getting it to work properly.

#

@vocal epoch If you're still around I'd appreciate the help!

hollow beacon
#

Once you've done that, you can just convert the position at which you want to move into a usable index and from there decide what you want to do based on the piece that's there (you use your get_piece_in_position)

severe venture
#

I feel like I'm massively overcomplicating this.

hollow beacon
#

That's the main source of "complications"

#

This way you won't need to store piece positions as separate x_pos & y_pos

#

And it will also facilitate the overall process for your new functions

severe venture
hollow beacon
#

Something along these lines :

def __init__(self):
  self._board = [[None for _ in range(8)] for _ in range(8)]  # 8x8 board
  self._whose_turn = "White"
  self._turn_number = 1
  self._game_state = 'UNFINISHED'
  self.initialize_board()

def initialize_board(self):
  self._board[0][0] = ChessPiece("Rook", "a", "8", "Black")
severe venture
#

So I initialize each individual piece?

hollow beacon
#

It's similar to what you were doing before but in a better approach

#

So yeah, you would still have to initialize each individual piece

severe venture
#

Okay let me add all of that.

severe venture
hollow beacon
#

No you don't need this

severe venture
#

Ah okay.

hollow beacon
#

Do you understand what the code I sent does? Or at least why you don't need the n_board?

severe venture
#

I understand the initialization of the board by adding each individual piece.

#

I don't understand why you don't need the board.

#

Also, I have to add the color in the name.

hollow beacon
# severe venture I don't understand why you don't need the board.

Think of self._board as your chessboard in the game, where you can see and move all the chess pieces. It's like a map that shows where everything is. The n_board idea, where you give each square a number from 0 to 63, isn't needed because self._board already makes it easy to see where the pieces are and how to move them

hollow beacon
severe venture
#

Here I'm initializing the individual locations but not the whole thing.

#
    def initialize_board(self):
        """Initializes the board to be played on."""
        self._board[0][0] = ChessPiece("Black Rook 1", "a", "8")
        self._board[0][0] = ChessPiece("Black Knight 1", "b", "8")
        self._board[0][0] = ChessPiece("Black Bishop 1", "c", "8")
        self._board[0][0] = ChessPiece("Black Queen", "d", "8")
        self._board[0][0] = ChessPiece("Black King", "e", "8")
        self._board[0][0] = ChessPiece("Black Bishop 2", "f", "8")
        self._board[0][0] = ChessPiece("Black Knight 2", "g", "8")
        self._board[0][0] = ChessPiece("Black Rook 2", "h", "8")

        self._board[0][0] = ChessPiece("White Rook 1", "a", "1")
        self._board[0][0] = ChessPiece("White Knight 1", "b", "1")
        self._board[0][0] = ChessPiece("White Bishop 1", "c", "1")
        self._board[0][0] = ChessPiece("White Queen", "d", "1")
        self._board[0][0] = ChessPiece("White King", "e", "1")
        self._board[0][0] = ChessPiece("White Bishop 2", "f", "1")
        self._board[0][0] = ChessPiece("White Knight 2", "g", "1")
        self._board[0][0] = ChessPiece("White Rook 2", "h", "1")

        return self._board
hollow beacon
#

!e

print([[None for _ in range(8)] for _ in range(8)])
limber gardenBOT
#

@hollow beacon :white_check_mark: Your 3.12 eval job has completed with return code 0.

[[None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None]]
hollow beacon
severe venture
#

Oh woops. forgot to update that.

#

Okay so this goes in the ChessVar init

self.initialize_board()
hollow beacon
severe venture
#

And then this goes in initialize board.

#
self._board = [[None for _ in range(8)] for _ in range(8)]
#

Gotcha.

#

I understand.

#

Here let me finish the pieces real quick.

severe venture
# hollow beacon Yes, this goes in the `ChessVar` class, and the first line of the `__init__` is ...

Okay can you confirm I've done this right?

    def initialize_board(self):
        """Initializes the board to be played on."""
        self._board = [[None for _ in range(8)] for _ in range(8)]

        self._board[0][0] = ChessPiece("Black Rook 1", "a", "8")
        self._board[0][1] = ChessPiece("Black Knight 1", "b", "8")
        self._board[0][2] = ChessPiece("Black Bishop 1", "c", "8")
        self._board[0][3] = ChessPiece("Black Queen", "d", "8")
        self._board[0][4] = ChessPiece("Black King", "e", "8")
        self._board[0][5] = ChessPiece("Black Bishop 2", "f", "8")
        self._board[0][6] = ChessPiece("Black Knight 2", "g", "8")
        self._board[0][7] = ChessPiece("Black Rook 2", "h", "8")
        self._board[1][0] = ChessPiece("Black Pawn 1", "a", "7")
        self._board[1][1] = ChessPiece("Black Pawn 2", "b", "7")
        self._board[1][2] = ChessPiece("Black Pawn 3", "c", "7")
        self._board[1][3] = ChessPiece("Black Pawn 4", "d", "7")
        self._board[1][4] = ChessPiece("Black Pawn 5", "e", "7")
        self._board[1][5] = ChessPiece("Black Pawn 6", "f", "7")
        self._board[1][6] = ChessPiece("Black Pawn 7", "g", "7")
        self._board[1][7] = ChessPiece("Black Pawn 8", "h", "7")

#
        self._board[7][0] = ChessPiece("White Rook 1", "a", "1")
        self._board[7][1] = ChessPiece("White Knight 1", "b", "1")
        self._board[7][2] = ChessPiece("White Bishop 1", "c", "1")
        self._board[7][3] = ChessPiece("White Queen", "d", "1")
        self._board[7][4] = ChessPiece("White King", "e", "1")
        self._board[7][5] = ChessPiece("White Bishop 2", "f", "1")
        self._board[7][6] = ChessPiece("White Knight 2", "g", "1")
        self._board[7][7] = ChessPiece("White Rook 2", "h", "1")
        self._board[6][0] = ChessPiece("White Pawn 1", "a", "2")
        self._board[6][1] = ChessPiece("White Pawn 2", "b", "2")
        self._board[6][2] = ChessPiece("White Pawn 3", "c", "2")
        self._board[6][3] = ChessPiece("White Pawn 4", "d", "2")
        self._board[6][4] = ChessPiece("White Pawn 5", "e", "2")
        self._board[6][5] = ChessPiece("White Pawn 6", "f", "2")
        self._board[6][6] = ChessPiece("White Pawn 7", "g", "2")
        self._board[6][7] = ChessPiece("White Pawn 8", "h", "2")

        return self._board
severe venture
#

The second number in the self._board set has a little yellow squigly with an error in it.

#

Unexpected type(s):(int, ChessPiece)Possible type(s):(SupportsIndex, None)(slice, Iterable[None])

hollow beacon
#

Can you send your code?

severe venture
hollow beacon
#

I'm not getting anything on my side

#

What did you do in order to get this?

severe venture
#

I just copied and pasted that.

hollow beacon
severe venture
#

Well yeah it still has the old code.

#

Is my syntax for putting the pieces in there incorrect?

hollow beacon
severe venture
#

From verify_move.

hollow beacon
#

That is in the code you just sent?

severe venture
#

Yeah it should be.

hollow beacon
#

How come I don't get any error then

severe venture
#

Oh it's not an error it's like a little yellow squiggly under it.

#

See?

#

Under the second int for setting up the board.

#

Is it not the correct syntax for assigning a value to a matrix?

hollow beacon
#

I do not have this on my side either

severe venture
#

Huh.

#

Well how can we test that it works?

hollow beacon
severe venture
#

Yeah that's old code.

#

I commented it out.

#

You can remove it.

hollow beacon
#

I just copy/pasted the latest you sent me

#

You're confusing me right now

severe venture
#

Yeah I just commented it out just now.

hollow beacon
#

What did you comment out lol

severe venture
#

A lot.

#

There's the recent version.

hollow beacon
#

Ok but now that you converted to a matrix a lot of your methods are obselete

severe venture
#

Yeah.

hollow beacon
#

But they are also a lot simpler to make now

severe venture
#

I like to hear that.

#

Okay so we initialize the matrix and we need to be able to check the legality of moves in make_move.

#

Here I'll just delete all the old stuff.

hollow beacon
#

So now we're back to my very first message

severe venture
#

Gotcha.

#

Okay let me see if I can figure it out.

hollow beacon
#

Also you're not supposed to return the board in the initialize_board method

severe venture
#

Woops.

#

Got it.

#

Do you know why I can't see the readme in PyCharm?

hollow beacon
#

Not really

#

I mainly use VSCode

#

But there shouldn't be any issue with PyCharm

severe venture
#

Fixed it.

#

Okay so I'm gonna take x_pos and y_pos and then convert to index.

hollow beacon
#

Ok so quick recap, the methods you will need in ChessVar are :

  1. print_board
  2. get_game_state
  3. verify_move
  4. make_move
  5. convert_pos_to_index
#

You shouldn't need any more than that except for the init and initialize obviously!

severe venture
#

Yeah it's just making those work.

#

Is there a cleverer way to do convert_pos_to_index than if x_pos = a return 0?

#

Feel like something like this could work

    def convert_pos_to_index(self, pos):
        """Converts the position to an index."""
        x_pos = pos[0]
        y_pos = pos[1]

        for i in range(0, 7):
            if i == x_pos:
                return
hollow beacon
#

My convert_pos_to_index converts a board position to array indices

#

So like a8 to something

severe venture
#

And array indices are like [0][0] right?

hollow beacon
#

Exactly

severe venture
#

I'm trying to do that but I don't want to do this stupid looking if chain.

#
        if x_pos == "a":
            return 0
        if x_pos == "b":
            return 1

hollow beacon
#

You don't need to

severe venture
#

How do I do it better?

hollow beacon
#

You can calculate the x coordinate using ord()

#

and then y coordinate normally because you know its a 8x8 board

severe venture
#

I don't think I'm allowed to use that.

#

We're not allowed to use anything we haven't been taught in our courses.

hollow beacon
#

Oh is this an assignment?

severe venture
#

Yeah it is.

#

And I'm struggling.

hollow beacon
#

I see

severe venture
#

So is there a way to iterate through without ord?

#

Or any other way to avoid the if chain?

hollow beacon
#

Have you been taught about dictionaries?

severe venture
#

Yup I have.

hollow beacon
#

Then yes there's still a very simple way

#

By creating a mapping of the columns ('a' through 'h') to their respective index (0 through 7)

severe venture
#
 pos_dict = {
            "a": 0,
            "b": 1,
            "c": 2,
            "d": 3,
            "e": 4,
hollow beacon
#

And the y coordinate formula stays the same

severe venture
#

Like this, correct?

hollow beacon
#

Correct

severe venture
#

Great!

#

And then y_pos I just need to subtract one from, yes?

hollow beacon
#

Well, it's not that I don't like helping you but it's near 3AM at my place, I might have to stop the fun here

severe venture
#

No worries at all. Would you maybe have some time tomorrow?

hollow beacon
severe venture
#

Well 8th file on the chess board would be 7th index right?

#
    # n_board = [[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7]
    #           [8 , 9 , 10, 11, 12, 13, 14, 15]
    #           [16, 17, 18, 19, 20, 21, 22, 23]
    #           [24, 25, 26, 27, 28, 29, 30, 31]
    #           [32, 33, 34, 35, 36, 37, 38, 39]
    #           [40, 41, 42, 43, 44, 45, 46, 47]
    #           [48, 49, 50, 51, 52, 53, 54, 55]
    #           [56, 57, 58, 59, 60, 61, 62, 63]]
severe venture
#

So shouldn't that be -1 from y_pos?

#

If I pass in "a8" then to reference that index it should be [7][7] I think.

hollow beacon
severe venture
#

I'll figure it out. ๐Ÿ˜„ Can't be too hard.

#

So would you have some time tomorrow to help me maybe?

#

I can work around your schedule.

hollow beacon
#

I would be surprised if nobody helped you until then

#

I'll let you know if you're still in need

severe venture
#

Yes but you get very different help/approaches depending on who helps.

hollow beacon
severe venture
#

No I think you're great!

#

I appreciate you taking the time to answer my questions and help me.

#

And I'll let you get some sleep so have a great night!

hollow beacon
#

Alright, will make sure to let you know

severe venture
#

Thanks!

hollow beacon
severe venture
#

!close

limber gardenBOT
#
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.