Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Y
YashAssign1
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
Yash Raosaheb Aringale
YashAssign1
Commits
03ab7908
Commit
03ab7908
authored
Jul 31, 2024
by
Will Billingsley
Browse files
Options
Downloads
Patches
Plain Diff
Fix bugs in boxComplete and draw methods
parent
a647841c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+49
-50
49 additions, 50 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
with
49 additions
and
50 deletions
src/main/java/dotsandboxes/DotsAndBoxesGrid.java
+
49
−
50
View file @
03ab7908
...
@@ -109,12 +109,10 @@ public class DotsAndBoxesGrid {
...
@@ -109,12 +109,10 @@ public class DotsAndBoxesGrid {
if
(
x
>=
width
-
1
||
x
<
0
||
y
>=
height
-
1
||
y
<
0
)
{
if
(
x
>=
width
-
1
||
x
<
0
||
y
>=
height
-
1
||
y
<
0
)
{
return
false
;
return
false
;
}
}
return
getHorizontal
(
x
,
y
)
&&
getHorizontal
(
x
,
y
+
1
)
&&
getVertical
(
x
,
y
)
&&
getVertical
(
x
+
1
,
y
);
// 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
;
}
}
/** Tries to claim a box for a player. If the box is complete, sets the ownership and returns true. */
/** 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
)
{
private
boolean
claimBox
(
int
x
,
int
y
,
int
p
)
{
if
(
boxComplete
(
x
,
y
))
{
if
(
boxComplete
(
x
,
y
))
{
...
@@ -139,11 +137,12 @@ public class DotsAndBoxesGrid {
...
@@ -139,11 +137,12 @@ public class DotsAndBoxesGrid {
throw
new
IndexOutOfBoundsException
(
String
.
format
(
"y was %d, which is out of range. Range is 0 to %d"
,
y
,
height
));
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.
if
(
horizontals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"Line already drawn"
);
}
this
.
horizontals
[
x
][
y
]
=
true
;
this
.
horizontals
[
x
][
y
]
=
true
;
// Try to claim the north or south boxes
boolean
claimN
=
claimBox
(
x
,
y
-
1
,
player
);
boolean
claimN
=
claimBox
(
x
,
y
-
1
,
player
);
boolean
claimS
=
claimBox
(
x
,
y
,
player
);
boolean
claimS
=
claimBox
(
x
,
y
,
player
);
if
(
claimN
||
claimS
)
{
if
(
claimN
||
claimS
)
{
...
@@ -156,6 +155,7 @@ public class DotsAndBoxesGrid {
...
@@ -156,6 +155,7 @@ public class DotsAndBoxesGrid {
}
}
}
}
/**
/**
* "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets that line to true)
* "Draws" a vertical line, from grid point (x, y) to (x, y + 1). (i.e. sets that line to true)
* @param x
* @param x
...
@@ -170,10 +170,11 @@ public class DotsAndBoxesGrid {
...
@@ -170,10 +170,11 @@ public class DotsAndBoxesGrid {
throw
new
IndexOutOfBoundsException
(
String
.
format
(
"y was %d, which is out of range. Range is 0 to %d"
,
y
,
height
-
1
));
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.
if
(
verticals
[
x
][
y
])
{
throw
new
IllegalStateException
(
"Line already drawn"
);
}
this
.
verticals
[
x
][
y
]
=
true
;
this
.
verticals
[
x
][
y
]
=
true
;
// Try to claim the north or south boxes
boolean
claimE
=
claimBox
(
x
,
y
,
player
);
boolean
claimE
=
claimBox
(
x
,
y
,
player
);
boolean
claimW
=
claimBox
(
x
-
1
,
y
,
player
);
boolean
claimW
=
claimBox
(
x
-
1
,
y
,
player
);
if
(
claimE
||
claimW
)
{
if
(
claimE
||
claimW
)
{
...
@@ -184,9 +185,7 @@ public class DotsAndBoxesGrid {
...
@@ -184,9 +185,7 @@ public class DotsAndBoxesGrid {
notifyObservers
();
notifyObservers
();
return
false
;
return
false
;
}
}
}
}
public
boolean
gameComplete
()
{
public
boolean
gameComplete
()
{
// Students who took COSC250 might recognise this style of code. This is Java's version of higher order functions.
// Students who took COSC250 might recognise this style of code. This is Java's version of higher order functions.
// The grid is complete if "for all rows, all the boxes in that row have a non-zero owner"
// The grid is complete if "for all rows, all the boxes in that row have a non-zero owner"
...
...
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