One-dimensional array modeling represents two-dimensional checkerboard state

Source: Internet
Author: User


When we want to write a chess game, it is not difficult to find that many board games can be represented by a two-dimensional array of chess, such as:

Kanki (two-dimensional array of 3*3), Othello (two-dimensional array of 8*8), Gobang (two-dimensional array of 15*15), etc.

Using a two-dimensional array to represent the chessboard, the subscript of the array is the coordinate of the piece, and the value in the array is the state of the pawn.

The advantage is that the data access is more intuitive, can be directly based on the subscript to quickly find a position of the pieces of the state.

But the downside is obvious, too.

Like what:

The first step is to traverse the chessboard with a double loop to deal with the vertical axis;

The second is to determine the state of chess pieces, such as the above mentioned three pieces, you need to judge the row, column and slash 8 direction of the pawn State, because according to row, column and slash subscript change characteristics, coupled with the judgment of the algorithm is not unified, need to use a number of different methods to deal with.

In this case, let me introduce a method, as the title shows, a one-dimensional array to represent the checkerboard state, so how can a one-dimensional array represent a two-dimensional array?

We use Tic-tac-Chess as an example:

First, take a look at the two-dimensional array to represent the Tic-Tac-chess board:

0 1 2

0 x x x

1 x x x

2 x x x

where x is the representation of each position of the chessboard, 0, 1, 2 is the coordinates of each position, such as the coordinates of the second piece is (0,1), the sixth piece coordinates are (in)

If the starting point is in the first piece, i.e. (x = 0,y = 0) This position, then go down will need to execute X + 1, y + 0, in order to go down a grid. In other directions, you need to handle two values.

Well, with a two-dimensional array is not much to say, I believe we all know how to do, the following talk about this topic: with a one-dimensional array representation, with a one-dimensional array is represented by two ways:

The first type:

0 1 2

3 4 5

6 7 8

Directly have how many pieces of the array to open, for example: Tic-tac-3*3 = 9, so a one-dimensional array of length 9 to represent the board, so that the first row in a two-dimensional array is numbered 0 1 2, the second row is numbered 3 4 5, the third row is numbered 6 7 8. That is, 6 elements in a one-dimensional array represent the 1th of the 3rd row in a two-dimensional array.

This loss of data access, but it is more concise processing of data, only a layer of traversal can search the entire board, no need to focus on two subscript. So how does it go in all directions?

In fact, it's not hard to find a pattern on the chart above,

To the left, the step quantity is 1,

The downward step is 3,

The right down step is 4,

The left-down step is 2,

The steps in the opposite direction are negative;

So we can use a one-dimensional array to represent the amount of stepping:

int dir[4] = {1,3,4,2};

But with this one-dimensional array storage method has a disadvantage, is more difficult to determine whether the cross-boundary, such as: in 3 this position, minus one to 2, in the array exists, so the program will determine the non-bounds, but, minus 1 of the operation is to the left, that is, 3 this position and left to walk a step has crossed the border

So let's introduce the second method of presentation:

dddd

Dxxx

Dxxx

Dxxx

Ddddd

This is a modeling approach proposed by Warren Smith: http://www.radagast.se/othello/endgame.c

As shown in the table above, a tic-tac-chess board is represented by a one-dimensional array of length 21, where all d is the flag and X is the state of the chess piece. See this board, some people will feel that the right does not use the flag D surround the whole chessboard, not the same way as the previous method can not judge the cross-border, in fact, it will be able to ensure that any one piece position to 8 directions can encounter the flag. We understand that by changing the letters to numbers.

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

16 17 18) 19 20

One-dimensional arrays are labeled 0, 1, 2, 3, 4, 8, 12, 16, 17, 18, 19, 20, and we can use the value-1, or any one that is not the same as the state of the pawn, the rest of the position is the chessboard.

It can be seen that

To the left, the step quantity is 1,

The downward step is 4,

The right down step is 5,

The left-down step is 3,

The steps in the opposite direction are negative;

Now let's take a look at the problem of crossing over, adding 13 to this position, one step to the left, minus 1, to 12, and 12 This position is the flag, so the cross-border end search.

Any chess position to go to all directions can meet the signs, we can calculate their own, here is not much to say.

Here's how these subscripts are converted from a two-dimensional array subscript:

The following x represents the longitudinal direction and y represents the transverse direction

First of all, let's say:

0 1 2

3 4 5

6 7 8

Checkerboard has R (3) row C (3) column

The first position is 0 * 3 + 0 = 0

The second position is 0 * 3 + 1 = 1

...

The fourth position is 1 * 3 + 0 = 3

...

The last one is 2 * 3 + 2 = 8

It is not difficult to introduce a formula: P = c * x + y, where P is a one-dimensional array subscript, C is the number of columns of the chessboard, X is the x-coordinate of the two-dimensional array, and y is the y-coordinate of the two-dimensional array. That is, p in a one-dimensional array can represent this position (x, y) in a two-dimensional array.

3*3 the steps in each direction of the chessboard have just been written, for {1,3,4,2}

