2

I am trying to write the program battleship. I have two gameboard matrices: one for player, one for computer. These are defined outside of main because I want them to be global variables because several functions manipulate/read them. I am using Python 2.6.1.

#create player game board (10x10 matrix filled with zeros)
playerBoard = [[0]*10 for i in range(10)]
#create computer game board (10x10 matrix filled with zeros)
computerBoard = [[0]*10 for i in range(10)]

Then I define the main function.

#define main function
def main():
    global playerBoard
    global computerBoard
    #keepGoing is true
    keepGoing = True
    #while keepGoing is true
    while keepGoing:
        #call main menu function. Set to response.
        response = mainMenu()
        #if response is 1
        if response == "1":
            #begin new game
            #call clearBoards function
            clearBoards()
            #call resetCounters function
            resetCounters()
            #call placeShips function (player)
            playerBoard = placeShips(playerBoard, "player")
            #call placeShips function (computer)
            computerBoard = placeShips(computerBoard, "computer")
            #call guessCycler function
            guessCycler()
        #if response is 2
        if response == "2":
            #keepGoing is false
            keepGoing = False

Despite my declaration of global playerboard and global computerBoard within main PyScripter still says those are local variables. I don't understand this. How can I make sure they are global?

Documents I have already looked at:
Using global variables in a function other than the one that created them
Changing global variables within a function
http://www.python-course.eu/global_vs_local_variables.php

Community
  • 1
  • 1
  • 2
    Somewhat off topic, but you shouldn't have a comment at every line that just explains what the next line does. It's redundant. – Thomas Orozco Dec 02 '13 at 16:20
  • Are global vars something you absolutely need? – gregb212 Dec 02 '13 at 16:22
  • 2
    you can test the values outside of the function. i do not use PyScripter, it's possible it's incorrect. – jpwagner Dec 02 '13 at 16:26
  • @ThomasOrozco I am in my first programming class and my teacher wants us to write pseudocode before actual code. That's why there is a comment for every line. It's useful for planning. In this section of code it's redundant but in the actual functions it's useful. – user3058083 Dec 02 '13 at 16:28
  • @gregb212 I use the gameboards in 8 functions in my program so it makes sense for them to be global, right? I check spaces are open for a ship to be placed, I place the ships (by changing values on the gameboard), I check the values on the gameboard to determine which ships have been hit. Etc. – user3058083 Dec 02 '13 at 16:31
  • @jpwagner I don't understand what you mean. Test what values? – user3058083 Dec 02 '13 at 16:33
  • @user3058083 `playerBoard` and `computerBoard` should probably be instance variables in an appropriately designed class rather than global variables, but it sounds as though your class has not yet covered object-oriented design, so global variables are fine in this situation. – chepner Dec 02 '13 at 16:37
  • Perhaps PyScripter is declaring it as local because it is still local to the module? Does that make any sense? I wonder if you had a second module with that global it would correctly declare it as `global`? – gregb212 Dec 02 '13 at 17:24
  • 1
    What version of PyScripter are you using? [This bug](http://code.google.com/p/pyscripter/issues/detail?id=421) seems to match your issue, but should have been fixed. In any case, I wouldn't worry that PyScripter wrongly says that your global variables are local if your program works as it should. – ekhumoro Dec 09 '13 at 02:38

1 Answers1

0

I definitely think you should reconsider if you need them to be global - You don't .-)

The cheap way, is do declare your stuff and the pass them on as parameters to the function

def MyFunc(board1):
    print board1

board1 = "Very weak horse"
MyFunc(board1)    

The real way to do it is to create a class and then access them using self

class MySuperClass():
     def __init__(self):
         self.horse = "No way up" 

     def myRealCoolFunc(self):
           print self.horse
 uhhh = MySuperClass()
 uhhh.myRealCoolFunc()
Svend Feldt
  • 758
  • 4
  • 17