Sunday, April 19, 2020

Simple Battleship game using Python


To run this code, one must have NumPy and Pandas installed with Python 

the code goes here:


# from numpy import *from random import *from pandas import *import string
from IPython.core.display import display, HTML


flag = 0loc_hist = []keys = list('0123456789')col_dic = {keys[i]:'○' for i in range(10)}comp_dic = {keys[i]:'○' for i in range(10)}battle_area = DataFrame(col_dic, index=list('ABCDEFGHIJK'), columns=list('0123456789'))unseen1 = battle_area.copy()attack_area = DataFrame(comp_dic, index=list('ABCDEFGHIJK'), columns=list('0123456789'))unseen2 = attack_area.copy()# print(battle_area)print('''       You can set the boat either vertical or horizontal                     Your points should be like: a2 b2''')


def fill(lst, ground):    globals()    for i in lst:        ground.at[i[0:1],i[1:]] = '■'    # print(battle_area)    return
def check(n,lst):    flag = 0    a = lst[0][:1]    b = lst[0][1:]    #print(a,b)    if len(lst) != n:        #print(flag)        return flag
    else:        for i in range(1,n):            if lst[i][:1] == a and ord(lst[i][1:]) - ord(lst[i-1][1:]) == 1:                flag = 1            elif lst[i][1:] == b and ord(lst[i][:1]) - ord(lst[i-1][:1]) == 1:                flag =1            else:                flag = 0        #print(flag)        return flag

def const_num(n):    globals()    ch = choice(string.ascii_letters).upper()    num = str(randint(0, 10))    if ch <= 'K':        lst = [ch+num]        if ord('K') - ord(ch) >= n:            for i in range(1,n):                lst.append(chr(ord(ch) + i) + num)        else:            for i in range(1,n):                lst.append(chr(ord(ch) - i) + num)    else:        return const_num(n)    return lst

def const_alpha(n):    globals()    ch = choice(string.ascii_letters).upper()    num = str(randint(0, 10))    if ch <= 'K':        lst = [ch + num]        if 9 - int(num) >= n:            for i in range(1,n):                lst.append(ch + str(int(num)+1))        else:            for i in range(n):                lst.append(ch + str(int(num)-1))        return lst
    else:        return const_alpha(n)
ran = [const_alpha, const_num]fill( choice(ran)(2),unseen1)# print(unseen1)
def display_side_by_side(dfs:list, captions:list):    """Display tables side by side to save vertical space    Input:        dfs: list of pandas.DataFrame        captions: list of table captions    """    output = ""    combined = dict(zip(captions, dfs))    for caption, df in combined.items():        output += df.style.set_table_attributes("style='display:inline'").set_caption(caption)._repr_html_()        output += "\xa0\xa0\xa0"    display(HTML(output))
display_side_by_side([battle_area,attack_area],['battle', 'attack'])
def boat_pos():    patrol_boat = input('Enter 2 consecutive points from the board with space in between: ').upper().split(' ')    if check(2,patrol_boat) == 0:        print('wrong input type, pls try again: ')        return boat_pos()    else:        fill(patrol_boat,battle_area)
def usr_attack():    point = input('At which point are you going to attack? ').upper()    # os.system('cls')    if len(point) == 2 and point[:1]<='K':        if attack_area.at[point[:1], point[1:]] == '▣' or attack_area.at[point[:1], point[1:]] == '◉':            print('Already attacked!!!')        elif unseen1.at[point[:1],point[1:]]=='■':            attack_area.at[point[:1], point[1:]] = '▣'            unseen1.at[point[:1], point[1:]] = '▣'            print('attack area : ', attack_area)        else:            attack_area.at[point[:1], point[1:]] = '◉'            print('attack area : ', attack_area)    else:        print(" Pls enter b/w A0 to K9")        return usr_attack()
def attack_gen():    globals()    a = choice(string.ascii_letters).upper()    b = str(randint(0,10))    if a <= 'K':        if battle_area.at[a, b] == '▣' or battle_area.at[a, b] == '◉':            return attack_gen()        elif battle_area.at[a,b]=='■':            battle_area.at[a,b] = '▣'            # loc_hist.append(a+b)            print('battle area : ', battle_area)            return battle_area
        else:            battle_area.at[a,b] = '◉'            print('battle area : ', battle_area)            return battle_area
    else:        attack_gen()

