Wednesday, 10 June 2015

Guess The Number Game in Python Posted By: Falak Khan

Code for Guess The Number Game in Python in Python

import random
value=random.randint(1,20)
won=False
wtp=True
while(wtp):
    print"hi ! I m thinking of a no.,can you guess it?"for tri in range(3):
        guess=int(raw_input("Enter a no: "))
        if(guess==value):
            won=True
            print"Congrats"break
        elif(tri<2):
                print"sorry! try again"
                print 3-tri-1,"tries left"if(won==False and tri==2):
            print"sorry ! you lost"
            print "NO. was:", value
    choice=str(raw_input("would you like to play again y/n "))
    if(choice=="n"):
        wtp=False
                         
        

Hangman Game

Code for Hangman Game in Python

import random
######################################################


def get_any(category):
    return random.sample(category, 1)[0]


#################################################################333


def list_of_alphabets(already_tried):
    alphabets=[chr(i) for i in range(ord('a'), ord('z')+1)]
    d = list(set(alphabets) - set(already_tried))
    d.sort()
    print"list of alphabets is :", d

def get_entities(fn):
    return open('animals.txt').read().split()



########################################################

## RETURNS GUESSED WORD ##

def guess(word,masked_word,c):
    to_return=""for i in range(len(word)):
        if(word[i]==c):
            to_return=to_return+c
        else:
            to_return=to_return+masked_word[i]
    return to_return


##########################################################


def mask(word):
    return"-" * len(word)



########################################################3



## INPUT GUESS LETTER FROM USER ##

def get_guess(already_tried):
   
    c=str(raw_input("Enter your next guess:  "))
  
    while(c in already_tried):
        print "you have already entered this, Enter another one "
        c=str(raw_input("Enter your next guess:  "))
    return c
    
        


########################################################



## INTERFACE ##

def draw_interface(missed,mask_word,guessed, already_tried):
    draw_hangman(missed)
    print "Guessed: ",guessed
    print mask_word
    list_of_alphabets(already_tried)
        

###########################################################

   
## HANGMAN IMAGES ##

def draw_hangman(tri):
    hangman=["""
              ############
              ##         !
              ##          
              ##          
              ##          
              ##          
              ##          
              ##          
            =======""","""

              ############
              ##         ! 
              ##       (@_@)
              ##
              ##
              ##
              ##    
              ##
              ##
           ========""","""

              ############
              ##         ! 
              ##       (@_@)
              ##        ( )
              ##       
              ##
              ##     
              ##
              ##
           ========""","""

            ############
            ##         ! 
            ##       (@_@)
            ##       <( )>
            ##       
            ##   
            ##    
            ##
            ##
         ========""","""

           ############
           ##         ! 
           ##       (~_~)
           ##       <( )>
           ##       _/ \_
           ##        
           ##     HANGED !!!
           ## 
           ##
        ========"""]
    
    print hangman[tri]
    print 4-tri,"tries left"


###################################################################
        
## PROGRAM BODY ##
animals=['elephant',"monkey","owl","peacock","Tigress"]
fruits=["apple","orange","peach","banana","watermelon"]
country=["pakistan","england","iran","america","australia"]
city=["islamabad","sydney","london","carmel","lahore"]
wtp=True 
while(wtp):
    already_tried = []
    print "choose a category : "
    category=str(raw_input(" Animal=a , Fruit=f , Country=c , city=t \n"))
    if(category=="a"):
        word = get_any(animals)
    elif(category=="f"):
        word=get_any(fruits)
    elif(category=="c"):
        word="pakistan"else:
        word="london"
    missed=0
    mask_word=mask(word)
    guessed=[ ]
    while(missed<=4):
        draw_interface(missed,mask_word,guessed, already_tried)
        if(missed!=4):
            c=get_guess(already_tried)
            already_tried.append(c)
        if(word.count(c)>0):
            mask_word=guess(word,mask_word,c)
        else:
            guessed.append(c)
            missed=missed+1
        if(word==mask_word):
            print "     YOU WON !!!"break
        print word
    choice=str(raw_input("would you like to play again y/n :"))
    while(choice!='y' and choice!='n'):
        choice=str(raw_input("PLEASE ENTER 'y/n' ONLY :  "))
    if(choice=="n"):
        wtp=False

