Jump to content

  • Log In with Google      Sign In   
  • Create Account


A.I. for a turn-based board game ...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
36 replies to this topic

#21 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 27 September 2012 - 08:06 AM

Move moves[33]; shouldn't this be Move moves[35] for this ?


Ooops! I shouldn't post code that I wrote late at night. :)

I think it should actually be 34 (31 non-empty combinations for bringing figures out of the base plus 3 moves). But 35 wouldn't hurt anything.

Sponsor:

#22 Álvaro   Crossbones+   -  Reputation: 11906

Like
1Likes
Like

Posted 27 September 2012 - 08:08 AM

Oh, also, the code I posted had
for (depth = 1; depth < 5 || now() < start_time + 120.0; ++depth) {

That means that it will dare explore more depth if less than two minutes have elapsed. I had that in there for a test. I will edit the post and change it to something more tolerable. I will also make the 33->34 change.

#23 Álvaro   Crossbones+   -  Reputation: 11906

Like
1Likes
Like

Posted 27 September 2012 - 08:17 AM

Wow ... great code!

Thanks.

I tried to run it and get the following error:

"Run-Time Check Failure #2 - Stack around the variable 'moves' was corrupted"
Edit: found the problem, since there can be maximum of 34 moves the declarations of "Move moves[33];" must be changed to "Move moves[34];" !

Yup, thanks for caching that. I guess my compiler probably reserves 36 bytes when asked for 33, which is what "saved" me in my tests.

P.S: there are 2 additional rules i idn't mention cuz of simplicity:
- once a piece moves into hostile base, that hostile base is broken, which means that any pieces moving into it will be destroyed

I did not implement that, and the rule didn't come up in the games I played. This is an important rule because without it there are infinite games (draws, I would say). I'll implement it later.

- a piece located directly beside a base will always move into it regardless of the move-type, e.g. if piece is on 3rd row beside the hosile base and the chossen move is "up", then the piece will go into the base on 3rd row and not , as expected, into base on 4th row.

Yes, I figured this out from playing with the emulator.

PPS: i am not sure but bringing out too many pieces out of a base may strategically not be so good. So in my move generation i only consider moves with only one base (in your case 1,2,4,8,16) with additional base moves only if there is a hostile piece outside beside the base. This reduces the number of possible moves a lot.
I might also be wrong with this consideration ... i have to get a better player with this game.

That seems a little harsh. My program picks one of these moves that you forbid occasionally. What I did was penalize them as costing 2 plies in depth. So the nominal depth that my program searches to is only for normal moves (1,2,4,8,16,32,33,34 in my notation). I have a table called "cost" (bad name, sorry) to encode that.

EDIT: Oh, I also generate normal moves before multiple-figures-out-of-base moves, in the hopes that this order will speed up alpha-beta.

Edited by alvaro, 27 September 2012 - 08:24 AM.


#24 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 27 September 2012 - 08:32 AM

Once again great work!

Next I will try to play you A.I. against the CPU in the emulator game (level 1-5) and see how it performs ;)

#25 SimonForsman   Crossbones+   -  Reputation: 5790

Like
0Likes
Like

Posted 27 September 2012 - 08:50 AM


Move moves[33]; shouldn't this be Move moves[35] for this ?


Ooops! I shouldn't post code that I wrote late at night. Posted Image

I think it should actually be 34 (31 non-empty combinations for bringing figures out of the base plus 3 moves). But 35 wouldn't hurt anything.


ah yes, i just thought, highest move value = 34 so 35 moves, (gotta learn to actually read the code and not just skim through it), you're right that it should be 34.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#26 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 27 September 2012 - 08:56 AM

I did not implement that, and the rule didn't come up in the games I played. This is an important rule because without it there are infinite games (draws, I would say). I'll implement it later.

In the emulator the door of the destroyed base stays always open!

Another rule: there cann't be more than 3 pieces in a base! So any additional piece moving into a base with already 3 pieces inside will be destroyed.
the change:

