shooting
This commit is contained in:
50
main.py
50
main.py
@@ -3,6 +3,14 @@ import sys
|
||||
from constants import *
|
||||
from player import *
|
||||
|
||||
def print_board(board):
|
||||
for i in range(BOARD_SIZE):
|
||||
for j in range(BOARD_SIZE):
|
||||
print(f"{board[i][j]} ", end="")
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
def main():
|
||||
print("Starting BattleShip!")
|
||||
|
||||
@@ -20,18 +28,16 @@ def main():
|
||||
good_inp = False
|
||||
player_board = []
|
||||
enemy_board = []
|
||||
|
||||
while good_inp == False:
|
||||
inp = input()
|
||||
good_inp, player_board = check_valid_input(inp)
|
||||
good_inp, player_board, ship_pos = check_valid_input(inp)
|
||||
enemy_board = player_board.copy()
|
||||
|
||||
player = Player(player_board)
|
||||
enemy = Player(enemy_board)
|
||||
player = Player(player_board, ship_pos)
|
||||
enemy = Player(enemy_board, ship_pos)
|
||||
|
||||
for i in range(BOARD_SIZE):
|
||||
for j in range(BOARD_SIZE):
|
||||
print(f"{player.board[i][j]} ", end="")
|
||||
print()
|
||||
print_board(player_board)
|
||||
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
@@ -48,7 +54,7 @@ def main():
|
||||
pygame.Surface.fill(screen, (255,255,255))
|
||||
|
||||
if first_iteration == False:
|
||||
player.update(enemy_board)
|
||||
player.update(enemy)
|
||||
else:
|
||||
first_iteration = False
|
||||
|
||||
@@ -60,6 +66,7 @@ def main():
|
||||
|
||||
|
||||
def check_valid_input(inp):
|
||||
ship_pos = []
|
||||
s = inp.split(' ')
|
||||
print("s", s)
|
||||
board = []
|
||||
@@ -70,7 +77,7 @@ def check_valid_input(inp):
|
||||
|
||||
if len(s) != 5:
|
||||
print("Not valid input. Please input again")
|
||||
return (False, board)
|
||||
return (False, board, ship_pos)
|
||||
else:
|
||||
count = 0
|
||||
for position in s:
|
||||
@@ -79,6 +86,7 @@ def check_valid_input(inp):
|
||||
position = position.lower()
|
||||
t = position.split("-")
|
||||
print("t", t)
|
||||
ship_pos.append((t[0], t[1]))
|
||||
|
||||
if t[0][0] == t[1][0] and ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107: #check if the fist chars match
|
||||
if int(t[0][1]) >= 0 and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and int(t[0][1]) != int(t[1][1]) and abs(int(t[0][1]) - int(t[1][1]))+1 < 6: #check if number is in correct range
|
||||
@@ -90,17 +98,13 @@ def check_valid_input(inp):
|
||||
for column in range(int(t[0][1]), int(t[1][1])+1, step):
|
||||
if board[ord(t[0][0])-97][column] == 0b01:
|
||||
print("this position is already taken!")
|
||||
return (False, board)
|
||||
return (False, board, ship_pos)
|
||||
|
||||
board[ord(t[0][0])-97][column] = 0b01 #first means if it is hit, second means placement
|
||||
|
||||
for i in range(BOARD_SIZE):
|
||||
for j in range(BOARD_SIZE):
|
||||
print(f"{board[i][j]} ", end="")
|
||||
print()
|
||||
print_board(board)
|
||||
else:
|
||||
print("e1")
|
||||
return (False, board)
|
||||
return (False, board, ship_pos)
|
||||
elif t[0][1] == t[1][1] and int(t[0][1]) <= 9 and int(t[1][1]) >= 0 and int(t[1][1]) <= 9 and abs(int(t[0][1]) - int(t[1][1]))+1 < 6:
|
||||
if ord(t[0][0]) >= 97 and ord(t[0][0]) <= 107 and ord(t[1][0]) >= 97 and ord(t[1][0]) <= 107 and t[0][0] != t[1][0]:
|
||||
if ord(t[0][0]) > ord(t[1][0]):
|
||||
@@ -111,22 +115,16 @@ def check_valid_input(inp):
|
||||
for row in range(ord(t[0][0])-97, ord(t[1][0])-97+1, step):
|
||||
if board[row][int(t[0][1])] == 0b01:
|
||||
print("this position is already taken!")
|
||||
return (False, board)
|
||||
return (False, board, ship_pos)
|
||||
|
||||
board[row][int(t[0][1])] = 0b01 #first means if it is hit, second means placement
|
||||
|
||||
for i in range(BOARD_SIZE):
|
||||
for j in range(BOARD_SIZE):
|
||||
print(f"{board[i][j]} ", end="")
|
||||
print()
|
||||
print_board(board)
|
||||
else:
|
||||
print("e2")
|
||||
return (False, board)
|
||||
return (False, board, ship_pos)
|
||||
|
||||
print("e3")
|
||||
return (True, board)
|
||||
|
||||
|
||||
return (True, board, ship_pos)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
36
player.py
36
player.py
@@ -1,21 +1,31 @@
|
||||
import pygame
|
||||
from pygame import Rect
|
||||
from ship import *
|
||||
|
||||
class Player():
|
||||
def __init__(self, board, ships = 5):
|
||||
def __init__(self, board, ships):
|
||||
self.board = board
|
||||
self.ships = ships #alive ships
|
||||
self.shot_positions = []
|
||||
for i in range(0, len(board)):
|
||||
self.shot_positions.append([])
|
||||
for j in range(0, len(board[0])):
|
||||
for _ in range(0, len(board[0])):
|
||||
self.shot_positions[i].append(0)
|
||||
#self.shot_positions[i][j] = 0b01
|
||||
|
||||
self.ships = []
|
||||
for ship in ships: #[(start, end), (start, end)
|
||||
self.ships.append(Ship(ship[0], ship[1]))
|
||||
|
||||
def get_hit(self, position):
|
||||
pass
|
||||
self.board[ord(position[0])-97][int(position[1])] = 0b11
|
||||
|
||||
def shoot(self, position):
|
||||
pass
|
||||
def shoot(self, position, enemy):
|
||||
print("p", position)
|
||||
|
||||
if enemy.board[ord(position[0])-97][int(position[1])] == 0b00:
|
||||
print("No ship at this position")
|
||||
else:
|
||||
enemy.get_hit(position)
|
||||
self.shot_positions[ord(position[0])-97][int(position[1])] = 0b11
|
||||
|
||||
def __check_valid_input(self, position):
|
||||
if len(position) != 2 and position != None:
|
||||
@@ -24,16 +34,18 @@ class Player():
|
||||
if ord(position[0]) >= 97 and ord(position[0]) <= 107 and int(position[1]) <= 9 and int(position[1]) >= 0:
|
||||
return True
|
||||
|
||||
if self.shot_positions[ord(position[0])-97][int(position[1])] == 0b11:
|
||||
print("You already shot at this position!")
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def update(self, enemy_board):
|
||||
def update(self, enemy):
|
||||
inp = input("Please input the position you want to fire.")
|
||||
while self.__check_valid_input(inp) == False:
|
||||
inp = input("Please input the position you want to fire.")
|
||||
|
||||
self.shoot(inp)
|
||||
|
||||
self.shoot(inp, enemy)
|
||||
|
||||
def draw(self, screen):
|
||||
start_pos_x = 125
|
||||
|
||||
Reference in New Issue
Block a user