#####################################################################
        

    

drawing house line by line

Code for drawing house line by line in C++ Programming

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void dda( float xa, float ya, float xb, float yb);


void main()
{
clrscr();

int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
for(int i=16;i>=14; i--)
{

delay(900);
setcolor(i);
line(216,110,410,110);

}
for(int i1=16;i1>=14; i1--)
{

delay(900);
setcolor(i1);
line(216,110,216,250);

}

for(int i2=16;i2>=14; i2--)
{

delay(900);
setcolor(i2);
line(216,250,410,250);
}
for(int i3=16;i3>=14; i3--)
{

delay(900);
setcolor(i3);
line(410,110,410,250);
}
for(int i4=16;i4>=14; i4--)
{

delay(900);
setcolor(i4);
line(216,120,410,120);
               }for(int i5=16;i5>=14; i5--)
{

delay(900);
setcolor(i5);
line(216,110,313,10);   }
for(int i6=16;i6>=14; i6--)
{

delay(900);
setcolor(i6);
line(410,110,313,10);
}for(int i7=16;i7>=14; i7--)
{

delay(900);
setcolor(i7);
line(250,250,250,150);   }
for(int i8=16;i8>=14; i8--)
{

delay(900);
setcolor(i8);
line(300,250,300,150);
}for(int i9=16;i9>=14; i9--)
{

delay(900);
setcolor(i9);
line(250,150,300,150);
 }for(int i10=16;i10>=14; i10--)
{

delay(900);
setcolor(i10);

line(320,150,320,200);   }
for(int i11=16;i11>=14; i11--)
{

delay(900);
setcolor(i11);
line(390,150,390,200);  }for(int i111=16;i111>=14; i111--)
{

delay(900);
setcolor(i111);
line(320,150,390,200);   }
for(int i12=16;i12>=14; i12--)
{

delay(900);
setcolor(i12);
line(320,200,390,150);
}for(int i13=16;i13>=14; i13--)
{

delay(900);
setcolor(i13);
line(320,150,390,150);
}for(int i14=16;i14>=14; i14--)
{

delay(900);
setcolor(i14);
line(320,200,390,200);  }
for(int i15=16;i15>=14; i15--)
{

delay(900);
setcolor(i15);
circle(313,60,10);       }
for(int i16=16;i16>=14; i16--)
{

delay(900);
setcolor(i16);

line(410,120,480,90);
}
for(int i17=16;i17>=14; i17--)
{

delay(900);
setcolor(i17);
line(313,10,480,90);
}
for(int i18=16;i18>=14; i18--)
{
delay(900);
setcolor(i18);
line(410,250,480,220);
}
for(int i19=16;i19>=14; i19--)
{

delay(900);
setcolor(i19);
line(480,90,480,220);     }for(int i20=16;i20>=14; i20--)
{

delay(900);
setcolor(i20);

line(410,110,468,87);      }for(int i21=16;i21>=14; i21--)
{

delay(900);
setcolor(i21);

line(436,150,460,140);      }for(int i22=16;i22>=14; i22--)
{

delay(900);
setcolor(i22);
line(437,200,461,190);       }for(int i23=16;i23>=14; i23--)
{

delay(900);
setcolor(i23);
line(437,150,437,200);    }for(int i24=16;i24>=14; i24--)
{

delay(900);
setcolor(i24);
line(460,140,460,190);     }for(int i25=16;i25>=14; i25--)
{

delay(900);
setcolor(i25);

line(436,160,460,150);
}for(int i26=16;i26>=14; i26--)
{

delay(900);
setcolor(i26);
line(436,190,460,180);
}for(int i27=16;i27>=14; i27--)
{

delay(900);
setcolor(i27);
circle(448,170,12);
}for(int i28=16;i28>=14; i28--)
{

delay(900);
setcolor(i28);

arc(185,275,300,20,70);
}for(int i29=16;i29>=14; i29--)
{

delay(900);
setcolor(i29);
arc(235,275,300,20,70);
 }


getch();
}

