Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
Othelo
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
Othelo
Commits
49970e8c
Commit
49970e8c
authored
Apr 11, 2020
by
nargessalehi98
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add inhertitance
parent
c15a4c10
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
199 additions
and
177 deletions
+199
-177
GameRules.class
out/production/Othello/GameRules.class
+0
-0
Main.class
out/production/Othello/Main.class
+0
-0
Map.class
out/production/Othello/Map.class
+0
-0
GameRules.java
src/GameRules.java
+182
-0
Main.java
src/Main.java
+9
-9
Map.java
src/Map.java
+8
-168
No files found.
out/production/Othello/GameRules.class
0 → 100644
View file @
49970e8c
File added
out/production/Othello/Main.class
View file @
49970e8c
No preview for this file type
out/production/Othello/Map.class
View file @
49970e8c
No preview for this file type
src/GameRules.java
View file @
49970e8c
import
java.util.ArrayList
;
import
java.util.Scanner
;
public
class
GameRules
{
public
class
GameRules
{
private
Map
map
;
private
String
empty
;
private
String
white
;
private
String
black
;
public
GameRules
()
{
empty
=
"| |"
;
white
=
"| ⚪ |"
;
black
=
"| ⚫ |"
;
map
=
new
Map
();
}
public
String
checkColor
(
int
row
,
int
col
)
{
ArrayList
<
String
>
temp
=
map
.
getRow
(
row
);
if
(
temp
.
get
(
col
).
equals
(
white
))
return
white
;
if
(
temp
.
get
(
col
).
equals
(
black
))
return
black
;
else
return
empty
;
}
public
boolean
checkFullMap
()
{
boolean
check
=
true
;
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
empty
))
check
=
false
;
}
}
return
check
;
}
public
boolean
checkPutDisk
(
int
row
,
int
col
,
String
Color
)
{
int
counter
=
0
;
for
(
int
plusRow
=
-
1
;
plusRow
<
2
;
plusRow
++)
{
for
(
int
plusCol
=
-
1
;
plusCol
<
2
;
plusCol
++)
{
int
Row
=
row
+
plusRow
;
int
Col
=
col
+
plusCol
;
if
(!(
plusCol
==
0
&&
plusRow
==
0
))
{
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(!
checkColor
(
Row
,
Col
).
equals
(
Color
)
&&
!
checkColor
(
Row
,
Col
).
equals
(
empty
))
{
while
(
true
)
{
Row
+=
plusRow
;
Col
+=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
Color
))
{
counter
++;
break
;
}
}
else
break
;
}
}
}
}
}
}
return
counter
!=
0
;
}
public
boolean
checkPossibleMove
(
String
Color
)
{
int
counter
=
0
;
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
empty
))
{
if
(
checkPutDisk
(
row
,
col
,
Color
))
{
counter
++;
}
}
}
}
return
counter
!=
0
;
}
public
void
findWinner
()
{
int
counterBlack
=
0
;
int
counterWhite
=
0
;
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
black
))
counterBlack
++;
if
(
checkColor
(
row
,
col
).
equals
(
white
))
counterWhite
++;
}
}
if
(
counterBlack
>
counterWhite
)
System
.
out
.
println
(
"Black won ! Number of disks: "
+
counterBlack
);
if
(
counterWhite
>
counterBlack
)
System
.
out
.
println
(
"White won ! Number of disks: "
+
counterWhite
);
if
(
counterBlack
==
counterWhite
)
System
.
out
.
println
(
"Equal !"
);
}
public
void
checkNeighbor
(
int
row
,
int
col
)
{
for
(
int
plusRow
=
-
1
;
plusRow
<
2
;
plusRow
++)
{
for
(
int
plusCol
=
-
1
;
plusCol
<
2
;
plusCol
++)
{
String
main
=
checkColor
(
row
,
col
);
int
Row
=
row
+
plusRow
;
int
Col
=
col
+
plusCol
;
if
(!(
plusCol
==
0
&&
plusRow
==
0
))
{
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(!
checkColor
(
Row
,
Col
).
equals
(
main
)
&&
!
checkColor
(
Row
,
Col
).
equals
(
empty
))
{
boolean
check
=
true
;
while
(
true
)
{
if
(
check
)
{
Row
+=
plusRow
;
Col
+=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
main
))
{
while
(
true
)
{
Row
-=
plusRow
;
Col
-=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
main
))
{
check
=
false
;
break
;
}
else
{
ArrayList
<
String
>
temp
=
map
.
getRow
(
Row
);
temp
.
remove
(
Col
);
temp
.
add
(
Col
,
main
);
map
.
removeRow
(
Row
);
map
.
addRow
(
Row
,
temp
);
}
}
}
}
if
(
checkColor
(
Row
,
Col
).
equals
(
empty
))
break
;
}
else
break
;
}
else
break
;
}
}
}
}
}
}
}
public
boolean
putDisk
(
String
Color
)
{
if
(
checkPossibleMove
(
Color
))
{
System
.
out
.
println
(
"Enter Number of row and letter of column :"
);
Scanner
scan
=
new
Scanner
(
System
.
in
);
int
row
=
scan
.
nextInt
();
row
=
row
-
1
;
String
colLetter
=
scan
.
next
();
int
col
=
map
.
getColNum
(
colLetter
);
if
(
col
!=
8
)
{
if
(
checkPutDisk
(
row
,
col
,
Color
))
{
ArrayList
<
String
>
temp
=
map
.
getRow
(
row
);
if
(
temp
.
get
(
col
).
equals
(
empty
))
{
temp
.
remove
(
col
);
temp
.
add
(
col
,
Color
);
map
.
removeRow
(
row
);
map
.
addRow
(
row
,
temp
);
}
checkNeighbor
(
row
,
col
);
}
else
{
System
.
out
.
println
(
"choose right position !"
);
putDisk
(
Color
);
}
}
else
{
System
.
out
.
println
(
"wrong input !"
);
putDisk
(
Color
);
}
return
false
;
}
else
{
System
.
out
.
println
(
"pass !"
);
return
true
;
}
}
public
void
print
()
{
map
.
print
();
}
}
}
src/Main.java
View file @
49970e8c
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Map
map
=
new
Map
();
GameRules
game
=
new
GameRules
();
map
.
print
();
game
.
print
();
while
(
true
)
{
while
(
true
)
{
String
white
=
"| \u26AA |"
;
String
white
=
"| \u26AA |"
;
String
black
=
"| \u26AB |"
;
String
black
=
"| \u26AB |"
;
System
.
out
.
println
(
"Black's turn :"
);
System
.
out
.
println
(
"Black's turn :"
);
boolean
checkBlack
=
map
.
putDisk
(
black
);
boolean
checkBlack
=
game
.
putDisk
(
black
);
map
.
print
();
game
.
print
();
System
.
out
.
println
(
"White's turn :"
);
System
.
out
.
println
(
"White's turn :"
);
boolean
checkWhite
=
map
.
putDisk
(
white
);
boolean
checkWhite
=
game
.
putDisk
(
white
);
if
(
checkBlack
&&
checkWhite
||
map
.
f
ullMap
())
{
if
(
checkBlack
&&
checkWhite
||
game
.
checkF
ullMap
())
{
System
.
out
.
println
(
"game is over !"
);
System
.
out
.
println
(
"game is over !"
);
map
.
findWinner
();
game
.
findWinner
();
game
.
print
();
break
;
break
;
}
}
map
.
print
();
game
.
print
();
}
}
}
}
}
}
src/Map.java
View file @
49970e8c
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Scanner
;
import
java.util.SortedMap
;
public
class
Map
extends
GameRules
{
public
class
Map
{
private
ArrayList
<
ArrayList
>
map
;
private
ArrayList
<
ArrayList
>
map
;
private
String
empty
;
private
String
empty
;
private
String
white
;
private
String
white
;
...
@@ -33,17 +30,6 @@ public class Map extends GameRules {
...
@@ -33,17 +30,6 @@ public class Map extends GameRules {
}
}
}
}
public
boolean
fullMap
()
{
boolean
check
=
true
;
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
empty
))
check
=
false
;
}
}
return
check
;
}
public
int
getColNum
(
String
c
)
{
public
int
getColNum
(
String
c
)
{
if
(
c
.
equals
(
"A"
))
if
(
c
.
equals
(
"A"
))
return
0
;
return
0
;
...
@@ -65,164 +51,18 @@ public class Map extends GameRules {
...
@@ -65,164 +51,18 @@ public class Map extends GameRules {
return
8
;
return
8
;
}
}
public
String
checkColor
(
int
row
,
int
col
)
{
public
ArrayList
<
String
>
getRow
(
int
index
){
ArrayList
<
String
>
temp
=
map
.
get
(
row
);
return
map
.
get
(
index
);
if
(
temp
.
get
(
col
).
equals
(
white
))
return
white
;
if
(
temp
.
get
(
col
).
equals
(
black
))
return
black
;
else
return
empty
;
}
}
public
boolean
checkPossibleMove
(
String
Color
)
{
public
void
removeRow
(
int
index
){
int
counter
=
0
;
map
.
remove
(
index
);
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
empty
))
{
if
(
checkPutDisk
(
row
,
col
,
Color
))
{
counter
++;
}
}
}
}
return
counter
!=
0
;
}
public
boolean
checkPutDisk
(
int
row
,
int
col
,
String
Color
)
{
int
counter
=
0
;
for
(
int
plusRow
=
-
1
;
plusRow
<
2
;
plusRow
++)
{
for
(
int
plusCol
=
-
1
;
plusCol
<
2
;
plusCol
++)
{
int
Row
=
row
+
plusRow
;
int
Col
=
col
+
plusCol
;
if
(!(
plusCol
==
0
&&
plusRow
==
0
))
{
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(!
checkColor
(
Row
,
Col
).
equals
(
Color
)
&&
!
checkColor
(
Row
,
Col
).
equals
(
empty
))
{
while
(
true
)
{
Row
+=
plusRow
;
Col
+=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
Color
))
{
counter
++;
break
;
}
}
else
break
;
}
}
}
}
}
}
return
counter
!=
0
;
}
}
public
void
findWinner
()
{
public
void
addRow
(
int
index
,
ArrayList
temp
){
int
counterBlack
=
0
;
map
.
add
(
index
,
temp
);
int
counterWhite
=
0
;
for
(
int
row
=
0
;
row
<
8
;
row
++)
{
for
(
int
col
=
0
;
col
<
8
;
col
++)
{
if
(
checkColor
(
row
,
col
).
equals
(
black
))
counterBlack
++;
if
(
checkColor
(
row
,
col
).
equals
(
white
))
counterWhite
++;
}
}
if
(
counterBlack
>
counterWhite
)
System
.
out
.
println
(
"Black won ! Number of disks: "
+
counterBlack
);
if
(
counterWhite
>
counterBlack
)
System
.
out
.
println
(
"White won ! Number of disks: "
+
counterWhite
);
if
(
counterBlack
==
counterWhite
)
System
.
out
.
println
(
"Equal !"
);
}
}
public
boolean
putDisk
(
String
Color
)
{
if
(
checkPossibleMove
(
Color
))
{
System
.
out
.
println
(
"Enter Number of row and letter of column :"
);
Scanner
scan
=
new
Scanner
(
System
.
in
);
int
row
=
scan
.
nextInt
();
row
=
row
-
1
;
String
colLetter
=
scan
.
next
();
int
col
=
getColNum
(
colLetter
);
if
(
col
!=
8
)
{
if
(
checkPutDisk
(
row
,
col
,
Color
))
{
ArrayList
<
String
>
temp
=
map
.
get
(
row
);
if
(
temp
.
get
(
col
).
equals
(
empty
))
{
temp
.
remove
(
col
);
temp
.
add
(
col
,
Color
);
map
.
remove
(
row
);
map
.
add
(
row
,
temp
);
}
checkNeighbor
(
row
,
col
);
}
else
{
System
.
out
.
println
(
"choose right position !"
);
putDisk
(
Color
);
}
}
else
{
System
.
out
.
println
(
"wrong input !"
);
putDisk
(
Color
);
}
return
false
;
}
else
{
System
.
out
.
println
(
"pass !"
);
return
true
;
}
}
public
void
checkNeighbor
(
int
row
,
int
col
)
{
for
(
int
plusRow
=
-
1
;
plusRow
<
2
;
plusRow
++)
{
for
(
int
plusCol
=
-
1
;
plusCol
<
2
;
plusCol
++)
{
String
main
=
checkColor
(
row
,
col
);
int
Row
=
row
+
plusRow
;
int
Col
=
col
+
plusCol
;
if
(!(
plusCol
==
0
&&
plusRow
==
0
))
{
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(!
checkColor
(
Row
,
Col
).
equals
(
main
)
&&
!
checkColor
(
Row
,
Col
).
equals
(
empty
))
{
boolean
check
=
true
;
while
(
true
)
{
if
(
check
)
{
Row
+=
plusRow
;
Col
+=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
main
))
{
while
(
true
)
{
Row
-=
plusRow
;
Col
-=
plusCol
;
if
(
Row
>=
0
&&
Row
<
8
&&
Col
>=
0
&&
Col
<
8
)
{
if
(
checkColor
(
Row
,
Col
).
equals
(
main
))
{
check
=
false
;
break
;
}
else
{
ArrayList
<
String
>
temp
=
map
.
get
(
Row
);
temp
.
remove
(
Col
);
temp
.
add
(
Col
,
main
);
map
.
remove
(
Row
);
map
.
add
(
Row
,
temp
);
}
}
}
}
if
(
checkColor
(
Row
,
Col
).
equals
(
empty
))
{
// check = false;
break
;
}
// if (Row < 0 || Row > 7 || Col < 0 || Col > 7) {
// check = false;
// break;
// }
}
}
else
break
;
}
}
}
}
}
}
}
public
void
print
()
{
public
void
print
()
{
System
.
out
.
println
(
" A B C D E F G H "
);
System
.
out
.
println
(
" A B C D E F G H "
);
System
.
out
.
println
(
" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"
);
System
.
out
.
println
(
" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"
);
...
@@ -233,5 +73,5 @@ public class Map extends GameRules {
...
@@ -233,5 +73,5 @@ public class Map extends GameRules {
System
.
out
.
println
(
" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"
);
System
.
out
.
println
(
" +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+"
);
}
}
}
}
}
}
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