What if it's 4 * 4, 5*5 or even more? Don't worry, the following calculation will know:

4*4 Board:

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

5*5 Board:

0 1 2) 3 4

5 6 7) 8 9

10 11 12) 13 14

15 16 17) 18 19

20 21 22) 23 24

It is not difficult to see 4*4 's step amount is {1,4,5,3}

5*5 's step amount is {1,5,6,4}

You see the rules? , yes, to the left and right of the step amount is not changed to 1, down is the number of columns, the right down is the number of columns + 1, the left is the number of columns –1;

So you can launch a formula: dir[4] = {1,c,c + 1, c-1};

The next step is to introduce a two-dimensional subscript from one-dimensional subscript, according to P = c * x + y, this formula,

Can see

y = p% c

x = (p–y)/C;

The second method:

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

16 17 18) 19 20

The same chessboard has R (3) row C (3) column

The chessboard is 5 6 7 9 10 11 13 14 15, then how to transfer from the two-dimensional? Can be seen:

5 = 3 + 2 + 0 + 0 * 4

6 = 3 + 2 + 1 + 0 * 4

...

9 = 3 + 2 + 0 + 1 * 4

...

15 = 3 + 2 + 2 + 2 * 4

Also can be introduced a rule P = C + 2 + y + x * (c + 1), where P is a one-dimensional array subscript, C is the number of columns of the chessboard, X is the x-coordinate of the two-dimensional array, y is the y-coordinate of the two-dimensional array. That is, p in a one-dimensional array can represent this position in a two-dimensional array (x, y).

Let's take a look at the step quantity problem (you can draw it yourself):

3 * 3 for {1,4,5,3}

4 * 4 for {1,5,6,4}

5 * 5 for {1,6,7,5}

It is also not difficult to see its regularity, dir[4] = {1, C + 1, C + 2, c},c as the number of checkerboard columns

The next step is to introduce a two-dimensional subscript from one-dimensional subscript, according to P = C + 2 + y + x * (c + 1), this formula,

The same can be seen

y = (P – (c + 2))% (c + 1),

x = (P – (c + 2)-y)/(c + 1)

OK, so much for today, let's look at an example of what I did when I wrote Ai Gobang:


/**     * Set player ID for initiator lazi     *     * @param playerid player ID     *    /public void initgamestate (int playerid) {        this.m _playerid = playerID;        This.winner = playerID;        This.m_board = new INT[15 * + + + +];        This.nullpos = new hashset<> ();        This.whitepos = new hashset<> ();        This.blackpos = new hashset<> ();        This.canputpos = new arraylist<> ();        for (int i = 0, I < ++i) {for            (int j = 0; J < ++j) {                m_board[17 + i + j * +] =-1;            }        }        this.len = 0;        This.is = false;}

I set the flag in the initialization to 0, the empty board is set to 1, the game set black chess is 1, White is 2

The following is an example of whether five sub-alignments are to be judged:


Direction Step amount    private static int f[] = {1, +, +, 15};/**     * Check if there is a five sub-     alignment * * @param dis chess position     * @param Playid  Player ID, white or black     *    /private void check (int dis, int playid) {for        (int i = 0; i < f.length; ++i) {            int c = 1;            int d = dis + f[i];//step in all directions until it encounters the position is the flag bit or already has the opponent's pawn end            while (m_board[d] = = Playid) {                C + +;                D + = F[i];            }            d = dis-f[i];            LOG.D ("D", D + "");//step in the opposite direction until it encounters the position is the flag bit or already has the opponent's pawn End while            (m_board[d] = = Playid) {                C + +;                D-= F[i];            }            if (c >= 5) {                winner = Playid;                LOG.D ("Over", winner + "Win");                is = true;                Break;}}}        

The following is a two-dimensional matrix rotation algorithm (c + +) made with one-dimensional array ideas:


#include <iostream> #include <string.h>using namespace Std;int result[1000];int main () {int n;while (cin > > n) {int num = 1;int i = 0, j = 0;memset (result, 0, sizeof (result)), for (int i = 0; i < n; ++i) {for (int j = 0; J &L T N ++J) {result[n + 2 + i + J * (n + 1)] =-1;}} int k = n + 1, f = 1;int dis[4] = {1, n + 1,-1,-(n + 1)};while (num <= n * N) {for (int i = 0; i < 4; ++i) {while (Result[k + dis[i]] = =-1) {result[k + = Dis[i]] = num++;}} int pos = 0;for (int i = n + 2; I <= (n + 2) * N; ++i) {if (Result[i]) {printf ("%5d", Result[i]);p os++;if (pos% n = = 0) { cout << Endl;}}} return 0;}



Here is the AI Gobang I wrote

Java version ai Gobang http://blog.csdn.net/a249900679/article/details/51052103 (using a two-dimensional array to represent the chessboard)

Android version ai Gobang frame http://git.oschina.net/ysx_xx/Chess-frame (with one-dimensional array to represent the chessboard)




One-dimensional array modeling represents two-dimensional checkerboard state

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.