4 Replies - 2902 Views - Last Post: 18 February 2012 - 07:06 AM Rate Topic: ***** 1 Votes

#1 69master  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-February 12

Text-Based Battleship

Posted 17 February 2012 - 06:30 PM

need to make a text-based battleship game with option of two players on the same computer or 1 player vs. computer with random ship placement and firing coordinates, this is what i have so far but iv hit a wall, i'm not getting the correct output for ship placement of aircraft carrier size 5, the grid becomes all 5's and not sure why
input:
1
1
1
1
1
1
1
(grid drawn)
1
1
6
1


output:
. 1 2 3 4 5 6 7 8 9 10


1 5 5 5 5 5 5 5 5 5 5

2 5 5 5 5 5 5 5 5 5 5

3 5 5 5 5 5 5 5 5 5 5

4 5 5 5 5 5 5 5 5 5 5

5 5 5 5 5 5 5 5 5 5 5

6 5 5 5 5 5 5 5 5 5 5

7 5 5 5 5 5 5 5 5 5 5

8 5 5 5 5 5 5 5 5 5 5

9 5 5 5 5 5 5 5 5 5 5

10 5 5 5 5 5 5 5 5 5 5


my code:


class start(object):
    def __init__(self):
        
            self.GameT = int(raw_input("One Player (1) or Two Player (2): "))
            self.Size = int(raw_input("What are the demenions of this game? (n X n): "))
            if self.Size < 2:
                self.Size = 10
            self.Air = int(raw_input("How many Aircraft Carriers? (Size 5): "))
            self.Bat = int(raw_input("How many Battleships? (Size 4): "))
            self.Sub = int(raw_input("How many Submarines? (Size 3): "))
            self.Cru = int(raw_input("How many Cruisers? (Size 3): "))
            self.Des = int(raw_input("How many Destroyers? (Size 2): "))
        
class board(object):
    def __init__(self):
        pass
    
    def gen(self, size):
        
        if size < 2:
            size = 10
            
        x = {}
        y = []
        for i in range(1, size + 1):
            y.append(0)
        for k in range(1, size + 1):
            x[k] = y
        return x
            


class player(object):
    
    def __init__(self, board, ships):
        shipcount = 0
        for key in ships:
            shipcount += ships[key]
        
        self.ShipPlacement = {}
        pass
    
def determine_hit(x, y, player):
    
    if player[x][y-1] == 0:
        player[x][y-1] = "M"
        return "MISS!"
    
    elif player[x][y-1] == 1:
        player[x][y-1] = "H"
        return "HIT!"
    
    else:
        return "Invalid"

def draw(board):
    
    print ""
    print ".",
    for key in board:
        if key <= 10:
            print " ", key,
        elif key > 10 and key < 100:
            print "", key,
        else:
            print key,
    print ""
    print ""
    for key in board:
        print ""
        if key < 10:
            print key, " ",
        elif key >= 10 and key < 100:
            print key, "",
        else:
            print key,
        index = 0
        for key2 in board:
            print board[key2][index], " ",
        index += 1
        print ""    

def translate(sh, sv, eh, ev, board, size):
    #************************************Horizontal********
    if sv == ev:
        if sh < eh:
            if eh - sh == size:
                count = 0
                print board[sh + count][sv - 1]
                print board[sh + count][sv - 1] + 1
                print board[sh + count][sv - 1] == 0
                while count < size:
                    if board[sh + count][sv - 1] == 0:
                        board[sh + count][sv - 1] = 5
                        print board
                        
                        print sh + count, "KK"
                    #else:
                     #   print board[sh + count][sv - 1]
                      #  print board[sh + count + 4][sv - 1]
                       # print "Invalid tits"
                    print board[sh + count][sv - 1]
                    print count
                    count += 1
        elif sh > eh:
            if sh - eh == size:
                count = 0
                while count < size:
                    if board[sh - count][sv - 1] == 0:
                        board[sh - count][sv - 1] = 1
                    else:
                        print "Invalid"
                        print board[sh - count][sv]
                        return
                    count += 1
    #************************************Vertical***********
    elif sh == eh:
        if sv < ev:
            if ev - sv == size:
                print ev - sv
                count = 0
                while count < size:
                    print board[sh]
                    print board[sv]
                    if board[sh][sv + count - 1] == 0:
                        board[sh][sv + count - 1] = 1
                    else:
                        print "Invalid"
                        return
                    count += 1        
        elif sv > ev:
            if sv - ev == size:
                count = 0
                while count < size:
                    if board[sh][sv - count - 1] == 0:
                        board[sh][sv - count - 1] = 1
                    else:
                        print "Invalid"
                        return
                    count += 1  
          
    return board          
            
       
