Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
Project3_Uno
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
Project3_Uno
Commits
a408c34f
Commit
a408c34f
authored
Apr 19, 2020
by
amir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
using HashMap for saving possible cards instead of arrayList
parent
ed2b30de
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
61 deletions
+62
-61
Comp.java
Comp.java
+11
-11
Human.java
Human.java
+7
-7
Main.java
Main.java
+12
-7
Player.java
Player.java
+32
-36
No files found.
Comp.java
View file @
a408c34f
...
...
@@ -9,10 +9,9 @@ public class Comp extends Player {
@Override
public
Card
chooseCardToThrow
(
ArrayList
<
Card
>
cards
,
Card
card
)
{
Card
c
=
null
;
if
((
getCounter
()
==
0
&&
possibleCards
.
size
()
>
0
)
||
(
getCounter
()
>
0
&&
possibleCards
.
size
()
>
1
))
{
int
number
=
r
.
nextInt
(
getPossibleCards
().
size
());
c
=
possibleCards
.
get
(
number
);
possibleCards
=
null
;
if
(
possibleCards
.
get
(
card
).
size
()
>
0
)
{
int
number
=
r
.
nextInt
(
possibleCards
.
get
(
card
).
size
());
c
=
possibleCards
.
get
(
card
).
get
(
number
);
getPlayerCards
().
remove
(
c
);
}
else
{
...
...
@@ -36,16 +35,16 @@ public class Comp extends Player {
int
color
=
r
.
nextInt
(
4
)
+
1
;
switch
(
color
){
case
1
:
s
=
"
r
ed"
;
s
=
"
R
ed"
;
break
;
case
2
:
s
=
"
g
reen"
;
s
=
"
G
reen"
;
break
;
case
3
:
s
=
"
y
ellow"
;
s
=
"
Y
ellow"
;
break
;
case
4
:
s
=
"
b
lue"
;
s
=
"
B
lue"
;
break
;
}
}
...
...
@@ -55,14 +54,15 @@ public class Comp extends Player {
@Override
public
Card
chooseColorToThrow
(
ArrayList
<
Card
>
cards
,
String
Color
)
{
Card
c
=
null
;
if
(
(
getCounter
()
==
0
&&
possibleCards
.
size
()
>
0
)
||
(
getCounter
()
>
0
&&
possibleCards
.
size
()
>
1
)
)
{
if
(
possibleColorCards
.
get
(
Color
).
size
()
>
0
)
{
int
number
=
r
.
nextInt
(
getPossibleCards
().
size
());
c
=
possibleCards
.
get
(
number
);
possibleCards
=
null
;
c
=
possibleColorCards
.
get
(
Color
).
get
(
number
);
getPlayerCards
().
remove
(
c
);
}
else
{
System
.
out
.
println
(
"kirekhar"
);
c
=
randomPunishmentCard
(
cards
);
System
.
out
.
println
(
c
.
toString
());
getPlayerCards
().
add
(
c
);
if
(
c
instanceof
Colored
)
{
if
(!
c
.
cardColor
.
equals
(
Color
))
{
...
...
Human.java
View file @
a408c34f
...
...
@@ -9,16 +9,16 @@ public class Human extends Player {
@Override
public
Card
chooseCardToThrow
(
ArrayList
<
Card
>
cards
,
Card
c
)
{
Card
card
=
null
;
if
(
((
getCounter
()
==
0
&&
possibleCards
.
size
()
>
0
)
||
(
getCounter
()
>
0
&&
possibleCards
.
size
()
>
1
))
)
{
if
(
possibleCards
.
get
(
c
).
size
()
>
0
)
{
System
.
out
.
println
(
"Enter number of the card you wanna throw it!"
);
int
number
=
scanner
.
nextInt
();
if
(
number
>
0
&&
number
<=
getPlayerCards
().
size
()
&&
possibleCards
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
possibleCards
.
get
(
c
).
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
//possibleCards = null;
System
.
out
.
println
(
"kire khar!"
);
}
else
{
while
(
number
<=
0
||
number
>
getPlayerCards
().
size
()
||
!
possibleCards
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
!
possibleCards
.
get
(
c
).
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
System
.
out
.
println
(
"Invalid input"
);
System
.
out
.
println
(
"Enter number of the card you wanna throw it!"
);
number
=
scanner
.
nextInt
();
...
...
@@ -47,7 +47,7 @@ public class Human extends Player {
public
String
chooseColorIfBlack
(
Card
card
){
String
s
=
null
;
if
(
card
instanceof
Black
){
System
.
out
.
println
(
"Choose a color:
red, green, yellow, b
lue"
);
System
.
out
.
println
(
"Choose a color:
Red, Green, Yellow, B
lue"
);
s
=
scanner
.
next
();
}
return
s
;
...
...
@@ -56,11 +56,11 @@ public class Human extends Player {
@Override
public
Card
chooseColorToThrow
(
ArrayList
<
Card
>
cards
,
String
Color
)
{
Card
card
=
null
;
if
(
(
getCounter
()
==
0
&&
possibleCards
.
size
()
>
0
)
||
(
getCounter
()
>
0
&&
possibleCards
.
size
()
>
1
)
)
{
if
(
possibleColorCards
.
get
(
Color
).
size
()
>
0
)
{
System
.
out
.
println
(
"Enter number of the card you wanna throw it!"
);
int
number
=
scanner
.
nextInt
();
if
(
number
>
0
&&
number
<=
getPlayerCards
().
size
()
&&
possibleC
ards
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
possibleC
olorCards
.
get
(
Color
)
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
//possibleCards = null;
System
.
out
.
println
(
"kire khar!"
);
card
=
getPlayerCards
().
get
(
number
-
1
);
...
...
@@ -68,7 +68,7 @@ public class Human extends Player {
return
card
;
}
else
{
while
(
number
<=
0
||
number
>
getPlayerCards
().
size
()
||
!
possibleC
ards
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
!
possibleC
olorCards
.
get
(
Color
)
.
contains
(
getPlayerCards
().
get
(
number
-
1
)))
{
System
.
out
.
println
(
"Invalid input"
);
System
.
out
.
println
(
"Enter number of the card you wanna throw it!"
);
number
=
scanner
.
nextInt
();
...
...
Main.java
View file @
a408c34f
...
...
@@ -128,25 +128,30 @@ public class Main {
System
.
out
.
println
(
"Clockwise"
);
else
System
.
out
.
println
(
"CounterClockwise"
);
if
(
nextColor
==
null
)
if
(
nextColor
==
null
)
{
players
.
get
(
turns
).
checkAllCards
(
nextCard
);
else
players
.
get
(
turns
).
printAllPossibleCards
(
turns
+
1
,
nextCard
);
}
else
{
players
.
get
(
turns
).
checkColors
(
nextColor
);
players
.
get
(
turns
).
printAllPossibleCards
(
turns
+
1
);
players
.
get
(
turns
).
printAllPossibleColoredCards
(
turns
+
1
,
nextColor
);
}
System
.
out
.
println
();
players
.
get
(
turns
).
printAllCards
(
turns
+
1
);
System
.
out
.
println
();
if
(
nextC
ard
!
=
null
)
if
(
nextC
olor
=
=
null
)
card
=
players
.
get
(
turns
).
chooseCardToThrow
(
cards
,
nextCard
);
else
card
=
players
.
get
(
turns
).
chooseColorToThrow
(
cards
,
nextColor
);
if
(
card
!=
null
)
{
System
.
out
.
println
(
card
.
toString
());
if
(
nextColor
!=
null
)
nextColor
=
null
;
nextColor
=
players
.
get
(
turns
).
chooseColorIfBlack
(
card
);
if
(
nextColor
==
null
)
nextCard
=
card
;
}
if
(
nextC
ard
!
=
null
)
if
(
nextC
olor
=
=
null
)
System
.
out
.
println
(
nextCard
.
toString
());
else
System
.
out
.
println
(
nextColor
);
...
...
Player.java
View file @
a408c34f
...
...
@@ -4,6 +4,7 @@ import com.sun.javafx.scene.control.skin.VirtualFlow;
import
java.lang.reflect.Array
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Random
;
...
...
@@ -13,7 +14,8 @@ public class Player {
// point of the player
private
int
playerPoint
;
protected
ArrayList
<
Card
>
possibleCards
=
new
ArrayList
<>();
protected
HashMap
<
Card
,
ArrayList
<
Card
>>
possibleCards
=
new
HashMap
<>();
protected
HashMap
<
String
,
ArrayList
<
Card
>>
possibleColorCards
=
new
HashMap
<>();
private
int
counter
=
0
;
/**
...
...
@@ -41,7 +43,7 @@ public class Player {
return
counter
;
}
public
ArrayList
<
Card
>
getPossibleCards
()
{
public
HashMap
<
Card
,
ArrayList
<
Card
>
>
getPossibleCards
()
{
return
possibleCards
;
}
...
...
@@ -55,14 +57,7 @@ public class Player {
public
void
checkAllCards
(
Card
card
){
boolean
isColored
=
false
;
if
(
possibleCards
==
null
)
possibleCards
=
new
ArrayList
<>();
if
(
possibleCards
.
size
()
>
1
){
for
(
int
i
=
1
;
i
<
possibleCards
.
size
();
i
++){
possibleCards
.
remove
(
i
);
i
--;
}
}
possibleCards
.
put
(
card
,
new
ArrayList
<
Card
>());
if
(
card
instanceof
Colored
)
isColored
=
true
;
Iterator
<
Card
>
cardIterator
=
playerCards
.
iterator
();
...
...
@@ -71,14 +66,14 @@ public class Player {
if
(
c
instanceof
Colored
)
{
if
(
isColored
)
if
(((
Colored
)
c
).
checkEquality
((
Colored
)
card
))
possibleCards
.
add
(
c
);
possibleCards
.
get
(
card
).
add
(
c
);
}
else
possibleCards
.
add
(
c
);
possibleCards
.
get
(
card
).
add
(
c
);
}
if
(
possibleCards
.
size
()
>
0
)
{
if
(
possibleCards
.
size
()
!=
searchWildDraws
(
))
deleteWildDraws
();
if
(
possibleCards
.
get
(
card
).
size
()
>
0
)
{
if
(
possibleCards
.
get
(
card
).
size
()
!=
searchWildDraws
(
card
))
deleteWildDraws
(
card
);
}
}
...
...
@@ -89,22 +84,28 @@ public class Player {
return
cards
.
get
(
next
);
}
public
void
printAllPossibleCards
(
int
playerNumber
){
public
void
printAllPossibleCards
(
int
playerNumber
,
Card
cardToPrint
){
System
.
out
.
println
(
"Player"
+
playerNumber
+
": (Possible cards)"
);
Iterator
<
Card
>
cardIterator
=
possibleCards
.
iterator
();
if
(
counter
!=
0
)
{
cardIterator
.
next
();
Iterator
<
Card
>
cardIterator
=
possibleCards
.
get
(
cardToPrint
).
iterator
();
while
(
cardIterator
.
hasNext
()){
Card
c
=
cardIterator
.
next
();
System
.
out
.
println
(
c
.
toString
());
}
while
(
cardIterator
.
hasNext
())
{
}
public
void
printAllPossibleColoredCards
(
int
playerNumber
,
String
colorToPrint
){
System
.
out
.
println
(
"Player"
+
playerNumber
+
": (Possible color cards)"
);
Iterator
<
Card
>
cardIterator
=
possibleColorCards
.
get
(
colorToPrint
).
iterator
();
while
(
cardIterator
.
hasNext
()){
Card
c
=
cardIterator
.
next
();
System
.
out
.
println
(
c
.
toString
());
}
counter
++;
}
private
int
searchWildDraws
(){
private
int
searchWildDraws
(
Card
card
){
int
counterWildDraw
=
0
;
Iterator
<
Card
>
cardIterator
=
possibleCards
.
iterator
();
Iterator
<
Card
>
cardIterator
=
possibleCards
.
get
(
card
).
iterator
();
while
(
cardIterator
.
hasNext
()){
Card
c
=
cardIterator
.
next
();
if
(
c
instanceof
WildDraw
)
...
...
@@ -113,10 +114,10 @@ public class Player {
return
counterWildDraw
;
}
private
void
deleteWildDraws
(){
for
(
int
i
=
0
;
i
<
possibleCards
.
size
();
i
++){
if
(
possibleCards
.
get
(
i
)
instanceof
WildDraw
){
possibleCards
.
remove
(
i
);
private
void
deleteWildDraws
(
Card
card
){
for
(
int
i
=
0
;
i
<
possibleCards
.
get
(
card
).
size
();
i
++){
if
(
possibleCards
.
get
(
card
).
get
(
i
)
instanceof
WildDraw
){
possibleCards
.
get
(
card
).
remove
(
i
);
i
--;
}
}
...
...
@@ -136,21 +137,16 @@ public class Player {
" {playerPoint="
+
playerPoint
+
" }"
;
}
public
void
checkColors
(
String
color
){
if
(
possibleCards
==
null
)
possibleCards
=
new
ArrayList
<>();
if
(
possibleCards
.
size
()
>
1
){
for
(
int
i
=
1
;
i
<
possibleCards
.
size
();
i
++){
possibleCards
.
remove
(
i
);
i
--;
}
}
System
.
out
.
println
(
"color = "
+
color
);
possibleColorCards
.
put
(
color
,
new
ArrayList
<
Card
>());
Iterator
<
Card
>
cardIterator
=
playerCards
.
iterator
();
while
(
cardIterator
.
hasNext
())
{
Card
c
=
cardIterator
.
next
();
if
(
c
instanceof
Colored
)
if
(
c
.
cardColor
.
equals
(
color
))
possibleC
ards
.
add
(
c
);
possibleC
olorCards
.
get
(
color
)
.
add
(
c
);
}
}
...
...
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