fiveinarow 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at nesrotom@fit.cvut.cz. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fiveinarow.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tomáš Nesrovnal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # Fiveinarow
2
+
3
+ Classis Five-in-a-row game written in Ruby + Gosu. It comes with simple AI!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fiveinarow'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fiveinarow
20
+
21
+ ## Usage
22
+
23
+ Left click on a cell to mark it with your symbol. AI will then play its turn.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fiveinarow. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fiveinarow"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fiveinarow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fiveinarow"
8
+ spec.version = Fiveinarow::VERSION
9
+ spec.authors = ["Tomáš Nesrovnal"]
10
+ spec.email = ["nesro@nesro.cz"]
11
+
12
+ spec.summary = %q{Five-in-a-row game}
13
+ spec.description = %q{Five-in-a-row is a classic board game. You need to make a line of five to win.}
14
+ spec.homepage = "https://github.com/nesro/fiveinarow"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "gosu", "~> 0.10.5"
33
+ end
@@ -0,0 +1,101 @@
1
+ require 'fiveinarow/version'
2
+ require 'fiveinarow/board'
3
+ require 'fiveinarow/cell'
4
+ require 'fiveinarow/hotseat_player'
5
+ require 'fiveinarow/ai_player'
6
+ require 'fiveinarow/z_order'
7
+ require 'rubygems'
8
+ require 'gosu'
9
+
10
+ module Fiveinarow
11
+ class Game < Gosu::Window
12
+
13
+ attr_accessor :state
14
+
15
+ def initialize
16
+ super(800, 800, false)
17
+ self.caption = 'Five In A Row'
18
+
19
+ @board = Board.new(self, 22)
20
+
21
+ @player_a = HotseatPlayer.new(Cell::PLAYER_A)
22
+ @player_b = AIPlayer.new(Cell::PLAYER_B)
23
+
24
+ @player_on_turn = @player_a
25
+
26
+ @font = Gosu::Font.new(60)
27
+ @background = Gosu::Image.new(self, 'media/background_800_800.png')
28
+ @the_end = Gosu::Image.new(self, 'media/the_end_800_800.png')
29
+ @last_milliseconds = 0
30
+ end
31
+
32
+ def draw
33
+ @background.draw(0, 0, ZOrder::Background)
34
+ @board.draw(mouse_x, mouse_y, @player_on_turn.sym)
35
+ if @state == :end
36
+ @the_end.draw(0, 0, ZOrder::TheEnd)
37
+ end
38
+
39
+ end
40
+
41
+ def needs_cursor?
42
+ true
43
+ end
44
+
45
+ # this is a callback for key up events or equivalent (there are
46
+ # constants for gamepad buttons and mouse clicks)
47
+ def button_up(key)
48
+ self.close if key == Gosu::KbEscape
49
+
50
+ # reset the game
51
+ if @state == :end && key == Gosu::MsLeft
52
+ @board = Board.new(self, 22)
53
+ @state = :game
54
+ return
55
+ end
56
+
57
+ if @player_on_turn.class == HotseatPlayer && key == Gosu::MsLeft
58
+ if @board.cell_clicked(mouse_x, mouse_y, @player_on_turn.sym)
59
+
60
+ if @state == :end
61
+ return
62
+ end
63
+
64
+ switch_players
65
+
66
+ if @player_on_turn.class == AIPlayer
67
+ @player_on_turn.make_move(@board)
68
+ switch_players
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ def switch_players
75
+ if @player_on_turn == @player_a
76
+ @player_on_turn = @player_b
77
+ else
78
+ @player_on_turn = @player_a
79
+ end
80
+ end
81
+
82
+ def update
83
+ self.update_delta
84
+ # with a delta we need to express the speed of our entities in
85
+ # terms of pixels/second
86
+ end
87
+
88
+ def update_delta
89
+ # Gosu::millisecodns returns the time since the window was created
90
+ # Divide by 1000 since we want to work in seconds
91
+ current_time = Gosu::milliseconds / 1000.0
92
+ # clamping here is important to avoid strange behaviors
93
+ @delta = [current_time - @last_milliseconds, 0.25].min
94
+ @last_milliseconds = current_time
95
+ end
96
+ end
97
+
98
+ game = Game.new
99
+ game.show
100
+
101
+ end
@@ -0,0 +1,99 @@
1
+ require_relative 'generic_player'
2
+
3
+ class AIPlayer < GenericPlayer
4
+
5
+ def make_move(board)
6
+ ai(board)
7
+ "prng = Random.new
8
+
9
+ loop do
10
+ row = prng.rand(0..board.size - 1)
11
+ col = prng.rand(0..board.size - 1)
12
+
13
+ if board.grid[row][col].e?
14
+ board.grid[row][col].set(@sym)
15
+ break
16
+ end
17
+ end"
18
+ end
19
+
20
+
21
+ def ai(board)
22
+ if self.sym == Cell::PLAYER_A
23
+ opsym = Cell::PLAYER_B
24
+ else
25
+ opsym = Cell::PLAYER_A
26
+ end
27
+
28
+ # 0 is EMPTY_CELL
29
+ # 1 is AI (this one)
30
+ # 2 is opponent
31
+ # -1 is end of pattern
32
+
33
+ [
34
+ # my four, go for win
35
+ [0, [0, 1, 1, 1, 1]],
36
+ [1, [1, 0, 1, 1, 1]],
37
+ [2, [1, 1, 0, 1, 1]],
38
+
39
+ # his four, need to block
40
+ [0, [0, 2, 2, 2, 2]],
41
+ [1, [2, 0, 2, 2, 2]],
42
+ [2, [2, 2, 0, 2, 2]],
43
+
44
+ # my three, go for four
45
+ [0, [0, 1, 1, 1, 0]],
46
+ [2, [0, 1, 0, 1, 1]],
47
+ [1, [1, 0, 1, 1, 0]],
48
+ [1, [0, 0, 1, 1, 1]],
49
+
50
+ # his three, need to block
51
+ [0, [0, 2, 2, 2, 0]],
52
+ [1, [2, 0, 2, 2, 0]],
53
+ [1, [0, 2, 0, 2, 2]],
54
+
55
+ # my two, let's make three
56
+ [2, [0, 1, 0, 1, 0]],
57
+ [1, [0, 0, 1, 1, 0]],
58
+ [1, [0, 0, 1, 1, 0, 0]],
59
+
60
+ # really nothing to do, lets make mark next to another
61
+ [1, [0, 0, 1, 0, 0]],
62
+ [1, [0, 0, 1, 0]],
63
+ [0, [0, 1, 0]],
64
+
65
+ # okay... so, just block something
66
+ [1, [0, 0, 2, 0]],
67
+ [0, [0, 2, 0]]
68
+
69
+ ].each do |pattern|
70
+ puts "pattern = #{pattern}"
71
+ (0..board.size - 1).each do |row|
72
+ (0..board.size - 1).each do |col|
73
+ [[1, 1], [1, 0], [-1, 1], [0, 1], [-1, -1], [-1, 0], [1, -1], [0, -1]].each do |k|
74
+ rd = k[0]
75
+ cd = k[1]
76
+ s = 0
77
+ pattern[1].each_with_index.map do |pval, pind|
78
+ if pval == 0
79
+ break if board.on(row + rd*pind, col + cd*pind) != 0
80
+ elsif pval == 1
81
+ break if board.on(row + rd*pind, col + cd*pind) != self.sym
82
+ elsif pval == 2
83
+ break if board.on(row + rd*pind, col + cd*pind) != opsym
84
+ end
85
+ s += 1
86
+ end
87
+ if s == pattern[1].length
88
+ board.mark_cell(row + rd*pattern[0], col+ cd*pattern[0], self.sym)
89
+ return true
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ puts "oh shit"
97
+ return false
98
+ end
99
+ end
@@ -0,0 +1,132 @@
1
+ require_relative 'cell'
2
+
3
+ require 'rubygems'
4
+ require 'gosu'
5
+
6
+ class Board
7
+ attr_accessor :size
8
+ attr_accessor :grid
9
+
10
+ def initialize(game, size)
11
+ @game = game
12
+ @size = size
13
+ @grid = Array.new(size) { Array.new(size) }
14
+
15
+ (0..(@size - 1)).each do |row|
16
+ (0..(@size - 1)).each do |col|
17
+ @grid[row][col] = Cell.new(row, col, Cell::EMPTY)
18
+ end
19
+ end
20
+
21
+ @last_cell = false
22
+
23
+ @cell_empty = Gosu::Image.new(@game, 'media/cell_white_35_35.png')
24
+ @cell_a = Gosu::Image.new(@game, 'media/cell_x_35_35.png')
25
+ @cell_b = Gosu::Image.new(@game, 'media/cell_o_35_35.png')
26
+ @cell_alpha = Gosu::Image.new(@game, 'media/cell_alpha_35_35.png')
27
+ @cell_win = Gosu::Image.new(@game, 'media/cell_win_35_35.png')
28
+
29
+ end
30
+
31
+ def on(row, col)
32
+ return 0 if row < 0
33
+ return 0 if row >= @size
34
+ return 0 if col < 0
35
+ return 0 if col >= @size
36
+ @grid[row][col].value
37
+ end
38
+
39
+ def is_winning_move(row, col)
40
+ v = @grid[row][col].value
41
+
42
+ [[1,1], [1,0], [-1,1], [0,1]].each do |i|
43
+ s = 0
44
+ rd = i[0]
45
+ cd = i[1]
46
+ (1..4).each do |j|
47
+ break if v != @grid[row + rd*j][col + cd*j].value
48
+ s += 1
49
+ end
50
+ rd = -rd
51
+ cd = - cd
52
+ (1..4).each do |j|
53
+ break if v != @grid[row + rd*j][col + cd*j].value
54
+ s += 1
55
+ end
56
+
57
+ return true if s >= 4
58
+ end
59
+
60
+ return false
61
+ end
62
+
63
+ def mark_cell(row, col, v)
64
+ return false if !@grid[row][col].e?
65
+ @grid[row][col].set(v)
66
+
67
+ puts "marking cell row=#{row} col=#{col} v=#{v}"
68
+ @last_cell = [row, col]
69
+
70
+ if is_winning_move(row, col)
71
+ @game.state = :end
72
+ puts "winning!!"
73
+ end
74
+
75
+ true
76
+ end
77
+
78
+ CELL_SIZE=36.55
79
+
80
+ # return true if the cell has changed
81
+ def cell_clicked(mx, my, v)
82
+ mx2 = ((mx - (CELL_SIZE/2)) / CELL_SIZE).round
83
+ my2 = ((my - (CELL_SIZE/2)) / CELL_SIZE).round
84
+ mark_cell(mx2, my2, v)
85
+ end
86
+
87
+ def draw(mx, my, cursor)
88
+
89
+ if @last_cell != false
90
+ @cell_alpha.draw(@last_cell[0] * CELL_SIZE, @last_cell[1] * CELL_SIZE, ZOrder::LastCell)
91
+ end
92
+
93
+ #if @game.stat = :end
94
+ # TODO: highlight winning sequence
95
+ #end
96
+
97
+ (0..(@size - 1)).each do |i|
98
+ (0..(@size - 1)).each do |j|
99
+ x = i*CELL_SIZE
100
+ y = j*CELL_SIZE
101
+
102
+ #puts "drawing i=#{i} j=#{j} c=#{@grid[i][j].value}"
103
+
104
+ if @grid[i][j].e?
105
+ @cell_empty.draw(x, y, ZOrder::PlayerCell)
106
+ elsif @grid[i][j].a?
107
+ @cell_a.draw(x, y, ZOrder::PlayerCell)
108
+ elsif @grid[i][j].b?
109
+ @cell_b.draw(x, y, ZOrder::PlayerCell)
110
+ else
111
+ puts "a ou"
112
+ end
113
+
114
+
115
+
116
+ # Gosu::draw_quad(x-size, y-size, 0xffffffff, x+size, y-size, 0xffffffff, x-size, y+size, 0xffffffff, x+size, y+size, 0xffffffff, 0)
117
+ end
118
+ end
119
+
120
+ if @game.state != :end
121
+ mx2 = ((mx - (CELL_SIZE/2)) / CELL_SIZE).round
122
+ my2 = ((my - (CELL_SIZE/2)) / CELL_SIZE).round
123
+ if @grid[mx2][my2].e?
124
+ if cursor == 1
125
+ @cell_a.draw(mx2 * CELL_SIZE, my2 * CELL_SIZE, ZOrder::PlayerCell)
126
+ elsif cursor == 2
127
+ @cell_b.draw(mx2 * CELL_SIZE, my2 * CELL_SIZE, ZOrder::PlayerCell)
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end