Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
Project1Othello
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
9831113
Project1Othello
Commits
5591ec07
Commit
5591ec07
authored
Apr 13, 2020
by
amir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
What to do (last javadocs)
parent
36de7cba
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
38 deletions
+53
-38
Graphic.java
Graphic.java
+28
-8
Main.java
Main.java
+25
-30
No files found.
Graphic.java
View file @
5591ec07
...
@@ -10,8 +10,11 @@ import java.util.Iterator;
...
@@ -10,8 +10,11 @@ import java.util.Iterator;
public
class
Graphic
extends
Display
{
public
class
Graphic
extends
Display
{
JFrame
f
=
new
JFrame
(
"othello"
);
// creating instance of JFrame
JFrame
f
=
new
JFrame
(
"othello"
);
// creating instance of JFrame
JLabel
l1
;
JLabel
l1
;
// start button
JButton
b
=
new
JButton
(
"start"
);
JButton
b
=
new
JButton
(
"start"
);
// list of buttons do we have
ArrayList
<
JButton
>
buttons
=
new
ArrayList
<>();
ArrayList
<
JButton
>
buttons
=
new
ArrayList
<>();
// Creating 64 button in the 64 boxes!!
JButton
button11
=
new
JButton
(
""
);
JButton
button11
=
new
JButton
(
""
);
JButton
button12
=
new
JButton
(
""
);
JButton
button12
=
new
JButton
(
""
);
JButton
button13
=
new
JButton
(
""
);
JButton
button13
=
new
JButton
(
""
);
...
@@ -77,6 +80,10 @@ public class Graphic extends Display {
...
@@ -77,6 +80,10 @@ public class Graphic extends Display {
JButton
button87
=
new
JButton
(
""
);
JButton
button87
=
new
JButton
(
""
);
JButton
button88
=
new
JButton
(
""
);
JButton
button88
=
new
JButton
(
""
);
/**
* Creates the graphic environment for the first time (if the display type == 1)
* @param displayType if equals to 1, constructing is done
*/
public
Graphic
(
byte
displayType
)
{
public
Graphic
(
byte
displayType
)
{
super
(
displayType
);
super
(
displayType
);
l1
=
new
JLabel
(
new
ImageIcon
(
"untitled.png"
));
l1
=
new
JLabel
(
new
ImageIcon
(
"untitled.png"
));
...
@@ -87,16 +94,17 @@ public class Graphic extends Display {
...
@@ -87,16 +94,17 @@ public class Graphic extends Display {
else
else
f
.
setVisible
(
true
);
f
.
setVisible
(
true
);
f
.
add
(
b
);
f
.
add
(
b
);
b
.
setBounds
(
300
,
621
,
1
0
0
,
40
);
//x axis, y axis, width, height
b
.
setBounds
(
300
,
621
,
1
8
0
,
40
);
//x axis, y axis, width, height
b
.
addActionListener
(
new
ActionListener
()
{
b
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
arg0
)
{
public
void
actionPerformed
(
ActionEvent
arg0
)
{
l1
.
setBounds
(
30
,
100
,
800
,
600
);
l1
.
setBounds
(
30
,
100
,
800
,
600
);
f
.
add
(
l1
);
f
.
add
(
l1
);
b
.
setVisible
(
false
);
b
.
setVisible
(
false
);
}
}
});
});
// all of the buttons except 4 buttons gets the color gray
button11
.
setBackground
(
Color
.
GRAY
);
button11
.
setBackground
(
Color
.
GRAY
);
// size and position of each button
button11
.
setBounds
(
143
,
135
,
58
,
58
);
button11
.
setBounds
(
143
,
135
,
58
,
58
);
f
.
add
(
button11
);
buttons
.
add
(
button11
);
f
.
add
(
button11
);
buttons
.
add
(
button11
);
button12
.
setBackground
(
Color
.
GRAY
);
button12
.
setBackground
(
Color
.
GRAY
);
...
@@ -130,7 +138,6 @@ public class Graphic extends Display {
...
@@ -130,7 +138,6 @@ public class Graphic extends Display {
button23
.
setBackground
(
Color
.
GRAY
);
button23
.
setBackground
(
Color
.
GRAY
);
button23
.
setBounds
(
143
+
2
*
58
,
135
+
58
,
58
,
58
);
button23
.
setBounds
(
143
+
2
*
58
,
135
+
58
,
58
,
58
);
f
.
add
(
button23
);
buttons
.
add
(
button23
);
f
.
add
(
button23
);
buttons
.
add
(
button23
);
JButton
button24
=
new
JButton
(
""
);
button24
.
setBackground
(
Color
.
GRAY
);
button24
.
setBackground
(
Color
.
GRAY
);
button24
.
setBounds
(
143
+
3
*
58
,
135
+
58
,
58
,
58
);
button24
.
setBounds
(
143
+
3
*
58
,
135
+
58
,
58
,
58
);
f
.
add
(
button24
);
buttons
.
add
(
button24
);
f
.
add
(
button24
);
buttons
.
add
(
button24
);
...
@@ -298,28 +305,42 @@ public class Graphic extends Display {
...
@@ -298,28 +305,42 @@ public class Graphic extends Display {
f
.
add
(
button88
);
buttons
.
add
(
button88
);
f
.
add
(
button88
);
buttons
.
add
(
button88
);
}
}
/**
* the overridden version of display in Display class
* this will show the black and white discs and blanks as the colors of buttons
* (gray == blank)
* @param a1 array of the player1's discs
* @param a2 array of the player2's discs
*/
@Override
@Override
public
void
display
(
int
[][]
a1
,
int
[][]
a2
)
{
public
void
display
(
int
[][]
a1
,
int
[][]
a2
)
{
super
.
display
(
a1
,
a2
);
super
.
display
(
a1
,
a2
);
for
(
int
i
=
1
;
i
<=
8
;
i
++)
{
for
(
int
i
=
1
;
i
<=
8
;
i
++)
{
for
(
int
j
=
1
;
j
<=
8
;
j
++)
{
for
(
int
j
=
1
;
j
<=
8
;
j
++)
{
if
(
a2
[
j
][
i
]
!=
0
)
{
if
(
a2
[
j
][
i
]
!=
0
)
{
//
the unicode of white circle
(there is a player2 disc)
// (there is a player2 disc)
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
WHITE
);
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
WHITE
);
}
}
else
{
else
{
if
(
a1
[
j
][
i
]
!=
0
)
{
if
(
a1
[
j
][
i
]
!=
0
)
{
//
the unicode of black circle
(there is a player1 disc)
// (there is a player1 disc)
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
BLACK
);
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
BLACK
);
}
}
else
else
//
the unicode of white square
(a blank)
// (a blank)
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
GRAY
);
searchInButtons
(
j
,
i
).
setBackground
(
Color
.
GRAY
);
}
}
}
}
}
}
}
}
/**
* gets a coordinate (x and y) and search in the list of buttons to find the buttons
* that is located in that position
* @param x is the x coordinate
* @param y is the y coordinate
* @return the button that locates on that position
*/
public
JButton
searchInButtons
(
int
x
,
int
y
){
public
JButton
searchInButtons
(
int
x
,
int
y
){
Iterator
<
JButton
>
buttonIterator
=
buttons
.
iterator
();
Iterator
<
JButton
>
buttonIterator
=
buttons
.
iterator
();
while
(
buttonIterator
.
hasNext
()){
while
(
buttonIterator
.
hasNext
()){
...
@@ -339,7 +360,6 @@ public class Graphic extends Display {
...
@@ -339,7 +360,6 @@ public class Graphic extends Display {
//System.out.println("koft!");
//System.out.println("koft!");
JButton
b1
=
buttonIterator
.
next
();
JButton
b1
=
buttonIterator
.
next
();
b1
.
addActionListener
(
new
ActionListener
()
{
b1
.
addActionListener
(
new
ActionListener
()
{
public
void
actionPerformed
(
ActionEvent
arg0
)
{
public
void
actionPerformed
(
ActionEvent
arg0
)
{
ints
[
0
]
=
(
b1
.
getX
()
-
143
)
/
58
+
1
;
ints
[
0
]
=
(
b1
.
getX
()
-
143
)
/
58
+
1
;
ints
[
1
]
=
(
b1
.
getY
()
-
135
)
/
58
+
1
;
ints
[
1
]
=
(
b1
.
getY
()
-
135
)
/
58
+
1
;
...
@@ -352,7 +372,7 @@ public class Graphic extends Display {
...
@@ -352,7 +372,7 @@ public class Graphic extends Display {
@Override
@Override
public
String
printing
(
String
toPrint
,
int
playerNumber
)
{
public
String
printing
(
String
toPrint
,
int
playerNumber
)
{
b
.
setVisible
(
false
);
b
.
setVisible
(
false
);
b
.
setText
(
super
.
printing
(
toPrint
,
playerNumber
));
b
.
setText
(
super
.
printing
(
toPrint
,
playerNumber
)
+
" (click here)"
);
b
.
setVisible
(
true
);
b
.
setVisible
(
true
);
return
super
.
printing
(
toPrint
,
playerNumber
);
return
super
.
printing
(
toPrint
,
playerNumber
);
}
}
...
...
Main.java
View file @
5591ec07
...
@@ -4,6 +4,7 @@ package com.company;
...
@@ -4,6 +4,7 @@ package com.company;
import
java.util.Scanner
;
import
java.util.Scanner
;
import
java.util.Timer
;
import
java.util.Timer
;
import
java.util.TimerTask
;
public
class
Main
{
public
class
Main
{
...
@@ -22,21 +23,20 @@ public class Main {
...
@@ -22,21 +23,20 @@ public class Main {
else
{
else
{
graphic
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
graphic
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
}
}
while
((
play1
.
getNumberOfDisks
(
play1
.
getArr1
())
+
play2
.
getNumberOfDisks
(
play2
.
getArr2
())
<
64
)
&&
while
((
play1
.
getNumberOfDisks
(
play1
.
getArr1
())
+
play2
.
getNumberOfDisks
(
play2
.
getArr2
())
<
64
)
&&
(
play1
.
getNumberOfDisks
(
play1
.
getArr1
())
!=
0
)
&&
(
play2
.
getNumberOfDisks
(
play2
.
getArr2
())
!=
0
))
(
play1
.
getNumberOfDisks
(
play1
.
getArr1
())
!=
0
)
&&
(
play2
.
getNumberOfDisks
(
play2
.
getArr2
())
!=
0
))
{
{
if
((
play1
.
numberOfPlayerPossibleMoves
(
play2
.
getArr2
(),
play1
.
getArr1
())
==
0
)
&&
if
((
play1
.
numberOfPlayerPossibleMoves
(
play2
.
getArr2
(),
play1
.
getArr1
())
==
0
)
&&
(
play2
.
numberOfPlayerPossibleMoves
(
play1
.
getArr1
(),
play2
.
getArr2
())
==
0
))
(
play2
.
numberOfPlayerPossibleMoves
(
play1
.
getArr1
(),
play2
.
getArr2
())
==
0
))
break
;
break
;
turns
++;
turns
++;
if
(
turns
%
2
==
1
)
{
if
(
turns
%
2
==
1
)
{
if
(
play1
.
numberOfPlayerPossibleMoves
(
play2
.
getArr2
(),
play1
.
getArr1
())
!=
0
)
{
if
(
play1
.
numberOfPlayerPossibleMoves
(
play2
.
getArr2
(),
play1
.
getArr1
())
!=
0
)
{
if
(
displayType
==
0
)
if
(
displayType
==
0
)
console
.
printing
(
"Player"
,
1
);
console
.
printing
(
"Player"
,
1
);
else
else
graphic
.
printing
(
"Player"
,
1
);
graphic
.
printing
(
"Player"
,
1
);
}
}
else
{
else
{
//if there is no possible move prints pass
//if there is no possible move prints pass
if
(
displayType
==
0
)
if
(
displayType
==
0
)
console
.
printing
(
"pass"
,
1
);
console
.
printing
(
"pass"
,
1
);
...
@@ -45,16 +45,13 @@ public class Main {
...
@@ -45,16 +45,13 @@ public class Main {
turns
++;
turns
++;
}
}
}
}
if
(
turns
%
2
==
0
)
{
if
(
turns
%
2
==
0
)
{
if
(
play2
.
numberOfPlayerPossibleMoves
(
play1
.
getArr1
(),
play2
.
getArr2
())
!=
0
)
{
if
(
play2
.
numberOfPlayerPossibleMoves
(
play1
.
getArr1
(),
play2
.
getArr2
())
!=
0
)
{
if
(
play1
.
numberOfPlayerPossibleMoves
(
play2
.
getArr2
(),
play1
.
getArr1
())
!=
0
)
{
if
(
displayType
==
0
)
if
(
displayType
==
0
)
console
.
printing
(
"Player"
,
2
);
console
.
printing
(
"Player"
,
2
);
else
else
graphic
.
printing
(
"Player"
,
2
);
graphic
.
printing
(
"Player"
,
2
);
}
else
{
}
}
else
{
//if there is no possible move prints pass
//if there is no possible move prints pass
if
(
displayType
==
0
)
if
(
displayType
==
0
)
console
.
printing
(
"pass"
,
2
);
console
.
printing
(
"pass"
,
2
);
...
@@ -66,8 +63,7 @@ public class Main {
...
@@ -66,8 +63,7 @@ public class Main {
int
[]
inputs
=
new
int
[
2
];
int
[]
inputs
=
new
int
[
2
];
if
(
displayType
==
0
)
{
if
(
displayType
==
0
)
{
inputs
=
console
.
getInput
();
inputs
=
console
.
getInput
();
}
}
else
{
else
{
inputs
=
graphic
.
getInput
();
inputs
=
graphic
.
getInput
();
}
}
int
y
=
inputs
[
1
];
int
y
=
inputs
[
1
];
...
@@ -75,27 +71,26 @@ public class Main {
...
@@ -75,27 +71,26 @@ public class Main {
System
.
out
.
println
(
"x = "
+
x
);
System
.
out
.
println
(
"x = "
+
x
);
System
.
out
.
println
(
"y = "
+
y
);
System
.
out
.
println
(
"y = "
+
y
);
if
((
x
>=
1
)
&&
(
x
<
9
)
&&
(
y
>=
1
)
&&
(
y
<
9
))
{
if
((
x
>=
1
)
&&
(
x
<
9
)
&&
(
y
>=
1
)
&&
(
y
<
9
))
{
if
((!
play1
.
containsInArray
(
play1
.
getArr1
(),
x
,
y
))
&&
(!
play2
.
containsInArray
(
play2
.
getArr2
(),
x
,
y
))){
if
((!
play1
.
containsInArray
(
play1
.
getArr1
(),
x
,
y
))
&&
(!
play2
.
containsInArray
(
play2
.
getArr2
(),
x
,
y
)))
{
if
(
turns
%
2
==
1
){
if
(
turns
%
2
==
1
)
{
play1
.
searchInNeighbors
(
play2
.
getArr2
(),
play1
.
getArr1
(),
x
,
y
);
play1
.
searchInNeighbors
(
play2
.
getArr2
(),
play1
.
getArr1
(),
x
,
y
);
}
}
else
else
play2
.
searchInNeighbors
(
play1
.
getArr1
(),
play2
.
getArr2
(),
x
,
y
);
play2
.
searchInNeighbors
(
play1
.
getArr1
(),
play2
.
getArr2
(),
x
,
y
);
}
}
}
}
if
(
displayType
==
0
)
{
if
(
displayType
==
0
)
{
console
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
console
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
}
}
else
{
else
{
graphic
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
graphic
.
display
(
play1
.
getArr1
(),
play2
.
getArr2
());
}
}
//System.out.println("player1: "+play1.getNumberOfDisks1()+" player2:"+play2.getNumberOfDisks2());
//System.out.println("player1: "+play1.getNumberOfDisks1()+" player2:"+play2.getNumberOfDisks2());
}
}
if
(
play1
.
getNumberOfDisks
(
play1
.
getArr1
())
>
play2
.
getNumberOfDisks
(
play2
.
getArr2
()))
/*
if (play1.getNumberOfDisks(play1.getArr1()) > play2.getNumberOfDisks(play2.getArr2()))
System.out.println("Player1 wins!");
System.out.println("Player1 wins!");
else
else
System
.
out
.
println
(
"Player2 wins!"
);
System.out.println("Player2 wins!");
*/
}
}
...
...
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