Skip to content

Commit

Permalink
Player 1 remote connecting support is added.
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Apr 12, 2015
1 parent 06fa73d commit e0d55e7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ http://maven.apache.org/xsd/maven-4.0.0.xsd">

<groupId>home.parham</groupId>
<artifactId>OpenTrax</artifactId>
<version>3.1</version>
<version>4.0</version>
<packaging>jar</packaging>

<name>Open Trax</name>
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/home/parham/trax/core/domain/TraxBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,7 @@ public TraxStatus isGameOver(){
return gameover;
}

// if (uniqueMoves().size() == 0) {
// gameover = TraxStatus.DRAW;
// return gameover;
// }

// Now check loop wins
/* Now check loop wins */
for (int i = 1; i < 8; i++) {
for (int j = 1; j < 8; j++) {
switch (getAt(i, j)) {
Expand Down Expand Up @@ -948,7 +943,7 @@ private boolean checkLine(int row, int col, char direction, char type){
+ "llduud" // 'l' 13..18
+ "rruddu"; // 'r' 19..24

for (; ; ) {
while (true) {
if (isBlank(row, col))
return false; // no line starts with a empty space or we are out of range
switch (direction) {
Expand Down Expand Up @@ -985,10 +980,12 @@ private boolean checkLine(int row, int col, char direction, char type){
col++;
break;
}
// if ((type == 'h') && (col == 9))
// return true; // left-right win
// if ((type == 'v') && (row == 9))
// return true; // top-bottom win
/*
if ((type == 'h') && (col == 9))
return true; // left-right win
if ((type == 'v') && (row == 9))
return true; // top-bottom win
*/
if ((row == start_row) && (col == start_col)) {
/* loop win */
return type == 'l';
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/home/parham/trax/core/player/PlayerSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public PlayerSimple(){
public String move(String otherPlayerMove){
String move = null;
try {
tb.makeMove(new TraxMove(otherPlayerMove));
if (!otherPlayerMove.equals(""))
tb.makeMove(new TraxMove(otherPlayerMove));
move = getRandomMove(tb);
} catch (IllegalMoveException e) {
System.err.println("[" + e.getMove() + "] : " + e.getMessage());
System.err.println(this.getClass().getName() + " : " + "[" + e.getMove() + "] : " + e.getMessage());
}
return move;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public ServerNetPlayer(){
@Override
public String move(String otherPlayerMove){
try {
PrintWriter ostream = new PrintWriter(player.getOutputStream());
ostream.println(otherPlayerMove);
ostream.flush();
if (otherPlayerMove != null) {
PrintWriter ostream = new PrintWriter(player.getOutputStream());
ostream.println(otherPlayerMove);
ostream.flush();
}
System.out.println("Waiting for remote player");
BufferedReader istream = new BufferedReader(new InputStreamReader(player.getInputStream()));
return istream.readLine();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/home/parham/trax/core/util/TraxVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

public final class TraxVersion {

private static String version = "V3.0";
private static String versionName = "Collina";
private static String version = "V4.0";
private static String versionName = "MSK";

public static String getVersion(){
return version + " (" + versionName + ")";
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/home/parham/trax/gui/ChoosePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ChoosePlayer extends JDialog {
private JRadioButton simpleAIButton;
private JRadioButton serverNetButton;

private int choosenOption;
private int chosenOption;

public ChoosePlayer(JFrame owner){
super(owner, "Choose Player", true);
Expand Down Expand Up @@ -83,22 +83,22 @@ public void actionPerformed(ActionEvent e){
}

private void onOK(){
choosenOption = 0;
chosenOption = 0;
if (humanButton.isSelected())
choosenOption = 0;
chosenOption = 0;
if (simpleAIButton.isSelected())
choosenOption = 1;
chosenOption = 1;
if (serverNetButton.isSelected())
choosenOption = 2;
chosenOption = 2;
dispose();
}

private void onCancel(){
choosenOption = 0;
chosenOption = 0;
dispose();
}

public int getChoosenOption(){
return choosenOption;
public int getChosenOption(){
return chosenOption;
}
}
79 changes: 56 additions & 23 deletions src/main/java/home/parham/trax/gui/GnuTraxGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public class GnuTraxGui extends JFrame {
private JPanel outerPanel;
private List<ImagePanel> board;
private TraxBoard traxBoard;
private Player player;
private boolean haveRemote;
private Player player1;
private Player player2;
private boolean haveRemote1;
private boolean haveRemote2;

public GnuTraxGui(){
super("GnuTrax " + TraxVersion.getVersion());
Expand All @@ -38,25 +40,48 @@ public GnuTraxGui(){
private void newGame(){
Commands.userNew();

ChoosePlayer playerChooser = new ChoosePlayer(this);
playerChooser.setVisible(true);
int op = playerChooser.getChoosenOption();
if (op == 1) {
haveRemote = true;
player = new PlayerSimple();
} else if (op == 0) {
haveRemote = false;
player = null;
} else if (op == 2) {
haveRemote = true;
player = new ServerNetPlayer();
ChoosePlayer playerChooser1 = new ChoosePlayer(this);
playerChooser1.setVisible(true);
int op1 = playerChooser1.getChosenOption();
if (op1 == 1) {
haveRemote1 = true;
player1 = new PlayerSimple();
} else if (op1 == 0) {
haveRemote1 = false;
player1 = null;
} else if (op1 == 2) {
haveRemote1 = true;
player1 = new ServerNetPlayer();
}

ChoosePlayer playerChooser2 = new ChoosePlayer(this);
playerChooser2.setVisible(true);
int op2 = playerChooser2.getChosenOption();
if (op2 == 1) {
haveRemote2 = true;
player2 = new PlayerSimple();
} else if (op2 == 0) {
haveRemote2 = false;
player2 = null;
} else if (op2 == 2) {
haveRemote2 = true;
player2 = new ServerNetPlayer();
}

traxBoard = GnuTrax.getInstance().getTraxBoard();
if (board != null && board.size() > 0) {
board.get(0).setImage(tiles[TraxBoard.EMPTY].getImage());
this.repaint();
}

if (haveRemote1 && haveRemote2) {
this.setEnabled(false);
String move = "";
while (true) {
move = makeRemoteMove(move, 1);
move = makeRemoteMove(move, 2);
}
}
}

private String getRowColForPos(int x, int y){
Expand Down Expand Up @@ -98,7 +123,7 @@ private void drawBoard(){

outerPanel = new JPanel();
outerPanel.setLayout(springLayout);
outerPanel.setBackground(new Color(0, 100, 0));
outerPanel.setBackground(new Color(0, 195, 0));

JLabel playerTurn = new JLabel(traxBoard.whoToMove().name());
playerTurn.setForeground(Color.ORANGE);
Expand Down Expand Up @@ -160,20 +185,25 @@ private void showEndGameDialog(String winner){
System.exit(0);
}

private void makeRemoteMove(String otherPlayerMove){
this.setEnabled(false);
private String makeRemoteMove(String otherPlayerMove, int playerNo){
String AIMove = null;
if (playerNo == 2)
AIMove = player2.move(otherPlayerMove);
if (playerNo == 1)
AIMove = player1.move(otherPlayerMove);

String AIMove = player.move(otherPlayerMove);
if (AIMove != null)
GnuTrax.getInstance().gotAMove(AIMove);
else
System.exit(1);

drawBoard();
checkForWinner();

this.setEnabled(true);
return AIMove;
}

public void setMove(int x, int y, Tile tile){
public void makeHumanMove(int x, int y, Tile tile){
String theMove = position(x, y, tile.getTileType());
GnuTrax.getInstance().gotAMove(theMove);

Expand All @@ -182,8 +212,11 @@ public void setMove(int x, int y, Tile tile){
if (checkForWinner())
return;

if (haveRemote)
makeRemoteMove(theMove);
if (haveRemote2) {
this.setEnabled(false);
makeRemoteMove(theMove, 2);
this.setEnabled(true);
}

checkForWinner();
}
Expand All @@ -206,7 +239,7 @@ private void newBoard(){

outerPanel = new JPanel();
outerPanel.setLayout(springLayout);
outerPanel.setBackground(new Color(0, 100, 0));
outerPanel.setBackground(new Color(0, 195, 0));
this.setContentPane(outerPanel);

JLabel playerTurn = new JLabel(traxBoard.whoToMove().name());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/home/parham/trax/gui/ImagePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void showMovesDialog(){
ChooseTile ct = new ChooseTile(this.gnuTrax, possibleMoves);
ct.setVisible(true);
if (ct.getChosenMove() != -1) {
this.gnuTrax.setMove(this.x, this.y, possibleMoves.get(ct.getChosenMove()));
this.gnuTrax.makeHumanMove(this.x, this.y, possibleMoves.get(ct.getChosenMove()));
}
}

Expand Down

0 comments on commit e0d55e7

Please sign in to comment.