Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
K
kachary3
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
kalpana acharya
kachary3
Commits
4883d091
Commit
4883d091
authored
Aug 7, 2024
by
kalpana acharya
Browse files
Options
Downloads
Patches
Plain Diff
assignment errors has been fixed
parent
13c3b093
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
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+20
-13
20 additions, 13 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+20
-12
20 additions, 12 deletions
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
with
40 additions
and
25 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+
20
−
13
View file @
4883d091
package
dotsandboxes
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.function.Consumer
;
...
...
@@ -50,6 +49,7 @@ public class DotsAndBoxesGrid {
final
int
players
;
private
int
player
=
1
;
public
int
getPlayer
()
{
return
player
;
}
...
...
@@ -98,7 +98,6 @@ public class DotsAndBoxesGrid {
return
boxOwners
[
x
][
y
];
}
/**
* Checks whether a box has been fully drawn (all four sides)
* @param x coordinate of the left side of the box
...
...
@@ -110,9 +109,13 @@ public class DotsAndBoxesGrid {
return
false
;
}
// 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).
return
true
;
// Check if all sides of the box are drawn
boolean
top
=
horizontals
[
x
][
y
];
boolean
bottom
=
horizontals
[
x
][
y
+
1
];
boolean
left
=
verticals
[
x
][
y
];
boolean
right
=
verticals
[
x
+
1
][
y
];
return
top
&&
bottom
&&
left
&&
right
;
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
...
...
@@ -139,7 +142,10 @@ public class DotsAndBoxesGrid {
throw
new
IndexOutOfBoundsException
(
String
.
format
(
"y was %d, which is out of range. Range is 0 to %d"
,
y
,
height
));
}
// FIXME: You need to throw an exception if the line was already drawn.
// Throw an exception if the line was already drawn
if
(
horizontals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"This horizontal line is already drawn."
);
}
this
.
horizontals
[
x
][
y
]
=
true
;
...
...
@@ -170,10 +176,14 @@ public class DotsAndBoxesGrid {
throw
new
IndexOutOfBoundsException
(
String
.
format
(
"y was %d, which is out of range. Range is 0 to %d"
,
y
,
height
-
1
));
}
// You need to throw an exception if the line was already drawn.
// Throw an exception if the line was already drawn
if
(
verticals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"This vertical line is already drawn."
);
}
this
.
verticals
[
x
][
y
]
=
true
;
// Try to claim the north or south boxes
// Try to claim the east or west boxes
boolean
claimE
=
claimBox
(
x
,
y
,
player
);
boolean
claimW
=
claimBox
(
x
-
1
,
y
,
player
);
if
(
claimE
||
claimW
)
{
...
...
@@ -184,7 +194,6 @@ public class DotsAndBoxesGrid {
notifyObservers
();
return
false
;
}
}
public
boolean
gameComplete
()
{
...
...
@@ -192,6 +201,4 @@ public class DotsAndBoxesGrid {
// The grid is complete if "for all rows, all the boxes in that row have a non-zero owner"
return
Arrays
.
stream
(
boxOwners
).
allMatch
((
row
)
->
Arrays
.
stream
(
row
).
allMatch
((
owner
)
->
owner
>
0
));
}
}
This diff is collapsed.
Click to expand it.
src/test/java/dotsandboxes/DotsAndBoxesGridTest.java
+
20
−
12
View file @
4883d091
...
...
@@ -32,19 +32,19 @@ public class DotsAndBoxesGridTest {
logger
.
info
(
"Testing square completion logic"
);
// Create a new game grid
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
();
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
(
3
,
3
,
2
);
// Draw lines to form a square
grid
.
draw
Line
(
0
,
0
,
0
,
1
);
// Top horizontal line
grid
.
draw
Line
(
0
,
0
,
1
,
0
);
// Left vertical line
grid
.
draw
Line
(
0
,
1
,
1
,
1
);
// Bottom horizontal line
grid
.
draw
Line
(
1
,
0
,
1
,
1
);
// Right vertical line
grid
.
draw
Horizontal
(
0
,
0
,
1
);
// Top horizontal line
grid
.
draw
Vertical
(
0
,
0
,
1
);
// Left vertical line
grid
.
draw
Horizontal
(
0
,
1
,
1
);
// Bottom horizontal line
grid
.
draw
Vertical
(
1
,
0
,
1
);
// Right vertical line
// Test if the square at (0, 0) is complete
boolean
isComplete
=
grid
.
isSquare
Complete
(
0
,
0
);
boolean
isComplete
=
grid
.
box
Complete
(
0
,
0
);
// The
bug is that isSquareComplete always returns true
assert
Fals
e
(
isComplete
,
"The square
completion check should fail because the method is broken
."
);
// The
test should pass now that the logic is fixed
assert
Tru
e
(
isComplete
,
"The square
at (0, 0) should be complete
."
);
}
@Test
...
...
@@ -52,14 +52,22 @@ public class DotsAndBoxesGridTest {
logger
.
info
(
"Testing drawing the same line twice throws exception"
);
// Create a new game grid
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
();
DotsAndBoxesGrid
grid
=
new
DotsAndBoxesGrid
(
3
,
3
,
2
);
// Draw a line once
grid
.
draw
Line
(
0
,
0
,
0
,
1
);
grid
.
draw
Horizontal
(
0
,
0
,
1
);
// Attempt to draw the same line again
assertThrows
(
IllegalStateException
.
class
,
()
->
{
grid
.
drawLine
(
0
,
0
,
0
,
1
);
},
"Drawing the same line twice should throw IllegalStateException."
);
grid
.
drawHorizontal
(
0
,
0
,
1
);
},
"Drawing the same horizontal line twice should throw IllegalStateException."
);
// Draw a vertical line once
grid
.
drawVertical
(
0
,
0
,
1
);
// Attempt to draw the same vertical line again
assertThrows
(
IllegalStateException
.
class
,
()
->
{
grid
.
drawVertical
(
0
,
0
,
1
);
},
"Drawing the same vertical line twice should throw IllegalStateException."
);
}
}
This diff is collapsed.
Click to expand it.
kalpana acharya
@kalpana
mentioned in issue
#1 (closed)
·
Aug 7, 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