How to make shell script.

Hello!
Very long time to no see you.
How are these days treats you?
I hope you are enjoying your life.
Today, I’ll introduce “shell script” using “bash shell”
“bash shell” is the most popular shell.
You can use it on Linux/Mac and windows(if you install free bash emulating software such as MSYS.

Simple bash shell script it only prints messages

# charactor "sharp"(or it pronounced "hash") represents source-code comment
# it is confortable to take memo on your own source-code
echo "Long time no see you!!"
echo "I'm very happy to came back here."

You can excecute it by putting these commands.

NAGATO:~/shell$ chmod +x first.sh
NAGATO:~/shell$ bash first.sh

“chmod” command changes the specified file’s 3 privileges of users.
The privileges are “eXecution”,”Read” and “Write”.
There are changed by the command argument(which are follow first command word “chmod”) such as “+x”(it means plus the privilege for excecute the file.
Then, “chmod +x first.sh” means add user’s privilege for executing file “first.sh”.
“bash” means execute bash files and you can execute bash file(which have file extension “.sh”) by it.

The result is below.

Long time no see you!!
I'm very happy to came back here.

It’s so easy to write bash file isn’t it?
See you next article!!

Advertisements

Programming for Nepali Traditional Game “Bagh Chaal” 2nd

Hello. This is second article about programming for Nepali Traditional Game “Bagh Chaal”.
In first article,I introduced a program which treats openning phase of “bagh chaal”.
This article is I’ll show the way to imprement all functionality for play the game.
I uploaded the program’s source code here.
You can play human vs human,human vs AI,AI vs AI match in this program.
Below is AI vs AI match and result.

NAGATO:~/cpp$ ./a.exe
Bagh Chaal game start!
Is Goat AI player(y or n)?
y
Is Tiger AI player(y or n)?
y
  a b c d e
5 T _ _ _ T
4 _ _ _ _ _
3 _ _ _ _ _
2 _ _ _ _ _
1 T _ _ _ T
remaining goat: 20
eaten goat: 0
from=    to=    del=
from=e5  to=e5  del=e5
from=    to=    del=
from=c5  to=c5  del=c5
from=    to=    del=
from=a1  to=a1  del=a1
from=    to=    del=
from=a5  to=a5  del=a5
from=    to=    del=
from=b5  to=b5  del=b5
from=    to=    del=
from=e1  to=e1  del=e1
from=    to=    del=
from=a2  to=a2  del=a2
from=    to=    del=
from=c4  to=c4  del=c4
from=    to=    del=
from=b4  to=b4  del=b4
from=    to=    del=
from=c4  to=c4  del=c4
from=    to=    del=
from=b3  to=b3  del=b3
from=    to=    del=
from=d1  to=d1  del=d1
from=    to=    del=
from=a5  to=a5  del=a5
Tiger win!!
  a b c d e
5 _ G _ G _
4 G _ G _ T
3 _ _ T T G
2 _ _ _ G T
1 _ G G _ _
remaining goat: 7
eaten goat: 5
  a b c d e
5 _ G _ G _
4 G _ G _ T
3 _ _ T T G
2 _ _ _ G T
1 _ G G G _
remaining goat: 6
eaten goat: 5
previous move: from=    to=    del=
Tiger win!!
Do you play Bagh Chaal more(y or n)?

I decided represent any players move by the struct(struct bands multiple data in one place and enables you to manage member of struct. struct is important feature of C language.And it appears in many other languages.)

// data structure of player's move
typedef struct {int from; int to; int del;} move;

Above declaration says that it defines struct which name is “move” and the struct have three elements “from”,”to”,and “del”.
I decided to make this struct for means coding easier.
In “Bagh Chaal” program, You have to manage board by a same way to playing game.
Then, you have to write a function of moving or putting piece on the game board.

void do_move(move m){
  if(m.from)board[m.from]=EMPTY;
  else remaining_goat--;
  if(m.to)board[m.to]=turn;
  if(m.del){
    board[m.del]=EMPTY;
    eaten_goat++;
  }
  turn=opposite_turn(turn);
}

In this function, I used some black magic for reduce an amount of data.
It means that if the value of “from” or “del” is 0, it represents
“no piece moved from any position”,”no piece is deleted” else it represents the position of moved piece’s original position or position of deleted goat.
This function also modifies number of remaining goats which player can put on the board and number of goat which eaten by tiger.
These modified values are used in other functions.
Even if you write the function of player’s move, Your program don’t notice the end of the game.

int is_tiger_win(){return eaten_goat>=5;}
int is_goat_win(){
  move ms[64];
  return !all_moves(ms,TIGER);
}
int examine_game_end(){
  if(is_goat_win()){print_line("Goat win!!"); return TRUE;}
  if(is_tiger_win()){print_line("Tiger win!!"); return TRUE;}
  return FALSE;
}

is_tiger_win() function is easy. Because, tigers win is determined by the number of eaten goat.
But judging goat side players winning is little difficlut.
You have to examine wheather all neighboring position is occupied by goats and no goat encountering on threat to eaten by tigers.

int all_moves(move* ms,int animal){
  int pos,i=0;
  const int *p;
  // put goat
  if(animal==GOAT&&remaining_goat!=0){
    for(pos=0;pos<64;pos++)
      if(board[pos]==EMPTY){
        ms[i].from=0;
        ms[i].to=pos;
        ms[i].del=0;
        i++;
      }
  }else{ // move goat or tiger
    for(pos=0;pos<64;pos++){
      if(board[pos]!=TIGER)continue;
      p=diagonal_move(pos)? DELTA8 : DELTA4;
      for(;*p;p++){
        if(board[pos+*p]==EMPTY){
          ms[i].from=pos;
          ms[i].to=pos+*p;
          ms[i].del=0;
          i++;
        }else if(animal==TIGER &&
                 board[pos+*p]==GOAT &&
                 board[pos+*p*2]==EMPTY){
          ms[i].from=pos;
          ms[i].to=pos+*p*2;
          ms[i].del=pos+*p;
          i++;
        }
      }
    }
  }
  ms[i].from=0;
  ms[i].to=0;
  ms[i].del=0;
  return i;
}

This function generates player’s all moves.
I think there are 3 type of condition which decide players moves.
If Goat side player have goats which is puttable on game board, player puts a goat.
Otherwise, he moves his goat.
And Tiger side player always moves his tiger.
Above function unify move generation of all patterns of players condtion.
This functions return value is number of moves.
Data of all generated moves is stored in first argument “ms”.
It is array of move struct.
You have to mind that all moves in “ms” stored correctory.
“from” and “del” must store 0 when no original position of piece move exists or no deleted piece exits.
Since this program use same memory space for storing moves,you have to make sure you cleared previous move data.

Oh.. this article growing long..

I realized that this program contains too many technics and I can’t express every technic concisely.
Then you don’t need to understand everything.
It is okay, if you got anything from this article.
Next time, I’ll write for any more simple theme relating to IT.
I planning examining of functionality of WordPress or any web programming tips.
(I want to know which language you use in web programming. There are so many languages for making website you know)
Thank you for read it! See you next time!!

C Programming, Constants and Variables..

In this tutorial you will learn about Character Set, C Character-Set Table, Special Characters, White Space, Keywords and Identifiers, Constants, Integer Constants, Decimal Integers, Octal Integers, Hexadecimal integer, Real Constants, Single Character Constants, String Constants, Backslash Character Constants [Escape Sequences] and Variables.

Instructions in C language are formed using syntax and keywords. It is necessary to strictly follow C language Syntax rules. Any instructions that mis-matches with C language Syntax generates an error while compiling the program. All programs must confirm to rules pre-defined in C Language. Keywords as special words which are exclusively used by C language, each keyword has its own meaning and relevance hence, Keywords should not be used either as Variable or Constant names.

Character Set

The character set in C Language can be grouped into the following categories.

1. Letters
2. Digits
3. Special Characters
4. White Spaces

White Spaces are ignored by the compiler until they are a part of string constant. White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers.

C Character-Set Table

Letters

Digits

Upper Case A to Z

0 to 9

Lower Case a to z

.

Special Characters

,

.Comma

&

.Ampersand

.

 .Period

^

.Caret

;

 .Semicolon

*

.Asterisk

:

.Colon

.Minus Sign

?

.Question Mark

+

.Plus Sign

.Aphostrophe

<

.Opening Angle (Less than sign)

.Quotation Marks

>

.Closing Angle (Greater than sign)

!

.Exclaimation Mark

(

.Left Parenthesis

|

.Vertical Bar

)

.Right Parenthesis

/

.Slash

[

.Left Bracket

\

.Backslash

]

.Right Bracket

~

.Tilde

{

.Left Brace

.Underscore

}

.Right Bracket

$

.Dollar Sign

#

.Number Sign

%

.Percentage Sign
.
.

.
.
.
White Space

1. Blank Space
2. Horizontal Tab
3. Carriage Return
4. New Line
5. Form Feed

Keywords and Identifiers

Every word in C language is a keyword or an identifier. Keywords in C language cannot be used as a variable name. They are specifically used by the compiler for its own purpose and they serve as building blocks of a c program.

The following are the Keyword set of C language.

.auto
.else
.register
.union

.break
.enum
.return
.unsigned

.case
.extern
.short
.void

.char
.float
.signed
.volatile

.const
.for
.size of
.while

.continue
.goto
.static
.

.default
.if
.struct
.

.do
.int
.switch
.

.double
.long
.typedef
.

some compilers may have additional keywords listed in C manual.

Identifiers refers to the name of user-defined variables, array and functions. A variable should be essentially a sequence of letters and or digits and the variable name should begin with a character.

Both uppercase and lowercase letters are permitted. The underscore character is also permitted in identifiers.

Programming for Nepali Traditional Game “Bagh Chaal”

I wrote simple console program for playing Nepal traditional board game “Bach Chal”.
But I have only 1 hour to make this program.
Then I could implemented only opening phase.
I’ll show you the way to play this program.
First, program shows start position of game.

Goat side Player can put goat 20 times.
Input two numbers of x-axis and y-axis of destinating position.

And return Enter.
You can see goat was putted.

When succeeded to put goat,Tiger side players turn starts.
Input tiger position which you want to move.
Then input move directory like that.

Goat eaten by tiger.
This is every thing of this program.
I’ll show the program all source code.
This program was compiled by gcc(it is free software distributed by FSF)
And executed by MSYS(it is also free software)

#include 

enum { FALSE, TRUE };
enum { EMPTY, TIGER, GOAT };

int position_ok(x,y){return 0<=x && x<5 && 0<=y && y<5;}

void print_board(int board[5][5]){
  int x,y;
  for(y=0;y<5;y++){
    if(y==0) printf("  0 1 2 3 4\n");
    printf("%d ",y);
    for(x=0;x<5;x++){
      switch(board[y][x]){
      case EMPTY:
        printf("_");
        break;
      case TIGER:
        printf("T");
        break;
      case GOAT:
        printf("G");
        break;
      default:
        fprintf(stderr, "Position is not valid!");
        break;
      }
      printf(" ");
    }
    printf("\n");
  }
  fflush(stdin);
}

void put_goat(int board[5][5]){
  printf("please position to put goat\n");
  fflush(stdin);
  int x, y;
  while(TRUE){
    scanf("%d %d",&y,&x);
    if(position_ok(x,y)){
      if(board[y][x]==EMPTY) break;
      else printf("that position already exists animal!\n");
    }else printf("position is invalid!\n");
    printf("please position to put goat\n");
    fflush(stdin);
  }
  board[y][x]=GOAT;
  print_board(board);
}
void move_tiger(int board[5][5]){
  printf("please select the tiger position\n");
  int x, y;
  while(TRUE){
    scanf("%d %d",&y,&x);
    if(position_ok(x,y)){
      if(board[y][x]==TIGER) break;
      else printf("doesn't exists tiger that position!\n");
    }else printf("position is invalid!\n");
    printf("please select the tiger position");
    fflush(stdin);
  }
  printf("please select the move direction of tiger\n");
  int dx,dy,next_x,next_y,eat_goat=FALSE;
  while(TRUE){
    scanf("%d %d",&dy,&dx);
    if(dx!=0 && dy!=0 && ((x-y)%2)!=0){
      printf("you can't move that direction!\n");
    }else{
      next_x=x+dx;
      next_y=y+dy;
      if(position_ok(next_x,next_y)){
        if(board[next_y][next_x]==EMPTY) break;
        else if(board[next_y][next_x]==GOAT) {
          next_x=next_x+dx;
          next_y=next_y+dy;
          if(position_ok(next_x,next_y)){
            if(board[next_y][next_x]==EMPTY){
              eat_goat=TRUE;
              break;
            }
          }
        }
      }
      printf("you can't move tiger there!\n");
    }
    printf("please select the move direction of tiger\n");
    fflush(stdin);
  }
  board[y][x]=EMPTY;
  board[next_y][next_x]=TIGER;
  if(eat_goat)board[y+dy][x+dx]=EMPTY;
  print_board(board);
}

int main()
{
  int remaining_goat=20;
  int board[5][5];
  int x, y;
  for(y=0;y<5;y++){
    for(x=0;x<5;x++){
      board[y][x]=EMPTY;
      if( ((x==0)||(x==4))&&((y==0)||(y==4)) ){
        board[y][x]=TIGER;
      }
    }
  }

  print_board(board);

  while(remaining_goat!=0){
    put_goat(board);
    remaining_goat--;
    printf("remaining goat is: %d\n",remaining_goat);
    move_tiger(board);
  }
  return 0;
}

Thank you for reading my first post.
Feel free to ask any question or request for this post!
Have a nice Monday!!

%d bloggers like this: