Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
Pentago
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9628055
Pentago
Commits
93b3ca67
Commit
93b3ca67
authored
Apr 11, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pre-final
parent
bd314920
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
366 additions
and
274 deletions
+366
-274
GameRules.class
out/production/Pentago.u/GameRules.class
+0
-0
Main.class
out/production/Pentago.u/Main.class
+0
-0
Map.class
out/production/Pentago.u/Map.class
+0
-0
GameRules.java
src/GameRules.java
+220
-0
Main.java
src/Main.java
+47
-38
Map.java
src/Map.java
+99
-236
No files found.
out/production/Pentago.u/GameRules.class
0 → 100644
View file @
93b3ca67
File added
out/production/Pentago.u/Main.class
View file @
93b3ca67
No preview for this file type
out/production/Pentago.u/Map.class
View file @
93b3ca67
No preview for this file type
src/GameRules.java
0 → 100644
View file @
93b3ca67
import
java.util.ArrayList
;
import
java.util.Scanner
;
/**
* provide game rules
* @author Narges Salehi
*/
public
class
GameRules
{
//map of game
private
Map
map
;
/**
* creat new game rules
*/
public
GameRules
()
{
map
=
new
Map
();
}
/**
* check all the lines with wining condition for a player - check circles color
* @param Color of player
* @return true or false
*/
public
boolean
checkWinner
(
String
Color
)
{
// count lines with wining condition
int
counter
=
0
;
// check horizontal and vertical lines
counter
+=
map
.
checkLine
(
0
,
1
,
2
,
0
,
1
,
map
.
getSquare
(
1
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
2
,
1
,
0
,
2
,
1
,
map
.
getSquare
(
2
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
3
,
4
,
5
,
3
,
4
,
map
.
getSquare
(
1
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
5
,
4
,
3
,
5
,
4
,
map
.
getSquare
(
2
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
6
,
7
,
8
,
6
,
7
,
map
.
getSquare
(
1
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
8
,
7
,
6
,
7
,
8
,
map
.
getSquare
(
2
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
0
,
3
,
6
,
0
,
3
,
map
.
getSquare
(
1
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
6
,
3
,
0
,
6
,
3
,
map
.
getSquare
(
3
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
1
,
4
,
7
,
1
,
4
,
map
.
getSquare
(
1
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
7
,
4
,
1
,
7
,
4
,
map
.
getSquare
(
3
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
2
,
5
,
8
,
2
,
5
,
map
.
getSquare
(
1
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
8
,
5
,
2
,
8
,
5
,
map
.
getSquare
(
3
),
map
.
getSquare
(
1
),
Color
);
counter
+=
map
.
checkLine
(
0
,
1
,
2
,
0
,
1
,
map
.
getSquare
(
3
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
2
,
1
,
0
,
2
,
1
,
map
.
getSquare
(
4
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
3
,
4
,
5
,
3
,
4
,
map
.
getSquare
(
3
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
5
,
4
,
3
,
5
,
4
,
map
.
getSquare
(
4
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
6
,
7
,
8
,
6
,
7
,
map
.
getSquare
(
3
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
8
,
7
,
6
,
7
,
8
,
map
.
getSquare
(
4
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
0
,
3
,
6
,
0
,
3
,
map
.
getSquare
(
2
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
6
,
3
,
0
,
6
,
3
,
map
.
getSquare
(
4
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
1
,
4
,
7
,
1
,
4
,
map
.
getSquare
(
2
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
7
,
4
,
1
,
7
,
4
,
map
.
getSquare
(
4
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
2
,
5
,
8
,
2
,
5
,
map
.
getSquare
(
2
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
8
,
5
,
2
,
8
,
5
,
map
.
getSquare
(
4
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
2
,
4
,
6
,
2
,
4
,
map
.
getSquare
(
2
),
map
.
getSquare
(
3
),
Color
);
counter
+=
map
.
checkLine
(
6
,
4
,
2
,
6
,
4
,
map
.
getSquare
(
3
),
map
.
getSquare
(
2
),
Color
);
counter
+=
map
.
checkLine
(
0
,
4
,
8
,
0
,
4
,
map
.
getSquare
(
1
),
map
.
getSquare
(
4
),
Color
);
counter
+=
map
.
checkLine
(
8
,
4
,
0
,
8
,
4
,
map
.
getSquare
(
4
),
map
.
getSquare
(
1
),
Color
);
// check diagonal lines
if
(
map
.
getSquare
(
2
).
get
(
1
).
equals
(
map
.
getSquare
(
2
).
get
(
3
))
&&
map
.
getSquare
(
2
).
get
(
1
).
equals
(
map
.
getSquare
(
1
).
get
(
8
))
&&
map
.
getSquare
(
2
).
get
(
1
).
equals
(
map
.
getSquare
(
3
).
get
(
1
))
&&
map
.
getSquare
(
2
).
get
(
1
).
equals
(
map
.
getSquare
(
3
).
get
(
3
)))
{
if
(
map
.
getSquare
(
2
).
get
(
1
).
equals
(
Color
))
counter
+=
1
;
}
if
(
map
.
getSquare
(
2
).
get
(
5
).
equals
(
map
.
getSquare
(
2
).
get
(
7
))
&&
map
.
getSquare
(
2
).
get
(
5
).
equals
(
map
.
getSquare
(
4
).
get
(
0
))
&&
map
.
getSquare
(
2
).
get
(
5
).
equals
(
map
.
getSquare
(
3
).
get
(
5
))
&&
map
.
getSquare
(
2
).
get
(
5
).
equals
(
map
.
getSquare
(
3
).
get
(
7
)))
{
if
(
map
.
getSquare
(
2
).
get
(
5
).
equals
(
Color
))
counter
+=
1
;
}
if
(
map
.
getSquare
(
1
).
get
(
1
).
equals
(
map
.
getSquare
(
1
).
get
(
5
))
&&
map
.
getSquare
(
1
).
get
(
1
).
equals
(
map
.
getSquare
(
2
).
get
(
6
))
&&
map
.
getSquare
(
1
).
get
(
1
).
equals
(
map
.
getSquare
(
4
).
get
(
1
))
&&
map
.
getSquare
(
1
).
get
(
1
).
equals
(
map
.
getSquare
(
4
).
get
(
5
)))
{
if
(
map
.
getSquare
(
1
).
get
(
1
).
equals
(
Color
))
counter
+=
1
;
}
if
(
map
.
getSquare
(
1
).
get
(
3
).
equals
(
map
.
getSquare
(
1
).
get
(
7
))
&&
map
.
getSquare
(
1
).
get
(
3
).
equals
(
map
.
getSquare
(
3
).
get
(
2
))
&&
map
.
getSquare
(
1
).
get
(
3
).
equals
(
map
.
getSquare
(
4
).
get
(
3
))
&&
map
.
getSquare
(
1
).
get
(
3
).
equals
(
map
.
getSquare
(
4
).
get
(
7
)))
{
if
(
map
.
getSquare
(
1
).
get
(
3
).
equals
(
Color
))
counter
+=
1
;
}
return
counter
>
0
;
}
/**
* rotate given square with given direction
* @param numberOfSquare number of Array list
* @param direction of rotate
*/
public
void
rotateSquare
(
int
numberOfSquare
,
String
direction
)
{
//clockwise
if
(
direction
.
equals
(
"C"
))
{
//creat a new array list
ArrayList
<
String
>
temp
=
new
ArrayList
<
String
>();
//add each circle of map after rotating in temp
temp
.
add
(
0
,
map
.
getSquare
(
numberOfSquare
).
get
(
6
));
temp
.
add
(
1
,
map
.
getSquare
(
numberOfSquare
).
get
(
3
));
temp
.
add
(
2
,
map
.
getSquare
(
numberOfSquare
).
get
(
0
));
temp
.
add
(
3
,
map
.
getSquare
(
numberOfSquare
).
get
(
7
));
temp
.
add
(
4
,
map
.
getSquare
(
numberOfSquare
).
get
(
4
));
temp
.
add
(
5
,
map
.
getSquare
(
numberOfSquare
).
get
(
1
));
temp
.
add
(
6
,
map
.
getSquare
(
numberOfSquare
).
get
(
8
));
temp
.
add
(
7
,
map
.
getSquare
(
numberOfSquare
).
get
(
5
));
temp
.
add
(
8
,
map
.
getSquare
(
numberOfSquare
).
get
(
2
));
//set temp to given square
map
.
setSquare
(
temp
,
numberOfSquare
);
}
//non-clockwise
if
(
direction
.
equals
(
"NC"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
<
String
>();
temp
.
add
(
0
,
map
.
getSquare
(
numberOfSquare
).
get
(
2
));
temp
.
add
(
1
,
map
.
getSquare
(
numberOfSquare
).
get
(
5
));
temp
.
add
(
2
,
map
.
getSquare
(
numberOfSquare
).
get
(
8
));
temp
.
add
(
3
,
map
.
getSquare
(
numberOfSquare
).
get
(
1
));
temp
.
add
(
4
,
map
.
getSquare
(
numberOfSquare
).
get
(
4
));
temp
.
add
(
5
,
map
.
getSquare
(
numberOfSquare
).
get
(
7
));
temp
.
add
(
6
,
map
.
getSquare
(
numberOfSquare
).
get
(
0
));
temp
.
add
(
7
,
map
.
getSquare
(
numberOfSquare
).
get
(
3
));
temp
.
add
(
8
,
map
.
getSquare
(
numberOfSquare
).
get
(
6
));
map
.
setSquare
(
temp
,
numberOfSquare
);
}
}
/**
* rotate square in map
*/
public
void
rotate
()
{
//check if there is an empty square skip rotating
if
(
map
.
isEmpty
())
{
System
.
out
.
println
(
"\nThere is at least one Empty Square ,You can Skip rotating by Entering skip else Enter anything :"
);
Scanner
scanner1
=
new
Scanner
(
System
.
in
);
String
skip
=
scanner1
.
next
();
if
(!(
skip
.
equals
(
"skip"
)))
{
System
.
out
.
print
(
"Enter number of square and rotate direction like 1 C :"
);
rotateCheck
();
}
}
else
{
System
.
out
.
print
(
"\nEnter number of square and rotate direction like 1 C :"
);
rotateCheck
();
}
}
/**
* check direction and square number for rotating
*/
private
void
rotateCheck
()
{
Scanner
scanner2
=
new
Scanner
(
System
.
in
);
int
square
=
scanner2
.
nextInt
();
String
dir
=
scanner2
.
next
();
if
(
square
==
1
)
{
rotateSquare
(
1
,
dir
);
}
else
if
(
square
==
2
)
{
rotateSquare
(
2
,
dir
);
}
else
if
(
square
==
3
)
{
rotateSquare
(
3
,
dir
);
}
else
if
(
square
==
4
)
{
rotateSquare
(
4
,
dir
);
}
}
/**
* put disk in map for player with given color
* @param Color of player
*/
public
void
putDisk
(
String
Color
)
{
System
.
out
.
println
(
"\nEnter the number of square and circle :"
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
int
square
=
scanner
.
nextInt
();
int
circle
=
scanner
.
nextInt
();
if
(
square
>
4
||
square
<
1
||
circle
>
9
||
circle
<
1
)
putDisk
(
Color
);
else
{
if
(
square
==
1
)
{
if
(
map
.
getSquare
(
1
).
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
map
.
getSquare
(
1
).
remove
(
circle
-
1
);
map
.
getSquare
(
1
).
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
}
if
(
square
==
2
)
{
if
(
map
.
getSquare
(
2
).
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
map
.
getSquare
(
2
).
remove
(
circle
-
1
);
map
.
getSquare
(
2
).
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
}
if
(
square
==
3
)
{
if
(
map
.
getSquare
(
3
).
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
map
.
getSquare
(
3
).
remove
(
circle
-
1
);
map
.
getSquare
(
3
).
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
}
if
(
square
==
4
)
{
if
(
map
.
getSquare
(
4
).
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
map
.
getSquare
(
4
).
remove
(
circle
-
1
);
map
.
getSquare
(
4
).
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
}
}
}
/**
* print map after applying rules
*/
public
void
print
()
{
map
.
print
();
}
}
src/Main.java
View file @
93b3ca67
import
java.util.Random
;
/**
* run whole game
*
* @author Narges Salehi
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
//creat a random number to set random color for player
Random
random
=
new
Random
();
int
rand
=
random
.
nextInt
(
2
);
//color of player
String
red
=
" 🔴 "
;
String
black
=
" ⚫ "
;
Map
map
=
new
Map
();
map
.
print
();
//creat a new game
GameRules
game
=
new
GameRules
();
game
.
print
();
if
(
rand
==
0
)
{
//start game
while
(
true
)
{
map
.
putDisk
(
red
);
map
.
print
();
if
(
map
.
checkWinner
(
red
))
{
game
.
putDisk
(
red
);
game
.
print
();
//check wining condition
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
map
.
rotate
();
map
.
print
();
if
(
map
.
checkWinner
(
red
)
&&
map
.
checkWinner
(
black
))
{
//rotate
game
.
rotate
();
game
.
print
();
//check wining condition
if
(
game
.
checkWinner
(
red
)
&&
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nequal!"
);
break
;
}
else
if
(
map
.
checkWinner
(
red
))
{
}
else
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
else
if
(
map
.
checkWinner
(
black
)){
}
else
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
map
.
putDisk
(
black
);
map
.
print
();
if
(
map
.
checkWinner
(
black
))
{
//same for next player
game
.
putDisk
(
black
);
game
.
print
();
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
map
.
rotate
();
map
.
print
();
if
(
map
.
checkWinner
(
red
)
&&
map
.
checkWinner
(
black
))
{
game
.
rotate
();
game
.
print
();
if
(
game
.
checkWinner
(
red
)
&&
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nequal!"
);
break
;
}
else
if
(
map
.
checkWinner
(
red
))
{
}
else
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
else
if
(
map
.
checkWinner
(
black
)){
}
else
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
...
...
@@ -53,41 +64,39 @@ public class Main {
}
}
else
{
while
(
true
)
{
map
.
putDisk
(
black
);
map
.
print
();
if
(
map
.
checkWinner
(
black
))
{
game
.
putDisk
(
black
);
game
.
print
();
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
map
.
rotate
();
map
.
print
();
if
(
map
.
checkWinner
(
red
)
&&
map
.
checkWinner
(
black
))
{
game
.
rotate
();
game
.
print
();
if
(
game
.
checkWinner
(
red
)
&&
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nequal!"
);
break
;
}
else
if
(
map
.
checkWinner
(
red
))
{
}
else
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
else
if
(
map
.
checkWinner
(
black
)){
}
else
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
map
.
putDisk
(
red
);
map
.
print
();
if
(
map
.
checkWinner
(
red
))
{
game
.
putDisk
(
red
);
game
.
print
();
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
map
.
rotate
();
map
.
print
();
if
(
map
.
checkWinner
(
red
)
&&
map
.
checkWinner
(
black
))
{
game
.
rotate
();
game
.
print
();
if
(
game
.
checkWinner
(
red
)
&&
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nequal!"
);
break
;
}
else
if
(
map
.
checkWinner
(
red
))
{
}
else
if
(
game
.
checkWinner
(
red
))
{
System
.
out
.
println
(
"\nRed is winner !"
);
break
;
}
else
if
(
map
.
checkWinner
(
black
)){
}
else
if
(
game
.
checkWinner
(
black
))
{
System
.
out
.
println
(
"\nblack is winner !"
);
break
;
}
...
...
src/Map.java
View file @
93b3ca67
This diff is collapsed.
Click to expand it.
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