file creation in masm

.model small
.stack 100h
.data
msg1 db 10,13,'enter the file name$'
msg2 db 10,13,'file is created$'
fn db 50 dup(?)
.code
print macro arg
mov dx,offset arg
mov ah,09h
int 21h
endm
.startup
print msg1
mov di,offset fn
read:
mov ah,01h
int 21h
cmp al,13
je crte
mov [di],al
inc di
jmp read
crte:
mov cx,0
mov dx,offset fn
mov ah,3ch
int 21h
print msg2
.exit
end

phonebook

Project on phonebook

 
 .Model small
.Data
    msg1 db 10, 13, ' -----------------------------------------------------------', 10, 13
         db 10, 13, '                            PHONEBOOK' ,10,13   
         db 10, 13, ' -----------------------------------------------------------', 10, 13
         db ' $'
    msg2 db 10, 13, 10, 13, ' Enter Name: $'
    msg3 db 10, 13, 10, 13, ' Enter Contact Name : $'
    msg15 db 10, 13, 10, 13, ' Access denied!$'
    msg17 db 10, 13, 10, 13, ' Contact Deleted.$'
    msg24 db '*.', 0
    msg14 db 10, 13, 10, 13, 'No Contacts!$',10,13,10,13
    msg16 db 10, 13, 10, 13, 'Contact created.$'
    msg35 db 10, 13, 10, 13, ' Sorry Contact Not Match    !!$'
    msg36 db 10, 13, 10, 13, ' C.Call  E.Edit  D.Delete $'
    msg37 db 10, 13, 'Calling...'
          db 10, 13, 'Press any key to End Call$'
    msg26 db 10, 13, '$'
    msg27 db 10, 13, 'Press any key to continue$',10,13
    msg28 db 10, 13, 10, 13, ' Enter Number (Press Esc to stop):$' 
    msg31 db '$'
    msg6 db 10, 13, 10, 13, '  Number : $' 
    msg21 db 10, 13, 10, 13, ' Sorry Contact Not Match    !!$'
    msg30 db 10, 13, 10, 13, ' Search Name : $'

    buffer1 db 80, 0, 80 dup(0)
    buffer2 db 80, 0, 80 dup(0)
    msg32 db 10, 13, 'A.Add  S.Search $' ,10 , 13    
.Code
start :    
    mov ax, @Data
    mov ds, ax
begin :
    mov ax, 3h        ; clear screen by
    int 10h                   ; changing video mode
    call disp2
option:
    lea dx,msg32
    call disp1

    call readch               ; read choice
    and al, 0fh
    cmp al, 1                 ; branch to appropriate
    jnz a                     ; module depending on
    jmp cr_file               ; the choice
a :    cmp al, 3
    jz vw_file
    mov ax, 4c00h             ; exit program
    int 21h



cr_file:lea dx, msg2              ; module for creating a file
    call disp1
    call read1                ; read name of file to be
    lea dx, buffer1[2]        ; created
    mov cx, 0
    mov ah, 3ch               ; create the file
    int 21h
    push ax                   ; push file handle onto stack.
    lea dx, msg28             ; ask if data is to
    call disp1                ; be input
    call readch               ; read choice
    and al, 0fh
    cmp al, 9                 ; if choice = 'Y' or 'y'
    jnz no
    lea dx, msg29
    call disp1
    pop bx                    ; retrieve file handle from stack.
    mov buffer1[1], 0
