Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COSC220 Assessment2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jennica Llamera
COSC220 Assessment2
Commits
f0a97991
You need to sign in or sign up before continuing.
Commit
f0a97991
authored
Jul 31, 2024
by
Jennica Llamera
Browse files
Options
Downloads
Patches
Plain Diff
Fixed box completion and redrawn line errors
parent
ddfb287b
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+12
-2
12 additions, 2 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+14
-30
14 additions, 30 deletions
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
with
26 additions
and
32 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+
12
−
2
View file @
f0a97991
...
...
@@ -112,8 +112,12 @@ public class DotsAndBoxesGrid {
// A box is complete if the north and south horizontals and the east and west verticals have all been drawn.
// FIXME: You'll need to fix this code (after writing a test first).
else
if
(
getHorizontal
(
x
,
y
)
&&
getHorizontal
(
x
,
y
+
1
)
&&
getVertical
(
x
,
y
)
&&
getVertical
(
x
+
1
,
y
))
{
return
true
;
}
return
false
;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
private
boolean
claimBox
(
int
x
,
int
y
,
int
p
)
{
...
...
@@ -140,6 +144,9 @@ public class DotsAndBoxesGrid {
}
// FIXME: You need to throw an exception if the line was already drawn.
if
(
horizontals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"Line has been already drawn!"
);
}
this
.
horizontals
[
x
][
y
]
=
true
;
...
...
@@ -171,8 +178,11 @@ public class DotsAndBoxesGrid {
}
// You need to throw an exception if the line was already drawn.
if
(
verticals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"Line has been already drawn!"
);
}
this
.
verticals
[
x
][
y
]
=
true
;
// Try to claim the north or south boxes
boolean
claimE
=
claimBox
(
x
,
y
,
player
);
boolean
claimW
=
claimBox
(
x
-
1
,
y
,
player
);
...
...
This diff is collapsed.
Click to expand it.
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+
14
−
30
View file @
f0a97991
...
...
@@ -24,7 +24,8 @@ public class DotsAndBoxesGridTest {
*
* This is a dummy test just to show that the test suite itself runs
*/
public
void
setUp
()
{
@BeforeEach
public
void
setup
()
{
// Setting up a new DotsAndBoxesGrid for every test
gridTest
=
new
DotsAndBoxesGrid
(
4
,
4
,
2
);
}
...
...
@@ -37,41 +38,24 @@ public class DotsAndBoxesGridTest {
// FIXME: You need to write tests for the two known bugs in the code.
@Test
public
void
test
ScoreCalcula
tion
()
{
logger
.
info
(
"Testing
score calculation on
box completion"
);
public
void
test
BoxComple
tion
()
{
logger
.
info
(
"Testing box completion"
);
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
(
4
,
3
,
2
);
grid
.
drawHorizontal
(
0
,
0
,
1
);
grid
.
drawVertical
(
0
,
0
,
1
);
grid
.
drawVertical
(
1
,
0
,
1
);
boolean
boxCompleted
=
grid
.
drawHorizontal
(
1
,
0
,
1
);
// This should complete the box
gridTest
.
drawHorizontal
(
0
,
0
,
1
);
gridTest
.
drawVertical
(
0
,
0
,
1
);
gridTest
.
drawHorizontal
(
0
,
1
,
1
);
gridTest
.
drawVertical
(
1
,
0
,
1
);
// Check if the box was completed and claimed by the correct player
assertTrue
(
boxCompleted
,
"Box should be completed and claimed"
);
assertEquals
(
1
,
grid
.
getBoxOwner
(
0
,
0
),
"The box owner should be player 1"
);
}
@Test
public
void
testInvalidMoveHandling
()
{
logger
.
info
(
"Testing invalid move handling"
);
assertTrue
(
gridTest
.
boxComplete
(
0
,
0
),
"Box should be completed"
);
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
(
4
,
3
,
2
);
grid
.
drawHorizontal
(
0
,
0
,
1
);
// Attempt to draw the same horizontal line again
Exception
exception
=
assertThrows
(
IndexOutOfBoundsException
.
class
,
()
->
{
grid
.
drawHorizontal
(
0
,
0
,
1
);
// This should throw an exception
});
assertEquals
(
"Line has already been drawn"
,
exception
.
getMessage
());
}
@Test
public
void
testBoxCompletionLogic
()
{
logger
.
info
(
"Testing box completion logic"
);
public
void
testVerticalLineRedrawn
()
{
logger
.
info
(
"Testing if throws exception if redrawn a line "
);
gridTest
.
drawVertical
(
0
,
0
,
1
);
assertThrows
(
IllegalStateException
.
class
,
()
->
gridTest
.
drawVertical
(
0
,
0
,
1
));
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
(
4
,
3
,
2
);
// Attempt to complete a box without drawing all lines
boolean
isComplete
=
grid
.
boxComplete
(
0
,
0
);
assertFalse
(
isComplete
,
"Box should not be complete without all lines drawn"
);
}
}
This diff is collapsed.
Click to expand it.
Jennica Llamera
@jllamera
mentioned in issue
#1 (closed)
·
Jul 31, 2024
mentioned in issue
#1 (closed)
mentioned in issue #1
Toggle commit list
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