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!!

Advertisements

About ながとタン
嫁で彼女で妹の有希と二人でいろいろがんばってます!

2 Responses to Programming for Nepali Traditional Game “Bagh Chaal” 2nd

  1. Bigvon says:

    Please email me on my priviate email, bigyandixit @ gmail com; i want to talk with you about one project !!

  2. laxmi says:

    the ai is not programmed correctly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: