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
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
import
javax.swing.*
;
import
java.util.ArrayList
;
import
java.util.Scanner
;
/**
* this class provide map info and applications - board game
*
* @author Narges Salehi
*/
public
class
Map
{
//4 Array list for each square of board game
private
ArrayList
<
String
>
square1
;
private
ArrayList
<
String
>
square2
;
private
ArrayList
<
String
>
square3
;
private
ArrayList
<
String
>
square4
;
String
red
=
" 🔴 "
;
String
black
=
" ⚫ "
;
/**
* creat a new map for board game
*/
public
Map
()
{
//defining map with empty value
square1
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
9
;
i
++)
{
square1
.
add
(
" ⚪ "
);
...
...
@@ -30,8 +36,22 @@ public class Map {
}
public
int
checkLine
(
int
main
,
int
a
,
int
b
,
int
c
,
int
d
,
ArrayList
one
,
ArrayList
two
,
String
Color
)
{
/**
* check given line of map that allows player to win - provide game wining rule
* @param main start point of line
* @param a next disk on the line
* @param b next disk on the line
* @param c next disk on the line
* @param d next disk on the line
* @param one a square that contains start point of line
* @param two a square that contains finish point of line
* @param Color color of player
* @return number of line with given condition of wining
*/
public
int
checkLine
(
int
main
,
int
a
,
int
b
,
int
c
,
int
d
,
ArrayList
<
String
>
one
,
ArrayList
<
String
>
two
,
String
Color
)
{
//count to one if the line has the condition of wining
int
counter
=
0
;
//check color of 5 disk in line
if
(
one
.
get
(
main
).
equals
(
one
.
get
(
a
))
&&
one
.
get
(
main
).
equals
(
one
.
get
(
b
))
&&
one
.
get
(
main
).
equals
(
two
.
get
(
c
))
&&
one
.
get
(
main
).
equals
(
two
.
get
(
d
)))
{
if
(
one
.
get
(
main
).
equals
(
Color
))
...
...
@@ -40,231 +60,91 @@ public class Map {
return
counter
;
}
public
boolean
checkWinner
(
String
Color
)
{
int
counter
=
0
;
counter
+=
checkLine
(
0
,
1
,
2
,
0
,
1
,
square1
,
square2
,
Color
);
counter
+=
checkLine
(
2
,
1
,
0
,
2
,
1
,
square2
,
square1
,
Color
);
counter
+=
checkLine
(
3
,
4
,
5
,
3
,
4
,
square1
,
square2
,
Color
);
counter
+=
checkLine
(
5
,
4
,
3
,
5
,
4
,
square2
,
square1
,
Color
);
counter
+=
checkLine
(
6
,
7
,
8
,
6
,
7
,
square1
,
square2
,
Color
);
counter
+=
checkLine
(
8
,
7
,
6
,
7
,
8
,
square2
,
square1
,
Color
);
counter
+=
checkLine
(
0
,
3
,
6
,
0
,
3
,
square1
,
square3
,
Color
);
counter
+=
checkLine
(
6
,
3
,
0
,
6
,
3
,
square3
,
square1
,
Color
);
counter
+=
checkLine
(
1
,
4
,
7
,
1
,
4
,
square1
,
square3
,
Color
);
counter
+=
checkLine
(
7
,
4
,
1
,
7
,
4
,
square3
,
square1
,
Color
);
counter
+=
checkLine
(
2
,
5
,
8
,
2
,
5
,
square1
,
square3
,
Color
);
counter
+=
checkLine
(
8
,
5
,
2
,
8
,
5
,
square3
,
square1
,
Color
);
counter
+=
checkLine
(
0
,
1
,
2
,
0
,
1
,
square3
,
square4
,
Color
);
counter
+=
checkLine
(
2
,
1
,
0
,
2
,
1
,
square4
,
square3
,
Color
);
counter
+=
checkLine
(
3
,
4
,
5
,
3
,
4
,
square3
,
square4
,
Color
);
counter
+=
checkLine
(
5
,
4
,
3
,
5
,
4
,
square4
,
square3
,
Color
);
counter
+=
checkLine
(
6
,
7
,
8
,
6
,
7
,
square3
,
square4
,
Color
);
counter
+=
checkLine
(
8
,
7
,
6
,
7
,
8
,
square4
,
square3
,
Color
);
counter
+=
checkLine
(
0
,
3
,
6
,
0
,
3
,
square2
,
square4
,
Color
);
counter
+=
checkLine
(
6
,
3
,
0
,
6
,
3
,
square4
,
square2
,
Color
);
counter
+=
checkLine
(
1
,
4
,
7
,
1
,
4
,
square2
,
square4
,
Color
);
counter
+=
checkLine
(
7
,
4
,
1
,
7
,
4
,
square4
,
square2
,
Color
);
counter
+=
checkLine
(
2
,
5
,
8
,
2
,
5
,
square2
,
square4
,
Color
);
counter
+=
checkLine
(
8
,
5
,
2
,
8
,
5
,
square4
,
square2
,
Color
);
counter
+=
checkLine
(
2
,
4
,
6
,
2
,
4
,
square2
,
square3
,
Color
);
counter
+=
checkLine
(
6
,
4
,
2
,
6
,
4
,
square3
,
square2
,
Color
);
counter
+=
checkLine
(
0
,
4
,
8
,
0
,
4
,
square1
,
square4
,
Color
);
counter
+=
checkLine
(
8
,
4
,
0
,
8
,
4
,
square4
,
square1
,
Color
);
if
(
square2
.
get
(
1
).
equals
(
square2
.
get
(
3
))
&&
square2
.
get
(
1
).
equals
(
square1
.
get
(
8
))
&&
square2
.
get
(
1
).
equals
(
square3
.
get
(
1
))
&&
square2
.
get
(
1
).
equals
(
square3
.
get
(
3
)))
{
if
(
square2
.
get
(
1
).
equals
(
Color
))
counter
+=
1
;
}
if
(
square2
.
get
(
5
).
equals
(
square2
.
get
(
7
))
&&
square2
.
get
(
5
).
equals
(
square4
.
get
(
0
))
&&
square2
.
get
(
5
).
equals
(
square3
.
get
(
5
))
&&
square2
.
get
(
5
).
equals
(
square3
.
get
(
7
)))
{
if
(
square2
.
get
(
5
).
equals
(
Color
))
counter
+=
1
;
}
if
(
square1
.
get
(
1
).
equals
(
square1
.
get
(
5
))
&&
square1
.
get
(
1
).
equals
(
square2
.
get
(
6
))
&&
square1
.
get
(
1
).
equals
(
square4
.
get
(
1
))
&&
square1
.
get
(
1
).
equals
(
square4
.
get
(
5
)))
{
if
(
square1
.
get
(
1
).
equals
(
Color
))
counter
+=
1
;
}
if
(
square1
.
get
(
3
).
equals
(
square1
.
get
(
7
))
&&
square1
.
get
(
3
).
equals
(
square3
.
get
(
2
))
&&
square1
.
get
(
3
).
equals
(
square4
.
get
(
3
))
&&
square1
.
get
(
3
).
equals
(
square4
.
get
(
7
)))
{
if
(
square1
.
get
(
3
).
equals
(
Color
))
counter
+=
1
;
}
return
counter
>
0
;
/**
* return square with given number
* @param num of square
* @return Array list
*/
public
ArrayList
<
String
>
getSquare
(
int
num
)
{
if
(
num
==
1
)
return
square1
;
if
(
num
==
2
)
return
square2
;
if
(
num
==
3
)
return
square3
;
if
(
num
==
4
)
return
square4
;
else
return
null
;
}
public
void
rotate
()
{
System
.
out
.
println
(
"\nEnter number of square and rotate direction like 1 C :"
);
Scanner
scanner1
=
new
Scanner
(
System
.
in
);
int
square
=
scanner1
.
nextInt
();
String
dir
=
scanner1
.
next
();
if
(
square
==
1
)
{
if
(
dir
.
equals
(
"C"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square1
.
get
(
6
));
temp
.
add
(
1
,
square1
.
get
(
3
));
temp
.
add
(
2
,
square1
.
get
(
0
));
temp
.
add
(
3
,
square1
.
get
(
7
));
temp
.
add
(
4
,
square1
.
get
(
4
));
temp
.
add
(
5
,
square1
.
get
(
1
));
temp
.
add
(
6
,
square1
.
get
(
8
));
temp
.
add
(
7
,
square1
.
get
(
5
));
temp
.
add
(
8
,
square1
.
get
(
2
));
square1
=
temp
;
}
if
(
dir
.
equals
(
"NC"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square1
.
get
(
2
));
temp
.
add
(
1
,
square1
.
get
(
5
));
temp
.
add
(
2
,
square1
.
get
(
8
));
temp
.
add
(
3
,
square1
.
get
(
1
));
temp
.
add
(
4
,
square1
.
get
(
4
));
temp
.
add
(
5
,
square1
.
get
(
7
));
temp
.
add
(
6
,
square1
.
get
(
0
));
temp
.
add
(
7
,
square1
.
get
(
3
));
temp
.
add
(
8
,
square1
.
get
(
6
));
/**
* set given Array list to given square
* @param temp given array list
* @param num of square
*/
public
void
setSquare
(
ArrayList
<
String
>
temp
,
int
num
)
{
if
(
num
==
1
)
square1
=
temp
;
}
}
else
if
(
square
==
2
)
{
if
(
dir
.
equals
(
"C"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square2
.
get
(
6
));
temp
.
add
(
1
,
square2
.
get
(
3
));
temp
.
add
(
2
,
square2
.
get
(
0
));
temp
.
add
(
3
,
square2
.
get
(
7
));
temp
.
add
(
4
,
square2
.
get
(
4
));
temp
.
add
(
5
,
square2
.
get
(
1
));
temp
.
add
(
6
,
square2
.
get
(
8
));
temp
.
add
(
7
,
square2
.
get
(
5
));
temp
.
add
(
8
,
square2
.
get
(
2
));
square2
=
temp
;
}
if
(
dir
.
equals
(
"NC"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square2
.
get
(
2
));
temp
.
add
(
1
,
square2
.
get
(
5
));
temp
.
add
(
2
,
square2
.
get
(
8
));
temp
.
add
(
3
,
square2
.
get
(
1
));
temp
.
add
(
4
,
square2
.
get
(
4
));
temp
.
add
(
5
,
square2
.
get
(
7
));
temp
.
add
(
6
,
square2
.
get
(
0
));
temp
.
add
(
7
,
square2
.
get
(
3
));
temp
.
add
(
8
,
square2
.
get
(
6
));
if
(
num
==
2
)
square2
=
temp
;
}
}
else
if
(
square
==
3
)
{
if
(
dir
.
equals
(
"C"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square3
.
get
(
6
));
temp
.
add
(
1
,
square3
.
get
(
3
));
temp
.
add
(
2
,
square3
.
get
(
0
));
temp
.
add
(
3
,
square3
.
get
(
7
));
temp
.
add
(
4
,
square3
.
get
(
4
));
temp
.
add
(
5
,
square3
.
get
(
1
));
temp
.
add
(
6
,
square3
.
get
(
8
));
temp
.
add
(
7
,
square3
.
get
(
5
));
temp
.
add
(
8
,
square3
.
get
(
2
));
if
(
num
==
3
)
square3
=
temp
;
}
if
(
dir
.
equals
(
"NC"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square3
.
get
(
2
));
temp
.
add
(
1
,
square3
.
get
(
5
));
temp
.
add
(
2
,
square3
.
get
(
8
));
temp
.
add
(
3
,
square3
.
get
(
1
));
temp
.
add
(
4
,
square3
.
get
(
4
));
temp
.
add
(
5
,
square3
.
get
(
7
));
temp
.
add
(
6
,
square3
.
get
(
0
));
temp
.
add
(
7
,
square3
.
get
(
3
));
temp
.
add
(
8
,
square3
.
get
(
6
));
square3
=
temp
;
}
}
else
if
(
square
==
4
)
{
if
(
dir
.
equals
(
"C"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square4
.
get
(
6
));
temp
.
add
(
1
,
square4
.
get
(
3
));
temp
.
add
(
2
,
square4
.
get
(
0
));
temp
.
add
(
3
,
square4
.
get
(
7
));
temp
.
add
(
4
,
square4
.
get
(
4
));
temp
.
add
(
5
,
square4
.
get
(
1
));
temp
.
add
(
6
,
square4
.
get
(
8
));
temp
.
add
(
7
,
square4
.
get
(
5
));
temp
.
add
(
8
,
square4
.
get
(
2
));
if
(
num
==
4
)
square4
=
temp
;
}
if
(
dir
.
equals
(
"NC"
))
{
ArrayList
<
String
>
temp
=
new
ArrayList
();
temp
.
add
(
0
,
square4
.
get
(
2
));
temp
.
add
(
1
,
square4
.
get
(
5
));
temp
.
add
(
2
,
square4
.
get
(
8
));
temp
.
add
(
3
,
square4
.
get
(
1
));
temp
.
add
(
4
,
square4
.
get
(
4
));
temp
.
add
(
5
,
square4
.
get
(
7
));
temp
.
add
(
6
,
square4
.
get
(
0
));
temp
.
add
(
7
,
square4
.
get
(
3
));
temp
.
add
(
8
,
square4
.
get
(
6
));
square4
=
temp
;
}
}
}
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
(
square1
.
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
square1
.
remove
(
circle
-
1
);
square1
.
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
}
if
(
square
==
2
)
{
if
(
square2
.
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
square2
.
remove
(
circle
-
1
);
square2
.
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
/**
* check if there is an empty square
* @return true of false
*/
public
boolean
isEmpty
()
{
//check square to find an empty square - if preset square is not empty check out next square else return true
boolean
check
=
true
;
for
(
String
circle1
:
square1
)
{
if
(!(
circle1
.
equals
(
" ⚪ "
)))
{
for
(
String
circle2
:
square2
)
{
if
(!(
circle2
.
equals
(
" ⚪ "
)))
{
for
(
String
circle3
:
square3
)
{
if
(!(
circle3
.
equals
(
" ⚪ "
)))
{
for
(
String
circle
:
square4
)
{
if
(!(
circle
.
equals
(
" ⚪ "
)))
{
check
=
false
;
return
check
;
}
}
if
(
square
==
3
)
{
if
(
square3
.
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
square3
.
remove
(
circle
-
1
);
square3
.
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
}
else
return
check
;
}
}
if
(
square
==
4
)
{
if
(
square4
.
get
(
circle
-
1
).
equals
(
" ⚪ "
))
{
square4
.
remove
(
circle
-
1
);
square4
.
add
(
circle
-
1
,
Color
);
}
else
{
System
.
out
.
println
(
"Already taken"
);
putDisk
(
Color
);
else
return
check
;
}
}
else
return
check
;
}
return
false
;
}
/**
* print map of game
*/
public
void
print
()
{
System
.
out
.
println
(
"Square 1 Square 2"
);
printSquares
(
square1
,
square2
);
System
.
out
.
println
(
" == == == == == == "
);
printSquares
(
square3
,
square4
);
System
.
out
.
print
(
"Square 3 Square 4"
);
}
/**
* print neighbor squares in a line
* @param square1 left square
* @param square2 right square
*/
private
void
printSquares
(
ArrayList
<
String
>
square1
,
ArrayList
<
String
>
square2
)
{
for
(
int
i
=
0
;
i
<
9
;
i
++)
{
if
(
i
==
2
||
i
==
5
||
i
==
8
)
{
System
.
out
.
print
(
square1
.
get
(
i
)
+
"‖"
);
...
...
@@ -281,23 +161,6 @@ public class Map {
}
else
System
.
out
.
print
(
square1
.
get
(
i
));
}
System
.
out
.
println
(
" == == == == == == "
);
for
(
int
i
=
0
;
i
<
9
;
i
++)
{
if
(
i
==
2
||
i
==
5
||
i
==
8
)
{
System
.
out
.
print
(
square3
.
get
(
i
)
+
"‖"
);
if
(
i
==
2
)
{
System
.
out
.
println
(
square4
.
get
(
0
)
+
square4
.
get
(
1
)
+
square4
.
get
(
2
));
System
.
out
.
println
();
}
if
(
i
==
5
)
{
System
.
out
.
println
(
square4
.
get
(
3
)
+
square4
.
get
(
4
)
+
square4
.
get
(
5
));
System
.
out
.
println
();
}
if
(
i
==
8
)
System
.
out
.
println
(
square4
.
get
(
6
)
+
square4
.
get
(
7
)
+
square4
.
get
(
8
));
}
else
System
.
out
.
print
(
square3
.
get
(
i
));
}
System
.
out
.
print
(
"Square 3 Square 4"
);
}
}
\ No newline at end of file
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