if __name__ == "__main__":
    settings = start()
    player_board = board()
    ships = {}
    ships["Air"] = settings.Air
    ships["Bat"] = settings.Bat
    ships["Sub"] = settings.Sub
    ships["Cru"] = settings.Cru
    ships["Des"] = settings.Des
    
    
    
    if settings.GameT == 1:
        ships
        p1board = player_board.gen(settings.Size)
        print p1board[1][1]
        human = player(p1board, ships)
        cpuboard = player_board.gen(settings.Size)
        cpu = player(cpuboard, ships)
        
        #********************************Graphic representation***
        
        draw(p1board)
            
            
    #*************************************Ship Placement (attempt)*******
            
        if settings.Air >= 1:
                
            count = 1
                
            while count <= settings.Air:
                
                start_h = int(raw_input("Starting horizontal corrodinate for your Aircraft Carrier (Size: 5): "))
                start_v = int(raw_input("Starting vertical corrodinate for your Aircraft Carrier (Size: 5): "))
                end_h = int(raw_input("Ending horizontal corrodinate for your Aircraft Carrier (Size: 5): "))
                end_v = int(raw_input("Ending vertical corrodinate for your Aircraft Carrier (Size: 5): "))
                print p1board[1][1]
                print end_h - start_h
                p1board = translate(start_h, start_v, end_h, end_v, p1board, 5)
                print "here: "
                draw(p1board)
                count += 1
                
        
        
    #************************************2 players***************   
    #if settings.GameT == 2:
     #   player1 = player_board.gen(settings.Size)
      #  player2 = player_board.gen(settings.Size)
       # print player1
        #print player2
        
        prompt_exit = raw_input("Hit enter to exit")



This post has been edited by Motoma: 17 February 2012 - 06:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Text-Based Battleship

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Text-Based Battleship

Posted 17 February 2012 - 07:08 PM

You inner loop in your draw() function isn't incrementing index.

I haven't been able to get this working for you, because your representation of the board is a little weird. The way I would handle this is like so:

n = int(raw_input("What are the dimension of this board? "))
if n == 1: n == 10

# Using list multiplication, we make an n by n board:
board = [[0] * n] * n

# To print the board, we use two for loops:
for row in board:
    for column in row:
        print column,
    print ""

# To access a single element in the board, we simply index
# WARNING: lists are zero based, so element 1 is actually 0,
# and element 10 is actually 9
print "The element at position (3, 3) is :", print board[2][2]



Was This Post Helpful? 1
  • +
  • -

#3 69master  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-February 12

Re: Text-Based Battleship

Posted 17 February 2012 - 07:34 PM

thank you very much, I wasn't aware of list multiplication, that makes things alot easier to read and comprehend :)
Was This Post Helpful? 0
  • +
  • -

#4 69master  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-February 12

Re: Text-Based Battleship

Posted 17 February 2012 - 08:29 PM

okay so iv adjusted my code to use lists, but my translation function is trying to iterate over None type.
If someone could write a simple horizontal ship placement (in translate function) i'm sure i could finish the rest




class start(object):
    def __init__(self):
        
            self.GameT = int(raw_input("One Player (1) or Two Player (2): "))
            self.Size = int(raw_input("What are the demenions of this game? (n X n): "))
            if self.Size < 2:
                self.Size = 10
            self.Air = int(raw_input("How many Aircraft Carriers? (Size 5): "))
            self.Bat = int(raw_input("How many Battleships? (Size 4): "))
            self.Sub = int(raw_input("How many Submarines? (Size 3): "))
            self.Cru = int(raw_input("How many Cruisers? (Size 3): "))
            self.Des = int(raw_input("How many Destroyers? (Size 2): "))
        