void make_move(Board &b, Move m, int player) {
  u32 &bnp = b.net[player];

  if (m<32) {
	for (unsigned i=0; i<5; ++i) {
		  if (test(m, i)) {
			b.base[i][player]--;
			bnp |= (077u<<(6*i)) & target[!player];
		  }
	}
  }
  else {
	u32 boarding = bnp & target[player];
	for (; boarding; boarding &= boarding-1) {
		  int row = lowest_bit_set(boarding) / 6;
		  b.base[row][!player] = 0;
		  if (b.base[row][player] < 3)  <---- limit to 3
			 b.base[row][player]++;
	}
	bnp &= ~target[player];
	bnp = player==0 ? bnp << 1 : bnp >> 1;
  
	if (m==32)
		  bnp = ((bnp & 00000000077u) << 24) + (bnp >> 6);
	else if (m==34)
		  bnp = ((bnp & 00077777777u) << 6) + ((bnp & 07700000000u) >> 24);
  
	bnp &= ~holes;
  }

  b.net[!player] &= ~bnp;
}


Now i hope we got all the rules Posted Image

Edited by AticAtac, 27 September 2012 - 09:07 AM.


#27 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 27 September 2012 - 09:30 AM

Another rule: there cann't be more than 3 pieces in a base! So any additional piece moving into a base with already 3 pieces inside will be destroyed.


Actually, I thought I tested that against the emulator and it's not true. The graphics only display three, but if you take a figure out of the base it still shows 3. Could you make sure one way or another?

Edited by alvaro, 27 September 2012 - 09:30 AM.


#28 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 27 September 2012 - 12:35 PM

Actually, I thought I tested that against the emulator and it's not true. The graphics only display three, but if you take a figure out of the base it still shows 3. Could you make sure one way or another?


I just verfied it, there are only 3 pieces allowed, the 4th piece is destroyed!

#29 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 27 September 2012 - 01:03 PM

