Battle ship Games Written in python, pythonship

Source: Internet
Author: User

Battle ship Games Written in python, pythonship

 

 

I recently learned python. This is a small game I wrote today.

from random import randintclass Board(object):    board = []    def __init__(self,row,col):        self.board = []        self.row = row        self.col = col        for i in range(row):            self.board.append( ["O"] * col )          def print_board(self):        space = (self.col * 2 - 8)/2        print "-" * space + " Board " + "-" * space        print "-" * self.col * 2        for r in self.board:            print " ".join(r)        print "-" * self.col * 2class Game(object):    loop_time = 4    def __init__(self,row,col):        self.row = row        self.col =col        self.actual_row = -1        self.actual_col = -1        self.guess_row = -100        self.guess_col = -100        self.main_loop()        def random_row(self):        return randint(1, self.row)        def random_col(self):        return randint(1, self.col)        def set_ship(self):        """the battle ship is here:"""        self.actual_col = self.random_col()        self.actual_row = self.random_row()        print self.actual_row        print self.actual_col        self.my_board_actual.board[self.actual_row - 1][self.actual_col - 1] = "S"        def get_input_from_player(self):        print "Please select where to hit on the board:"        self.guess_row = int(raw_input("To hit Row:")) - 1        self.guess_col = int(raw_input("To hit Col:")) - 1        def check_if_hit(self):        if self.my_board_actual.board[self.guess_row][self.guess_col] == "S":            print "Congratulations! You sunk my battle ship!"            self.my_board.board[self.guess_row][self.guess_col] = "S"            return True        else:            if self.guess_row < 0 or self.guess_col < 0 or self.guess_row > self.row or self.guess_col > self.col:                print "Ooops, that's not even in the ocean."            elif self.my_board.board[self.guess_row][self.guess_col] == "X":                print "You guessed that one already."                       else:                print "You missed my battleship!"                self.my_board.board[self.guess_row][self.guess_col] = "X"            return False                        def main_loop(self):               print "Game Start: Let's play Battleship!"        turn = 1        #print "Turn", turn        my_board = Board(self.row,self.col)        my_board_actual = Board(self.row,self.col)        self.my_board = my_board        self.my_board_actual = my_board_actual        my_board.print_board()                self.set_ship()                result = False                while turn < self.loop_time + 1:            print "\nTurn", turn            self.get_input_from_player()            result = self.check_if_hit()            my_board.print_board()            if result:                turn = self.loop_time + 2            else:                turn += 1        else:            if not result:                print "Game Over"                                                                        my_game = Game(5,5)

 


Battle ship

Yes !!
Both of them can be combined with the speed of Returning blood per second !!! It can be combined with the skill of Returning blood to the head of the ship a of the rest team !!!

How to get the Battleship Chess game

Well, this is to guess the location of the opposite ship. If you hit it, there would be a cross.
For example, if a small ship has two grids and you hit them all, the small ship will be destroyed,
Only one grid is displayed as a cross .--

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.