class board(object):
    def __init__(self):
        pass
    
    def gen(self, size):
        
        board = [[0] * size] * size
        return board


class player(object):
    
    def __init__(self, board, ships):
        shipcount = 0
        for key in ships:
            shipcount += ships[key]
        
        self.ShipPlacement = {}
        pass
    
def determine_hit(x, y, player):
    
    if player[x][y-1] == 0:
        player[x][y-1] = "M"
        return "MISS!"
    
    elif player[x][y-1] == 1:
        player[x][y-1] = "H"
        return "HIT!"
    
    else:
        return "Invalid"

def draw(board):

    for row in board:
        for column in row:
            print column,
        print ""    

def translate(sc, sr, ec, er, board, size):
    #************************************Horizontal********
    if sr == er:
        if sc < ec:
            if ec - sc == size:
                count = -1
                print board[sc + count][sr - 1]
                print board[sc + count][sr - 1] + 1
                print board[sc + count][sr - 1] == 0
                while count < size - 1:
                    if board[sc + count][sr - 1] == 0:
                        board[sc + count][sr - 1] = 5
                        print board
                        
                        print sc + count, "KK"
                    print count
                    count += 1

if __name__ == "__main__":
    settings = start()
    player_board = board()
    ships = {}
    ships["Air"] = settings.Air
    ships["Bat"] = settings.Bat
    ships["Sub"] = settings.Sub
    ships["Cru"] = settings.Cru
    ships["Des"] = settings.Des
    
    
    
    if settings.GameT == 1:
        ships
        p1board = player_board.gen(settings.Size)
        print p1board[1][1]
        human = player(p1board, ships)
        cpuboard = player_board.gen(settings.Size)
        cpu = player(cpuboard, ships)
        
        #********************************Graphic representation***
        
        draw(p1board)
            
            
    #*************************************Ship Placement (attempt)*******
            
        if settings.Air >= 1:
                
            count = 1
                
            while count <= settings.Air:
                
                start_c = int(raw_input("Starting column for your Aircraft Carrier (Size: 5): "))
                start_r = int(raw_input("Starting row for your Aircraft Carrier (Size: 5): "))
                end_c = int(raw_input("Ending column for your Aircraft Carrier (Size: 5): "))
                end_r = int(raw_input("Ending row for your Aircraft Carrier (Size: 5): "))
                print p1board[1][1]
                print end_c - start_c
                print translate(start_c, start_r, end_c, end_r, p1board, 5)
                print "here: "
                draw(p1board)
                count += 1
                
        
        
    #************************************2 players***************   
    #if settings.GameT == 2:
     #   player1 = player_board.gen(settings.Size)
      #  player2 = player_board.gen(settings.Size)
       # print player1
        #print player2
        
        prompt_exit = raw_input("Hit enter to exit")
       


Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Text-Based Battleship

Posted 18 February 2012 - 07:06 AM

You seem to be really missing the point of a class....
class board(object):
    def __init__(self):
        pass
    
    def gen(self, size):
        
        board = [[0] * size] * size
        return board


If init has pass, that should send up a flag. If an object method doesn't use self, that's also a bad sign.

Your board code is fundamentally this, which is fine:
def genBoard():
	return [[0] * size] * size



So, why would you want to use a board class? Well, each player has a board. It has methods that makes sense...

class Board(object):
	def __init__(self, size):
		self.size = size
		self.clear()
		
	def clear(self):
		self.grid = [[0] * self.size] * self.size

	def draw(self):
		for row in self.grid:
			for column in row:
				print column,
			print ""    

	def determine_hit(self, x, y):
		if self.grid[x][y] == 0:
			self.grid[x][y] = "M"
			return "MISS!"
		if self.grid[x][y] == 1:
			self.grid[x][y] = "H"
			return "HIT!"
		return "Invalid"



It would seem that each player wants a board. Indeed, you're passing a board, you're just not doing anything with it!

e.g.
class Player(object):
	def __init__(self, ships):
		self.ships = ships
		self.board = Board()
		self.ShipPlacement = {}

	def getShipCount(self):
		# your code here
			
	def getLiveShipCount(self):
		# your code here



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1