boat_pos()
def itt(j):    globals()    if unseen1[str(j)].str.contains('■').any():        usr_attack()        print('attack area: ', attack_gen())        return itt(j)    elif j <= 8:        return itt(j+1)    else:        if not unseen1[str(j)].str.contains('■').any():            print('Congratulations Captain!!! You won the war 🤗')            return attack_area
        else:            print('Sorry Captain, you have lost the war, try next time 😞!!')print(itt(0))

Create Towers of Hanoi with python using stacks

Source: GeeksforGeeks
class Node:    def __init__(self, value, next_node=None):        self.value = value        self.next_node = next_node
    def get_value(self):        return self.value

    def get_next_node(self):        return self.next_node

    def set_next_node(self, next_node):        self.next_node = next_node

class Stack:    def __init__(self, name):        self.size = 0        self.top_item = None        self.limit = 1000        self.name = name
    def push(self, value):        if self.has_space():            item = Node(value)            item.set_next_node(self.top_item)            self.top_item = item
            self.size += 1        else:            print("No more room!")
    def pop(self):        if self.size > 0:            item_to_remove = self.top_item
            self.top_item = item_to_remove.get_next_node()            self.size -= 1            return item_to_remove.get_value()        print("This stack is totally empty.")
    def peek(self):        if self.size > 0:            return self.top_item.get_value()        print("Nothing to see here!")
    def has_space(self):        return self.limit > self.size

    def is_empty(self):        return self.size == 0
    def get_size(self):        return self.size

    def get_name(self):        return self.name

    def print_items(self):        pointer = self.top_item
        print_list = []        while (pointer):            print_list.append(pointer.get_value())            pointer = pointer.get_next_node()        print_list.reverse()        print("{0} Stack: {1}".format(self.get_name(), print_list))


print("\nLet's play Towers of Hanoi!!")
# Create the Stacksstacks = []left_stack = Stack('Left')middle_stack = Stack('Middle')right_stack = Stack('Right')stacks.append(left_stack)stacks.append(middle_stack)stacks.append(right_stack)
# Set up the Gamenum_disks = int(input('\nHow many disks do you want to play with?\n'))while num_disks < 3:    num_disks = int(input('\nEnter a number greater than or equal to 3\n'))for i in range(num_disks, 0, -1):    left_stack.push(i)num_optimal_moves = 2 ** num_disks - 1print("\nThe fastest you can solve this game is in {0} moves".format(num_optimal_moves))

# Get User Inputdef get_input():    choices = [stack.get_name()[0] for stack in stacks]    while True:        for i in range(len(stacks)):            name = stacks[i].get_name()            letter = choices[i]            print('Enter {0} for {1}'.format(letter, name))        user_input = input()        if user_input in choices:            for i in range(len(stacks)):                if user_input == choices[i]:                    return stacks[i]

# Play the Game
num_user_moves = 0while (right_stack.get_size() != num_disks):    print("\n\n\n...Current Stacks...")    for i in stacks:        i.print_items()    while True:        print("\nWhich stack do you want to move from?\n")        from_stack = get_input()        if len(from_stack) == 0:            print('\n\nInvalid Move. Try Again')    print("\nWhich stack do you want to move to?\n")    to_stack = get_input
    elif len(to_stack) == 0 or to_stack.peak() > from_stack.peak():    disk = from_stack.pop()    to_stack.push(disk)    num_user_moves += 1    breakelse:    print('\n\nInvalid Move. Try Again')print("\n\nYou completed the game in {0} moves, and the optimal number of moves is {1}".format(num_user_moves,                                                                                               num_optimal_moves))