Skip to content

Commit 904675b

Browse files
committed
Code cleanup + Client/Server
1 parent ea01eee commit 904675b

File tree

14 files changed

+490
-119
lines changed

14 files changed

+490
-119
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ dependencies {
2828
testImplementation 'junit:junit:4.12'
2929
androidTestImplementation 'com.android.support.test:runner:1.0.2'
3030
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
31+
androidTestImplementation 'com.android.support.test:rules:1.0.2'
3132
}

app/src/androidTest/java/fr/m2ihm/a1819/shi_fu_me/ExampleInstrumentedTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public void useAppContext() {
2323

2424
assertEquals("fr.m2ihm.a1819.shi_fu_me", appContext.getPackageName());
2525
}
26+
27+
@Test
28+
public void updateImage() {
29+
30+
}
2631
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fr.m2ihm.a1819.shi_fu_me;
2+
3+
import android.support.test.InstrumentationRegistry;
4+
import android.support.test.rule.ActivityTestRule;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
8+
import org.junit.Assert;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
13+
import fr.m2ihm.a1819.shi_fu_me.activities.InGameActivity;
14+
15+
@RunWith(AndroidJUnit4.class)
16+
public class InGameActivityInstrumentedTest {
17+
18+
@Rule
19+
public ActivityTestRule<InGameActivity> activityTestRule = new ActivityTestRule<>(InGameActivity.class, true, true);
20+
21+
@Test
22+
public void scoreInitial0() {
23+
InGameActivity activity = activityTestRule.getActivity();
24+
Assert.assertEquals(activity.getTxt_your_score().getText().toString(), "Votre score 0");
25+
Assert.assertEquals(activity.getTxt_adv_score().getText().toString(), "Score adversaire 0");
26+
}
27+
28+
@Test
29+
public void imgSetOnClickScissor() {
30+
InGameActivity activity = activityTestRule.getActivity();
31+
activity.clickBtnCiseaux();
32+
Assert.assertEquals(activity.getImg_you().getDrawable(), InstrumentationRegistry.getTargetContext().getDrawable(R.drawable.ciseaux));
33+
}
34+
35+
@Test
36+
public void imgSetOnClickPaper() {
37+
InGameActivity activity = activityTestRule.getActivity();
38+
activity.clickBtnFeuille();
39+
Assert.assertEquals(activity.getImg_you().getDrawable(), InstrumentationRegistry.getTargetContext().getDrawable(R.drawable.feuille));
40+
}
41+
42+
@Test
43+
public void imgSetOnClickStone() {
44+
InGameActivity activity = activityTestRule.getActivity();
45+
activity.clickBtnPierre();
46+
Assert.assertEquals(activity.getImg_you().getDrawable(), InstrumentationRegistry.getTargetContext().getDrawable(R.drawable.pierre));
47+
}
48+
}

app/src/main/java/fr/m2ihm/a1819/shi_fu_me/activities/InGameActivity.java

Lines changed: 98 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,118 +6,166 @@
66
import android.os.Bundle;
77
import android.support.annotation.Nullable;
88
import android.support.v7.app.AppCompatActivity;
9-
import android.widget.ImageView;
9+
import android.support.v7.widget.AppCompatImageView;
1010
import android.widget.TextView;
1111

1212

