RubyShogi 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ##
2
+ # RubyShogi, a Shogi Engine
3
+ # Author: Toni Helminen
4
+ # License: GPLv3
5
+ ##
6
+
7
+ module RubyShogi
8
+
9
+ module Utils
10
+ def Utils.log(x)
11
+ File.open("ruby_shogi-log.txt", 'a+') { |file| file.write "#{x}\n" }
12
+ end
13
+ end # module Utils
14
+
15
+ end # module RubyShogi
@@ -0,0 +1,117 @@
1
+ ##
2
+ # RubyShogi, a Shogi Engine
3
+ # Author: Toni Helminen
4
+ # License: GPLv3
5
+ ##
6
+
7
+ module RubyShogi
8
+
9
+ class Xboard
10
+ def initialize(random_mode = false)
11
+ @random_mode = random_mode
12
+ @engine = RubyShogi::Engine.new(random_mode: random_mode)
13
+ @movestogo_orig = 40
14
+ @forcemode = false
15
+ Signal.trap("SIGPIPE", "SYSTEM_DEFAULT")
16
+ trap("INT", "IGNORE") # no interruptions
17
+ end
18
+
19
+ def print_xboard
20
+ rv = @random_mode ? " random" : ""
21
+ puts "feature myname=\"#{RubyShogi::NAME} #{RubyShogi::VERSION}#{rv}\""
22
+ puts "feature variants=\"shogi\""
23
+ puts "feature setboard=1"
24
+ puts "feature ping=1"
25
+ puts "feature done=1"
26
+ end
27
+
28
+ def play
29
+ @engine.think
30
+ end
31
+
32
+ def update_movestogo
33
+ if @engine.movestogo == 1
34
+ @engine.movestogo = @movestogo_orig
35
+ elsif @engine.movestogo > 0
36
+ @engine.movestogo -= 1
37
+ end
38
+ end
39
+
40
+ def cmd_variant(variant)
41
+ @variant = variant
42
+ end
43
+
44
+ def cmd_new
45
+ @engine = RubyShogi::Engine.new(random_mode: @random_mode)
46
+ @canmakemove = true
47
+ end
48
+
49
+ def cmd_level(level)
50
+ @engine.movestogo = level.to_i
51
+ @movestogo_orig = @engine.movestogo
52
+ end
53
+
54
+ def cmd_go
55
+ if @canmakemove
56
+ puts "move #{play}"
57
+ @canmakemove = false
58
+ end
59
+ end
60
+
61
+ def cmd_move(move)
62
+ #@engine.board.print_board
63
+ update_movestogo # update counter
64
+ if @engine.make_move?(move)
65
+ @canmakemove = true
66
+ if @canmakemove && ! @engine.gameover
67
+ puts "move #{play}"
68
+ @canmakemove = false
69
+ end
70
+ end
71
+ end
72
+
73
+ def go
74
+ puts "#{RubyShogi::NAME} #{RubyShogi::VERSION} by #{RubyShogi::AUTHOR}"
75
+ @movestogo_orig = 40
76
+ @canmakemove = true
77
+ $stdin.each do |cmd|
78
+ cmd.strip!
79
+ case cmd
80
+ when "xboard" then
81
+ when "hard" then
82
+ when "easy" then
83
+ when "random" then
84
+ when "nopost" then
85
+ when "post" then
86
+ when "white" then
87
+ when "black" then
88
+ # ignore
89
+ when "remove" then @engine.history_remove
90
+ when "undo" then @engine.history_undo
91
+ when "?" then @engine.move_now = true
92
+ when /^computer/ then
93
+ when /^st/ then
94
+ when /^otim/ then
95
+ when /^accepted/ then
96
+ when /^result/ then
97
+ # ignore
98
+ when /^protover/ then print_xboard
99
+ when /^ping\s+(.*)/ then puts "pong #{$1}"
100
+ when /^variant\s+(.*)/ then cmd_variant($1)
101
+ when "new" then cmd_new
102
+ when "list" then @engine.move_list
103
+ when /^level\s+(.+)\s+.*/ then cmd_level($1)
104
+ when /^time\s+(.+)/ then @engine.time = 0.01 * $1.to_i
105
+ when /^setboard\s+(.+)/ then @engine.board.fen($1)
106
+ when "quit" then return
107
+ when "p" then @engine.board.print_board
108
+ when "force" then @forcemode = true
109
+ when "go" then cmd_go
110
+ else # assume move
111
+ cmd_move(cmd)
112
+ end
113
+ end
114
+ end
115
+ end # class Xboard
116
+
117
+ end # module RubyShogi
@@ -0,0 +1,25 @@
1
+ ##
2
+ # RubyShogi, a Shogi Engine
3
+ # Author: Toni Helminen
4
+ # License: GPLv3
5
+ ##
6
+
7
+ module RubyShogi
8
+
9
+ module Zobrist
10
+ HASH = []
11
+
12
+ def Zobrist.init
13
+ return if HASH.length > 0
14
+ 10_000.times do |i|
15
+ HASH.push(rand(1024) | (rand(1024) << 10) \
16
+ | (rand(1024) << 20) | (rand(1024) << 30) | (rand(1024) << 40))
17
+ end
18
+ end
19
+
20
+ def Zobrist.get(n)
21
+ HASH[n]
22
+ end
23
+ end # module Zobrist
24
+
25
+ end # module RubyShogi
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: RubyShogi
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Toni Helminen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: RubyShogi, a Shogi Engine
14
+ email: kalleankka1@gmail.com
15
+ executables:
16
+ - ruby_shogi
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/ruby_shogi
21
+ - lib/ruby_shogi.rb
22
+ - lib/ruby_shogi/bench.rb
23
+ - lib/ruby_shogi/board.rb
24
+ - lib/ruby_shogi/cmd.rb
25
+ - lib/ruby_shogi/engine.rb
26
+ - lib/ruby_shogi/eval.rb
27
+ - lib/ruby_shogi/history.rb
28
+ - lib/ruby_shogi/mgen.rb
29
+ - lib/ruby_shogi/mgen_black.rb
30
+ - lib/ruby_shogi/mgen_white.rb
31
+ - lib/ruby_shogi/perft.rb
32
+ - lib/ruby_shogi/ruby_shogi.rb
33
+ - lib/ruby_shogi/tactics.rb
34
+ - lib/ruby_shogi/tokens.rb
35
+ - lib/ruby_shogi/utils.rb
36
+ - lib/ruby_shogi/xboard.rb
37
+ - lib/ruby_shogi/zobrist.rb
38
+ homepage: https://github.com/SamuraiDangyo/RubyShogi
39
+ licenses:
40
+ - GPL-3.0
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.7.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: a Shogi Engine
62
+ test_files: []