Skip to content

Instantly share code, notes, and snippets.

@albionbrown
Last active April 27, 2022 16:39
Show Gist options
  • Select an option

  • Save albionbrown/045002da6ac9ce78e9dbdb8ed7b8fcda to your computer and use it in GitHub Desktop.

Select an option

Save albionbrown/045002da6ac9ce78e9dbdb8ed7b8fcda to your computer and use it in GitHub Desktop.

Revisions

  1. albionbrown revised this gist Apr 27, 2022. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #! /bin/python

    import turtle
    import random

    print("Let's play Battleships!")

    @@ -9,6 +10,7 @@
    line_length = 300
    cell_length = 42
    your_ships = []
    opp_ships = []
    all_cells = []
    originX = -100
    originY = 100
    @@ -135,5 +137,14 @@ def enter_my_ships() :
    })
    # End of enter_my_ships

    def gen_opp_ships():
    opp_ships.append({
    "column": random.randint(1,7),
    "row": random.randint(1,7),
    #"orientation": orientation
    })


    enter_my_ships()
    print_my_ships()
    gen_opp_ships()
  2. albionbrown revised this gist Apr 13, 2022. 1 changed file with 35 additions and 12 deletions.
    47 changes: 35 additions & 12 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,7 @@
    ship_offset = -21
    numbers = []
    font_size = 30
    number_of_ships = 2

    def write_letter(number, x, y):

    @@ -97,20 +98,42 @@ def print_my_ships():
    ship.sety(originY - ((i["row"] - 1) * cell_length) + ship_offset)
    # End of print_my_ships

    def check_ship_exists(x, y):

    for ship in your_ships:
    if (ship["column"] == x and ship["row"] == y):
    return True

    return False

    # Set up the game
    print_grid()

    print("Your turn to place your battleships")

    for i in range(0, 2):
    column = int(input("Enter the column: "))
    row = int(input("Enter the row: "))
    #orientation = input("Enter the orientation. H for horizontal and V for vertical")
    def enter_my_ships() :
    print("Your turn to place your battleships!")

    your_ships.append({
    "column": column,
    "row": row,
    #"orientation": orientation
    })
    for i in range(0, number_of_ships):

    entry_valid = False

    while (not entry_valid):
    print("Ship "+str(i+1))
    column = int(input("Enter the column: "))
    row = int(input("Enter the row: "))
    #orientation = input("Enter the orientation. H for horizontal and V for vertical")

    if (check_ship_exists(column, row)):
    print("A ship is already in that location! Try again.")

    else:
    entry_valid = True

    your_ships.append({
    "column": column,
    "row": row,
    #"orientation": orientation
    })
    # End of enter_my_ships

    print_my_ships()
    enter_my_ships()
    print_my_ships()
  3. albionbrown revised this gist Apr 13, 2022. 1 changed file with 35 additions and 4 deletions.
    39 changes: 35 additions & 4 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,22 @@
    originX = -100
    originY = 100
    ship_offset = -21
    numbers = []
    font_size = 30

    def write_letter(number, x, y):

    a = turtle.Turtle()
    a.penup()
    a.speed("fastest")
    a.color("black")
    style = ('Arial', font_size)
    a.setx(x)
    a.sety(y)
    a.write(number, font=style, align='center')
    a.hideturtle()
    numbers.append(a)
    # End of write_letter

    def print_grid():

    @@ -52,6 +68,22 @@ def print_grid():
    grid_turtle.forward(cell_length)
    grid_turtle.right(90)
    grid_turtle.pendown()

    write_letter('1', originX+20, originY+20)
    write_letter('2', originX+62, originY+20)
    write_letter('3', originX+104, originY+20)
    write_letter('4', originX+146, originY+20)
    write_letter('5', originX+188, originY+20)
    write_letter('6', originX+230, originY+20)
    write_letter('7', originX+272, originY+20)

    write_letter('1', originX-20, originY-35)
    write_letter('2', originX-20, originY-77)
    write_letter('3', originX-20, originY-119)
    write_letter('4', originX-20, originY-161)
    write_letter('5', originX-20, originY-203)
    write_letter('6', originX-20, originY-245)
    write_letter('7', originX-20, originY-287)
    # End of print_grid

    def print_my_ships():
    @@ -73,13 +105,12 @@ def print_my_ships():
    for i in range(0, 2):
    column = int(input("Enter the column: "))
    row = int(input("Enter the row: "))
    orientation = input("Enter the orientation. H for horizontal and V for vertical")
    #orientation = input("Enter the orientation. H for horizontal and V for vertical")

    your_ships.append({
    "column": column,
    "row": row,
    "orientation": orientation
    #"orientation": orientation
    })

    print_my_ships()

    print_my_ships()
  4. albionbrown revised this gist Apr 13, 2022. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@
    cell_length = 42
    your_ships = []
    all_cells = []
    originX = -100
    originY = 100
    ship_offset = -21

    def print_grid():

    @@ -20,8 +23,8 @@ def print_grid():
    grid_turtle.speed("fastest")

    # Draw rows
    grid_turtle.setx(-100)
    grid_turtle.sety(100)
    grid_turtle.setx(originX)
    grid_turtle.sety(originY)
    grid_turtle.pendown()

    for i in range(0, rows):
    @@ -54,11 +57,12 @@ def print_grid():
    def print_my_ships():
    for i in your_ships:
    ship = turtle.Turtle()
    ship.penup()
    ship.speed("fastest")
    ship.shape("circle")
    ship.color("blue")
    ship.setx(your_ships[i]["column"] * cell_length)
    ship.sety(your_ships[i]["row"] * cell_length)
    ship.show()

    ship.setx(originX + (i["column"] * cell_length) + ship_offset)
    ship.sety(originY - ((i["row"] - 1) * cell_length) + ship_offset)
    # End of print_my_ships

    # Set up the game
    @@ -78,8 +82,4 @@ def print_my_ships():
    })

    print_my_ships()






  5. albionbrown revised this gist Apr 7, 2022. 1 changed file with 19 additions and 6 deletions.
    25 changes: 19 additions & 6 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,7 @@
    columns = 8
    rows = 8
    line_length = 300
    cell_length = 42
    your_ships = []
    all_cells = []

    @@ -29,7 +30,7 @@ def print_grid():
    grid_turtle.back(line_length)

    grid_turtle.right(90)
    grid_turtle.forward(42)
    grid_turtle.forward(cell_length)
    grid_turtle.left(90)
    grid_turtle.pendown()

    @@ -45,28 +46,40 @@ def print_grid():
    grid_turtle.back(line_length)

    grid_turtle.left(90)
    grid_turtle.forward(42)
    grid_turtle.forward(cell_length)
    grid_turtle.right(90)
    grid_turtle.pendown()

    # End of print_grid

    def print_my_ships():
    for i in your_ships:
    ship = turtle.Turtle()
    ship.color("blue")
    ship.setx(your_ships[i]["column"] * cell_length)
    ship.sety(your_ships[i]["row"] * cell_length)
    ship.show()

    # End of print_my_ships

    # Set up the game
    print_grid()

    print("Your turn to place your battleships")

    for i in range(0, 2):
    column = input("Enter the column: ")
    row = input("Enter the row: ")
    column = int(input("Enter the column: "))
    row = int(input("Enter the row: "))
    orientation = input("Enter the orientation. H for horizontal and V for vertical")

    your_ships.append({
    "column": column,
    "row": row,
    "orientation": orientation
    })

    print_my_ships()

    print(your_ships)




  6. albionbrown revised this gist Apr 6, 2022. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -54,9 +54,19 @@ def print_grid():
    print_grid()

    print("Your turn to place your battleships")
    column = input("Enter the column: ")
    row = input("Enter the row: ")
    orientation = input("Enter the orientation. H for horizontal and V for vertical")

    for i in range(0, 2):
    column = input("Enter the column: ")
    row = input("Enter the row: ")
    orientation = input("Enter the orientation. H for horizontal and V for vertical")

    your_ships.append({
    "column": column,
    "row": row,
    "orientation": orientation
    })

    print(your_ships)



  7. albionbrown revised this gist Apr 6, 2022. 1 changed file with 32 additions and 6 deletions.
    38 changes: 32 additions & 6 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,11 @@

    print("Let's play Battleships!")

    columns = 7
    rows = 7
    columns = 8
    rows = 8
    line_length = 300
    your_ships = []
    all_cells = []

    def print_grid():

    @@ -15,22 +18,45 @@ def print_grid():
    grid_turtle.penup()
    grid_turtle.speed("fastest")

    # Draw columns
    # Draw rows
    grid_turtle.setx(-100)
    grid_turtle.sety(100)
    grid_turtle.pendown()

    for i in range(0, columns):
    grid_turtle.forward(300)
    for i in range(0, rows):
    grid_turtle.forward(line_length)
    grid_turtle.penup()
    grid_turtle.back(300)
    grid_turtle.back(line_length)

    grid_turtle.right(90)
    grid_turtle.forward(42)
    grid_turtle.left(90)
    grid_turtle.pendown()

    # Draw columns
    grid_turtle.setx(-100)
    grid_turtle.sety(100)
    grid_turtle.pendown()
    grid_turtle.right(90)

    for i in range(0, columns):
    grid_turtle.forward(line_length)
    grid_turtle.penup()
    grid_turtle.back(line_length)

    grid_turtle.left(90)
    grid_turtle.forward(42)
    grid_turtle.right(90)
    grid_turtle.pendown()


    # Set up the game
    print_grid()

    print("Your turn to place your battleships")
    column = input("Enter the column: ")
    row = input("Enter the row: ")
    orientation = input("Enter the orientation. H for horizontal and V for vertical")



  8. albionbrown revised this gist Apr 6, 2022. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion Battleships.py
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,15 @@ def print_grid():
    grid_turtle.sety(100)
    grid_turtle.pendown()

    grid_turtle.forward(300)
    for i in range(0, columns):
    grid_turtle.forward(300)
    grid_turtle.penup()
    grid_turtle.back(300)

    grid_turtle.right(90)
    grid_turtle.forward(42)
    grid_turtle.left(90)
    grid_turtle.pendown()

    # Set up the game
    print_grid()
  9. albionbrown created this gist Apr 6, 2022.
    28 changes: 28 additions & 0 deletions Battleships.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #! /bin/python

    import turtle

    print("Let's play Battleships!")

    columns = 7
    rows = 7

    def print_grid():

    #screen_turtle = turtle.Turtle()
    #screen_turtle.bgcolor('blue')
    grid_turtle = turtle.Turtle()
    grid_turtle.penup()
    grid_turtle.speed("fastest")

    # Draw columns
    grid_turtle.setx(-100)
    grid_turtle.sety(100)
    grid_turtle.pendown()

    grid_turtle.forward(300)

    # Set up the game
    print_grid()