Sep-26-2018, 05:50 PM
Yeah sure, no problem mate :)
I think the players are great examples.
Player:
Hope this example helps :) The next step would be the board.
I think the players are great examples.
Player:
class Player: def __init__(self, board, ships): self.board = board self.op_board = list(board) self.ships = ships self.counter = 0 sef inc_counter(self): self.counter += 1 def place_ships(self): """ Place all ships on the given board """ for ship in self.ships: for _ in range(0, (5 - ships[ship])): while True: checkcoords = [] x = random.randint(1, 10) y = random.randint(1, 10) o = random.randint(0, 1) if o == 0: ori = "v" # vertical else: ori = "h" # horizontal # if ship would be placed outside of the board skip if (ori == "v" and y + self.ships[ship] > 10) or (ori == "h" and x + self.ships[ship] > 10): pass else: if ori == "v": # vertical placement # if no ship is near this position place it for i in range(-1, (self.ships[ship] + 1)): for j in range(-1, 2): checkcoords.append(board[y + i][x + j]) if ': ' not in checkcoords: for i in range(self.ships[ship]): self.board[y + i][x] = ': ' break elif ori == "h": # horizontal placement # if no ship is near this position place it for i in range(-1, (self.ships[ship] + 1)): for j in range(-1, 2): checkcoords.append(self.board[y + j][x + i]) if ': ' not in checkcoords: for i in range(self.ships[ship]): self.board[y][x + i] = ': ' break def get_board(self): return self.board def hit_or_miss(self, a, b, all_buttons, info): """ check if there was a hit or a missed done by the player :param a: clicked y position :param b: clicked x position :param all_buttons: all buttons of the opponent board :param info: write some info to the screen """ global AI # if a already hit place was clicked again write message if self.board[a + 1][b + 1] == 'O ' or self.board[a + 1][b + 1] == 'X ': info.set("You have already fired there, " + player + "!") return 0 elif self.board[a + 1][b + 1] == ': ': # if a ship was hit change the button and the players board info.set("A hit, nice shot " + player + "!") all_buttons[a][b].configure(text="X", fg="black", bg="red3", activebackground="red3") return 1 else: # in case of a miss change the button and trigger the AI info.set("Seems like you missed that one, " + player + "!") all_buttons[a][b].configure(text="O", fg="White", activeforeground="white") return 2 class AI(Player): def __init__(self, opponent, board, ships): super(AI,self).__init__(board,ships) self.opponent = opponent def shoot(self, every_button, info, x_s=None, y_s=None): if x_s is None or y_s is None: x = random.randint(0, 10) y = random.randint(0, 10) else: x = x_s y = y_s # if AI got one hit, destroy complete ship if self.opponent.hit_or_miss(y, x, every_button, info) == 1: self.inc_score() every_button[0][y- 1][x - 1].configure(text="X", fg="black", bg="red3") # depending of where the rest of the ship is located, shot it if self.opponent.getBoard()[y - 1][x] == ': ': ai_shoots(every_button, info, y - 1, x) elif self.opponent.getBoard()[y + 1][x] == ': ': ai_shoots(every_button, info, y + 1, x) elif player_1_board[y_coord][x_coord - 1] == ': ': ai_shoots(every_button, info, y, x - 1) elif player_1_board[y_coord][x_coord + 1] == ': ': ai_shoots(every_button, info, y, x + 1) else: # shot some random position if ship is destroyed x = random.randint(1, 10) y = random.randint(1, 10) ai_shoots(every_button, info, y, x) elif self.opponent.hit_or_miss(y, x, every_button, info) == 0: # if position was already shoot at, try a new position x = random.randint(1, 10) y = random.randint(1, 10) ai_shoots(every_button, info, y, x) else: # if water was hit just change the button every_button[0][y - 1][x - 1].configure(text="O", fg="white")I hope I haven't forgot anything, the problem is, that I can't test it right now. But this is an example of how the players could be implemented as classes accourding to your code. Note that the way you used the methods before has changed a little bit and there still can be made adjustments.
Hope this example helps :) The next step would be the board.