Skip to content

Commit 3dc2145

Browse files
committed
Added game modes
1 parent 6280579 commit 3dc2145

File tree

12 files changed

+99
-14
lines changed

12 files changed

+99
-14
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
android:label="@string/app_name"
99
android:supportsRtl="true"
1010
android:theme="@style/AppTheme">
11-
<activity android:name=".MainActivity">
11+
<activity
12+
android:name=".MainActivity"
13+
android:screenOrientation="portrait">
1214
<intent-filter>
1315
<action android:name="android.intent.action.MAIN" />
1416

app/src/main/java/com/zduo/dotsandboxes/MainActivity.java

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.content.DialogInterface;
55
import android.os.Bundle;
66
import android.support.v7.app.AppCompatActivity;
7+
import android.view.Menu;
8+
import android.view.MenuItem;
79
import android.widget.ImageView;
810
import android.widget.TextView;
911

@@ -18,7 +20,8 @@
1820
public class MainActivity extends AppCompatActivity implements PlayersStateView {
1921

2022
protected GameView gameView;
21-
protected TextView player1state, player2state, player1occupying, player2occupying;
23+
protected TextView player1name, player2name, player1state, player2state, player1occupying,
24+
player2occupying;
2225
ImageView currentPlayerPointer;
2326
Player[] players;
2427
Integer[] playersOccupying = new Integer[]{0, 0};
@@ -32,17 +35,19 @@ protected void onCreate(Bundle savedInstanceState) {
3235
gameView = (GameView) findViewById(R.id.gameView);
3336
gameView.setPlayersState(this);
3437

38+
player1name = (TextView) findViewById(R.id.player1name);
39+
player2name = (TextView) findViewById(R.id.player2name);
3540
player1state = (TextView) findViewById(R.id.player1state);
3641
player2state = (TextView) findViewById(R.id.player2state);
3742
player1occupying = (TextView) findViewById(R.id.player1occupying);
3843
player2occupying = (TextView) findViewById(R.id.player2occupying);
3944
currentPlayerPointer = (ImageView) findViewById(R.id.playerNowPointer);
4045

41-
startGame();
46+
players = new Player[]{new HumanPlayer("Human"), new RandomAIPlayer("Computer")};
47+
startGame(players);
4248
}
4349

44-
private void startGame() {
45-
players = new Player[]{new HumanPlayer("Human"), new RandomAIPlayer("Computer")};
50+
private void startGame(Player[] players) {
4651
gameView.startGame(players);
4752
updateState();
4853
}
@@ -100,4 +105,66 @@ public void onClick(DialogInterface dialogInterface, int i) {
100105
}
101106
});
102107
}
108+
109+
@Override
110+
public boolean onCreateOptionsMenu(Menu menu) {
111+
getMenuInflater().inflate(R.menu.main, menu);
112+
return super.onCreateOptionsMenu(menu);
113+
}
114+
115+
@Override
116+
public boolean onOptionsItemSelected(MenuItem item) {
117+
int id = item.getItemId();
118+
119+
if (id == R.id.action_new) {
120+
runOnUiThread(new Runnable() {
121+
@Override
122+
public void run() {
123+
new AlertDialog.Builder(MainActivity.this)
124+
.setTitle("Dots And Boxes")
125+
.setMessage("New game versus")
126+
.setPositiveButton("Computer", new DialogInterface.OnClickListener() {
127+
public void onClick(DialogInterface dialog, int which) {
128+
new AlertDialog.Builder(MainActivity.this)
129+
.setTitle("Who goes first?")
130+
.setPositiveButton("Computer", new DialogInterface.OnClickListener() {
131+
@Override
132+
public void onClick(DialogInterface dialogInterface, int i) {
133+
players = new Player[]{new RandomAIPlayer("Computer"),
134+
new HumanPlayer("Human")};
135+
startGame(players);
136+
137+
player1name.setText("Computer");
138+
player2name.setText("Human");
139+
}
140+
})
141+
.setNegativeButton("Human", new DialogInterface.OnClickListener() {
142+
@Override
143+
public void onClick(DialogInterface dialogInterface, int i) {
144+
players = new Player[]{new HumanPlayer("Human"),
145+
new RandomAIPlayer("Computer")};
146+
startGame(players);
147+
148+
player1name.setText("Human");
149+
player2name.setText("Computer");
150+
}
151+
}).show();
152+
}
153+
})
154+
.setNeutralButton("Human", new DialogInterface.OnClickListener() {
155+
@Override
156+
public void onClick(DialogInterface dialogInterface, int i) {
157+
players = new Player[]{new HumanPlayer("Player 1"), new HumanPlayer("Player 2")};
158+
startGame(players);
159+
160+
player1name.setText("Player 1");
161+
player2name.setText("Player 2");
162+
}
163+
}).show();
164+
}
165+
});
166+
}
167+
168+
return super.onOptionsItemSelected(item);
169+
}
103170
}