So first game won against emu on level 1 Posted Image
(settings: "for (depth = 1; depth < 5 || now() < start_time + 1; ++depth)", i didn't saved the console output)

Second game against emu (level 2) won

Edited by AticAtac, 27 September 2012 - 01:14 PM.


#30 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 27 September 2012 - 02:03 PM

I played one game against level 4 yesterday and my program won.

#31 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 27 September 2012 - 02:36 PM

yep, cpu at level 3 and 4 lost to your program! Poor C64 Posted Image

Last thing left now is extending the board structure to store the "blown door" state and the make_move(...) logic.

Your code was written very well and smart !
Thanks again Posted Image

#32 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 29 September 2012 - 05:54 PM

Well, I think there is only one rule that you didn't get right: It is OK for an attacker to enter a destroyed enemy base (the piece simply goes back to the corresponding base at the other side of the board). I've played enough games against the program that I am fairly confident I have the rules right.

This is my current code:
#include <iostream>
#include <algorithm>
#include <ctime>
// A function to measure time in seconds. I usually implement it wigh
// gettimeofday, but I am posting this one because it's standard.
double now() {
  return static_cast<double>(std::clock())/CLOCKS_PER_SEC;
}
typedef unsigned u32;
bool test(u32 x, int bit) {
  return (x >> bit) & 1;
}
// Count number of set bits
unsigned int popcount(unsigned int w) {
  w -= (w >> 1) & 0x55555555u;
  w =  (w & 0x33333333u) + ((w >> 2) & 0x33333333u);
  w =  (w + (w >> 4)) & 0x0f0f0f0fu;
  return (w * 0x01010101u) >> 24;
}
int lowest_bit_set(unsigned int w) {
  static int const de_Bruijn_table[32] = {
    0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
    31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  };
 
  w  &= -(int)w;
  return de_Bruijn_table[(w * 0x077cb531) >> 27];
}
const u32 holes = 00022002200u;
struct Board {
  u32 net[2];
  char base[5][2];
 
  Board() {
    for (int i=0; i<2; ++i) {
	  net[i] = 0;
	  for (int j=0; j<5; ++j)
	    base[j][i] = 3;
    }
  }
};
std::ostream &operator<<(std::ostream &os, Board const &b) {
  for (int i=0; i<5; ++i) {
    os << "*0123"[1+b.base[i][0]] << ' ';
    for (int j=0; j<6; ++j) {
	  int bit = i*6+j;
	  os << "->< "[test(b.net[0],bit) + 2*test(b.net[1],bit) + 3*test(holes,bit)] << ' ';
    }
    os << "*0123"[1+b.base[i][1]] << '\n';
  }
  return os;
}
typedef char Move; // 1-31 are out-of-base moves, 32 is advance up, 33 is advance and 34 is advance down
int generate_moves(Board const &b, Move *m, int player) {
  Move *original_m = m;
 
  // Move figures on the net (put these moves first because they are often the best)
  if (b.net[player] != 0) {
    *m++ = 32;
    *m++ = 33;
    *m++ = 34;
  }
  // Now generate moves bringing out figures from bases
  char mask_of_non_empty_bases = 0;
  for (unsigned i=0; i<5; ++i)
    mask_of_non_empty_bases |= (b.base[i][player]>0) << i;
  char move_order[] = {1,2,4,8,16, 3,5,6,9,10,12,17,18,20,24, 7,11,13,14,19,21,22,25,26,28, 15,23,27,29,30, 31};
  for (unsigned i=0; i<31; ++i) {
    char c = move_order[i];
    if ((c & mask_of_non_empty_bases) == c)
	  *m++ = c;
  }
 
  return m - original_m;
}
void make_move(Board &b, Move m, int player) {
  static u32 const target[2] = {04040404040u, 00101010101u};
 
  u32 &bnp = b.net[player];
 
  if (m<32) {
    for (unsigned i=0; i<5; ++i) {
	  if (test(m, i)) {
	    b.base[i][player]--;
	    bnp |= (077u<<(6*i)) & target[!player];
	  }
    }
  }
  else {
    // Loop over the figures that are boarding the enemy bases
    for (u32 boarding = bnp & target[player]; boarding; boarding &= boarding-1) {
	  int row = lowest_bit_set(boarding) / 6;
	  b.base[row][!player] = -1;
	  if (b.base[row][player] >= 0 && b.base[row][player] < 3)
	    b.base[row][player]++;
    }
    bnp &= ~target[player];
    bnp = player==0 ? bnp << 1 : bnp >> 1;
   
    if (m==32)
	  bnp = ((bnp & 00000000077u) << 24) + (bnp >> 6);
    else if (m==34)
	  bnp = ((bnp & 00077777777u) << 6) + ((bnp & 07700000000u) >> 24);
   
    bnp &= ~holes;
  }
 
  b.net[!player] &= ~bnp;
}
int eval(Board const &b) {
  static int const base_value_table[5] = {-200, 0, 500, 1000, 1500};
  int on_base_diff = 0;
  for (int i=0; i<5; ++i)
    on_base_diff += base_value_table[b.base[i][0]+1] - base_value_table[b.base[i][1]+1];
 
  int on_net_diff = 400 * (popcount(b.net[0]) - popcount(b.net[1]));
 
  int noise = -32 + std::rand() % 64;
 
  int score = on_base_diff + on_net_diff + noise;
 
  return score;
}
int negamax(Board const &board, int alpha, int beta, int depth, int player) {
  static int const depth_reduction[35] = {
    0,
    1, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 1, 1};
  if (depth <= 0)
    return player==0 ? eval(board) : -eval(board);
 
  Move moves[34];
  int n_moves = generate_moves(board, moves, player);
  for (int i=0; i<n_moves; ++i) {
    Board copy = board;
    make_move(copy, moves[i], player);
    int score = -negamax(copy,
						 -beta,
						 -alpha,
						 depth-depth_reduction[static_cast<int>(moves[i])],
						 !player);
    if (score > alpha) {
	  alpha = score;
	  if (score > beta)
	    break;
    }
  }
 
  return alpha;
}
Move search_root(Board const &board, int player) {
  double start_time = now();
  Move moves[34];
  int n_moves = generate_moves(board, moves, player);
  if (n_moves == 0)
    return 0;
  int depth, alpha;
  for (depth = 1; depth < 5 || now() < start_time + 2.0; ++depth) {
    alpha = -999999;
    for (int i=0; i<n_moves; ++i) {
	  Board copy = board;
	  make_move(copy, moves[i], player);
	  int score = -negamax(copy,
						   -999999,
						   -alpha,
						   depth-1,
						   !player);
	  if (score > alpha) {
	    alpha = score;
	    std::rotate(moves, moves + i, moves + i + 1);
	    std::cout << depth << ": (" << 0+moves[0] << ") " << alpha << ' ' << now()-start_time << '\n';
	  }
    }
    if (std::abs(alpha)>500000)
	  break;
    std::cout << depth << ". (" << 0+moves[0] << ") " << alpha << ' ' << now()-start_time << '\n';
  }
  --depth;
  std::cout << depth << ". (" << 0+moves[0] << ") " << alpha << ' ' << now()-start_time << '\n';
  return moves[0];
}
int main(int argc, char **argv) {
  int seed = argc == 2 ? atoi(argv[1]) : std::time(0);
  std::cout << "seed = " << seed << '\n';
  std::srand(seed);
 
  Board board;
  int computer;
  std::cout << "Which player am I? (0=pirates (left), 1=sailors (right)) ";
  std::cin >> computer;
  int player;
  std::cout << "Who goes first? (0=pirates (left), 1=sailors (right)) ";
  std::cin >> player;
 
  while (1) {
    std::cout << board << '\n';
    Move move;
    if (player == computer)
	  move = search_root(board, player);
    else {
	  int i;
	  std::cin >> i;
	  move = i ? i : search_root(board, player);
    }
    if (move == 0)
	  break;
    make_move(board, move, player);
    player = !player;
  }
}


#33 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 30 September 2012 - 03:18 AM

Well, I think there is only one rule that you didn't get right: It is OK for an attacker to enter a destroyed enemy base (the piece simply goes back to the corresponding base at the other side of the board). I've played enough games against the program that I am fairly confident I have the rules right.


The rule works like this:
As you already wrote, attacker can enter destroyed base but an attacker cann't come back to destroyed base.
E.g.: attacker is at height 3 beside enemy's base, he can go through it, doesn't matter destroyed or not, but if his home base at height 3 is destroyed he will be also destroyed.

As i see now in your source code above you already implemented this rule correctly. So base[]=-1 means destroyed base (and is drawn with "*").

Edited by AticAtac, 30 September 2012 - 03:21 AM.


#34 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 30 September 2012 - 04:19 AM

OK, then. I guess I didn't understand your wording of the rule and I initially implemented as "when boarding an enemy base, the boarding piece is destroyed if either the enemy base or the base where it is normally respawned is destroyed" (since you "enter" both, in some sense).

As you see your original description of the rules was full of holes, but I am happy that eventually I managed to understand the game. It is actually kind of neat!

#35 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 30 September 2012 - 04:51 AM

Yep, when it comes to describing rules i am lost Posted Image
You did it very well !

My next step is to make the code iterative, the problem:
I will run this on smartphones (iOS, Android) where i cann't use multi-threading. One challange i will be facing is to somehow update the screen while the CPU is "thinking".
My first idea is to call screen-updates from the Negmax-function "periodically" without losing too much CPU-time for the A.I.

Edited by AticAtac, 30 September 2012 - 04:53 AM.


#36 Álvaro   Crossbones+   -  Reputation: 11906

Like
0Likes
Like

Posted 30 September 2012 - 05:44 AM

What do you mean you can't use multithreading on smartphones?

https://www.google.com/search?q=multithreading+ios
https://www.google.com/search?q=multithreading+android

#37 AticAtac   Members   -  Reputation: 313

Like
0Likes
Like

Posted 30 September 2012 - 06:17 AM

I have to check this, i am working with AirplaySDK (www.airplaysdk.com) and last time i checked, it wasn't supporting multithreading.

Edit:
Looks like it is supporting now multithreading ... so i will go for it.

Edited by AticAtac, 30 September 2012 - 06:20 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS