From e577c389e3eb83c84da63a0605d37578152092c8 Mon Sep 17 00:00:00 2001 From: htom Date: Sat, 26 Jul 2025 14:03:13 +0200 Subject: [PATCH] basic input handling --- input.txt | 1 + main.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main1.py | 17 +++++++---- 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 input.txt diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..8d5a585 --- /dev/null +++ b/input.txt @@ -0,0 +1 @@ +a0-c0 h1-j1 c4-g4 a6-a5 j5-j7 diff --git a/main.py b/main.py index 37b3ac0..c0ba0a4 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,25 @@ def main(): pygame.quit() sys.exit(1) + print("Please input your list of positions, from smallest ship to the largest, for your ships.") + print("Format: a[A]1-a[A]5 b[B]2-b[B]3. [A] means it can either be upper or lower case.") + print("You need to place five ships with sizes: 5, 4, 3, 3, 2. Otherwise the game won't start") + print("Have fun") + + good_inp = False + player_board = [] + enemy_board = [] + while good_inp == False: + inp = input() + good_inp, player_board = check_valid_input(inp) + enemy_board = player_board.copy() + + + for i in range(10): + for j in range(10): + print(f"{board[i][j]} ", end="") + print() + screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) time = pygame.time.Clock() @@ -32,5 +51,70 @@ def main(): dt = time.tick(60) / 1000 #converted to ms + +def check_valid_input(inp): + s = inp.split(' ') + print("s", s) + board = [] + for i in range(10): + board.append([]) + for _ in range(10): + board[i].append(0) + + if len(s) != 5: + print("Not valid input. Please input again") + return (False, board) + else: + for position in s: + position = position.lower() + t = position.split("-") + print("t", t) + + 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 + if int(t[0][1]) > int(t[1][1]): + step = -1 + else: + step = 1 + + print(int(t[0][1]), int(t[1][1])) + 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) + + print("in") + board[ord(t[0][0])-97][column] = 0b01 #first means if it is hit, second means placement + + print(board) + else: + print("e1") + return (False, board) + 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: + print("in2") + 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]): + step = -1 + else: + step = 1 + + 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) + + print("in3") + board[row][int(t[0][1])] = 0b01 #first means if it is hit, second means placement + + print(board) + else: + print("e2") + return (False, board) + + print("e3") + return (True, board) + + + if __name__ == "__main__": main() diff --git a/main1.py b/main1.py index 2428706..3bfa2fe 100644 --- a/main1.py +++ b/main1.py @@ -5,16 +5,21 @@ import sys def main(): term = blessed.Terminal() - print(term.home + term.clear + term.move_y(term.height // 2)) - print(term.black_on_darkkhaki(term.center('press any key to continue.'))) + print(term.home + term.clear + term.move_y(term.height)) dt = 0 - with term.cbreak(), term.hidden_cursor(): + with term.cbreak(), term.hidden_cursor(), term.fullscreen(): while True: - inp = term.inkey(timeout=2) - print(term.move_down(2) + 'You pressed ' + term.bold(repr(inp))) - time.sleep(60/1000) + t = [] + while len(t) != 2: + inp = term.inkey(timeout=2) + if (ord(inp) >= 97 and ord(inp) <= 122) or (ord(inp) >= 65 and ord(inp) <= 90): + t.append(inp) + #inp = input("give input\n") + + print(term.move_down(2) + 'You pressed ' + term.bold(repr(inp))) + print(term.home + term.clear + term.move_y(term.height)) if __name__ == "__main__":