-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
78 lines (67 loc) · 2.8 KB
/
ai.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from board import Board
from direction import Direction
from pygame import Vector2
import pygame
import random
import itertools
class Ai(Board):
def __init__(self, x, y, pixels, border, main_window) -> None:
super().__init__(x, y, pixels, border, main_window)
self.ai_shots = []
self.ai_ships = []
self.ai_hits = []
self.ai_temp_ship = []
self.ai_ships_len = [8, 8, 6, 4, 2]
self.ai_temp_orientation = Direction.NORTH
def ai_place_ships(self):
for ship_len in self.ai_ships_len:
placed = False
while not placed:
overlap = False
position = []
direction = Direction(random.randint(0, 3))
start_vector = Vector2(random.randint(
0, self.x-1), random.randint(0, self.y-1))
if direction == Direction.NORTH:
position = [Vector2(start_vector.x, start_vector.y-i)
for i in range(ship_len)]
if direction == Direction.EAST:
position = [Vector2(start_vector.x + i, start_vector.y)
for i in range(ship_len)]
if direction == Direction.SOUTH:
position = [Vector2(start_vector.x, start_vector.y+i)
for i in range(ship_len)]
if direction == Direction.WEST:
position = [Vector2(start_vector.x-i, start_vector.y)
for i in range(ship_len)]
if position:
ship_head_x, ship_head_y = position[0]
ship_tail_x, ship_tail_y = position[-1]
if ship_head_x >= self.x or ship_head_x < 0:
continue
if ship_head_y >= self.y or ship_head_y < 0:
continue
if ship_tail_x >= self.x or ship_tail_x < 0:
continue
if ship_tail_y >= self.y or ship_tail_y < 0:
continue
if self.ai_ships:
placed_vectors = list(
itertools.chain.from_iterable(self.ai_ships))
for vec in placed_vectors:
if vec in position:
overlap = True
if overlap:
continue
self.ai_ships.append(position)
placed = True
border = 0.5
game_window_x = 16
game_window_y = 32
pixels = 30
pygame.init()
game_window = pygame.display.set_mode(
((game_window_x+border)*pixels, (game_window_y+border)*pixels))
test = Ai(game_window_x, game_window_x, pixels, border, game_window)
pygame.init()
test.ai_place_ships()