Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sidecheck
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Kevin Lyda
sidecheck
Commits
f08d9286
Commit
f08d9286
authored
8 years ago
by
Kevin Lyda
Browse files
Options
Downloads
Patches
Plain Diff
Check for pieces across a board.
parents
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
sidecheck.go
+181
-0
181 additions, 0 deletions
sidecheck.go
sidecheck_test.go
+97
-0
97 additions, 0 deletions
sidecheck_test.go
with
278 additions
and
0 deletions
sidecheck.go
0 → 100644
+
181
−
0
View file @
f08d9286
package
sidecheck
import
(
"strings"
)
const
(
BOARD
int
=
iota
PIECE
SELECTED
)
type
coords
struct
{
x
,
y
int
}
func
coordsAround
(
x
,
y
int
)
[]
coords
{
return
[]
coords
{
{
x
-
1
,
y
-
1
},
{
x
-
1
,
y
},
{
x
-
1
,
y
+
1
},
{
x
,
y
-
1
},
{
x
,
y
+
1
},
{
x
+
1
,
y
-
1
},
{
x
+
1
,
y
},
{
x
+
1
,
y
+
1
},
}
}
type
Board
struct
{
rows
,
cols
int
board
[][]
int
}
func
NewBoard
(
rows
,
cols
int
)
*
Board
{
board
:=
Board
{
rows
,
cols
,
make
([][]
int
,
rows
),
}
for
i
:=
0
;
i
<
rows
;
i
++
{
board
.
board
[
i
]
=
make
([]
int
,
cols
)
}
return
&
board
}
func
StringToBoard
(
b
string
)
*
Board
{
rows
:=
strings
.
Split
(
b
,
"
\n
"
)
rowMax
:=
len
(
rows
)
if
rowMax
==
0
{
panic
(
"Board needs to have at least 1 row"
)
}
colMax
:=
len
(
rows
[
0
])
if
colMax
==
0
{
panic
(
"Board needs to have at least 1 column"
)
}
board
:=
*
NewBoard
(
rowMax
,
colMax
)
for
i
:=
0
;
i
<
rowMax
;
i
++
{
if
len
(
rows
[
i
])
!=
colMax
{
panic
(
"Column mismatch"
)
}
for
j
:=
0
;
j
<
colMax
;
j
++
{
switch
rows
[
i
][
j
]
{
case
'.'
:
board
.
board
[
i
][
j
]
=
BOARD
case
'o'
:
board
.
board
[
i
][
j
]
=
PIECE
default
:
panic
(
"Unknown board char encountered"
)
}
}
}
return
&
board
}
// Piecep returns true if there's a piece at x, y.
func
(
b
Board
)
Piecep
(
x
,
y
int
)
bool
{
if
x
<
0
||
y
<
0
||
x
>=
b
.
rows
||
y
>=
b
.
cols
{
return
false
}
return
b
.
board
[
x
][
y
]
==
PIECE
}
// String converts Board to a string.
func
(
b
Board
)
String
()
string
{
var
result
string
spotChar
:=
[]
string
{
"."
,
"o"
,
"+"
}
for
_
,
row
:=
range
b
.
board
{
for
_
,
spot
:=
range
row
{
result
+=
spotChar
[
spot
]
}
result
+=
"
\n
"
}
return
result
}
// NScheck checks if any pieces allow for a path across.
func
(
b
Board
)
NScheck
()
bool
{
for
j
:=
0
;
j
<
b
.
cols
;
j
++
{
if
b
.
Piecep
(
0
,
j
)
{
if
found
:=
b
.
nscheck
(
newPiece
(
0
,
j
));
found
{
return
true
}
}
}
return
false
}
func
(
b
Board
)
nscheck
(
p
*
piece
)
bool
{
if
p
.
x
==
(
b
.
rows
-
1
)
{
return
true
}
cs
:=
coordsAround
(
p
.
x
,
p
.
y
)
for
_
,
c
:=
range
cs
{
if
c
.
y
>
0
&&
b
.
Piecep
(
c
.
x
,
c
.
y
)
&&
!
p
.
seen
(
c
.
x
,
c
.
y
)
{
next
:=
newPiece
(
c
.
x
,
c
.
y
)
next
.
parent
=
p
if
found
:=
b
.
nscheck
(
next
);
found
{
return
true
}
}
}
return
false
}
// WEcheck checks if any pieces allow for a path across.
func
(
b
Board
)
WEcheck
()
bool
{
for
i
:=
0
;
i
<
b
.
rows
;
i
++
{
if
b
.
Piecep
(
i
,
0
)
{
if
found
:=
b
.
wecheck
(
newPiece
(
i
,
0
));
found
{
return
true
}
}
}
return
false
}
func
(
b
Board
)
wecheck
(
p
*
piece
)
bool
{
if
p
.
y
==
(
b
.
cols
-
1
)
{
return
true
}
cs
:=
coordsAround
(
p
.
x
,
p
.
y
)
for
_
,
c
:=
range
cs
{
if
c
.
x
>
0
&&
b
.
Piecep
(
c
.
x
,
c
.
y
)
&&
!
p
.
seen
(
c
.
x
,
c
.
y
)
{
next
:=
newPiece
(
c
.
x
,
c
.
y
)
next
.
parent
=
p
if
found
:=
b
.
wecheck
(
next
);
found
{
return
true
}
}
}
return
false
}
// piece tracks a connected line of pieces through a board.
type
piece
struct
{
x
,
y
int
parent
*
piece
}
// newPiece creates a new node.
func
newPiece
(
x
,
y
int
)
*
piece
{
p
:=
&
piece
{
x
:
x
,
y
:
y
,
parent
:
nil
,
}
return
p
}
// seen returns is a coord has been seen.
func
(
p
piece
)
seen
(
x
,
y
int
)
bool
{
pp
:=
p
.
parent
for
pp
!=
nil
{
if
pp
.
x
==
x
&&
pp
.
y
==
y
{
return
true
}
pp
=
pp
.
parent
}
return
false
}
This diff is collapsed.
Click to expand it.
sidecheck_test.go
0 → 100644
+
97
−
0
View file @
f08d9286
package
sidecheck
import
(
"fmt"
"testing"
)
type
boardTestType
struct
{
NS
,
WE
bool
board
string
}
var
boardTests
=
[]
boardTestType
{
{
false
,
true
,
`.....
.....
ooooo
.....
.....`
,
},
{
false
,
false
,
`...........o................
............o...............
.............o..............
.............o..............
.............o..............
.............o..............
.............o..............
...........ooo..............
...........o.o..............
...........ooo..............
............................`
,
},
{
true
,
false
,
`.............o..............
.............o..............
.............o..............
.............o..............
.............o..............
.............o..............
.............o..............
.............o..............
.............o.......ooooooo
.............ooooooooo......
.....................o......`
,
},
{
false
,
true
,
`............................
............................
............................
............................
............................
.........o................oo
ooooooooo................o..
....o....oooooo.........o...
....o.........o........o....
.....o......oo.......oo.....
......oooo....oo...oo.......
..........ooo...ooo.........
............................`
,
},
}
func
TestBoards
(
t
*
testing
.
T
)
{
for
_
,
boardTest
:=
range
boardTests
{
board
:=
StringToBoard
(
boardTest
.
board
)
if
boardTest
.
NS
!=
board
.
NScheck
()
{
fmt
.
Println
(
"NS FAIL"
)
if
boardTest
.
NS
{
t
.
Errorf
(
"Failed to find NS path."
)
}
else
{
t
.
Errorf
(
"Found NS path but shouldn't have."
)
}
}
if
boardTest
.
WE
!=
board
.
WEcheck
()
{
fmt
.
Println
(
"WE FAIL"
)
if
boardTest
.
WE
{
t
.
Errorf
(
"Failed to find WE path."
)
}
else
{
t
.
Errorf
(
"Found WE path but shouldn't have."
)
}
}
fmt
.
Println
(
board
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment