HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

2 Player Battleship with Multiple Ships

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
shipswithplayermultiplebattleship

Problem

This is it. My best game, a fully functional, 2 player game of battleship.

  • My coordinate picker can be seen here. I didn't make all the changes they wanted.



  • I would prefer some insight on whether I should make a board class



-
The skilstak.colors library, is also called solarized and can be found here.

  • c.cl=clear,c.g=green,c.m=magenta,c.y=yellow,c.x=reset,c.r=red



-
I don't really care if you add any "grammar" PEP8 violations

  • I made it so you can easily add ships or change the size, but haven't added it yet.



  • Edit: I keep updating it, so you can follow along here.



Constants / Imports

from operator import itemgetter
import getch
import skilstak.colors as c #colors
BOARD_LENGTH = 10
BOARD_HEIGHT = 10
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#

The library

```
class Ship:
def __init__(self, size, symbol):
self.size = size
self.symbol = symbol
self.coords = [(x,0) for x in range(size)]
self.previous_coords = [(None,None) for _ in range(size)]
def shift(self,direction): #moves the ships coordinates

x_values = list(map(itemgetter(0), self.coords))
y_values = list(map(itemgetter(1), self.coords))
if direction == 'LEFT' and 0 not in x_values:
change = (-1,0)
elif direction == 'DOWN' and BOARD_HEIGHT-1 not in y_values:
change = (0,1)
elif direction == 'UP' and 0 not in y_values:
change = (0,-1)
elif direction == 'RIGHT' and BOARD_LENGTH-1 not in x_values:
change = (1,0)
else:
change = (0,0)
for i, coord in enumerate(self.coords):
self.previous_coords[i] = self.coords[i]
self.coords[i] = (coord[0] + change[0], coord[1] + change[1])

def INFO = 'use WASD to move, and r to rotate your ship. Press q to quit and enter to confirm your selection.' ACTION_KEYS = { 'w':'UP', 's':'DOWN', 'd':'RIGHT', 'a':'LEFT', }


The library

```
class Ship:
def __init__(self, size, symbol):
self.size = size
self.symbol = symbol
self.coords = [(x,0) for x in range(size)]
self.previous_coords = [(None,None) for _ in range(size)]
def shift(self,direction): #moves the ships coordinates

x_values = list(map(itemgetter(0), self.coords))
y_values = list(map(itemgetter(1), self.coords))
if direction == 'LEFT' and 0 not in x_values:
change = (-1,0)
elif direction == 'DOWN' and BOARD_HEIGHT-1 not in y_values:
change = (0,1)
elif direction == 'UP' and 0 not in y_values:
change = (0,-1)
elif direction == 'RIGHT' and BOARD_LENGTH-1 not in x_values:
change = (1,0)
else:
change = (0,0)
for i, coord in enumerate(self.coords):
self.previous_coords[i] = self.coords[i]
self.coords[i] = (coord[0] + change[0], coord[1] + change[1])

def

Solution

Here are some quick observations just based on the code snippets in your question.

-
Use list comprehension or the zip() function:

# You can change this:
x_values = list(map(itemgetter(0), self.coords))
y_values = list(map(itemgetter(1), self.coords))

# To this:
x_values = [self.coords[0] for coord in self.coords]
y_values = [self.coords[1] for coord in self.coords]

# Or even better:
x_values, y_values = zip(*self.coords)


-
You can store your change directions as a dictionary as well:

CHANGE = {
    'LEFT': (-1, 0),
    ...
}


-
Why did you do this?

side_col = [1,2,3,....]

# Do this instead:
side_col = range(1, 31)


-
Making a board class would be advisable and would help you refactor some common functionality. You could implement bound checking methods in there.

I'll edit this answer as I find more.

Code Snippets

# You can change this:
x_values = list(map(itemgetter(0), self.coords))
y_values = list(map(itemgetter(1), self.coords))

# To this:
x_values = [self.coords[0] for coord in self.coords]
y_values = [self.coords[1] for coord in self.coords]

# Or even better:
x_values, y_values = zip(*self.coords)
CHANGE = {
    'LEFT': (-1, 0),
    ...
}
side_col = [1,2,3,....]

# Do this instead:
side_col = range(1, 31)

Context

StackExchange Code Review Q#155793, answer score: 2

Revisions (0)

No revisions yet.