app/src/main/java/com/zduo/dotsandboxes/model/Game.java renamed to app/src/main/java/com/zduo/dotsandboxes/model/Graph.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Observable;
44

5-
public class Game extends Observable {
5+
public class Graph extends Observable {
66
private Player[] players;
77
private int currentPlayerIndex;
88
private int width;
@@ -12,7 +12,7 @@ public class Game extends Observable {
1212
private int[][] verticalLines;
1313
private Line latestLine;
1414

15-
public Game(int width, int height, Player[] players) {
15+
public Graph(int width, int height, Player[] players) {
1616
this.width = width;
1717
this.height = height;
1818
this.players = players;

app/src/main/java/com/zduo/dotsandboxes/model/Player.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public abstract class Player {
44
protected final String name;
5-
protected Game game;
5+
protected Graph game;
66

77
public Player(String name) {
88
this.name = name;
@@ -18,11 +18,11 @@ public static int indexIn(Player player, Player[] players) {
1818

1919
public abstract Line move();
2020

21-
public Game getGame() {
21+
public Graph getGame() {
2222
return game;
2323
}
2424

25-
public void addToGame(Game game) {
25+
public void addToGame(Graph game) {
2626
this.game = game;
2727
}
2828

app/src/main/java/com/zduo/dotsandboxes/view/GameView.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import android.graphics.Color;
66
import android.graphics.Paint;
77
import android.util.AttributeSet;
8+
import android.util.Log;
89
import android.view.MotionEvent;
910
import android.view.View;
1011

1112
import com.zduo.dotsandboxes.R;
1213
import com.zduo.dotsandboxes.model.Direction;
13-
import com.zduo.dotsandboxes.model.Game;
14+
import com.zduo.dotsandboxes.model.Graph;
1415
import com.zduo.dotsandboxes.model.HumanPlayer;
1516
import com.zduo.dotsandboxes.model.Line;
1617
import com.zduo.dotsandboxes.model.Player;
@@ -31,7 +32,7 @@ public class GameView extends View implements Observer {
3132
protected static final float add6 = (float) 9 / 824;
3233

3334
protected final int[] playerColors;
34-
protected Game game;
35+
protected Graph game;
3536
protected Line move;
3637
protected Paint paint;
3738
protected PlayersStateView playersState;
@@ -58,7 +59,7 @@ public void setPlayersState(PlayersStateView playersState) {
5859
}
5960

6061
public void startGame(Player[] players) {
61-
game = new Game(5, 5, players);
62+
game = new Graph(5, 5, players);
6263
game.addObserver(this);
6364
new Thread() {
6465
@Override
@@ -183,7 +184,11 @@ private void receiveInput(MotionEvent event) {
183184
else
184185
direction = Direction.VERTICAL;
185186
move = new Line(direction, a, b);
186-
((HumanPlayer) game.currentPlayer()).add(move);
187+
try {
188+
((HumanPlayer) game.currentPlayer()).add(move);
189+
} catch (Exception e) {
190+
Log.e("GameView", e.toString());
191+
}
187192
}
188193
}
189194

app/src/main/res/menu/main.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
4+
5+
<item
6+
android:id="@+id/action_new"
7+
android:title="@string/action_new"
8+
app:showAsAction="always" />
9+
</menu>
-1.97 KB
Loading
-1.04 KB
Loading
-3.11 KB
Loading
-1.94 KB
Loading

0 commit comments

Comments
 (0)