Ejemplo n.º 1
0
	def attack_board(self,guess_col, guess_row,ships, mark, hum_com): #searches board at x,y for ship in ships, deletes ship if found
			guess_col -=1
			guess_row-=1
			if (guess_row in range(0,self.row)) and guess_col in range(0,self.col):
				if (self.board[guess_row][guess_col] == "O"): #if no shots on this spot yet, continue		
					if ships.checkship(guess_col, guess_row, "kill") == True:
						self.board[guess_row][guess_col] = mark
						if mark == "H":
							print "Hit!"
						else:
							return
					else:		
						print "miss"
						count = self.is_close(guess_col, guess_row,ships)
						if count == "0":
							self.board[guess_row][guess_col] = "X" #instead of 0
						else:	#if shot is touching a ship
							self.board[guess_row][guess_col] = str(count) #save touch count
				else: #if self.board[guess_row][guess_col] != "O":
					if hum_com == "human":
						print "You can't hit the same place twice."
						self.attack_board(numbers_only(raw_input("new col?")), numbers_only(raw_input("new row?")), ships, mark, hum_com)
					else: 
						board.comp_attack(self,ships, mark)
			else:	#if outside x,y range
				print "You've missed the board."
				self.attack_board(numbers_only(raw_input("new col?")), numbers_only(raw_input("new row?")), ships, mark, hum_com)	
Ejemplo n.º 2
0
def tic_tac():
    hum_com1 = raw_input("player one human or computer?\n")
    player_1 = raw_input("name of player one?\n")
    hum_com2 = raw_input("player two human or computer?\n")
    player_2 = raw_input("name of player two?\n")
    players = [player_1, player_2]  # player ref
    hum_coms = [hum_com1, hum_com2]  # bot ref
    xy = ["X", "P"]  # symbol ref
    table = Board(player_1, "fill")
    table.size_it(3, 3)
    open_spots = Shipcl(9, table, "mac", "fill")
    for i in range(0, 9):  # 9 turns
        player = int(ceil((i) % 2))  # if even ->0, odd->1
        print "\n%s, you're turn. " % (players[player])
        table.print_board()
        if hum_coms[player] == "human":
            col = numbers_only(raw_input("col?"))
            row = numbers_only(raw_input("row?"))
            table.attack_board(col, row, open_spots, xy[player], "human")
        else:
            coords = table.comp_attack(open_spots, xy[player])
            col = coords[0]
            row = coords[1]
        if check_win(table, xy[player], col, row) == True:
            print "\n%s wins!" % (players[player])
            break  # winner found

    else:
        print "\nDraw!"  # if check_win doesnt break loop
    table.print_board()
    print "Game over"
Ejemplo n.º 3
0
	def cre_ship(self,grid,humcom):	#creates self.ship_amt many ships
		for i in range(int(self.ship_amt)):
			if i > (grid.row*grid.col): #If ship count exceeds grid #need from board class
				print "The board is full."
				break
			if humcom == "human":
				ships_col = numbers_only(raw_input("pick ship "+ str(i+1)+"'s loc: \ncol in range 1 to " + str(grid.col) +" ? ")) -1
				ships_row = numbers_only(raw_input("row in range 1 to " + str(grid.row) + " ? "))-1
				while self.checkship(ships_col, ships_row, "search") == True &(ships_col in range(grid.col) and ships_row in range(grid.row)): 
					ships_col = numbers_only(raw_input("Invalid spot, try again. col: "))-1
					ships_row = numbers_only(raw_input("try again. row: "))-1
			else:
				ships_col = (randint(0,grid.col-1))
				ships_row = (randint(0,grid.row-1))
				while self.checkship(ships_col, ships_row,"search") == True: #generate new coordinates while spot is already filled
					ships_col = (randint(0,grid.col-1))
					ships_row =(randint(0,grid.row-1))
			self.ships[i] = (ships_col,ships_row)  #save new ships location at index i