LoginSignup
0
0

More than 5 years have passed since last update.

カードゲームを作る その7(戦闘API UT その2)

Posted at

この記事は

とあるカードゲームが非常に面白く、
そのゲームの見た目クローンを作ろうと一人のプログラマーが奮闘していくブログのような記事です。

その6の続き
ここでは、UTのテストケースの記述、テスト実施をしています。

キャラクター主要ステータス

UId Kingdom Spiritual Tribal Pluto
Name 王国使い 精霊使い 蛮族使い 幽魔使い
CurrentDeckNo 1 1 1 1
UseCardId T01 T02 T03 T04
CardName 王国の負け組 精霊の負け組 蛮族の負け組 幽魔の負け組
CardHP 2 2 2 2
CardAtk 1 1 1 1

テストの仕様

↓自分/相手→ 王国 精霊 蛮族 幽魔
王国 - - X O
精霊 - - O X
蛮族 O X - -
幽魔 X O - -

O・・・自分の勝ち
X・・・相手の勝ち
-・・・引き分け

UTの実装

test/controllers/battles_controller_test.rb
require 'test_helper'

class BattlesControllerTest < ActionController::TestCase
  setup do
#    @battle = battles(:one)
#    @battle = battle
    request.env["HTTP_ACCEPT"] = 'application/json'
  end

  test "should get index" do
    get :index
    assert_response :success
    # assert_not_nil assigns(:battles)
  end

  test "should get battle won the pluto" do
    get :index, battle: { BType: "arena", UId: "Kingdom", BTarget: "Pluto", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Kingdom" do
    get :index, battle: { BType: "arena", UId: "Tribal", BTarget: "Kingdom", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Spiritual" do
    get :index, battle: { BType: "arena", UId: "Pluto", BTarget: "Spiritual", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle won the Tribal" do
    get :index, battle: { BType: "arena", UId: "Spiritual", BTarget: "Tribal", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 1, res['BWinner']
  end
  test "should get battle losed the pluto" do
    get :index, battle: { BType: "arena", UId: "Spiritual", BTarget: "Pluto", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Kingdom" do
    get :index, battle: { BType: "arena", UId: "Pluto", BTarget: "Kingdom", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Spiritual" do
    get :index, battle: { BType: "arena", UId: "Tribal", BTarget: "Spiritual", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end
  test "should get battle losed the Tribal" do
    get :index, battle: { BType: "arena", UId: "Spiritual", BTarget: "Tribal", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal -1, res['BWinner']
  end

  test "should get battle draw the same pluto" do
    get :index, battle: { BType: "arena", UId: "Pluto", BTarget: "Pluto", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Kingdom" do
    get :index, battle: { BType: "arena", UId: "Kingdom", BTarget: "Kingdom", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Spiritual" do
    get :index, battle: { BType: "arena", UId: "Spritual", BTarget: "Spritual", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the same Tribal" do
    get :index, battle: { BType: "arena", UId: "Tribal", BTarget: "Tribal", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end

  test "should get battle draw the pluto" do
    get :index, battle: { BType: "arena", UId: "Tribal", BTarget: "Pluto", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Kingdom" do
    get :index, battle: { BType: "arena", UId: "Spritual", BTarget: "Kingdom", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Spiritual" do
    get :index, battle: { BType: "arena", UId: "Kingdom", BTarget: "Spritual", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
  test "should get battle draw the Tribal" do
    get :index, battle: { BType: "arena", UId: "Pluto", BTarget: "Tribal", V: "1" }
    assert_response :success

    res = JSON.parse( response.body )
    assert_equal 0,  res['Result']
    assert_equal 0, res['BWinner']
  end
end

実行結果

:
:
 10) Failure:
BattlesControllerTest#test_should_get_battle_draw_the_Kingdom [/Urakata/test/controllers/battles_controller_test.rb:129]:
Expected: 0
  Actual: true
:
:

予定通り全て失敗しています。これからテストが通るように実装していきます。

実装

app/controllers/battles_controller.rb
class BattlesController < ApplicationController
  before_action :set_battle, only: [:show, :update, :destroy]

  # GET /battles
  # GET /battles.json
  def index
    # @battles = Battle.all
    #
    # render json: @battles
    # render json: ["hoge", params[:BType],
    #                       params[:UId], params[:BTarget], params[:V] ]
    _BType = params[:BType]
    _UId   = params[:UId]
    _BTarget = params[:BTarget]
    _V = params[:V]

    _Result = "0"
    case _UId
    when "Kingdom"
      case _BTarget
      when "Kingdom"
        _BWinner = 0
      when "Spritual"
        _BWinner = 0
      when "Tribal"
        _BWinner = -1
      when "Pluto"
        _BWinner = 1
      else
        _Result = 9
      end
    when "Spiritual"
      case _BTarget
      when "Kingdom"
        _BWinner = 0
      when "Spritual"
        _BWinner = 0
      when "Tribal"
        _BWinner = 1
      when "Pluto"
        _BWinner = -1
      else
        _Result = 9
      end
    when "Tribal"
      case _BTarget
      when "Kingdom"
        _BWinner = 1
      when "Spritual"
        _BWinner = -1
      when "Tribal"
        _BWinner = 0
      when "Pluto"
        _BWinner = 0
      else
        _Result = 9
      end
    when "Pluto"
      case _BTarget
      when "Kingdom"
        _BWinner = -1
      when "Spritual"
        _BWinner = 1
      when "Tribal"
        _BWinner = 0
      when "Pluto"
        _BWinner = 0
      else
        _Result = 9
      end
    else
      _Result = 9
    end
    _BProcess = { 'Turn' => 1, 'me01' => { 't01' => { 'HP' => 0, 'ATK' => 1 }}}

    _ret = {'Result' => _Result, 'BWinner' => _BWinner, 'BProcess' => _BProcess }
    render json:  _ret
  end

実行

実行結果
:
:
 15) Failure:
BattlesControllerTest#test_should_get_battle_won_the_pluto [/Urakata/test/controllers/battles_controller_test.rb:21]:
Expected: 0
  Actual: 9


 16) Failure:
BattlesControllerTest#test_should_get_battle_losed_the_Kingdom [/Urakata/test/controllers/battles_controller_test.rb:62]:
Expected: 0
  Actual: 9

22 runs, 41 assertions, 16 failures, 0 errors, 0 skips

........うまく動きませんでした。。。
次回デバッグ環境を整えて想定通りに動いているか確認します。


いくつか言い訳

  • 不定期連載です。
  • 多分途中で挫折します。
  • 筆者のメモも兼ねているため過去の記事も遠慮なく随時編集いたします。
  • プログラミングの開発経験はあってもゲームの開発経験はこれが初めてです。

等色々ありますが、生暖かい目で応援してくださると光栄です。

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