Skip to content

Commit

Permalink
Fixed bug in == method of connections (thx wollw!)
Browse files Browse the repository at this point in the history
Also:
- Fixed link in readme to code of conduct.
- Removed trailing whitespace.
  • Loading branch information
SKoschnicke committed Dec 7, 2015
1 parent 047ec4c commit cce7951
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 130 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ Bug reports and pull requests are welcome on GitHub at
https://github.com/CAU-Kiel-Tech-Inf/socha_ruby_client. This project
is intended to be a safe, welcoming space for collaboration, and
contributors are expected to adhere to the
[Contributor Covenant](contributor-covenant.org) code of conduct.
[code of conduct](CODE_OF_CONDUCT.md) (from
[Contributor Covenant](http://contributor-covenant.org)).
7 changes: 7 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
= 0.1.2

- Fixed bug in == (test for equality) method of connections (thanks to
wollw!).
- Fixed link in readme to code of conduct.
- Removed trailing whitespace.

= 0.1.1

Compatibility to Ruby 1.9 (source encoding issues)
Expand Down
74 changes: 37 additions & 37 deletions lib/software_challenge_client/board.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Board
# @!attribute [r] connections
# @return [Array<Connection>] the board's connections
attr_reader :connections

def initialize
self.init
end

# Initializes the board
#
# @param init [Boolean] if 'true', then the board will be initialized with swamps, otherwise the board will be completely empty
Expand All @@ -31,9 +31,9 @@ def initialize(init)
self.makeClearBoard
end
end

# Initializes the board with swamps
def init
def init
@fields = Array.new(Constants::SIZE) {Array.new(Constants::SIZE)}
@fields[0][0] = Field.new(FieldType::SWAMP, 0, 0)
@fields[0][Constants::SIZE - 1] = Field.new(FieldType::SWAMP, 0, Constants::SIZE - 1)
Expand All @@ -48,14 +48,14 @@ def init
@fields[Constants::SIZE - 1][y] = Field.new(FieldType::BLUE, Constants::SIZE - 1, y);
end
for x in 1..(Constants::SIZE - 2)
for y in 1..(Constants::SIZE - 2)
for y in 1..(Constants::SIZE - 2)
@fields[x][y] = Field.new(FieldType::NORMAL, x, y);
end
end
self.placeSwamps()
@connections = Array.new;
end

# Places swamps at random coordinates
def placeSwamps
# big swamp
Expand Down Expand Up @@ -88,25 +88,25 @@ def placeSwamps
self.fields[x][y].type = FieldType::SWAMP
end

# creates a cleared board

# creates a cleared board
def makeClearBoard
@fields = Array.new(Constants::SIZE, Array.new(Constants::SIZE))
@connections = Array.new
end

# gets the owner's color for the field at the coordinate (x, y)
#
#
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
# @return [PlayerColor] owner's color of field (x, y)
def getOwnerColor(x, y)
def getOwnerColor(x, y)
return self.fields[x][y].ownerColor
end


# sets the owner's color for the field at the coordinate (x, y)
#
#
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
# @param player [Player] new owner of field (x, y)
Expand All @@ -116,7 +116,7 @@ def put(x, y, player)
end

# creates wires at the coordinate (x, y), if it is possible
#
#
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
def createNewWires(x, y)
Expand Down Expand Up @@ -144,11 +144,11 @@ def createNewWires(x, y)
if self.checkPossibleWire(x, y, x + 1, y + 2)
self.createWire(x, y, x + 1, y + 2)
end
end

end

# creates a new wire
#
#
# @param x1 [Integer] x-coordinate starting point
# @param y1 [Integer] y-coordinate starting point
# @param x2 [Integer] x-coordinate ending point
Expand All @@ -158,7 +158,7 @@ def createWire(x1, y1, x2, y2)
end

# checks, if a wire can be placed at specified coordinates
#
#
# @param x1 [Integer] x-coordinate starting point
# @param y1 [Integer] y-coordinate starting point
# @param x2 [Integer] x-coordinate ending point
Expand All @@ -174,7 +174,7 @@ def checkPossibleWire(x1, y1, x2, y2)
end

# checks, if a blocking wire exists
#
#
# @param x1 [Integer] x-coordinate starting point
# @param y1 [Integer] y-coordinate starting point
# @param x2 [Integer] x-coordinate ending point
Expand All @@ -187,7 +187,7 @@ def existsBlockingWire(x1, y1, x2, y2)
for y in smallerY..biggerY # checks all 6 Fields, from
# where there could be
# blocking connections
if !self.fields[x][y].ownerColor.nil? && (x != x1 || y != y1) &&
if !self.fields[x][y].ownerColor.nil? && (x != x1 || y != y1) &&
(x != x2 || y != y2) # excludes the Fields with no owner and
# the fields (x1, y2), (x2, y2)
# themselves.
Expand All @@ -199,17 +199,17 @@ def existsBlockingWire(x1, y1, x2, y2)
end
return false
end

# gets connections for the coordinate (x, y)
#
#
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
# @return [Array] Array of connections from field (x, y)
def getConnections(x, y)
xyConnections = Array.new
if !self.connections.nil?
for c in self.connections
if c.x1 == x && c.y1 == y
if c.x1 == x && c.y1 == y
xyConnections.push(Connection.new(x, y, c.x2, c.y2, c.ownerColor))
end
if c.x2 == x && c.y2 == y
Expand All @@ -229,11 +229,11 @@ def onSegment(px,py,qx,qy,rx,ry)
end
return false
end

def orientation(px,py,qx,qy,rx,ry)
val = (qy - py) * (rx - qx) -
(qx - px) * (ry - qy)

if val == 0
return 0
end
Expand All @@ -242,44 +242,44 @@ def orientation(px,py,qx,qy,rx,ry)
end
return 2
end

def doIntersect(p1x,p1y, q1x,q1y, p2x,p2y, q2x,q2y)
o1 = orientation(p1x,p1y, q1x,q1y, p2x,p2y)
o2 = orientation(p1x,p1y, q1x,q1y, q2x,q2y)
o3 = orientation(p2x,p2y, q2x,q2y, p1x,p1y)
o4 = orientation(p2x,p2y, q2x,q2y, q1x,q1y)

if o1 != o2 && o3 != o4
return true
end

if o1 == 0 && onSegment(p1x,p1y, p2x,p2y, q1x,q1y)
return true
end

if o2 == 0 && onSegment(p1x,p1y, q2x,q2y, q1x,q1y)
return true
end

if o3 == 0 && onSegment(p2x,p2x, p1x,p1y, q2x,q2y)
return true
end

if o4 == 0 && onSegment(p2x,p2y, q1x,q1y, q2x,q2y)
return true
end

return false
end

# checks for the wire (x1, y1) -> (x2, y2), if it is blocked by any connection going out from (x,y).
#
#
# @param x1 [Integer] x-coordinate starting point
# @param y1 [Integer] y-coordinate starting point
# @param x2 [Integer] x-coordinate ending point
# @param y2 [Integer] y-coordinate ending point
# @param x [Integer] x-coordinate comparison field
# @param y [Integer] y-coordinate comparison field
# @param y [Integer] y-coordinate comparison field
# @return [Boolean] 'true', if another wire would block the creation of a new wire at specified coordinates
def isWireBlocked(x1, y1, x2, y2, x, y)
for c in getConnections(x, y)
Expand All @@ -293,7 +293,7 @@ def isWireBlocked(x1, y1, x2, y2, x, y)
def to_s
return self.fields.map { |f| f.map {|i| (i.ownerColor==PlayerColor::RED ? 'R' : (i.ownerColor==PlayerColor::BLUE ? 'B' : (i.type==FieldType::SWAMP ? 'S' : (i.type==FieldType::RED ? 'r' : (i.type==FieldType::BLUE ? 'b' : ' '))))) }.join(",")}.join("\n")
end

def ==(another_board)
for x in 0..(Constants.SIZE - 1)
for y in 0..(Constants.SIZE - 1)
Expand All @@ -310,7 +310,7 @@ def ==(another_board)
return false
end
end

return true;
end
end
2 changes: 1 addition & 1 deletion lib/software_challenge_client/client_interface.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: UTF-8
class ClientInterface
attr_accessor :gamestate

def getMove
raise "Not yet implemented"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/software_challenge_client/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Condition
# @!attribute [r] reason
# @return [String] winning reason
attr_reader :reason

# Initializes the winning Condition with a player and a reason
# @param winer [Player] winning player
# @param reason [String] winning reason
def initialize(winner, reason)
@winner = winner
@reason = reason
end

end
24 changes: 15 additions & 9 deletions lib/software_challenge_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@ class Connection
# @!attribute [r] ownerColor
# @return [PlayerColor] connection's owner's color
attr_reader :ownerColor

# Initializer
#
#
# @param x1 [Integer] x-coordinate starting point
# @param y1 [Integer] y-coordinate starting point
# @param x2 [Integer] x-coordinate ending point
# @param y2 [Integer] y-coordinate ending point
# @param owner [PlayerColor] connection's owner's color
def initialize(x1, y1, x2, y2, ownerColor)
def initialize(x1, y1, x2, y2, ownerColor)
@x1 = x1
@x2 = x2
@y1 = y1
@y2 = y2
@ownerColor = ownerColor
end

def ==(another_connection)
if(self.x1 == another_connection.x1 && self.y1 == another_connection.y1 && self.x2 == another_connection.x2 && self.y2 == another_connection.y2 ||
self.x1 == another_connection.x2 && self.y1 == another_connection.y2 && self.x2 == another_connection.x1 && self.y2 == another_connection.y1)
return ownerColor == c.ownerColor
if (self.x1 == another_connection.x1 &&
self.y1 == another_connection.y1 &&
self.x2 == another_connection.x2 &&
self.y2 == another_connection.y2 ||
self.x1 == another_connection.x2 &&
self.y1 == another_connection.y2 &&
self.x2 == another_connection.x1 &&
self.y2 == another_connection.y1)
return ownerColor == another_connection.ownerColor
else
return false
end
end

def to_s
return "#{self.ownerColor} : (#{self.x1}, #{self.y1}) - (#{self.x2}, #{self.y2})"
end
end
end
12 changes: 6 additions & 6 deletions lib/software_challenge_client/debug_hint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# @author Ralf-Tobias Diekert
# A debug hint, that can be added to a move
class DebugHint

# @!attribute [r] content
# @return [String] a hint
attr_reader :content

# @overload initialize
# Creates an empty hint
# @overload initialize(key, value)
Expand All @@ -17,17 +17,17 @@ class DebugHint
# Creates a hint with specified content
# @param content of the hint
def initialize

end

def initialize(key, value)
if key.nil?
self.content = "#{value}"
else
else
self.content = "#{key} = #{value}"
end
end

def initialize(content)
self.content = "#{content}"
end
Expand Down
14 changes: 7 additions & 7 deletions lib/software_challenge_client/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class Field
# @!attribute [r] y
# @return [Integer] the field's y-coordinate
attr_reader :y

# Initializer
#
#
# @param type [FieldType] field type
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
Expand All @@ -29,14 +29,14 @@ def initialize(type, x, y)
@x = x
@y = y
end

def ==(another_field)
return self.ownerColor == another_field.ownerColor &&
self.type == another_field.type &&
self.x == another_field.x &&
return self.ownerColor == another_field.ownerColor &&
self.type == another_field.type &&
self.x == another_field.x &&
self.y == another_field.y
end

def to_s
return "Field: x = #{self.x}, y = #{self.y}, owner = #{self.ownerColor}, type = #{self.type}"
end
Expand Down
Loading

0 comments on commit cce7951

Please sign in to comment.