
I has a reference to the Tkinter buttons (it shouldn't, as stated before) and to the row and column of the piece (that info in contained in the index of the board array containing the object).Ī BoardPart object really has 3 states: empty, with a peg, or it doesn't exist (in which case it can't have a peg). The BoardPart class contains useless info. Yet your game logic isn't independent enough to allow that. Before adding the Tkinter UI, it can be nice to be able to test the game logic in the terminal. It would be better to have a game object that can be called by the Tkinter model, or by any other model. This is not really the case, for example your Tkinter buttons point to a BoardPart item which in turn contain a reference to the button. You said you tried to separate the game logic from the Tkinter representation. The check_move method has 4 levels of nested if / elif / else statements, not counting the loops, making the program flow very hard to follow.
#Peg solitaire traingular board java code code
You also copied and slightly modified code in the check_move method, which is not friendly to read. This example fills a 7*7 array with None representing invalid spaces, True representing occupied spaces and False representing empty space.Īnother method would be to parse a file containing a representation of the initial board, like. I still think it is an improvement, as there are a lot less things to change if you want to change the starting board. I'll admit it gets hard to read, and still isn't very flexible. One option would be using list comprehensions: board = or j in else None for j in range(7)] for i in range(7)] What if you want to allow another empty space than the center one? Or a different shape of board? I Have seen a lot of variation around this game, and your solution is not flexible at all. To initialize the board, you manually put in 49 instances of a class with some parameters changing. It makes it harder to read and much harder to maintain. There is a lot of copied and pasted code in your work. There is room for improvement in your code, I'll give a few pointers of things that stand out to me.
#Peg solitaire traingular board java code update
I still want to update the game graphics, because they're still rough sketches haha. I couldn't figure out a way to check if there were no more possible moves or if the player won the game(if there was only one piece left), so if the player loses or wins the program doesn't do anything. Move_piece(hole_in_between, selected_hole= selected_hole, target_hole= target_hole)ĭef move_piece(hole_in_between, selected_hole, target_hole): If hole.row = selected_hole.row + 1 and lumn = selected_lumn: If hole.row = selected_hole.row - 1 and lumn = selected_lumn:Įlif selected_hole.row - target_hole.row = -2:

If selected_hole.row - target_hole.row = 2: If lumn = selected_lumn + 1 and hole.row = selected_hole.row:Įlif selected_hole.has_piece and not target_hole.has_piece and selected_lumn = target_lumn: Move_piece(hole_in_between, selected_hole, target_hole)Įlif selected_lumn - target_lumn = -2: If lumn = selected_lumn - 1 and hole.row = selected_hole.row: If selected_hole.has_piece and not target_hole.has_piece and selected_hole.row = target_hole.row: class BoardPart():ĭef _init_(self, row, column, button, has_piece, is_selected, is_button):īoard = ĭef check_moves(selected_hole, target_hole):

Here I check if the 'selected' and 'targeted' pieces are valid. Selected_(relief= 'raised')ī(image= PIECE_IMAGE)ī(image= HOLE_IMAGE) Game_logic.check_moves(selected_button, clicked_button) If not any(button.is_selected for button in game_buttons): Screen_id(row=board_part.row, column=board_lumn)Įmpty_part = tk.Frame(frame, width= BUTTON_WIDTH, height= BUTTON_HEIGHT, bg='#e5e8ec')Įmpty_id(row=board_part.row, column=board_lumn) Screen_button = tk.Button(frame, width=BUTTON_WIDTH, height=BUTTON_HEIGHT, bg='white', command=lambda x= board_part: click_button(x)) HOLE_IMAGE = tk.PhotoImage(file='restaumhole.png')Ĭanvas = tk.Canvas(root, width= WIDTH, height= HEIGHT, bg='#c5d1ed')įace(relx= 0.5, rely= 0.05, relwidth= 0.9, relheight= 0.9, anchor= 'n') PIECE_IMAGE = tk.PhotoImage(file='restaumpiece.png') This is the main code, I tried to separate the game-logic from the Tkinter representation of the board and stuff. It's kind of hard to explain it since I believe this game doesn't exist outside Brazil. For a move to be allowed, the selected piece must be one tile away from where it will 'jump' and the hole in which it will go has to be empty. The idea of the game is that there are holes in which there are pieces inside, the player has to pick pieces and 'jump' one another until there is only one left on the board. This time I wanted to make a board game that is very popular in Brazil, it's called 'resta um', it means 'there's only one left'.