write :    call readch               ; read data character by character.
    mov buffer1[0], al
    cmp buffer1[0], 27        ; check if character is 'Esc'(stop).
    jz no
    cmp buffer1[0], 0dh
    jne neol
    lea dx, msg26
    call disp1
    mov si, dx
    mov byte ptr ds:[si + 2], 0
    mov cx, 3
    jmp com
neol :    mov cx, 1
    lea dx, buffer1[0]
com :    mov ah, 40h               ; write to the file
    int 21h
    mov byte ptr ds:[si + 2], '$'
    jmp write
no :    lea dx, msg16             ; creation successful
    call disp1
    call readch
    jmp begin   



vw_file:lea dx, msg30             ; module to view the
    call disp1        ; contents of a file
    call read1                ; read name of file
    lea dx, msg6
    call disp1
    call disp1
    lea dx, buffer1[2]        ; open the file
    mov ax, 3d02h
    int 21h
    mov buffer2[0], 0
    cmp ax, 2                 ; error if file not found
    jnz v_err
    lea dx, msg14
    call disp1
    jmp endv
v_err : cmp ax, 3                 ; error if path not found
    jnz cont2
    ;jnz push
    lea dx, msg21
    call disp1
    jmp endv
cont2 :    mov bx, ax
    push ax
    mov cx, 1
    lea dx, buffer1
    mov ah, 3fh               ; read the file
    int 21h
    cmp ax, 0                 ; stop if end-of-file
        jz push
        jnz show2
show2 :     mov buffer1[1], '$'
    lea dx, buffer1
    call disp1
    pop ax
    jmp cont2
    cmp buffer1[0], 0dh
       ; jnz show
    inc buffer2[0]
    cmp buffer2[0], 23        ; check if end of page
    jnz show1
    ;jnz push
    lea dx, msg26
    call disp1
    call readch
    mov buffer2[0], 0
    lea dx, msg26
    call disp1
push:   mov ah,02
        mov al,0
    mov cx,0
    mov dx,10
    int 21
    ;int 21h
    ;push ax                   ; push file handle onto stack.
    lea dx, msg28             ; ask if data is to
    call disp1                ; be input
    call readch               ; read choice
    and al, 0fh
    cmp al, 9                 ; if choice = 'Y' or 'y'
    jnz no1
    lea dx, msg29
    call disp1
    pop bx                    ; retrieve file handle from stack.
    mov buffer1[1], 0
write1 :    call readch               ; read data character by character.
    mov buffer1[0], al
    cmp buffer1[0], 27        ; check if character is 'Esc'(stop).
    jz no1
    cmp buffer1[0], 0dh
    jne neol1
    lea dx, msg26
    call disp1
    mov si, dx
    mov byte ptr ds:[si + 2], 0
    mov cx, 3
    jmp com1
neol1 :    mov cx, 1
    lea dx, buffer1[0]
com1:    mov ah, 40h               ; write to the file
    int 21h
    mov byte ptr ds:[si + 2], '$'
    jmp write1
no1 :    lea dx, msg16             ; creation successful
    call disp1
    call readch
    jmp begin
    ;jmp cr_file
show1 :     mov buffer1[1], '$'
    lea dx, buffer1
    call disp1
    pop ax
    jmp cont2
endv :  call readch
    jmp begin
cm_dir :lea dx, msg9              ; module for removing directory
    call disp1
    call read1                ; read name of directory to
    lea dx, buffer1[2]        ; be removed
    mov ah, 3ah               ; remove directory
    int 21h
    cmp ax, 3                 ; error if path not found
    jnz err61
    lea dx, msg21
    call disp1
    jmp endg1
err61 :  cmp ax, 5                 ; error if access denied
    jnz dir1
    lea dx, msg15
    call disp1
    jmp endg
done20 : lea dx, msg20             ; deletion successful
    call disp1
endg1 :    call readch
        jmp begin


dir1:   mov ax, 3h                ; module for displaying
    int 10h                   ; contents of directory
    lea dx, msg24
    mov cx, 0
    mov ah, 4eh               ; Get first file
    int 21h                   ; in directory
    cmp ax, 18                ; Check if no files
    jnz list1                  ; in directory
    lea dx, msg14             ; Display message
    call disp1                ; 'File not found'
    call readch
    jmp begin
