Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Johan Gustav Thrane-Nielsen
inf101.v20.sem2.losning
Commits
20394c11
Commit
20394c11
authored
Mar 25, 2020
by
Anna Maria Eilertsen
Browse files
added docs
parent
49cc2733
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/sem2/mnkgames/MNKGame.java
View file @
20394c11
...
...
@@ -2,6 +2,13 @@ package inf101.v20.sem2.mnkgames;
import
inf101.v20.lab4.grid.Grid
;
/**
* An mnk-game
* https://en.wikipedia.org/wiki/M,n,k-game
*
* @author Anna Eilertsen - anna.eilertsen@uib.no
*
*/
public
abstract
class
MNKGame
{
private
int
width
;
private
int
height
;
...
...
@@ -10,18 +17,20 @@ public abstract class MNKGame {
private
Piece
currentPlayer
;
private
Piece
winnerPiece
;
/**
* A BLACK or WHITE piece in a two-player MNKGame or NONE denoting no piece
*/
public
enum
Piece
{
NONE
,
BLACK
,
WHITE
;
public
Piece
next
()
{
return
this
==
Piece
.
BLACK
?
Piece
.
WHITE
:
Piece
.
BLACK
;
}
}
/**
*
*
Creates a new game
* @param m width: dimensions along x-axis
* @param n height: dimensions along y-axis
* @param k
row length win condition
* @param k
the number of pieces in a row required to win the game
*/
MNKGame
(
int
m
,
int
n
,
int
k
){
width
=
m
;
...
...
@@ -32,19 +41,34 @@ public abstract class MNKGame {
winnerPiece
=
Piece
.
NONE
;
}
/**
* @return gets the width of the game: used to delimit x values
*/
public
int
getWidth
()
{
return
width
;
}
/**
* @return gets the height of the game: used to delimit y values
*/
public
int
getHeight
()
{
return
height
;
}
/**
* @return how many pieces in a row is required to win this game
*/
public
int
getRowsToWin
()
{
return
k
;
}
/**
* Fetches the piece at the provided position
* @param x the x-coordinate such that 0 <= x < width
* @param y the y-coordinate such that 0 <= y < height
* @throws IllegalArgumentException if the arguments were invalid
* @return the piece in the provided position or Piece.NONE if the position has no piece
*/
public
Piece
getPieceAt
(
int
x
,
int
y
)
{
if
(
x
>=
width
||
x
<
0
||
y
>=
height
||
y
<
0
)
throw
new
IllegalArgumentException
(
"Invalid coordinate"
);
...
...
@@ -72,14 +96,26 @@ public abstract class MNKGame {
currentPlayer
=
currentPlayer
.
next
();
}
/**
* @return true if someone placed a piece that let them win game, false otherwise
*/
public
boolean
hasWinner
()
{
return
winnerPiece
!=
Piece
.
NONE
;
}
/**
* @return the piece that won the game
*/
public
Piece
getWinner
()
{
return
winnerPiece
;
}
/**
* Checks for k in a row such that the line intersects (x, y)
* @param x the x coordinate
* @param y the y coordinate
* @return true if there are k in a row such that the line intersects (x, y), false otherwise
*/
private
boolean
checkWinCondition
(
int
x
,
int
y
)
{
int
count
=
0
;
Piece
current
=
grid
.
get
(
x
,
y
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment