0
0

More than 3 years have passed since last update.

対戦ゲームを作る。 その4

Posted at

概要

対戦ゲームを作る。
そこに、石が置けるか評価する関数の実装。

考え方

前後左右、9方向に、違う石があって、同じ石があるのか、探索する。

サンプルコード

    function check(put, d) {
        var x = put % 8; 
        var y = Math.floor(put / 8);
        var res = 0;
        if (x == 0 && (d == -9 || d == -1 || d ==  7)) res = 1;
        if (x == 7 && (d == -7 || d ==  1 || d ==  9)) res = 1;
        if (y == 0 && (d == -9 || d == -8 || d == -7)) res = 1;
        if (y == 7 && (d ==  7 || d ==  8 || d ==  9)) res = 1;
        return res;
    }
    function oku(put, iro, ban) {
        var res = 0;
        var turn = 0;
        if (iro == 0) turn = 2;
        var dir = new Array(-9, -8, -7, -1, 1, 7, 8, 9);
        var tugi;
        var i;
        var count;
        if (ban[put] == 1)
        {
            for (i = 0; i < 8; i++)
            {
                count = 0;
                tugi = put;
                do
                {
                    if (check(tugi, dir[i]) == 1) break;
                    count++;
                    tugi += dir[i];
                } 
                while (ban[tugi] == turn);
                if ((count > 1) && (ban[tugi] == iro))
                {
                    res = -1;
                    tugi = put;
                    do
                    {
                        ban[tugi] = iro;
                        tugi += dir[i];
                    }
                    while (ban[tugi] == turn);
                }
            }
        }
        return res;
    }

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0