list1 :    mov ah, 2fh               ; Get dta address
    int 21h
    mov byte ptr es:[bx + 42], 0
    add bx, 1eh
    mov buffer1[0], 0
char1 :    mov dl, byte ptr es:[bx]  ; Get character of
    inc bx                    ; filename from DTA
    inc buffer1[0]
    cmp dl, '.'               ; Check if extension
    jnz cont30                 ; is starting
cont40 :    lea dx, msg31
    call disp1
    inc buffer1[0]
    cmp buffer1[0], 0bh       ; Check for end of filename
    jne cont40                 ; buffer - 13 characters
    jmp char1
cont30 :    mov ah, 02h               ; Display character
    int 21h                   ; of filename
    cmp dl, 0                 ; Check for end
    jne char1                  ; of file name
    lea dx, msg26
    call disp1
    inc cx
    cmp cx, 23                ; Check for end of page
    jne cont10
    lea dx, msg27
    call disp1
    call readch
    mov cx, 0
    lea dx, msg26
    call disp1
cont10 :    mov ah, 4fh               ; Get next file
    int 21h
        jnc list1
    lea dx, msg27
    call disp1
    call readch
    ;jmp begin
jmp dl_file1
cp_file:lea dx, msg6              ; module for copying a file
    call disp1                ; read name of file to
    call read1                ; to be copied
    mov cx, bx
    lea dx, msg7
    call disp1
    call read2                ; read path of destination
    mov buffer2[bx], '\'      ; directory
    inc bx
    mov ax, 2
cp :    mov si, ax                ; concatenating path and filename
    mov dl, buffer1[si]
    mov buffer2[bx], dl
    inc bx
    inc al
    cmp cx, ax
    jne cp
    mov cx, 0
    lea dx, buffer2[2]        ; create the file in
    mov ah, 3ch               ; destination directory
    int 21h
    cmp ax, 3                 ; Display error message
    jnz cont                  ; if path not found
    lea dx, msg21
    call disp1
    jmp cp_file               ; on error read data again
cont :    push ax
    lea dx, buffer1[2]        ; open source file
    mov ax, 3d00h
    int 21h
    push ax
rd :    pop bx
    lea dx, buffer1
    mov cx, 80h               ; read source file
    mov ah, 3fh
    int 21h
    cmp ax, 0                 ; check if entire file
    jz fin                    ; has been read
    mov cx, bx
    pop bx
    push bx
    push cx
    mov cx, ax
    lea dx, buffer1           ; write into new file to
    mov ah, 40h               ; complete copy task
    int 21h
    jmp rd                    ; read file further
fin :    lea dx, msg25             ; copy successful
    call disp1
    call readch
    jmp begin
        disp1 proc                ; module for display of
      mov ah, 09h             ; a string on screen
      int 21h
      ret
    disp1 endp

    readch proc               ; module for reading a
      mov ah, 01h             ; character from keyboard
      int 21h
      ret
    readch endp

    read1 proc                ; module for reading
      mov [buffer1], 80       ; first string
      lea dx, buffer1
      mov ah, 0ah             ; read string from keyboard
      int 21h
      mov bl, buffer1[1]
      mov bh, 0
      add bx, 2
      mov buffer1[bx], 0      ; ASCIIZ string, so
      ret                     ; terminate with 0
    read1 endp

    read2 proc                ; module for reading
      lea dx, buffer2         ; second string
      mov ah, 0ah
      int 21h
      mov bl, buffer2[1]
      mov bh, 0
      add bx, 2
      mov buffer2[bx], 0
      ret
    read2 endp
    end start

Repository Pattern Example Sourcecode C#

Repository Pattern Example Sourcecode C#

 

This article contains demo project for how to implement repository pattern in c#.
Please click on download button link to download complete sourcecode for this project.