1313
import butterknife.BindView;
1414
import butterknife.ButterKnife;
1515
import butterknife.OnClick;
1616
import fr.m2ihm.a1819.shi_fu_me.R;
17+
import fr.m2ihm.a1819.shi_fu_me.models.Game;
18+
import fr.m2ihm.a1819.shi_fu_me.p2p.Client;
1719
import fr.m2ihm.a1819.shi_fu_me.p2p.Common;
20+
import fr.m2ihm.a1819.shi_fu_me.p2p.Server;
1821
import fr.m2ihm.a1819.shi_fu_me.p2p.WiFiDirectBroadcastReceiver;
19-
import fr.m2ihm.a1819.shi_fu_me.utils.Choice;
20-
22+
import fr.m2ihm.a1819.shi_fu_me.models.Choice;
23+
import fr.m2ihm.a1819.shi_fu_me.utils.AndroidDrawableResource;
24+
import fr.m2ihm.a1819.shi_fu_me.utils.AndroidImageViewDrawer;
25+
import fr.m2ihm.a1819.shi_fu_me.utils.Opponent;
26+
import fr.m2ihm.a1819.shi_fu_me.utils.OpponentFactory;
27+
28+
/**
29+
* Activité affichée pendant le jeu
30+
* @version 1.0.0
31+
*/
2132
public class InGameActivity extends AppCompatActivity {
2233

23-
private Choice playerChoice;
24-
private Choice computerChoice;
25-
private int yourScore = 0;
26-
private int advScore = 0;
34+
final Game.GameType GAME_TYPE = Game.GameType.SINGLEPLAYER;
35+
36+
Game game = new Game();
37+
Opponent opponent = OpponentFactory.getOpponent(GAME_TYPE);
38+
39+
AndroidImageViewDrawer imgDrawerPlayer;
40+
AndroidImageViewDrawer imgDrawerOpponent;
41+
42+
@BindView(R.id.img_you) AppCompatImageView img_you;
43+
@BindView(R.id.img_adv) AppCompatImageView img_adv;
2744

28-
@BindView(R.id.img_you) ImageView img_you;
29-
@BindView(R.id.img_adv) ImageView img_adv;
3045
@BindView(R.id.txt_your_score) TextView txt_your_score;
3146
@BindView(R.id.txt_adv_score) TextView txt_adv_score;
32-
private WifiP2pManager manager;
33-
private WifiP2pManager.Channel channel;
34-
private WiFiDirectBroadcastReceiver reciever;
47+
48+
private WiFiDirectBroadcastReceiver receiver;
3549
private IntentFilter intentFilter;
36-
private Common clientServer;
3750

51+
private Server server;
52+
private Client client;
53+
54+
/**
55+
* Callback du clique sur le bouton ciseaux
56+
*/
3857
@OnClick(R.id.btn_ciseaux) public void clickBtnCiseaux() {
39-
playerChoice = Choice.CISEAUX;
40-
computerChoice = Choice.randomChoice();
41-
setImages();
42-
score();
58+
game.setPlayerChoice(Choice.CISEAUX);
59+
game.setAdvChoice(opponent.getChoice());
60+
game.updateScore();
61+
updateUi();
4362
}
4463

64+
/**
65+
* Callback du clique sur le bouton feuille
66+
*/
4567
@OnClick(R.id.btn_feuille) public void clickBtnFeuille() {
46-
playerChoice = Choice.FEUILLE;
47-
computerChoice = Choice.randomChoice();
48-
setImages();
49-
score();
68+
game.setPlayerChoice(Choice.FEUILLE);
69+
game.setAdvChoice(opponent.getChoice());
70+
game.updateScore();
71+
updateUi();
5072
}
5173

74+
/**
75+
* Callback du clique sur le boutton pierre
76+
*/
5277
@OnClick(R.id.btn_pierre) public void clickBtnPierre() {
53-
playerChoice = Choice.PIERRE;
54-
computerChoice = Choice.randomChoice();
55-
setImages();
56-
score();
78+
game.setPlayerChoice(Choice.PIERRE);
79+
game.setAdvChoice(opponent.getChoice());
80+
game.updateScore();
81+
updateUi();
5782
}
5883

59-
private void setImages() {
60-
img_you.setImageResource(playerChoice.getDrawable());
61-
img_adv.setImageResource(computerChoice.getDrawable());
62-
}
63-
64-
private void score() {
65-
if (this.playerChoice.win(this.computerChoice))
66-
this.yourScore++;
67-
else if(this.computerChoice.win(this.playerChoice))
68-
this.advScore++;
69-
84+
/**
85+
* Met à jours l'affichage en fonction des scores et des choix des joueurs
86+
*/
87+
private void updateUi() {
88+
imgDrawerPlayer.drawResource((AndroidDrawableResource) game.getPlayerChoice().getRessource());
89+
imgDrawerOpponent.drawResource((AndroidDrawableResource) game.getAdvChoice().getRessource());
7090
StringBuilder strB_adv = new StringBuilder();
7191
StringBuilder strB_you = new StringBuilder();
7292

7393
strB_adv.append("Score adversaire ");
74-
strB_adv.append(this.advScore);
94+
strB_adv.append(this.game.getAdvScore());
7595
this.txt_adv_score.setText(strB_adv);
7696

7797
strB_you.append("Votre score ");
78-
strB_you.append(this.yourScore);
98+
strB_you.append(this.game.getPlayerScore());
7999
this.txt_your_score.setText(strB_you);
80100
}
81101

102+
82103
@Override
83104
public void onCreate(@Nullable Bundle savedInstanceState) {
84105
super.onCreate(savedInstanceState);
85106
setContentView(R.layout.activity_in_game);
86107
ButterKnife.bind(this);
87108
setTitle(R.string.app_name);
88109

89-
manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
90-
channel = manager.initialize(this, getMainLooper(), null);
91-
reciever = new WiFiDirectBroadcastReceiver(manager, channel, this);
110+
imgDrawerPlayer = new AndroidImageViewDrawer(img_you);
111+
imgDrawerOpponent = new AndroidImageViewDrawer(img_adv);
112+
113+
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
114+
assert manager != null;
115+
WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
116+
receiver = new WiFiDirectBroadcastReceiver(manager, channel, this);
92117

93118
intentFilter = new IntentFilter();
94119
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
95120
intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
96121
intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
97122
intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
98123

99-
img_adv.setRotation(180);
100-
this.txt_your_score.setText("Votre score ");
101-
this.txt_adv_score.setText("Score adversaire ");
124+
updateUi();
102125
}
103126

104127
@Override
105128
protected void onResume() {
106129
super.onResume();
107-
registerReceiver(reciever, intentFilter);
130+
registerReceiver(receiver, intentFilter);
108131
}
109132

110133
@Override
111134
protected void onPause() {
112135
super.onPause();
113-
unregisterReceiver(reciever);
136+
unregisterReceiver(receiver);
137+
}
138+
139+
140+
public AppCompatImageView getImg_you() {
141+
return img_you;
142+
}
143+
144+
public AppCompatImageView getImg_adv() {
145+
return img_adv;
146+
}
147+
148+
public TextView getTxt_your_score() {
149+
return txt_your_score;
150+
}
151+
152+
public TextView getTxt_adv_score() {
153+
return txt_adv_score;
154+
}
155+
156+
public Server getServer() {
157+
return server;
158+
}
159+
160+
public void setServer(Server server) {
161+
this.server = server;
114162
}
115163

116-
public Common getClientServer() {
117-
return clientServer;
164+
public Client getClient() {
165+
return client;
118166
}
119167

120-
public void setClientServer(Common clientServer) {
121-
this.clientServer = clientServer;
168+
public void setClient(Client client) {
169+
this.client = client;
122170
}
123171
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package fr.m2ihm.a1819.shi_fu_me.models;
2+
import android.support.annotation.NonNull;
3+
4+
import fr.m2ihm.a1819.shi_fu_me.R;
5+
import fr.m2ihm.a1819.shi_fu_me.utils.AndroidDrawableResource;
6+
import fr.m2ihm.a1819.shi_fu_me.utils.DrawableResource;
7+
8+
import java.util.Random;
9+
10+
/**
11+
* Classe permettant de représenter les différents choix du jeu
12+
* @version 1.0.0
13+
*/
14+
public enum Choice {
15+
16+
UNSET (new AndroidDrawableResource(R.drawable.ciseaux)),
17+
PIERRE(new AndroidDrawableResource(R.drawable.pierre)),
18+
FEUILLE(new AndroidDrawableResource(R.drawable.feuille)),
19+
CISEAUX(new AndroidDrawableResource(R.drawable.ciseaux));
20+
21+
/**
22+
* Objet permetant de gérer l'aléatoir
23+
*/
24+
private static Random random = new Random();
25+
26+
/**
27+
* Effectue un choix aléatoir parmis les différents choix possible
28+
* @return Un choix aléatoir
29+
*/
30+
public static @NonNull Choice randomChoice() {
31+
Choice choice;
32+
do {
33+
choice = Choice.values()[random.nextInt(Choice.values().length)];
34+
}while (choice.equals(UNSET));
35+
return choice;
36+
}
37+
38+
/**
39+
* L"identifiant du drawable utilisé pour représenter le choix
40+
*/
41+
42+
private DrawableResource ressource;
43+
44+
/**
45+
* Constructeur d'un choix
46+
* @param ressource Ressource utilisé pour être rendue
47+
*/
48+
Choice(DrawableResource ressource) {
49+
this.ressource = ressource;
50+
}
51+
52+
/**
53+
* Test si le choix passé en paramètre est battus par le choix actuel
54+
* @param adv Le choix à tester
55+
* @return Vraie si adv est battu par le le choix actuel, faux sinon ou si exéquo
56+
*/
57+
public boolean win(@NonNull Choice adv) {
58+
switch (adv) {
59+
case PIERRE: return this.equals(FEUILLE);
60+
case CISEAUX: return this.equals(PIERRE);
61+
case FEUILLE: return this.equals(CISEAUX);
62+
default: return false;
63+
}
64+
}
65+
66+
/**
67+
* Obtiens la ressource utilisé pour le rendu
68+
* @return La ressource représentant le choix
69+
*/
70+
public @NonNull DrawableResource getRessource() {
71+
return ressource;
72+
}
73+
}

0 commit comments

Comments
 (0)