Battleship project - adding stats


#1

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/extra-credit?action=resume_content_item

I have an idea of how to add stats, but I don’t know how to include it in the code.

‘’‘For stats, you have to .append to a list, probably with an if conditional. You have to store the value of when game_win is true (or knowing when the game is over), and then appending to the list with the argument that signifies a win.’’’

Yeah, so I want to be able to do that for the extra part of the ‘battleship’ project, but I don’t know how to track the win score, so that I can append it as an argument to the stats list. The win score is just going to be the turn when the game was won.

Rematches are a different story, but one step at a time.


#2

these stats will only work for as long as the program is running (so rematches). If the user closes the program, lists are cleared. Do you want to persist the data? Then you will need to write the data to the hard drive (file or databases are options) and read these at program boot


#3

Could I write it to a user’s browser cache instead? Make it a top 10 score, so any below that are deleted?


#4

yes and no. The only programming language supported by browsers is Javascript, so that would require transpiling your python code into JS code, which i think is technically possible with certain Python libraries, but it adds a lot of complexity.

Then i would just write the program in JS.

But i am slightly confused as to why you picked python if writing to browser cache was your intention all along? Pick the right tool (language) for the job.


#5

Nah, just curious. Anyway, here’s my code right now.

from random import randint

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
#print ship_row
#print ship_col

turn_stat = []

# Everything from here on should be in your for loop
# don't forget to properly indent!
for turn in range(4):
  print "Turn", turn + 1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank the metal ■■■■!"
    game_win = True
    turn_stat.append(turn)
    print "Last games were finished in the following number of turns:" \ turn_stat +1
   # rematch = raw_input ("Rematch? ")
    #	if rematch = "y" or "Y":
    break
  else:
    if guess_row not in range(5) or \
      guess_col not in range(5):
      print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col] == "X":
      print( "You guessed that one already." )
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"    
    if turn == 3:
  		print "Game Over"
    print_board(board)

I’m getting an ‘unexpected character after line continuation’ error on line 38. I dunno why it’s considered unexpected.

EDIT: Actually, I’ll link it so you don’t have to count the lines. https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/extra-credit?action=resume_content_item


#6

here:

    if guess_row not in range(5) or \
      guess_col not in range(5):

you do it right, you use the line continuation character (\) and put the remaining code on the next line. on line 38, you just have the code on a single line, so you don’t need \


#7

Okay, I do the line continuation correct, but then I get ‘invalid syntax’ for line 39 ‘turn_stat +1’. Doesn’t work when I make turn_stat into an integer either.

EDIT: Took out the continuation , but now it says, I can’t concatenate a list, but I don’t know why. I thought I could before. If I remove the +, then it gives syntax error


#8

understanding what is happening and what might cause the error is important. Feel free to google the error, to learn what might cause it

if you want further help from me, please post an updated version of your code.


#9

Well it works now.

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/extra-credit?action=resume_content_item

Now, I’m interested in knowing how to save to turn_stat list to hard drive?


#10

The codecademy editor is not suitable for this

there are several options, a simple database like sqlite or a file.


#11

Why did it recommend it as an exercise?

How do I incorporate sqlite or a file?


#12

it is the lesson? To persist the data after the program is done running?

Python include sqlite3 api by default, so you could use that. As for a file, you would first need to determine what kind of file (txt, csv or something else) you want.


#13

Would this run in codeacademy, or do I need my own IDE (or whatever it’s called)?


#14

i wouldn’t use codecademy editor for this, its not nice given all the validation behind an exercise. Using something like repl.it would already be slightly better, but ideally create your local development environment.


#15

I’ve got the python command line for version 2.7…


#16

that is good, now just a text-editor to write your script, and understanding how to execute the scripts

the more you do this yourself, the better, but let me know if you need help

or you could start with the python IDLE


#17

How do I get python IDLE?


#18

now you had to wait 5 hours for my answer, couldn’t you find it on google? Would have saved you time.

python standard ships with the IDLE (integrated development and learning environment)